#ifndef DATATYPE_H
#define DATATYPE_H
#include <QObject>
#include <QDataStream>
#include <QByteArray>
#include <QDebug>
#include <QBuffer>
#include <QBuffer>
#include <QDataStream>
#include <QByteArray>
#include <QDebug>

struct DataPacket
{
    quint8 frameHeader;       // 帧头 (1 byte)
    quint32 packetSeq;        // 包序号 (4 bytes)
    quint16 dataLength;       // 数据块长度 (2 bytes)
    quint8 batteryLevel;      // 电量 (1 byte)
    quint8 channelCount;      // 通道数量 (1 byte)
    qint16 pitchAngle;        // 俯仰角 (2 bytes)
    qint16 rollAngle;         // 滚动角 (2 bytes)
    qint16 yawAngle;          // 偏航角 (2 bytes)
    quint16 ecg;              // 心电 (2 bytes)
    quint16 spo2;             // 血氧 (2 bytes)
    quint8 reserved1;         // 预留1 (1 byte)
    quint8 reserved2;         // 预留2 (1 byte)
    quint8 reserved3;         // 预留3 (1 byte)
    quint8 reserved4;         // 预留4 (1 byte)

    QByteArray dataBlock;     // 数据块 (192 bytes 或 6 bytes)
    quint8 syncSource;        // 同步触发源 (1 byte)
    quint8 syncSeq;           // 同步触发序号 (1 byte)
    quint8 checksum;          // 校验和 (1 byte)
    quint16 packetTail;       // 包尾 (2 bytes)

    // 序列化头部
    void serializeHeader(QDataStream &out )
    {
        out <<frameHeader <<packetSeq << dataLength << batteryLevel << channelCount
            << pitchAngle << rollAngle << yawAngle << ecg << spo2
            << reserved1 << reserved2 << reserved3 << reserved4;
    }

    // 序列化整个数据包
    QByteArray serialize()
    {
        QByteArray buffer;
        QBuffer bufferDevice(&buffer);
        bufferDevice.open(QIODevice::WriteOnly);

        QDataStream out(&bufferDevice);
        out.setVersion(QDataStream::Qt_5_13);

        serializeHeader(out); // 序列化头部，但不包括校验和
        out.writeRawData(dataBlock.data(), dataBlock.size());
        out << syncSource << syncSeq;
        out << checksum;
        out << packetTail;
        return buffer;
    }

    // 反序列化整个数据包
    void deserialize(const QByteArray &buffer) {
        QBuffer bufferDevice(const_cast<QByteArray*>(&buffer));
        bufferDevice.open(QIODevice::ReadOnly);

        QDataStream in(&bufferDevice);
        in.setVersion(QDataStream::Qt_5_13);

        in >>frameHeader >> packetSeq >> dataLength >> batteryLevel >> channelCount
           >> pitchAngle >> rollAngle >> yawAngle >> ecg >> spo2
           >> reserved1 >> reserved2 >> reserved3 >> reserved4;

        dataBlock.resize(dataLength);
        in.readRawData(dataBlock.data(), dataLength);

        in >> syncSource >> syncSeq;

        in >> checksum;

        in >> packetTail;


    }

    void  deserializeHeader(const QByteArray &buffer)
    {
        QBuffer bufferDevice(const_cast<QByteArray*>(&buffer));
        bufferDevice.open(QIODevice::ReadOnly);

        QDataStream in(&bufferDevice);
        in.setVersion(QDataStream::Qt_5_13);

        in >>frameHeader>> packetSeq >> dataLength >> batteryLevel >> channelCount
           >> pitchAngle >> rollAngle >> yawAngle >> ecg >> spo2
           >> reserved1 >> reserved2 >> reserved3 >> reserved4;
    }
};




#endif // DATATYPE_H
