106 lines
3.7 KiB
C++
106 lines
3.7 KiB
C++
#ifndef WDREMOTERECEIVER_H
|
||
#define WDREMOTERECEIVER_H
|
||
|
||
#include "IWDRemoteReceiver.h"
|
||
#include <atomic>
|
||
#include <mutex>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
class IVrZeroMQClient;
|
||
class IVrZeroMQSubscriber;
|
||
|
||
/**
|
||
* @brief IWDRemoteReceiver 实现
|
||
*
|
||
* - 控制:IVrZeroMQClient (REQ-REP)
|
||
* - 结果订阅:IVrZeroMQSubscriber on topic="result"
|
||
* - 原始图像订阅:IVrZeroMQSubscriber on topic="raw"
|
||
*/
|
||
class WDRemoteReceiver : public IWDRemoteReceiver
|
||
{
|
||
public:
|
||
WDRemoteReceiver();
|
||
~WDRemoteReceiver() override;
|
||
|
||
// IWDRemoteReceiver
|
||
int Connect(const ConnectConfig& cfg) override;
|
||
int SearchDevices(std::vector<WDRemoteDeviceInfo>& devices,
|
||
int timeoutMs) override;
|
||
int Open(const std::string& machineCode,
|
||
int timeoutMs) override;
|
||
int Disconnect() override;
|
||
bool IsConnected() const override;
|
||
|
||
int StartWork(int timeoutMs) override;
|
||
int StartWork(WDRemoteWorkMode mode, int timeoutMs) override;
|
||
int StopWork (int timeoutMs) override;
|
||
int StopWork (WDRemoteWorkMode mode, int timeoutMs) override;
|
||
int RequestSingleDetection(int timeoutMs) override;
|
||
int SetExposure(double exposureTime, int timeoutMs) override;
|
||
int SetGain (double gain, int timeoutMs) override;
|
||
int SetAlgoParams(const WDRemoteAlgoParams& params, int timeoutMs) override;
|
||
WDRemoteServerInfo GetServerInfo(int timeoutMs) override;
|
||
int SendCustomCommand(const std::string& jsonReq,
|
||
std::string& jsonResp,
|
||
int timeoutMs) override;
|
||
void SetDetectMode(const std::string& mode) override;
|
||
|
||
void SetDetectionCallback(DetectionCallback cb) override;
|
||
void SetRawImageCallback (RawImageCallback cb) override; // 双目图像
|
||
void SetEventCallback (EventCallback cb) override;
|
||
|
||
private:
|
||
int sendCommandAndParse(const std::string& jsonReq,
|
||
std::string& jsonResp,
|
||
int timeoutMs);
|
||
|
||
// 内部订阅管理(StartWork/StopWork 自动调用)
|
||
int StartSubscribeDetection();
|
||
int StopSubscribeDetection();
|
||
int StartSubscribeRawImage();
|
||
int StopSubscribeRawImage();
|
||
|
||
void onResultMessage (const char* topic, size_t topicLen,
|
||
const char* data, size_t dataLen);
|
||
void onRawImageMessage(const char* topic, size_t topicLen,
|
||
const char* data, size_t dataLen);
|
||
|
||
void emitEvent(WDRemoteEventType ev, const std::string& msg);
|
||
|
||
static bool parseDetectionJson(const std::string& jsonStr,
|
||
WDRemoteDetectionFrame& outFrame);
|
||
static bool parseDeviceInfoJson(const std::string& jsonStr,
|
||
const std::string& serverIp,
|
||
WDRemoteDeviceInfo& outDevice);
|
||
|
||
private:
|
||
enum class WorkState
|
||
{
|
||
Idle,
|
||
LiveStream,
|
||
Detection,
|
||
};
|
||
|
||
ConnectConfig m_cfg;
|
||
std::atomic<bool> m_bConnected{false};
|
||
|
||
IVrZeroMQClient* m_pCtrlClient{nullptr};
|
||
IVrZeroMQSubscriber* m_pSubResult{nullptr};
|
||
IVrZeroMQSubscriber* m_pSubRaw{nullptr};
|
||
|
||
std::mutex m_mtxCtrl; // 串行化 REQ-REP
|
||
mutable std::mutex m_mtxCallback;
|
||
mutable std::mutex m_mtxDetectMode;
|
||
mutable std::mutex m_mtxWorkState;
|
||
|
||
DetectionCallback m_cbDetection;
|
||
RawImageCallback m_cbRawImage; // 双目图像回调
|
||
EventCallback m_cbEvent;
|
||
std::vector<WDRemoteDeviceInfo> m_lastDevices;
|
||
std::string m_detectMode{"binocular"}; // "binocular" | "mono"
|
||
WorkState m_workState{WorkState::Idle};
|
||
};
|
||
|
||
#endif // WDREMOTERECEIVER_H
|