85 lines
2.5 KiB
C++
85 lines
2.5 KiB
C++
#ifndef SCREWPOSITIONTCPPROTOCOL_H
|
||
#define SCREWPOSITIONTCPPROTOCOL_H
|
||
|
||
#include <functional>
|
||
#include <vector>
|
||
#include <string>
|
||
#include <map>
|
||
#include <QByteArray>
|
||
#include "IYTCPServer.h"
|
||
#include "IVrConfig.h"
|
||
|
||
/**
|
||
* @brief 机器人法兰位姿(触发信号中携带)
|
||
*/
|
||
struct RobotPose6D
|
||
{
|
||
double x = 0.0;
|
||
double y = 0.0;
|
||
double z = 0.0;
|
||
double rx = 0.0;
|
||
double ry = 0.0;
|
||
double rz = 0.0;
|
||
};
|
||
|
||
/**
|
||
* @brief ScrewPosition 文本协议 TCP 服务器
|
||
*
|
||
* 触发格式(客户端→服务端,换行符结尾):
|
||
* S1 RbtX-23.45 RbtY-23.45 RbtZ-23.45 RbtRX-23.45 RbtRY-123.45 RbtRZ-23.45\n
|
||
* S 表示螺杆检测,T 表示工具盘检测,后跟相机索引
|
||
*
|
||
* 返回格式(服务端→客户端):
|
||
* PointNum_X1_Y1_Z1_RX1_RY1_RZ1/X2_Y2_Z2_RX2_RY2_RZ2/\n
|
||
*/
|
||
class ScrewPositionTCPProtocol
|
||
{
|
||
public:
|
||
using ConnectionCallback = std::function<void(bool connected)>;
|
||
|
||
/**
|
||
* @brief 检测触发回调
|
||
* @param cameraIndex 相机索引
|
||
* @param detectionType 检测类型
|
||
* @param robotPose 机器人当前位姿
|
||
* @return 是否成功触发
|
||
*/
|
||
using DetectionTriggerCallback = std::function<bool(int cameraIndex,
|
||
DetectionType detectionType,
|
||
const RobotPose6D& robotPose)>;
|
||
|
||
ScrewPositionTCPProtocol();
|
||
~ScrewPositionTCPProtocol();
|
||
|
||
int Initialize(uint16_t port = 7800);
|
||
void Deinitialize();
|
||
bool IsRunning() const;
|
||
|
||
void SetConnectionCallback(const ConnectionCallback& callback);
|
||
void SetDetectionTriggerCallback(const DetectionTriggerCallback& callback);
|
||
|
||
/**
|
||
* @brief 发送文本结果
|
||
* @param text 文本内容(不含换行,内部自动添加)
|
||
* @param pClient nullptr 则广播
|
||
*/
|
||
int SendTextResult(const std::string& text, const TCPClient* pClient = nullptr);
|
||
|
||
private:
|
||
void OnTCPEvent(const TCPClient* pClient, TCPServerEventType eventType);
|
||
void OnTCPDataReceived(const TCPClient* pClient, const char* pData, unsigned int nLen);
|
||
void ParseTextCommand(const TCPClient* pClient, const QByteArray& line);
|
||
|
||
private:
|
||
IYTCPServer* m_pTCPServer = nullptr;
|
||
bool m_bServerRunning = false;
|
||
uint16_t m_nPort = 7800;
|
||
|
||
ConnectionCallback m_connectionCallback;
|
||
DetectionTriggerCallback m_detectionTriggerCallback;
|
||
|
||
std::map<const TCPClient*, QByteArray> m_clientBuffers;
|
||
};
|
||
|
||
#endif // SCREWPOSITIONTCPPROTOCOL_H
|