
#include "widget.h"
#include <QApplication>
#include "datatype.h"
QByteArray createAndSerializeDataPacket() {
    DataPacket packet;
    packet.frameHeader = 1;
    packet.packetSeq = 2;
    packet.dataLength = 3; // 64通道数据
    packet.batteryLevel = 4;
    packet.channelCount = 5;
    packet.pitchAngle = 6;
    packet.rollAngle = 7;
    packet.yawAngle = 8;
    packet.ecg = 9;
    packet.spo2 =10;
    packet.reserved1 = 1;
    packet.reserved2 = 2;
    packet.reserved3 = 3;
    packet.reserved4 = 4;

    // 填充数据块
    for (int i = 0; i < 192; ++i) {
        packet.dataBlock.append(static_cast<char>(i % 256));
    }

    packet.syncSource = 5;
    packet.syncSeq = 6;
    packet.packetTail = 8;

    // 序列化数据包
    QByteArray serializedPacket = packet.serialize();
    qDebug() << "Serialized Packet (Hex):" << serializedPacket.toHex(' ');
    return serializedPacket;

}
#include <QTextCodec>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForLocale(codec);
    Widget w;
    w.show();
 #if  0

    QString data="aa0000b21403ca3d4000000000000000000692000000000000ffed4dffedddffedb6ffed56ffee1fffedadffee5affed90ffedd5ffed3cffee6bffed67ffed86ffedc7ffee1fffedb9ffee11ffedd3ffee1cffedc0ffee2cffed67ffeddfffedeeffedb9ffed5effedf1ffed8affee42ffedd7ffede9ffee43ffed2affed89ffed9bffee28ffed8fffee05ffeda8ffee3affed7cffedfaffed9fffed8cffee0fffedc0ffedb7ffee16ffed89ffedadffedecffee36ffee18ffedfaffee42ffedaaffed59ffedffffede3ffee54ffee0bffed26ffee7fffeeb80000ffed4fffede1ffedb2ffed57ffee1fffeda8ffee5effed8affeddaffed40ffee6bffed71ffed87ffedc7ffee27ffedbcffee13ffedcfffee17ffedbfffee28ffed6cffede0ffede8ffedbaffed5bffede8ffed82ffee48ffeddbffededffee44ffed2dffed88ffed96ffee24ffed8bffedffffedaaffee3cffed82ffedf8ffed9bffed93ffee11ffedbdffedb2ffee1fffed89ffedacffede8ffee36ffee16ffedf7ffee40ffeda4ffed5dffee06ffedd9ffee51ffee0cffed25ffee83ff00000000ffed55ffedddffedb1ffed54ffee22ffedafffee5cffed85ffedd9ffed40ffee72ffed6effed8affedcbffee25ffedbcffee0bffedceffee18ffedc2ffee24ffed6effede6ffededffedbbffed5cffedeeffed85ffee53ffedd6ffedf0ffee4cffed30ffed8cffed96ffee27ffed95ffee01ffedadffee39ffed82ffedf9ffed9dffed94ffee11ffedbdffedb2ffee1fffed87ffedacffede9ffee33ffee1fffedf7ffee40ffeda4ffed57ffee02ffeddeffee4cffee11ffed24ffee0000eebd0000ffed54ffeddcffedb6ffed59ffee1fffedb1ffee58ffed87ffedd4ffed46ffee6dffed70ffed86ffedcfffee22ffedb4ffee0effedd4ffee1cffedc0ffee1effed67ffede5ffedebffedbaffed5affedf2ffed88ffee4dffedd5ffedf0ffee42ffed33ffed8affeda0ffee1bffed93ffee06ffedafffee38ffed7bffedf7ffeda0ffed8fffee0effedbeffedb6ffee1fffed85ffeda8ffedf2ffee33ffee17ffedf4ffee3effeda6ffed5affedf9ffede3ffee4effee0cffed2300007dffeeb80000ffed56ffede1ffedb3ffed55ffee1effedafffee59ffed85ffedd0ffed42ffee69ffed69ffed84ffedc7ffee20ffedb6ffee18ffedcfffee1bffedbfffee25ffed64ffede1ffede9ffedbcffed58ffedf7ffed83ffee4bffedd5ffededffee45ffed2fffed8effed96ffee1affed8dffee08ffedafffee33ffed86ffedf7ffeda2ffed8fffee10ffedbaffedb4ffee1bffed8dffedaaffedf0ffee3bffee15ffedfaffee38ffeda6ffed57ffee02ffede4ffee4effee03ff0000ffee7cffeebe0000005555";
    DataPacket datapack;
    datapack.deserialize(QByteArray( data.toStdString().c_str()));


   qDebug()<<"包序号\n :"+QString::number(datapack.packetSeq)<<endl;;
   qDebug()<<"数据块长度 \n:"+QString::number(datapack.dataLength)<<endl;;;       // 数据块长度 (2 bytes)
   qDebug()<<"电量\n"+QString::number(datapack.batteryLevel)<<endl;;;      // 电量 (1 byte)
   qDebug()<<"通道数量\n"+QString::number(datapack.channelCount)<<endl;;;      // 通道数量 (1 byte)
   qDebug()<<"俯仰角\n"+QString::number(datapack.pitchAngle)<<endl;;;        // 俯仰角 (2 bytes)
   qDebug()<<"滚动角\n"+QString::number(datapack.rollAngle)<<endl;;;         // 滚动角 (2 bytes)
   qDebug()<<"偏航角\n"+QString::number(datapack.yawAngle)<<endl;;;          // 偏航角 (2 bytes)
   qDebug()<<"心电\n"+QString::number(datapack.ecg)<<endl;;;              // 心电 (2 bytes)
   qDebug()<<"血氧\n"+QString::number(datapack.spo2)<<endl;;;             // 血氧 (2 bytes)

   QByteArray  str = createAndSerializeDataPacket();
   DataPacket packet;
   packet.deserialize(str);
   qDebug() << "Deserialization successful"<<str;
   qDebug() << "Frame Header:" << (int)packet.frameHeader;
   qDebug() << "Packet Seq:" << packet.packetSeq;
   qDebug() << "Data Length:" << packet.dataLength;
   qDebug() << "Battery Level:" << (int)packet.batteryLevel;
   qDebug() << "Channel Count:" << (int)packet.channelCount;
   qDebug() << "Pitch Angle:" << packet.pitchAngle;
   qDebug() << "Roll Angle:" << packet.rollAngle;
   qDebug() << "Yaw Angle:" << packet.yawAngle;
   qDebug() << "ECG:" << packet.ecg;
   qDebug() << "SpO2:" << packet.spo2;
   qDebug() << "SpO2:" <<packet.reserved1 ;
  qDebug() << "SpO2:" << packet.reserved2;
   qDebug() << "SpO2:" <<packet.reserved3 ;
   qDebug() << "SpO2:" <<packet.reserved4 ;

   qDebug() << "Sync Source:" << (int)packet.syncSource;
   qDebug() << "Sync Seq:" << (int)packet.syncSeq;
   qDebug() << "Checksum:" << (int)packet.checksum;
   qDebug() << "Packet Tail:" << packet.packetTail;

   // 打印数据块
   qDebug() << "Data Block:" << packet.dataBlock;

#endif
    return a.exec();
}
