GrabBag/App/DroneScrewbolt/DroneScrewServer/DroneScrewZmqProtocol.h

150 lines
4.9 KiB
C
Raw Normal View History

2026-06-26 17:55:15 +08:00
#ifndef DRONESCREW_ZMQ_PROTOCOL_H
#define DRONESCREW_ZMQ_PROTOCOL_H
#include <QObject>
#include <QString>
#include <QByteArray>
#include <QJsonObject>
#include <atomic>
#include <functional>
#include <memory>
#include <mutex>
#include "IVrZeroMQServer.h"
#include "IVrZeroMQPubSub.h"
#include "Algo/IDroneScrewAlgo.h"
#include "IMvsDevice.h"
class QUdpSocket;
/**
* @brief ZMQ
*
* - REP socket
* - PUB socket
*
* 使 JSON Protobuf
*
*
* { "cmd": "start" }
* { "cmd": "stop" }
* { "cmd": "single" }
* { "cmd": "set_exposure", "value": 10000 }
* { "cmd": "set_gain", "value": 1.0 }
* { "cmd": "set_algo_params", "score": 0.5, "nms": 0.45, "width": 640, "height": 640 }
* { "cmd": "swap_cameras" }
* { "cmd": "get_info" }
*
*
* { "ok": true, "code": 0, "msg": "", ... }
*
*
* topic: "result"
* payload: { "frameId": .., "ts": .., "boxes": [ {cls,score,x,y,w,h}, ... ] }
*/
class DroneScrewZmqProtocol : public QObject
{
Q_OBJECT
public:
explicit DroneScrewZmqProtocol(QObject* parent = nullptr);
~DroneScrewZmqProtocol() override;
/**
* @brief ZMQ
* @param controlPort REQ-REP
* @param resultPort PUB (topic="result")
* @param rawImagePort PUB (topic="raw")<=0
*/
bool startServer(int controlPort, int resultPort, int rawImagePort);
void stopServer();
void setRtspUrl(const QString& url) { m_rtspUrl = url; }
void setSingleDetectionHandler(std::function<DroneScrewResult()> handler)
{
m_singleDetectionHandler = std::move(handler);
}
void setStartWorkHandler(std::function<int()> handler)
{
m_startWorkHandler = std::move(handler);
}
void setStopWorkHandler(std::function<int()> handler)
{
m_stopWorkHandler = std::move(handler);
}
void setStartLiveStreamHandler(std::function<int()> handler)
{
m_startLiveStreamHandler = std::move(handler);
}
void setStopLiveStreamHandler(std::function<int()> handler)
{
m_stopLiveStreamHandler = std::move(handler);
}
void setSwapCameraRolesHandler(std::function<int()> handler)
{
m_swapCameraRolesHandler = std::move(handler);
}
void setServerInfoProvider(std::function<QJsonObject()> provider)
{
m_serverInfoProvider = std::move(provider);
}
void setCalibrationInfoProvider(std::function<QJsonObject()> provider)
{
m_calibrationInfoProvider = std::move(provider);
}
int controlPort() const { return m_controlPort; }
int resultPort() const { return m_resultPort; }
int rawImagePort() const { return m_rawImagePort; }
signals:
void rawImagePublishEnabledRequested(bool enabled);
void detectModeRequested(const std::string& mode);
void setExposureRequested(double exposureTime);
void setGainRequested(double gain);
void updateAlgoParamsRequested(const DroneScrewAlgoParams& params);
public slots:
/**
* @brief presenter ZMQ topic="result"
*/
void publishDetectionResult(const DroneScrewResult& result);
/**
* @brief presenter ZMQ PUBtopic="raw"广
*
* payload = WDRemoteBinocularRawImageHeader(88B) + left raw bytes + right raw bytes
*/
void publishRawImage(const MvsImageData& left, const MvsImageData& right);
private:
QByteArray handleControlMessage(const QByteArray& reqData);
bool startDiscoveryServer();
void stopDiscoveryServer();
void handleDiscoveryDatagrams();
private:
IVrZeroMQServer* m_pZmqServer{nullptr};
IVrZeroMQPublisher* m_pZmqPublisher{nullptr}; // topic="result"
IVrZeroMQPublisher* m_pZmqRawPublisher{nullptr}; // topic="raw"
QUdpSocket* m_pDiscoverySocket{nullptr}; // UDP broadcast discovery
std::atomic<bool> m_bRunning{false};
QString m_rtspUrl;
int m_controlPort{0};
int m_resultPort{0};
int m_rawImagePort{0};
std::mutex m_pubMutex;
std::mutex m_rawMutex;
std::mutex m_singleMutex;
std::function<DroneScrewResult()> m_singleDetectionHandler;
std::function<int()> m_startWorkHandler;
std::function<int()> m_stopWorkHandler;
std::function<int()> m_startLiveStreamHandler;
std::function<int()> m_stopLiveStreamHandler;
std::function<int()> m_swapCameraRolesHandler;
std::function<QJsonObject()> m_serverInfoProvider;
std::function<QJsonObject()> m_calibrationInfoProvider;
};
#endif // DRONESCREW_ZMQ_PROTOCOL_H