71 lines
2.4 KiB
C++
71 lines
2.4 KiB
C++
#ifndef UDPCOMMUNICATION_H
|
||
#define UDPCOMMUNICATION_H
|
||
|
||
#include <QObject>
|
||
#include <QUdpSocket>
|
||
#include <QTimer>
|
||
#include "global.h"
|
||
|
||
// ===================== UDP 通信层(收包 / 发包) =====================
|
||
// 与车载端经 UDP 报文通信,JSON 文本协议:
|
||
// 上行(UI→车载端)命令见各 sendXxx();
|
||
// 下行(车载端→UI)遥测见 parse() 中 "t" 字段分支。
|
||
// 本类同时承担“通信超时判定”:连续 2s 未收到心跳则判为通信异常。
|
||
class UdpCommunication : public QObject
|
||
{
|
||
Q_OBJECT
|
||
public:
|
||
explicit UdpCommunication(Telemetry* tel, QObject* parent = nullptr);
|
||
|
||
bool start();
|
||
void stop();
|
||
|
||
// 是否启用内置模拟车载端(离线演示)
|
||
bool mockEnabled() const { return m_mock; }
|
||
int localPort() const { return m_localPort; }
|
||
|
||
// ---------- 上行命令(发包) ----------
|
||
void sendChassisMove(const QString& dir, double speed);
|
||
void sendArmMove(const QString& axis, int dir); // dir: +1 / -1 / 0(停止)
|
||
void sendArmReturn();
|
||
void sendArmPause();
|
||
void sendGasStart();
|
||
void sendGasStop();
|
||
void sendWashStart(const QString& mode); // "clean" / "disinfect"
|
||
void sendWashStop();
|
||
void sendBallValve(int valve, bool open); // valve: 1=D1, 2=D2
|
||
void sendPumpPower(const QString& pump, bool on); // "plunger"/"air"/"leaf"
|
||
void sendPumpRpm(const QString& pump, int rpm);
|
||
void sendSetParam(const QString& key, double value);
|
||
void sendSelfCheckStart();
|
||
void sendEstop();
|
||
void sendEstopReset();
|
||
|
||
signals:
|
||
void telemetryUpdated(); // 任意遥测包到达(页面据此刷新)
|
||
void commOkChanged(bool ok); // 通信状态变化
|
||
void estopReceived(bool triggered); // 收到急停
|
||
void selfCheckItem(int index, bool ok); // 自检单项结果(0~9)
|
||
void selfCheckDone(bool allOk); // 自检完成
|
||
void faultAppended(const QString& system, const QString& code, const QString& desc);
|
||
|
||
private slots:
|
||
void onReadyRead();
|
||
void onHeartbeatTimeout();
|
||
|
||
private:
|
||
void sendJson(const QJsonObject& obj);
|
||
void parse(const QJsonObject& obj);
|
||
|
||
Telemetry* m_tel = nullptr;
|
||
QUdpSocket* m_socket = nullptr;
|
||
QTimer* m_hbTimer = nullptr;
|
||
int m_localPort = 9000;
|
||
QString m_remoteHost = "127.0.0.1";
|
||
int m_remotePort = 9001;
|
||
bool m_mock = true;
|
||
bool m_lastCommOk = true;
|
||
};
|
||
|
||
#endif // UDPCOMMUNICATION_H
|