/** * @file TCPServerMethods.cpp * @brief ScrewPositionPresenter TCP通信相关方法实现 */ #include "ScrewPositionPresenter.h" #include "VrLog.h" #include #include #include #include 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; response["MessageType"] = "ScanResult"; response["Timestamp"] = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"); QJsonObject data; data["Success"] = result.success; data["ErrorCode"] = result.errorCode; data["Message"] = result.message; data["CameraIndex"] = cameraIndex; // 构建螺杆检测结果数组 QJsonArray screwArray; for (const auto& screw : result.screwInfoList) { QJsonObject screwObj; // 螺杆中心点坐标 QJsonObject center; center["X"] = screw.centerX; center["Y"] = screw.centerY; center["Z"] = screw.centerZ; screwObj["Center"] = center; // 轴向方向向量 QJsonObject axialDir; axialDir["X"] = screw.axialDirX; axialDir["Y"] = screw.axialDirY; axialDir["Z"] = screw.axialDirZ; screwObj["AxialDirection"] = axialDir; // 旋转角度(-30 ~ 30度) screwObj["RotateAngle"] = screw.rotateAngle; screwArray.append(screwObj); } data["ScrewCount"] = static_cast(result.screwInfoList.size()); data["Screws"] = screwArray; response["Data"] = data; // 发送检测结果 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, screw count: %zu\n", cameraIndex, result.screwInfoList.size()); } } int ScrewPositionPresenter::InitTCPServer() { if (m_pTCPServer) { LOG_WARNING("TCP server already initialized\n"); return 0; } m_pTCPServer = new TCPServerProtocol(); // 设置连接状态回调 m_pTCPServer->SetConnectionCallback([this](bool connected) { LOG_DEBUG("TCP connection status changed: %s\n", connected ? "connected" : "disconnected"); m_bTCPConnected = connected; }); // 设置检测触发回调 m_pTCPServer->SetDetectionTriggerCallback([this](bool start, int cameraIndex, qint64 timestamp) -> bool { Q_UNUSED(timestamp); if (start) { LOG_DEBUG("TCP triggered detection, cameraIndex: %d\n", cameraIndex); return TriggerDetection(cameraIndex); } return true; }); // 从ConfigManager获取端口配置 uint16_t port = 5020; // 默认端口 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; } bool ScrewPositionPresenter::startServer(quint16 port) { if (port == 0 && m_pConfigManager) { ConfigResult configResult = m_pConfigManager->GetConfigResult(); port = configResult.tcpPort; } if (port == 0) { port = 5020; // 默认端口 } return InitTCPServer() == 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"); } } int ScrewPositionPresenter::InitTcpServer(int nPort) { if (m_pTCPServer) { LOG_WARNING("TCP server already initialized\n"); return 0; } m_pTCPServer = new TCPServerProtocol(); // 设置回调 m_pTCPServer->SetConnectionCallback([this](bool connected) { m_bTCPConnected = connected; }); m_pTCPServer->SetDetectionTriggerCallback([this](bool start, int cameraIndex, qint64 timestamp) -> bool { Q_UNUSED(timestamp); if (start) { return TriggerDetection(cameraIndex); } return true; }); int ret = m_pTCPServer->Initialize(static_cast(nPort)); if (ret != 0) { delete m_pTCPServer; m_pTCPServer = nullptr; return ret; } return 0; } void ScrewPositionPresenter::onTcpDataReceivedFromCallback(const TCPClient* pClient, const char* pData, const unsigned int nLen) { Q_UNUSED(pClient); Q_UNUSED(pData); Q_UNUSED(nLen); // TCP数据接收由TCPServerProtocol内部处理 } void ScrewPositionPresenter::onTcpClientEventFromCallback(const TCPClient* pClient, TCPServerEventType eventType) { Q_UNUSED(pClient); Q_UNUSED(eventType); // TCP事件由TCPServerProtocol内部处理 } void ScrewPositionPresenter::OnTCPConnectionChanged(bool connected) { m_bTCPConnected = connected; LOG_DEBUG("TCP connection changed: %s\n", connected ? "connected" : "disconnected"); } bool ScrewPositionPresenter::OnTCPDetectionTrigger(bool startWork, int cameraIndex, qint64 timestamp) { Q_UNUSED(timestamp); if (startWork) { return TriggerDetection(cameraIndex); } return true; } void ScrewPositionPresenter::SendDetectionResultToClient(const DetectionResult& result) { _SendDetectionResultToTCP(result, result.cameraIndex); }