257 lines
7.4 KiB
C++
257 lines
7.4 KiB
C++
#ifndef RADAR_WARNING_DEVICE_H
|
||
#define RADAR_WARNING_DEVICE_H
|
||
|
||
#include <QObject>
|
||
#include <QByteArray>
|
||
#include <QString>
|
||
#include <QVector>
|
||
#include <QtGlobal>
|
||
#include <stdexcept>
|
||
|
||
namespace payload_protocol {
|
||
|
||
// 雷达告警设备识别码(协议中未明确给出,假设值)
|
||
constexpr quint8 RADAR_WARNING_DEVICE_ID = 0x03;
|
||
|
||
// 雷达告警帧头(注意字节序:示例中显示为0xEB90,但报文示例为90 EB,可能是小端序)
|
||
constexpr quint8 RADAR_HEADER1 = 0xEB;
|
||
constexpr quint8 RADAR_HEADER2 = 0x90;
|
||
|
||
// 雷达告警帧长度
|
||
constexpr qint32 RADAR_UPLINK_FRAME_LENGTH = 13; // 上行帧长度
|
||
constexpr qint32 RADAR_DOWNLINK_FRAME_LENGTH = 30; // 下行帧长度
|
||
|
||
// 报文类型枚举
|
||
enum class RadarMessageType : quint8 {
|
||
HEARTBEAT = 0x00, // 00H: 心跳报文
|
||
START_STOP = 0x04 // 04H: 开始停止报文
|
||
};
|
||
|
||
// 工作状态枚举
|
||
enum class RadarWorkStatus : quint8 {
|
||
STOP = 0, // 停止
|
||
START = 1 // 开始
|
||
};
|
||
|
||
// 威胁等级枚举(0-5)
|
||
enum class ThreatLevel : quint8 {
|
||
LEVEL_0 = 0, // 无威胁
|
||
LEVEL_1 = 1,
|
||
LEVEL_2 = 2,
|
||
LEVEL_3 = 3,
|
||
LEVEL_4 = 4,
|
||
LEVEL_5 = 5 // 最高威胁
|
||
};
|
||
|
||
// 雷达告警下行数据结构(设备 -> 飞控)
|
||
struct RadarDownlinkData {
|
||
// 字节0-1: 帧头(在序列化时添加)
|
||
|
||
// 字节2: 设备号(低7位) + 保留位(高1位)
|
||
struct {
|
||
quint8 device_id : 7; // bit0~6: 设备号(0-127)
|
||
quint8 reserved_bit : 1; // bit7: 保留位
|
||
} device_info;
|
||
|
||
// 字节3: 报文类型
|
||
RadarMessageType message_type;
|
||
|
||
// 字节4-14: 保留(11字节)
|
||
QByteArray reserved_bytes1;
|
||
|
||
// 字节15: 威胁等级(0-5)
|
||
ThreatLevel threat_level;
|
||
|
||
// 字节16-28: 保留(13字节)
|
||
QByteArray reserved_bytes2;
|
||
|
||
// 字节29: 校验和(在序列化时计算)
|
||
|
||
RadarDownlinkData();
|
||
|
||
// 序列化为字节流
|
||
QByteArray serialize() const;
|
||
|
||
// 从字节流反序列化
|
||
bool deserialize(const QByteArray& data);
|
||
|
||
// 验证校验和
|
||
bool validateChecksum(const QByteArray& data) const;
|
||
|
||
// 检查是否触发告警(用于飞控判断)
|
||
bool shouldTriggerAlarm(ThreatLevel set_threat_level) const;
|
||
|
||
// 获取威胁等级数值
|
||
quint8 getThreatLevelValue() const;
|
||
|
||
// 设置威胁等级
|
||
void setThreatLevel(ThreatLevel level);
|
||
|
||
// 设置设备号
|
||
void setDeviceId(quint8 id);
|
||
|
||
// 获取设备号
|
||
quint8 getDeviceId() const;
|
||
};
|
||
|
||
// 雷达告警上行数据结构(飞控 -> 设备)
|
||
struct RadarUplinkData {
|
||
// 字节0-1: 帧头(在序列化时添加)
|
||
|
||
// 字节2: 设备号(0-255)
|
||
quint8 device_id;
|
||
|
||
// 字节3: 报文类型(低4位) + 工作状态(高4位)
|
||
struct {
|
||
quint8 message_type : 4; // bit0~3: 报文类型(04H)
|
||
quint8 work_status : 4; // bit4~7: 工作状态(1:开始, 0:停止)
|
||
} control_info;
|
||
|
||
// 字节4-11: 保留(8字节)
|
||
QByteArray reserved_bytes;
|
||
|
||
// 字节12: 校验和(在序列化时计算)
|
||
|
||
RadarUplinkData();
|
||
|
||
// 序列化为字节流
|
||
QByteArray serialize() const;
|
||
|
||
// 从字节流反序列化
|
||
bool deserialize(const QByteArray& data);
|
||
|
||
// 验证校验和
|
||
bool validateChecksum(const QByteArray& data) const;
|
||
|
||
// 设置开始命令
|
||
void setStartCommand();
|
||
|
||
// 设置停止命令
|
||
void setStopCommand();
|
||
|
||
// 检查是否为开始命令
|
||
bool isStartCommand() const;
|
||
|
||
// 检查是否为停止命令
|
||
bool isStopCommand() const;
|
||
|
||
// 检查命令是否有效
|
||
bool isCommandValid() const;
|
||
};
|
||
|
||
// 雷达告警设备类 - 支持信号槽机制
|
||
class RadarWarningDevice : public QObject {
|
||
Q_OBJECT
|
||
|
||
public:
|
||
explicit RadarWarningDevice(QObject* parent = nullptr);
|
||
explicit RadarWarningDevice(quint8 device_id, QObject* parent = nullptr);
|
||
|
||
// 获取设备ID
|
||
quint8 getDeviceId() const { return device_id_; }
|
||
|
||
// 设置设备ID
|
||
void setDeviceId(quint8 id) { device_id_ = id; }
|
||
|
||
// 创建控制指令(上行数据)
|
||
QByteArray createControlCommand(const RadarUplinkData& uplink_data) const;
|
||
|
||
// 解析状态数据(下行数据) - 槽函数
|
||
Q_SLOT bool parseStatusData(const QByteArray& data);
|
||
|
||
// 手动解析状态数据(兼容原有接口)
|
||
bool parseStatusData(const QByteArray& data, RadarDownlinkData& downlink_data) const;
|
||
|
||
// 发送控制指令 - 槽函数
|
||
Q_SLOT void sendControlCommand(const RadarUplinkData& uplink_data);
|
||
|
||
// 发送原始指令数据 - 槽函数
|
||
Q_SLOT void sendRawCommand(const QByteArray& command_data);
|
||
|
||
// 验证帧头
|
||
static bool validateFrameHeader(const QByteArray& data);
|
||
|
||
// 验证报文类型
|
||
static bool validateMessageType(const QByteArray& data, RadarMessageType expected_type);
|
||
|
||
// 创建心跳报文(下行模拟)
|
||
static QByteArray createHeartbeatMessage(quint8 device_id, ThreatLevel threat_level);
|
||
|
||
// 创建开始命令
|
||
static QByteArray createStartCommand(quint8 device_id);
|
||
|
||
// 创建停止命令
|
||
static QByteArray createStopCommand(quint8 device_id);
|
||
|
||
// 计算校验和(用于雷达告警协议,从第3个字节开始计算)
|
||
static quint8 calculateChecksum(const QByteArray& data);
|
||
|
||
// 检查是否应该触发S型机动(根据威胁等级判断)
|
||
static bool shouldTriggerManeuver(ThreatLevel current_threat, ThreatLevel threshold);
|
||
|
||
signals:
|
||
// 状态更新信号
|
||
void statusUpdated(const payload_protocol::RadarDownlinkData& status);
|
||
|
||
// 威胁等级变化信号
|
||
void threatLevelChanged(payload_protocol::ThreatLevel old_level,
|
||
payload_protocol::ThreatLevel new_level);
|
||
|
||
// 机动建议信号
|
||
void maneuverSuggested(payload_protocol::ThreatLevel current_threat,
|
||
payload_protocol::ThreatLevel threshold);
|
||
|
||
// 设备连接状态变化信号
|
||
void connectionStatusChanged(bool connected);
|
||
|
||
// 心跳丢失信号
|
||
void heartbeatLost();
|
||
|
||
// 指令发送信号
|
||
void commandSent(const QByteArray& command_data);
|
||
|
||
// 错误信号
|
||
void errorOccurred(const QString& error_message);
|
||
|
||
public slots:
|
||
// 设置威胁阈值
|
||
Q_SLOT void setThreatThreshold(payload_protocol::ThreatLevel threshold);
|
||
|
||
// 开始工作
|
||
Q_SLOT void startOperation();
|
||
|
||
// 停止工作
|
||
Q_SLOT void stopOperation();
|
||
|
||
// 请求心跳
|
||
Q_SLOT void requestHeartbeat();
|
||
|
||
// 重置设备
|
||
Q_SLOT void resetDevice();
|
||
|
||
private slots:
|
||
// 处理内部状态更新
|
||
void handleStatusUpdate(const payload_protocol::RadarDownlinkData& status);
|
||
|
||
private:
|
||
quint8 device_id_; // 设备ID(0-255)
|
||
|
||
// 威胁阈值
|
||
ThreatLevel threat_threshold_ = ThreatLevel::LEVEL_2;
|
||
|
||
// 最后接收到的状态
|
||
RadarDownlinkData last_status_;
|
||
|
||
// 心跳计数器
|
||
qint32 heartbeat_missed_count_ = 0;
|
||
|
||
// 在数据中设置设备ID
|
||
void setDeviceIdInData(QByteArray& data, quint8 device_id) const;
|
||
};
|
||
} // namespace payload_protocol
|
||
|
||
Q_DECLARE_METATYPE(payload_protocol::ThreatLevel)
|
||
Q_DECLARE_METATYPE(payload_protocol::RadarDownlinkData)
|
||
Q_DECLARE_METATYPE(payload_protocol::RadarUplinkData)
|
||
|
||
#endif // RADAR_WARNING_DEVICE_H
|