208 lines
7.2 KiB
C++
Raw Normal View History

#include "WorkpieceHolePresenter.h"
#include "VrLog.h"
#include "VrError.h"
#include <QDateTime>
// 初始化TCP服务器
int WorkpieceHolePresenter::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();
// 从配置获取端口默认7800
int port = 7800;
// TODO: 从配置文件获取端口配置
// ConfigResult configResult = m_pConfigManager->GetConfigResult();
// port = configResult.tcpPort;
// 初始化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 WorkpieceHolePresenter::OnTCPConnectionChanged(bool connected)
{
LOG_DEBUG("TCP connection status changed: %s\n", connected ? "connected" : "disconnected");
m_bTCPConnected = connected;
// 通知状态更新
if (auto pStatus = GetStatusCallback<IYWorkpieceHoleStatus>()) {
if (connected) {
pStatus->OnStatusUpdate("TCP客户端已连接");
} else {
pStatus->OnStatusUpdate("TCP客户端已断开");
}
// 更新机械臂连接状态
pStatus->OnRobotConnectionChanged(connected);
}
// 更新工作状态
CheckAndUpdateWorkStatus();
}
// TCP检测触发回调
bool WorkpieceHolePresenter::OnTCPDetectionTrigger(bool startWork, int cameraIndex, qint64 timestamp)
{
if (auto pStatus = GetStatusCallback<IYWorkpieceHoleStatus>()) {
pStatus->OnWorkStatusChanged(WorkStatus::Working);
}
LOG_DEBUG("TCP detection trigger: startWork=%s, cameraIndex=%d, timestamp=%lld\n",
startWork ? "true" : "false", cameraIndex, timestamp);
// 通知UI接收到TCP扫描请求
if (auto pStatus = GetStatusCallback<IYWorkpieceHoleStatus>()) {
pStatus->OnStatusUpdate("【TCP协议】接收到扫描请求 (ScanRequest)");
}
if (!startWork) {
LOG_WARNING("Received stop work signal, ignoring\n");
return false;
}
// 检查相机连接状态
if (!m_bCameraConnected) {
LOG_ERROR("Camera not connected, cannot start detection\n");
if (auto pStatus = GetStatusCallback<IYWorkpieceHoleStatus>()) {
pStatus->OnStatusUpdate("【TCP协议】相机未连接无法启动检测");
}
return false;
}
// 通知UI已发送响应
if (auto pStatus = GetStatusCallback<IYWorkpieceHoleStatus>()) {
pStatus->OnStatusUpdate("【TCP协议】已发送扫描响应 (ScanResponse)");
}
// 启动检测
int result = StartDetection(cameraIndex, false); // false表示手动触发
bool success = (result == 0);
if (success) {
LOG_DEBUG("Detection started successfully via TCP trigger\n");
if (auto pStatus = GetStatusCallback<IYWorkpieceHoleStatus>()) {
pStatus->OnStatusUpdate("开始执行扫描检测...");
}
} else {
LOG_ERROR("Failed to start detection via TCP trigger, error code: %d\n", result);
if (auto pStatus = GetStatusCallback<IYWorkpieceHoleStatus>()) {
pStatus->OnStatusUpdate("检测启动失败");
}
}
return success;
}
// 发送检测结果给TCP客户端
void WorkpieceHolePresenter::_SendDetectionResultToTCP(const WorkpieceHoleDetectionResult& 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);
// 创建主结果对象(使用新的协议格式)
QJsonObject resultObject;
resultObject["MessageType"] = "ScanResult";
resultObject["Timestamp"] = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
// 创建Data对象
QJsonObject data;
data["WorkpieceType"] = detectionResult.workpieceType;
// 构造孔位置数组
try {
// 添加孔位置列表
QJsonArray holesArray;
for (size_t i = 0; i < detectionResult.holes.size(); i++) {
QJsonObject holeObj;
holeObj["x"] = detectionResult.holes[i].x;
holeObj["y"] = detectionResult.holes[i].y;
holeObj["z"] = detectionResult.holes[i].z;
holesArray.append(holeObj);
}
data["Holes"] = holesArray;
// 添加孔方向列表
QJsonArray holesDirArray;
for (size_t i = 0; i < detectionResult.holesDir.size(); i++) {
QJsonObject holeDirObj;
holeDirObj["x"] = detectionResult.holesDir[i].x;
holeDirObj["y"] = detectionResult.holesDir[i].y;
holeDirObj["z"] = detectionResult.holesDir[i].z;
holesDirArray.append(holeDirObj);
}
data["HolesDir"] = holesDirArray;
// 添加中心点坐标
QJsonObject centerObj;
centerObj["x"] = detectionResult.center.x;
centerObj["y"] = detectionResult.center.y;
centerObj["z"] = detectionResult.center.z;
data["Center"] = centerObj;
// 添加Y方向向量
QJsonObject yDirObj;
yDirObj["x"] = detectionResult.y_dir.x;
yDirObj["y"] = detectionResult.y_dir.y;
yDirObj["z"] = detectionResult.y_dir.z;
data["Y_Dir"] = yDirObj;
// 添加Z方向向量
QJsonObject zDirObj;
zDirObj["x"] = detectionResult.z_dir.x;
zDirObj["y"] = detectionResult.z_dir.y;
zDirObj["z"] = detectionResult.z_dir.z;
data["Z_Dir"] = zDirObj;
// 将Data对象添加到结果中
resultObject["Data"] = data;
// 发送结果
int sendResult = m_pTCPServer->SendDetectionResult(resultObject);
if (sendResult == 0) {
LOG_INFO("Detection result sent to TCP clients successfully\n");
// 通知UI已发送检测结果
if (auto pStatus = GetStatusCallback<IYWorkpieceHoleStatus>()) {
pStatus->OnStatusUpdate("【TCP协议】已发送扫描结果 (ScanResult)");
}
} 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());
m_pTCPServer->SendDetectionResult(resultObject);
}
}