111 lines
3.5 KiB
C++

/**
* @file TCPServerMethods.cpp
* @brief ScrewPositionPresenter TCP通信相关方法实现
*/
#include "ScrewPositionPresenter.h"
#include "DetectionOutputConverter.h"
#include "VrLog.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDateTime>
void ScrewPositionPresenter::_SendDetectionResultToTCP(const DetectionResult& result, int cameraIndex)
{
if (!m_pTCPServer || !m_pTCPServer->IsRunning()) {
LOG_WARNING("TCP protocol not running, skip sending result\n");
return;
}
QJsonObject response;
if (m_currentDetectionType == DETECTION_TYPE_TOOL_DISK) {
ProtocolDetectionOutput output = DetectionOutputConverter::ConvertToolDiskResult(cameraIndex);
output.timestamp = m_requestTimestamp;
response = DetectionOutputConverter::ToJson(output);
} else {
ProtocolDetectionOutput output = DetectionOutputConverter::ConvertScrewResult(
result.screwInfoList, cameraIndex, result.success, result.errorCode, result.message);
output.timestamp = m_requestTimestamp;
response = DetectionOutputConverter::ToJson(output);
}
int ret = m_pTCPServer->SendDetectionResult(response);
if (ret != 0) {
LOG_ERROR("Failed to send detection result via TCP, error: %d\n", ret);
} else {
LOG_DEBUG("Detection result sent via TCP, camera: %d, type: %d\n",
cameraIndex, m_currentDetectionType);
}
}
int ScrewPositionPresenter::InitTCPServer()
{
if (m_pTCPServer) {
LOG_WARNING("TCP server already initialized\n");
return 0;
}
m_pTCPServer = new ScrewPositionTCPProtocol();
// 设置连接状态回调
m_pTCPServer->SetConnectionCallback([this](bool connected) {
LOG_DEBUG("TCP connection status changed: %s\n", connected ? "connected" : "disconnected");
m_bTCPConnected = connected;
});
// 设置带检测类型的检测触发回调
m_pTCPServer->SetDetectionTriggerWithTypeCallback(
[this](bool start, int cameraIndex, qint64 timestamp, DetectionType detectionType) -> bool {
if (start) {
LOG_DEBUG("TCP triggered detection, cameraIndex: %d, type: %d\n", cameraIndex, detectionType);
m_currentDetectionType = detectionType;
m_requestTimestamp = timestamp;
return TriggerDetection(cameraIndex);
}
return true;
});
// 从ConfigManager获取端口配置
uint16_t port = 7800; // 默认端口
if (m_pConfigManager) {
ConfigResult configResult = m_pConfigManager->GetConfigResult();
port = configResult.tcpPort;
}
// 初始化TCP服务器
int ret = m_pTCPServer->Initialize(port);
if (ret != 0) {
LOG_ERROR("Failed to initialize TCP protocol on port %d, error: %d\n", port, ret);
delete m_pTCPServer;
m_pTCPServer = nullptr;
return ret;
}
LOG_DEBUG("TCP protocol initialized on port %d\n", port);
return 0;
}
void ScrewPositionPresenter::stopServer()
{
if (m_pTCPServer) {
m_pTCPServer->Deinitialize();
delete m_pTCPServer;
m_pTCPServer = nullptr;
m_bTCPConnected = false;
LOG_DEBUG("TCP server stopped\n");
}
}
void ScrewPositionPresenter::OnTCPConnectionChanged(bool connected)
{
m_bTCPConnected = connected;
LOG_DEBUG("TCP connection changed: %s\n", connected ? "connected" : "disconnected");
}
void ScrewPositionPresenter::SendDetectionResultToClient(const DetectionResult& result)
{
_SendDetectionResultToTCP(result, result.cameraIndex);
}