206 lines
5.7 KiB
C++
206 lines
5.7 KiB
C++
/**
|
|
* @file TCPServerMethods.cpp
|
|
* @brief BagThreadPositionPresenter TCP通信相关方法实现
|
|
*/
|
|
|
|
#include "BagThreadPositionPresenter.h"
|
|
#include "VrLog.h"
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QJsonArray>
|
|
#include <QDateTime>
|
|
|
|
void BagThreadPositionPresenter::_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 threadArray;
|
|
for (const auto& thread : result.threadInfoList) {
|
|
QJsonObject threadObj;
|
|
|
|
// 拆线中心点坐标
|
|
QJsonObject center;
|
|
center["X"] = thread.centerX;
|
|
center["Y"] = thread.centerY;
|
|
center["Z"] = thread.centerZ;
|
|
threadObj["Center"] = center;
|
|
|
|
// 轴向方向向量
|
|
QJsonObject axialDir;
|
|
axialDir["X"] = thread.axialDirX;
|
|
axialDir["Y"] = thread.axialDirY;
|
|
axialDir["Z"] = thread.axialDirZ;
|
|
threadObj["AxialDirection"] = axialDir;
|
|
|
|
// 旋转角度
|
|
threadObj["RotateAngle"] = thread.rotateAngle;
|
|
|
|
threadArray.append(threadObj);
|
|
}
|
|
|
|
data["ThreadCount"] = static_cast<int>(result.threadInfoList.size());
|
|
data["Threads"] = threadArray;
|
|
|
|
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, thread count: %zu\n",
|
|
cameraIndex, result.threadInfoList.size());
|
|
}
|
|
}
|
|
|
|
int BagThreadPositionPresenter::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 BagThreadPositionPresenter::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 BagThreadPositionPresenter::stopServer()
|
|
{
|
|
if (m_pTCPServer) {
|
|
m_pTCPServer->Deinitialize();
|
|
delete m_pTCPServer;
|
|
m_pTCPServer = nullptr;
|
|
m_bTCPConnected = false;
|
|
LOG_DEBUG("TCP server stopped\n");
|
|
}
|
|
}
|
|
|
|
int BagThreadPositionPresenter::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<uint16_t>(nPort));
|
|
if (ret != 0) {
|
|
delete m_pTCPServer;
|
|
m_pTCPServer = nullptr;
|
|
return ret;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void BagThreadPositionPresenter::onTcpDataReceivedFromCallback(const TCPClient* pClient, const char* pData, const unsigned int nLen)
|
|
{
|
|
Q_UNUSED(pClient);
|
|
Q_UNUSED(pData);
|
|
Q_UNUSED(nLen);
|
|
// TCP数据接收由TCPServerProtocol内部处理
|
|
}
|
|
|
|
void BagThreadPositionPresenter::onTcpClientEventFromCallback(const TCPClient* pClient, TCPServerEventType eventType)
|
|
{
|
|
Q_UNUSED(pClient);
|
|
Q_UNUSED(eventType);
|
|
// TCP事件由TCPServerProtocol内部处理
|
|
}
|
|
|
|
void BagThreadPositionPresenter::OnTCPConnectionChanged(bool connected)
|
|
{
|
|
m_bTCPConnected = connected;
|
|
LOG_DEBUG("TCP connection changed: %s\n", connected ? "connected" : "disconnected");
|
|
}
|
|
|
|
bool BagThreadPositionPresenter::OnTCPDetectionTrigger(bool startWork, int cameraIndex, qint64 timestamp)
|
|
{
|
|
Q_UNUSED(timestamp);
|
|
if (startWork) {
|
|
return TriggerDetection(cameraIndex);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void BagThreadPositionPresenter::SendDetectionResultToClient(const DetectionResult& result)
|
|
{
|
|
_SendDetectionResultToTCP(result, result.cameraIndex);
|
|
}
|