156 lines
5.1 KiB
C++
156 lines
5.1 KiB
C++
// 初始化TCP服务器
|
||
int LapWeldPresenter::InitTCPServer()
|
||
{
|
||
LOG_DEBUG("Start initializing TCP server\n");
|
||
|
||
if (m_pTCPServer) {
|
||
LOG_WARNING("TCP server already initialized\n");
|
||
return 0;
|
||
}
|
||
|
||
// 创建TCP服务器协议实例
|
||
m_pTCPServer = new TCPServerProtocol();
|
||
|
||
// 从配置获取端口,默认5020
|
||
int port = 5020;
|
||
if (m_vrConfig) {
|
||
// TODO: 从配置文件获取端口配置
|
||
// port = m_vrConfig->GetTCPPort();
|
||
}
|
||
|
||
// 初始化TCP服务器
|
||
int nRet = m_pTCPServer->Initialize(port);
|
||
if (nRet != 0) {
|
||
LOG_ERROR("Failed to initialize TCP server, return code: %d\n", nRet);
|
||
delete m_pTCPServer;
|
||
m_pTCPServer = nullptr;
|
||
return nRet;
|
||
}
|
||
|
||
// 设置连接状态回调
|
||
m_pTCPServer->SetConnectionCallback([this](bool connected) {
|
||
this->OnTCPConnectionChanged(connected);
|
||
});
|
||
|
||
// 设置检测触发回调
|
||
m_pTCPServer->SetDetectionTriggerCallback([this](bool startWork, int cameraIndex, qint64 timestamp) {
|
||
return this->OnTCPDetectionTrigger(startWork, cameraIndex, timestamp);
|
||
});
|
||
|
||
LOG_INFO("TCP server protocol initialized successfully on port %d\n", port);
|
||
return 0;
|
||
}
|
||
|
||
// TCP连接状态改变回调
|
||
void LapWeldPresenter::OnTCPConnectionChanged(bool connected)
|
||
{
|
||
LOG_DEBUG("TCP connection status changed: %s\n", connected ? "connected" : "disconnected");
|
||
|
||
m_bTCPConnected = connected;
|
||
|
||
// 通知状态更新
|
||
if (m_pStatus) {
|
||
if (connected) {
|
||
m_pStatus->OnStatusUpdate("TCP客户端已连接");
|
||
} else {
|
||
m_pStatus->OnStatusUpdate("TCP客户端已断开");
|
||
}
|
||
}
|
||
|
||
// 更新工作状态
|
||
CheckAndUpdateWorkStatus();
|
||
}
|
||
|
||
// TCP检测触发回调
|
||
bool LapWeldPresenter::OnTCPDetectionTrigger(bool startWork, int cameraIndex, qint64 timestamp)
|
||
{
|
||
LOG_DEBUG("TCP detection trigger: startWork=%s, cameraIndex=%d, timestamp=%lld\n",
|
||
startWork ? "true" : "false", cameraIndex, timestamp);
|
||
|
||
if (!startWork) {
|
||
LOG_WARNING("Received stop work signal, ignoring\n");
|
||
return false;
|
||
}
|
||
|
||
// 检查相机连接状态
|
||
if (!m_bCameraConnected) {
|
||
LOG_ERROR("Camera not connected, cannot start detection\n");
|
||
return false;
|
||
}
|
||
|
||
// 启动检测
|
||
int result = StartDetection(cameraIndex, false); // false表示手动触发
|
||
bool success = (result == 0);
|
||
|
||
if (success) {
|
||
LOG_DEBUG("Detection started successfully via TCP trigger\n");
|
||
if (m_pStatus) {
|
||
m_pStatus->OnStatusUpdate("TCP触发检测开始");
|
||
}
|
||
} else {
|
||
LOG_ERROR("Failed to start detection via TCP trigger, error code: %d\n", result);
|
||
if (m_pStatus) {
|
||
m_pStatus->OnStatusUpdate("TCP触发检测失败");
|
||
}
|
||
}
|
||
|
||
return success;
|
||
}
|
||
|
||
// 发送检测结果给TCP客户端
|
||
void LapWeldPresenter::_SendDetectionResultToTCP(const DetectionResult& detectionResult, int cameraIndex)
|
||
{
|
||
if (!m_pTCPServer || !m_bTCPConnected) {
|
||
LOG_DEBUG("TCP server not available, skipping result transmission\n");
|
||
return;
|
||
}
|
||
|
||
LOG_DEBUG("Sending detection result to TCP clients, camera index: %d\n", cameraIndex);
|
||
|
||
// 构造TCP协议的检测结果数据
|
||
TCPServerProtocol::DetectionResultData tcpResult;
|
||
tcpResult.code = 0;
|
||
tcpResult.success = true;
|
||
tcpResult.message = "检测成功";
|
||
tcpResult.timestamp = QDateTime::currentMSecsSinceEpoch();
|
||
|
||
// 转换检测结果为JSON格式的3D坐标点数组
|
||
// 根据README.md的协议格式:result为二维数组,每个焊缝线包含多个3D坐标点
|
||
try {
|
||
for (const auto& target : detectionResult.targets) {
|
||
std::vector<QJsonObject> weldLine;
|
||
|
||
// 遍历目标点云数据,提取3D坐标
|
||
for (size_t i = 0; i < target.pointCloud.size(); i += 3) {
|
||
if (i + 2 < target.pointCloud.size()) {
|
||
QJsonObject point;
|
||
point["x"] = target.pointCloud[i];
|
||
point["y"] = target.pointCloud[i + 1];
|
||
point["z"] = target.pointCloud[i + 2];
|
||
weldLine.push_back(point);
|
||
}
|
||
}
|
||
|
||
if (!weldLine.empty()) {
|
||
tcpResult.result.push_back(weldLine);
|
||
}
|
||
}
|
||
|
||
// 发送结果
|
||
int sendResult = m_pTCPServer->SendDetectionResult(tcpResult);
|
||
if (sendResult == 0) {
|
||
LOG_DEBUG("Detection result sent to TCP clients successfully\n");
|
||
} else {
|
||
LOG_ERROR("Failed to send detection result to TCP clients, error code: %d\n", sendResult);
|
||
}
|
||
} catch (const std::exception& e) {
|
||
LOG_ERROR("Exception while preparing TCP detection result: %s\n", e.what());
|
||
|
||
// 发送错误响应
|
||
tcpResult.code = -1;
|
||
tcpResult.success = false;
|
||
tcpResult.message = "检测结果处理异常";
|
||
tcpResult.result.clear();
|
||
m_pTCPServer->SendDetectionResult(tcpResult);
|
||
}
|
||
} |