2025-11-01 17:39:39 +08:00
|
|
|
|
#include "WorkpiecePresenter.h"
|
|
|
|
|
|
#include "VrLog.h"
|
|
|
|
|
|
#include "VrError.h"
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化TCP服务器
|
|
|
|
|
|
int WorkpiecePresenter::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();
|
|
|
|
|
|
|
2025-11-26 22:44:38 +08:00
|
|
|
|
// 从配置获取端口,默认7800
|
2025-11-02 16:48:52 +08:00
|
|
|
|
int port = 7800;
|
2025-11-26 22:44:38 +08:00
|
|
|
|
// TODO: 从配置文件获取端口配置
|
|
|
|
|
|
// ConfigResult configResult = m_pConfigManager->GetConfigResult();
|
|
|
|
|
|
// port = configResult.tcpPort;
|
2025-11-01 17:39:39 +08:00
|
|
|
|
|
|
|
|
|
|
// 初始化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 WorkpiecePresenter::OnTCPConnectionChanged(bool connected)
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_DEBUG("TCP connection status changed: %s\n", connected ? "connected" : "disconnected");
|
|
|
|
|
|
|
|
|
|
|
|
m_bTCPConnected = connected;
|
|
|
|
|
|
|
|
|
|
|
|
// 通知状态更新
|
2025-11-09 21:01:51 +08:00
|
|
|
|
if (auto pStatus = GetStatusCallback<IYWorkpieceStatus>()) {
|
2025-11-01 17:39:39 +08:00
|
|
|
|
if (connected) {
|
2025-11-09 21:01:51 +08:00
|
|
|
|
pStatus->OnStatusUpdate("TCP客户端已连接");
|
2025-11-01 17:39:39 +08:00
|
|
|
|
} else {
|
2025-11-09 21:01:51 +08:00
|
|
|
|
pStatus->OnStatusUpdate("TCP客户端已断开");
|
2025-11-01 17:39:39 +08:00
|
|
|
|
}
|
2025-11-02 16:48:52 +08:00
|
|
|
|
|
|
|
|
|
|
// 更新机械臂连接状态
|
2025-11-09 21:01:51 +08:00
|
|
|
|
pStatus->OnRobotConnectionChanged(connected);
|
2025-11-01 17:39:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新工作状态
|
|
|
|
|
|
CheckAndUpdateWorkStatus();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TCP检测触发回调
|
|
|
|
|
|
bool WorkpiecePresenter::OnTCPDetectionTrigger(bool startWork, int cameraIndex, qint64 timestamp)
|
|
|
|
|
|
{
|
2025-12-18 21:36:41 +08:00
|
|
|
|
if (auto pStatus = GetStatusCallback<IYWorkpieceStatus>()) {
|
|
|
|
|
|
pStatus->OnWorkStatusChanged(WorkStatus::Working);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-01 17:39:39 +08:00
|
|
|
|
LOG_DEBUG("TCP detection trigger: startWork=%s, cameraIndex=%d, timestamp=%lld\n",
|
|
|
|
|
|
startWork ? "true" : "false", cameraIndex, timestamp);
|
|
|
|
|
|
|
2025-12-18 21:36:41 +08:00
|
|
|
|
// 通知UI接收到TCP扫描请求
|
|
|
|
|
|
if (auto pStatus = GetStatusCallback<IYWorkpieceStatus>()) {
|
|
|
|
|
|
pStatus->OnStatusUpdate("【TCP协议】接收到扫描请求 (ScanRequest)");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-01 17:39:39 +08:00
|
|
|
|
if (!startWork) {
|
|
|
|
|
|
LOG_WARNING("Received stop work signal, ignoring\n");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查相机连接状态
|
|
|
|
|
|
if (!m_bCameraConnected) {
|
|
|
|
|
|
LOG_ERROR("Camera not connected, cannot start detection\n");
|
2025-12-18 21:36:41 +08:00
|
|
|
|
if (auto pStatus = GetStatusCallback<IYWorkpieceStatus>()) {
|
|
|
|
|
|
pStatus->OnStatusUpdate("【TCP协议】相机未连接,无法启动检测");
|
|
|
|
|
|
}
|
2025-11-01 17:39:39 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-18 21:36:41 +08:00
|
|
|
|
// 通知UI已发送响应
|
|
|
|
|
|
if (auto pStatus = GetStatusCallback<IYWorkpieceStatus>()) {
|
|
|
|
|
|
pStatus->OnStatusUpdate("【TCP协议】已发送扫描响应 (ScanResponse)");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-01 17:39:39 +08:00
|
|
|
|
// 启动检测
|
|
|
|
|
|
int result = StartDetection(cameraIndex, false); // false表示手动触发
|
|
|
|
|
|
bool success = (result == 0);
|
|
|
|
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
|
|
LOG_DEBUG("Detection started successfully via TCP trigger\n");
|
2025-12-18 21:36:41 +08:00
|
|
|
|
if (auto pStatus = GetStatusCallback<IYWorkpieceStatus>()) {
|
|
|
|
|
|
pStatus->OnStatusUpdate("开始执行扫描检测...");
|
|
|
|
|
|
}
|
2025-11-01 17:39:39 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
LOG_ERROR("Failed to start detection via TCP trigger, error code: %d\n", result);
|
2025-12-18 21:36:41 +08:00
|
|
|
|
if (auto pStatus = GetStatusCallback<IYWorkpieceStatus>()) {
|
|
|
|
|
|
pStatus->OnStatusUpdate("检测启动失败");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-01 17:39:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return success;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 发送检测结果给TCP客户端
|
|
|
|
|
|
void WorkpiecePresenter::_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);
|
|
|
|
|
|
|
2025-12-18 21:36:41 +08:00
|
|
|
|
// 创建主结果对象(使用新的协议格式)
|
2025-11-02 16:48:52 +08:00
|
|
|
|
QJsonObject resultObject;
|
2025-12-18 21:36:41 +08:00
|
|
|
|
resultObject["MessageType"] = "ScanResult";
|
|
|
|
|
|
resultObject["Timestamp"] = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
|
|
// 创建Data对象
|
|
|
|
|
|
QJsonObject data;
|
|
|
|
|
|
data["WorkpieceType"] = detectionResult.workpieceType;
|
|
|
|
|
|
|
2025-11-02 16:48:52 +08:00
|
|
|
|
// 构造新的JSON格式:type + L/T/R/B分组
|
2025-11-01 17:39:39 +08:00
|
|
|
|
try {
|
2025-12-18 21:36:41 +08:00
|
|
|
|
// 检查角点数量必须为12个(左3、顶3、右3、底3),否则不发送
|
|
|
|
|
|
if (detectionResult.positions.size() < 12) {
|
|
|
|
|
|
LOG_WARNING("Expected 12 corner points, but got %zu points. Not sending result.\n", detectionResult.positions.size());
|
|
|
|
|
|
if (auto pStatus = GetStatusCallback<IYWorkpieceStatus>()) {
|
|
|
|
|
|
pStatus->OnStatusUpdate("【TCP协议】角点数量不足,未发送结果");
|
|
|
|
|
|
}
|
|
|
|
|
|
return;
|
2025-11-01 17:39:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-02 16:48:52 +08:00
|
|
|
|
// 角点索引说明(根据DetectPresenter.cpp中的顺序):
|
2025-12-18 21:36:41 +08:00
|
|
|
|
// 索引 0-2: 边1 - 从下到上
|
|
|
|
|
|
// 索引 3-5: 边2 - 从左到右
|
|
|
|
|
|
// 索引 6-8: 边3 - 从上到下
|
|
|
|
|
|
// 索引 9-11: 边4 - 从右到左
|
|
|
|
|
|
|
|
|
|
|
|
// 构造边1角点
|
|
|
|
|
|
QJsonObject side1;
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2025-11-02 16:48:52 +08:00
|
|
|
|
QJsonArray point;
|
|
|
|
|
|
point.append(detectionResult.positions[i].x);
|
|
|
|
|
|
point.append(detectionResult.positions[i].y);
|
|
|
|
|
|
point.append(detectionResult.positions[i].z);
|
2025-12-18 21:36:41 +08:00
|
|
|
|
side1[QString("P%1").arg(i + 1)] = point;
|
2025-11-02 16:48:52 +08:00
|
|
|
|
}
|
2025-12-18 21:36:41 +08:00
|
|
|
|
data["1"] = side1;
|
2025-11-02 16:48:52 +08:00
|
|
|
|
|
2025-12-18 21:36:41 +08:00
|
|
|
|
// 构造边2角点
|
|
|
|
|
|
QJsonObject side2;
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2025-11-02 16:48:52 +08:00
|
|
|
|
QJsonArray point;
|
|
|
|
|
|
point.append(detectionResult.positions[i + 3].x);
|
|
|
|
|
|
point.append(detectionResult.positions[i + 3].y);
|
|
|
|
|
|
point.append(detectionResult.positions[i + 3].z);
|
2025-12-18 21:36:41 +08:00
|
|
|
|
side2[QString("P%1").arg(i + 1)] = point;
|
2025-11-02 16:48:52 +08:00
|
|
|
|
}
|
2025-12-18 21:36:41 +08:00
|
|
|
|
data["2"] = side2;
|
2025-11-02 16:48:52 +08:00
|
|
|
|
|
2025-12-18 21:36:41 +08:00
|
|
|
|
// 构造边3角点
|
|
|
|
|
|
QJsonObject side3;
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2025-11-02 16:48:52 +08:00
|
|
|
|
QJsonArray point;
|
|
|
|
|
|
point.append(detectionResult.positions[i + 6].x);
|
|
|
|
|
|
point.append(detectionResult.positions[i + 6].y);
|
|
|
|
|
|
point.append(detectionResult.positions[i + 6].z);
|
2025-12-18 21:36:41 +08:00
|
|
|
|
side3[QString("P%1").arg(i + 1)] = point;
|
2025-11-01 17:39:39 +08:00
|
|
|
|
}
|
2025-12-18 21:36:41 +08:00
|
|
|
|
data["3"] = side3;
|
2025-11-02 16:48:52 +08:00
|
|
|
|
|
2025-12-18 21:36:41 +08:00
|
|
|
|
// 构造边4角点
|
|
|
|
|
|
QJsonObject side4;
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2025-11-02 16:48:52 +08:00
|
|
|
|
QJsonArray point;
|
|
|
|
|
|
point.append(detectionResult.positions[i + 9].x);
|
|
|
|
|
|
point.append(detectionResult.positions[i + 9].y);
|
|
|
|
|
|
point.append(detectionResult.positions[i + 9].z);
|
2025-12-18 21:36:41 +08:00
|
|
|
|
side4[QString("P%1").arg(i + 1)] = point;
|
2025-11-02 16:48:52 +08:00
|
|
|
|
}
|
2025-12-18 21:36:41 +08:00
|
|
|
|
data["4"] = side4;
|
|
|
|
|
|
|
|
|
|
|
|
// 添加中心点坐标
|
|
|
|
|
|
QJsonArray centerPoint;
|
|
|
|
|
|
centerPoint.append(detectionResult.center.x);
|
|
|
|
|
|
centerPoint.append(detectionResult.center.y);
|
|
|
|
|
|
centerPoint.append(detectionResult.center.z);
|
|
|
|
|
|
data["Center"] = centerPoint;
|
|
|
|
|
|
|
|
|
|
|
|
// 添加长度信息
|
|
|
|
|
|
QJsonObject lengths;
|
|
|
|
|
|
lengths["len_A1"] = detectionResult.len_A1;
|
|
|
|
|
|
lengths["len_A2"] = detectionResult.len_A2;
|
|
|
|
|
|
lengths["len_B1"] = detectionResult.len_B1;
|
|
|
|
|
|
lengths["len_B2"] = detectionResult.len_B2;
|
|
|
|
|
|
data["Lengths"] = lengths;
|
|
|
|
|
|
|
|
|
|
|
|
// 将Data对象添加到结果中
|
|
|
|
|
|
resultObject["Data"] = data;
|
2025-11-01 17:39:39 +08:00
|
|
|
|
|
|
|
|
|
|
// 发送结果
|
2025-11-02 16:48:52 +08:00
|
|
|
|
int sendResult = m_pTCPServer->SendDetectionResult(resultObject);
|
2025-11-01 17:39:39 +08:00
|
|
|
|
if (sendResult == 0) {
|
|
|
|
|
|
LOG_INFO("Detection result sent to TCP clients successfully\n");
|
2025-12-18 21:36:41 +08:00
|
|
|
|
// 通知UI已发送检测结果
|
|
|
|
|
|
if (auto pStatus = GetStatusCallback<IYWorkpieceStatus>()) {
|
|
|
|
|
|
pStatus->OnStatusUpdate("【TCP协议】已发送扫描结果 (ScanResult)");
|
|
|
|
|
|
}
|
2025-11-01 17:39:39 +08:00
|
|
|
|
} 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());
|
2025-11-02 16:48:52 +08:00
|
|
|
|
m_pTCPServer->SendDetectionResult(resultObject);
|
2025-11-01 17:39:39 +08:00
|
|
|
|
}
|
2025-11-02 16:48:52 +08:00
|
|
|
|
}
|