102 lines
3.6 KiB
C++
102 lines
3.6 KiB
C++
|
|
#include "ScrewPositionTCPProtocol.h"
|
|||
|
|
#include "VrLog.h"
|
|||
|
|
#include <QDateTime>
|
|||
|
|
|
|||
|
|
ScrewPositionTCPProtocol::ScrewPositionTCPProtocol()
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
ScrewPositionTCPProtocol::~ScrewPositionTCPProtocol()
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void ScrewPositionTCPProtocol::SetDetectionTriggerWithTypeCallback(const DetectionTriggerWithTypeCallback& callback)
|
|||
|
|
{
|
|||
|
|
m_detectionTypeCallback = callback;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void ScrewPositionTCPProtocol::ParseJSONCommand(const TCPClient* pClient, const QJsonObject& jsonData)
|
|||
|
|
{
|
|||
|
|
QString messageType = jsonData["MessageType"].toString();
|
|||
|
|
qint64 timestamp = jsonData["Timestamp"].toVariant().toLongLong();
|
|||
|
|
|
|||
|
|
LOG_DEBUG("Received MessageType: %s, Timestamp: %lld\n", messageType.toStdString().c_str(), timestamp);
|
|||
|
|
|
|||
|
|
if (messageType == "ScrewRequest") {
|
|||
|
|
HandleScrewDetectCommand(pClient, timestamp);
|
|||
|
|
} else if (messageType == "ToolRequest") {
|
|||
|
|
HandleToolDiskDetectCommand(pClient, timestamp);
|
|||
|
|
} else if (messageType == "ScanRequest") {
|
|||
|
|
// 兼容旧协议,映射为螺杆检测
|
|||
|
|
HandleScrewDetectCommand(pClient, timestamp);
|
|||
|
|
} else {
|
|||
|
|
LOG_WARNING("Unknown MessageType: %s\n", messageType.toStdString().c_str());
|
|||
|
|
QJsonObject errResponse;
|
|||
|
|
errResponse["MessageType"] = "Error";
|
|||
|
|
errResponse["Timestamp"] = timestamp;
|
|||
|
|
QJsonObject errData;
|
|||
|
|
errData["ErrorCode"] = -3;
|
|||
|
|
errData["ErrorMessage"] = QString("未知消息类型: %1").arg(messageType);
|
|||
|
|
errResponse["Data"] = errData;
|
|||
|
|
SendDetectionResult(errResponse, pClient);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void ScrewPositionTCPProtocol::HandleScrewDetectCommand(const TCPClient* pClient, qint64 timestamp)
|
|||
|
|
{
|
|||
|
|
LOG_DEBUG("Handling ScrewRequest, timestamp: %lld\n", timestamp);
|
|||
|
|
TriggerDetection(pClient, timestamp, DETECTION_TYPE_SCREW, "ScrewResponse", "螺杆检测启动失败");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void ScrewPositionTCPProtocol::HandleToolDiskDetectCommand(const TCPClient* pClient, qint64 timestamp)
|
|||
|
|
{
|
|||
|
|
LOG_DEBUG("Handling ToolRequest, timestamp: %lld\n", timestamp);
|
|||
|
|
TriggerDetection(pClient, timestamp, DETECTION_TYPE_TOOL_DISK, "ToolResponse", "工具盘检测启动失败");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool ScrewPositionTCPProtocol::TriggerDetection(const TCPClient* pClient, qint64 timestamp,
|
|||
|
|
DetectionType type, const QString& responseType,
|
|||
|
|
const QString& errorDesc)
|
|||
|
|
{
|
|||
|
|
// 发送确认响应(回传请求的timestamp)
|
|||
|
|
QJsonObject response;
|
|||
|
|
response["MessageType"] = responseType;
|
|||
|
|
response["Timestamp"] = timestamp;
|
|||
|
|
QJsonObject data;
|
|||
|
|
data["Status"] = "Accepted";
|
|||
|
|
response["Data"] = data;
|
|||
|
|
SendDetectionResult(response, pClient);
|
|||
|
|
|
|||
|
|
if (!m_detectionTypeCallback) {
|
|||
|
|
LOG_WARNING("Detection trigger callback not set\n");
|
|||
|
|
QJsonObject errResp;
|
|||
|
|
errResp["MessageType"] = "Error";
|
|||
|
|
errResp["Timestamp"] = timestamp;
|
|||
|
|
QJsonObject errData;
|
|||
|
|
errData["ErrorCode"] = -4;
|
|||
|
|
errData["ErrorMessage"] = "检测服务未准备就绪";
|
|||
|
|
errResp["Data"] = errData;
|
|||
|
|
SendDetectionResult(errResp, pClient);
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
int cameraIndex = -1;
|
|||
|
|
bool success = m_detectionTypeCallback(true, cameraIndex, timestamp, type);
|
|||
|
|
|
|||
|
|
if (!success) {
|
|||
|
|
LOG_ERROR("Failed to trigger detection, type: %d\n", type);
|
|||
|
|
QJsonObject errResp;
|
|||
|
|
errResp["MessageType"] = "Error";
|
|||
|
|
errResp["Timestamp"] = timestamp;
|
|||
|
|
QJsonObject errData;
|
|||
|
|
errData["ErrorCode"] = -5;
|
|||
|
|
errData["ErrorMessage"] = errorDesc;
|
|||
|
|
errResp["Data"] = errData;
|
|||
|
|
SendDetectionResult(errResp, pClient);
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
LOG_DEBUG("Detection triggered successfully, type: %d\n", type);
|
|||
|
|
return true;
|
|||
|
|
}
|