GrabBag/App/ScrewPosition/ScrewPositionApp/Presenter/Inc/ScrewPositionTCPProtocol.h
2026-04-19 12:28:59 +08:00

64 lines
1.8 KiB
C++

#ifndef SCREWPOSITIONTCPPROTOCOL_H
#define SCREWPOSITIONTCPPROTOCOL_H
#include <functional>
#include <map>
#include <string>
#include <vector>
#include <QByteArray>
#include "IVrConfig.h"
#include "IYTCPServer.h"
struct RobotPose6D
{
double x = 0.0;
double y = 0.0;
double z = 0.0;
double a = 0.0;
double b = 0.0;
double c = 0.0;
};
class ScrewPositionTCPProtocol
{
public:
using ConnectionCallback = std::function<void(bool connected)>;
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);
// Protocol:
// request : T1_X_Y_Z_A_B_C\r\n
// response: PointNum_X1_Y1_Z1_A1_B1_C1/.../\r\n
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