2025-12-10 00:01:32 +08:00
|
|
|
|
#ifndef BINOCULARMARKRECEIVER_H
|
|
|
|
|
|
#define BINOCULARMARKRECEIVER_H
|
|
|
|
|
|
|
|
|
|
|
|
#include "IBinocularMarkReceiver.h"
|
2025-12-18 21:36:41 +08:00
|
|
|
|
#include "IVrTCPClient.h"
|
2025-12-10 00:01:32 +08:00
|
|
|
|
#include <mutex>
|
2025-12-18 21:36:41 +08:00
|
|
|
|
#include <condition_variable>
|
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
#include <vector>
|
2025-12-10 00:01:32 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief BinocularMarkReceiver实现类
|
|
|
|
|
|
* TCP客户端,接收BinocularMarkApp发送的Mark检测结果
|
|
|
|
|
|
*/
|
2025-12-18 21:36:41 +08:00
|
|
|
|
class BinocularMarkReceiver : public IBinocularMarkReceiver
|
2025-12-10 00:01:32 +08:00
|
|
|
|
{
|
|
|
|
|
|
public:
|
2025-12-18 21:36:41 +08:00
|
|
|
|
BinocularMarkReceiver();
|
2025-12-10 00:01:32 +08:00
|
|
|
|
~BinocularMarkReceiver() override;
|
|
|
|
|
|
|
|
|
|
|
|
// 实现IBinocularMarkReceiver接口
|
2025-12-18 21:36:41 +08:00
|
|
|
|
int Connect(const std::string& serverIp, uint16_t serverPort) override;
|
2025-12-10 00:01:32 +08:00
|
|
|
|
int Disconnect() override;
|
|
|
|
|
|
bool IsConnected() const override;
|
2025-12-18 21:36:41 +08:00
|
|
|
|
SingleDetectionResult RequestSingleDetection(int timeoutMs = 5000) override;
|
|
|
|
|
|
ImageData RequestSingleImage(int timeoutMs = 5000) override;
|
|
|
|
|
|
int StartWork() override;
|
|
|
|
|
|
int StopWork() override;
|
2025-12-20 16:18:12 +08:00
|
|
|
|
int StartCapture() override;
|
|
|
|
|
|
int StopCapture() override;
|
|
|
|
|
|
std::string GetCalibrationMatrix(int timeoutMs = 3000) override;
|
2025-12-18 21:36:41 +08:00
|
|
|
|
int SetCalibrationMatrix(const std::string& calibrationXml) override;
|
2025-12-20 16:18:12 +08:00
|
|
|
|
int SetExposureTime(SVrCameraEnum target, double exposureTime) override;
|
|
|
|
|
|
int SetGain(SVrCameraEnum target, double gain) override;
|
|
|
|
|
|
CameraInfo GetCameraInfo(SVrCameraEnum target, int timeoutMs = 3000) override;
|
2025-12-10 00:01:32 +08:00
|
|
|
|
void SetMarkResultCallback(MarkResultCallback callback) override;
|
2025-12-20 16:18:12 +08:00
|
|
|
|
void SetImageCallback(ImageCallback callback) override;
|
2025-12-18 21:36:41 +08:00
|
|
|
|
void SetEventCallback(EventCallback callback) override;
|
2025-12-10 00:01:32 +08:00
|
|
|
|
|
2025-12-18 21:36:41 +08:00
|
|
|
|
private:
|
2025-12-10 00:01:32 +08:00
|
|
|
|
/**
|
2025-12-18 21:36:41 +08:00
|
|
|
|
* @brief 解析数据帧(处理粘包)
|
2025-12-10 00:01:32 +08:00
|
|
|
|
*/
|
2025-12-18 21:36:41 +08:00
|
|
|
|
void parseFrames();
|
2025-12-10 00:01:32 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
2025-12-18 21:36:41 +08:00
|
|
|
|
* @brief 处理单个JSON消息
|
|
|
|
|
|
* @param jsonData JSON数据
|
2025-12-10 00:01:32 +08:00
|
|
|
|
*/
|
2025-12-18 21:36:41 +08:00
|
|
|
|
void handleJsonMessage(const std::string& jsonData);
|
2025-12-10 00:01:32 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
2025-12-18 21:36:41 +08:00
|
|
|
|
* @brief 处理Mark结果消息
|
|
|
|
|
|
* @param jsonStr JSON字符串
|
2025-12-10 00:01:32 +08:00
|
|
|
|
*/
|
2025-12-18 21:36:41 +08:00
|
|
|
|
void handleMarkResult(const std::string& jsonStr);
|
2025-12-10 00:01:32 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
2025-12-18 21:36:41 +08:00
|
|
|
|
* @brief 处理单次检测结果消息
|
|
|
|
|
|
* @param jsonStr JSON字符串
|
2025-12-10 00:01:32 +08:00
|
|
|
|
*/
|
2025-12-18 21:36:41 +08:00
|
|
|
|
void handleSingleDetectionResult(const std::string& jsonStr);
|
2025-12-10 00:01:32 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
2025-12-18 21:36:41 +08:00
|
|
|
|
* @brief 处理图像数据消息
|
|
|
|
|
|
* @param jsonStr JSON字符串
|
2025-12-10 00:01:32 +08:00
|
|
|
|
*/
|
2025-12-18 21:36:41 +08:00
|
|
|
|
void handleImageData(const std::string& jsonStr);
|
2025-12-10 00:01:32 +08:00
|
|
|
|
|
2025-12-20 16:18:12 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 处理服务器心跳消息
|
|
|
|
|
|
* @param jsonStr JSON字符串
|
|
|
|
|
|
*/
|
|
|
|
|
|
void handleHeartbeat(const std::string& jsonStr);
|
|
|
|
|
|
|
2025-12-10 00:01:32 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 处理心跳应答消息
|
2025-12-18 21:36:41 +08:00
|
|
|
|
* @param jsonStr JSON字符串
|
2025-12-10 00:01:32 +08:00
|
|
|
|
*/
|
2025-12-18 21:36:41 +08:00
|
|
|
|
void handleHeartbeatAck(const std::string& jsonStr);
|
2025-12-10 00:01:32 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 处理命令应答消息
|
2025-12-18 21:36:41 +08:00
|
|
|
|
* @param jsonStr JSON字符串
|
2025-12-10 00:01:32 +08:00
|
|
|
|
*/
|
2025-12-18 21:36:41 +08:00
|
|
|
|
void handleCommandResponse(const std::string& jsonStr);
|
2025-12-10 00:01:32 +08:00
|
|
|
|
|
2025-12-20 16:18:12 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 处理相机信息响应消息
|
|
|
|
|
|
* @param jsonStr JSON字符串
|
|
|
|
|
|
*/
|
|
|
|
|
|
void handleCameraInfoResponse(const std::string& jsonStr);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 处理标定矩阵响应消息
|
|
|
|
|
|
* @param jsonStr JSON字符串
|
|
|
|
|
|
*/
|
|
|
|
|
|
void handleCalibrationMatrixResponse(const std::string& jsonStr);
|
|
|
|
|
|
|
2025-12-10 00:01:32 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 构造数据帧
|
|
|
|
|
|
* @param jsonData JSON数据
|
|
|
|
|
|
* @return 完整的数据帧(帧头+长度+数据+帧尾)
|
|
|
|
|
|
*/
|
2025-12-18 21:36:41 +08:00
|
|
|
|
std::string buildFrame(const std::string& jsonData);
|
2025-12-10 00:01:32 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 发送JSON消息
|
|
|
|
|
|
* @param messageType 消息类型
|
2025-12-18 21:36:41 +08:00
|
|
|
|
* @param jsonData JSON数据字符串
|
2025-12-10 00:01:32 +08:00
|
|
|
|
* @return 0-成功,其他-失败
|
|
|
|
|
|
*/
|
2025-12-18 21:36:41 +08:00
|
|
|
|
int sendJsonMessage(const std::string& messageType, const std::string& jsonData);
|
2025-12-10 00:01:32 +08:00
|
|
|
|
|
|
|
|
|
|
private:
|
2025-12-18 21:36:41 +08:00
|
|
|
|
// TCP回调函数
|
|
|
|
|
|
static void tcpRecvCallback(IVrTCPClient* pClient, const char* pData, const int nLen, void* pParam);
|
|
|
|
|
|
static void linkEventCallback(IVrTCPClient* pClient, bool connected, void* pParam);
|
2025-12-10 00:01:32 +08:00
|
|
|
|
|
2025-12-18 21:36:41 +08:00
|
|
|
|
// 心跳线程函数
|
|
|
|
|
|
void heartbeatThreadFunc();
|
|
|
|
|
|
|
|
|
|
|
|
IVrTCPClient* m_pTcpClient; // TCP客户端
|
2025-12-10 00:01:32 +08:00
|
|
|
|
std::string m_serverIp; // 服务器IP
|
2025-12-18 21:36:41 +08:00
|
|
|
|
uint16_t m_serverPort; // 服务器端口
|
|
|
|
|
|
std::atomic<bool> m_bConnected; // 连接状态
|
2025-12-10 00:01:32 +08:00
|
|
|
|
|
2025-12-18 21:36:41 +08:00
|
|
|
|
std::vector<char> m_dataBuffer; // 数据缓冲区(处理粘包)
|
2025-12-10 00:01:32 +08:00
|
|
|
|
|
|
|
|
|
|
MarkResultCallback m_markResultCallback; // Mark结果回调
|
2025-12-20 16:18:12 +08:00
|
|
|
|
ImageCallback m_imageCallback; // 图像回调
|
2025-12-18 21:36:41 +08:00
|
|
|
|
EventCallback m_eventCallback; // 事件回调
|
|
|
|
|
|
|
|
|
|
|
|
// 同步等待结果存储
|
|
|
|
|
|
SingleDetectionResult m_pendingSingleDetectionResult;
|
|
|
|
|
|
ImageData m_pendingImageData;
|
2025-12-20 16:18:12 +08:00
|
|
|
|
CameraInfo m_pendingLeftCameraInfo;
|
|
|
|
|
|
CameraInfo m_pendingRightCameraInfo;
|
|
|
|
|
|
std::string m_pendingCalibrationMatrix;
|
2025-12-18 21:36:41 +08:00
|
|
|
|
std::condition_variable m_cvSingleDetection; // 单次检测条件变量
|
|
|
|
|
|
std::condition_variable m_cvImageData; // 图像数据条件变量
|
2025-12-20 16:18:12 +08:00
|
|
|
|
std::condition_variable m_cvLeftCameraInfo; // 左相机信息条件变量
|
|
|
|
|
|
std::condition_variable m_cvRightCameraInfo; // 右相机信息条件变量
|
|
|
|
|
|
std::condition_variable m_cvCalibrationMatrix; // 标定矩阵条件变量
|
2025-12-18 21:36:41 +08:00
|
|
|
|
bool m_bSingleDetectionReady;
|
|
|
|
|
|
bool m_bImageDataReady;
|
2025-12-20 16:18:12 +08:00
|
|
|
|
bool m_bLeftCameraInfoReady;
|
|
|
|
|
|
bool m_bRightCameraInfoReady;
|
|
|
|
|
|
bool m_bCalibrationMatrixReady;
|
2025-12-18 21:36:41 +08:00
|
|
|
|
|
|
|
|
|
|
// 心跳线程
|
|
|
|
|
|
std::thread m_heartbeatThread;
|
|
|
|
|
|
std::atomic<bool> m_bHeartbeatRunning;
|
2025-12-20 16:18:12 +08:00
|
|
|
|
std::atomic<bool> m_bHeartbeatEnabled; // 是否启用心跳
|
2025-12-18 21:36:41 +08:00
|
|
|
|
int m_nHeartbeatInterval; // 心跳间隔(秒)
|
2025-12-20 16:18:12 +08:00
|
|
|
|
std::condition_variable m_cvHeartbeat; // 心跳线程条件变量
|
|
|
|
|
|
std::mutex m_mutexHeartbeat; // 心跳线程互斥锁
|
2025-12-10 00:01:32 +08:00
|
|
|
|
|
|
|
|
|
|
mutable std::mutex m_mutex; // 线程安全锁
|
|
|
|
|
|
|
|
|
|
|
|
// 帧格式常量
|
2025-12-18 21:36:41 +08:00
|
|
|
|
static constexpr const char* FRAME_HEADER = "##START#";
|
|
|
|
|
|
static constexpr const char* FRAME_TAIL = "#END";
|
2025-12-10 00:01:32 +08:00
|
|
|
|
static constexpr int FRAME_HEADER_SIZE = 8;
|
|
|
|
|
|
static constexpr int FRAME_TAIL_SIZE = 4;
|
2025-12-18 21:36:41 +08:00
|
|
|
|
static constexpr int FRAME_LENGTH_SIZE = 8;
|
2025-12-10 00:01:32 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // BINOCULARMARKRECEIVER_H
|