#ifndef UART1_H
#define UART1_H
#include<QSerialPortInfo>
#include<QSerialPort>
#include <QList>
#include <QFile>
#include <QDebug>
#include <QObject>
#include <QtWidgets/QWidget>
#include <QApplication>

#include <stdio.h>
#include<sys/time.h>
#include<unistd.h>
#include <time.h>

struct UU_data1{

QByteArray wirelessTData_account;

QByteArray wirelessTData_adress;

QByteArray wirelessTData_data;

QByteArray concentratorTData_adress;

QByteArray concentratorTData_data;

QByteArray policeData;

QByteArray TevTop;

QByteArray TevFreq;

QByteArray TevLevel;

QByteArray TevAve;

QByteArray Tevperiodicity;

QByteArray UltTop;

QByteArray UltFreq;

QByteArray UltLevel;

QByteArray UltAve;

QByteArray UltEffective;

QByteArray tevData;

QByteArray ultData;

QByteArray SecFanStartTemperature;

QByteArray SecTripTemperature;

QByteArray SecAlarmTemperature;

QByteArray SecReturnTemperature;

QByteArray SecFanStartTempeture;

QByteArray SecWheelVolAA;
QByteArray SecWheelVolBB;
QByteArray SecWheelVolCC;
QByteArray SecZeroSequenceVol;

QByteArray SecActivePowerAA;
QByteArray SecActivePowerBB;
QByteArray SecActivePowerCC;

QByteArray SecActivePowerCom;

QByteArray SecReactivePower;
QByteArray SecReactivePowerAA;
QByteArray SecReactivePowerBB;
QByteArray SecReactivePowerCC;
QByteArray SecReactivePowerCom;

QByteArray SecWheelPowerFactor;
QByteArray SecWheelPowerFactorAA;
QByteArray SecWheelPowerFactorBB;
QByteArray SecWheelPowerFactorCC;
QByteArray SecWheelPowerFactorCom;

QByteArray SecWheelFrequency;

QByteArray SecWheelATemperature;
QByteArray SecWheelBTemperature;
QByteArray SecWheelCTemperature;
QByteArray SecWheelTTemperature;

QByteArray SecWheelPositiveElectricEnergyzong;
QByteArray SecWheelPositiveElectricEnergyjian;
QByteArray SecWheelPositiveElectricEnergyfeng;
QByteArray SecWheelPositiveElectricEnergyping;
QByteArray SecWheelPositiveElectricEnergygu;

QByteArray SecWheelNegativeElectricEnergyzong;
QByteArray SecWheelNegativeElectricEnergyjian;
QByteArray SecWheelNegativeElectricEnergyfeng;
QByteArray SecWheelNegativeElectricEnergyping;
QByteArray SecWheelNegativeElectricEnergygu;

QByteArray SecWheelActivePowerCom;

QByteArray SecFirstReactivePower;

QByteArray SecSecondReactivePower;

QByteArray SecThirdReactivePower;

QByteArray SecFourthReactivePower;

QByteArray SecCT;

QByteArray SecCT_U;

QByteArray WheelLineVAB;
QByteArray WheelLineVBC;
QByteArray WheelLineVCA;

QByteArray UnstableVandIV;
QByteArray UnstableVandIA;

QByteArray WheelVamA;
QByteArray WheelVamB;
QByteArray WheelVamC;

QByteArray PhaseAngleAA;
QByteArray PhaseAngleAB;
QByteArray PhaseAngleAC;
QByteArray PhaseAngleVA;
QByteArray PhaseAngleVB;
QByteArray PhaseAngleVC;

QByteArray AberrationRateVA;
QByteArray AberrationRateVB;
QByteArray AberrationRateVC;
QByteArray AberrationRateAA;
QByteArray AberrationRateAB;
QByteArray AberrationRateAC;

QByteArray HarmonicKFactorVA;
QByteArray HarmonicKFactorVB;
QByteArray HarmonicKFactorVC;
QByteArray HarmonicKFactorAA;
QByteArray HarmonicKFactorAB;
QByteArray HarmonicKFactorAC;

QByteArray HarmonicWavePeakFactorVA;
QByteArray HarmonicWavePeakFactorVB;
QByteArray HarmonicWavePeakFactorVC;
QByteArray HarmonicWavePeakFactorAA;
QByteArray HarmonicWavePeakFactorAB;
QByteArray HarmonicWavePeakFactorAC;

QByteArray PositiveReactivePowerzong;
QByteArray PositiveReactivePowerjian;
QByteArray PositiveReactivePowerfeng;
QByteArray PositiveReactivePowerping;
QByteArray PositiveReactivePowergu;

QByteArray ReverseReactivePowerzong;
QByteArray ReverseReactivePowerjian;
QByteArray ReverseReactivePowerfeng;
QByteArray ReverseReactivePowerping;
QByteArray ReverseReactivePowergu;

QByteArray WirelessT_CheckAccount;

QByteArray wirelessTData_State;

QByteArray Demandhave;
QByteArray Demandwu;

QByteArray McuState;

QByteArray SecData;

QByteArray Led;

QByteArray Rtc_time;

int SIM_quality;
int Wifi_quality;
};

struct UU_dataana{
    float A_Current;//三相电流
    float B_Current;
    float C_Current;
    float A_vol;//三相电压
    float B_vol;
    float C_vol;
    float Frequency;//频率
    float A_vol_phase;//三相电压相角
    float B_vol_phase;
    float C_vol_phase;
    float A_current_phase;//三相电流相角
    float B_current_phase;
    float C_current_phase;
    float UnstableI;//不平衡电流
    float UnstableV;//不平衡电压
    float A_vol_xiebo_jibian;//三相电压谐波畸变率
    float B_vol_xiebo_jibian;
    float C_vol_xiebo_jibian;
    float A_current_xiebo_jibian;//三相电流谐波畸变率
    float B_current_xiebo_jibian;
    float C_current_xiebo_jibian;
    float Wireless_data[60];
    float A_temp;
    float B_temp;
    float C_temp;
    float T_temp;
    float A_vol_K;
    float B_vol_K;
    float C_vol_K;
    float A_current_K;
    float B_current_K;
    float C_current_K;
    float A_vol_top;
    float B_vol_top;
    float C_vol_top;
    float A_current_top;
    float B_current_top;
    float C_current_top;
};
struct UU_dataacc{
    float P_Power_zong;
    float P_Power_jian;
    float P_Power_feng;
    float P_Power_ping;
    float P_Power_gu;
    float Com_PowerFactor;
    float A_Active_Power;
    float B_Active_Power;
    float C_Active_Power;
    float A_Reactive_Power;
    float B_Reactive_Power;
    float C_Reactive_Power;
    float Com_Active_Power;
    float Com_Reactive_Power;
};
struct UU_datadig{
    QByteArray policeDataDig;//超声波、TEV
    QByteArray McuState;
    QByteArray McuConnectState;
    QByteArray McuAlarmState;
    QByteArray wirelessTData_State;
    int alarm_Pop_ult;//超声波
    int alarm_Tev;//TEV异常
    int self_jufang;//局放自检
    int self_wireless;//无线测温自检
    int alarm_rxy;//热像仪掉线
    QString rxy_img1;//热像仪1图片
    QString rxy_img2;//热像仪2图片

    bool policeDataDig_flag;
    bool McuState_flag;
    bool McuConnectState_flag;
    bool McuAlarmState_flag;
    bool wirelessTData_State_flag;
};
struct UU_datadig_compare{
    QByteArray policeDataDig;//超声波、TEV
    QByteArray McuState;
    QByteArray McuConnectState;
    QByteArray McuAlarmState;
    QByteArray wirelessTData_State;
    int alarm_Pop_ult;//超声波
    int alarm_Tev;//TEV异常
    int self_jufang;//局放自检
    int self_wireless;//无线测温自检
    int alarm_rxy;//热像仪掉线
    QString rxy_img1;//热像仪1图片
    QString rxy_img2;//热像仪2图片
};
namespace Ui {
class UART1;
}

class UART1 : public QWidget
{
    Q_OBJECT

public:
    void happenInfoError(QByteArray data);
    void memcpySerialData(char *data,QByteArray &A,uint16_t len);
    explicit UART1(QWidget *parent = nullptr);
    ~UART1();
    void openserial(QString COM);
    void setserial();
    void ShuJu_Write(QByteArray data);
    void serial_close();
    void ModBusCRC16(QByteArray data);

    int SetSystemTime(char *dt);

signals:
    /**
     * @brief 告警监测
     * @param status :
     *
     * BIT4 电源监测 1为电压不足关闭显示,0为正常
     * BIT3 风机 1为故障 0为正常
     * BIT2 高温1为报警 0为正常
     * BIT1 超温跳闸 1为报警 0为正常
     * BIT0 门触点1为报警0为正常。
     *
     */
    void sigAlarmMonitor(uint32_t);
    void sigSelfTestMonitor(uint32_t);
    void sigLedControl(int);

public slots:
    void ShuJu_Read();
     void receverDataFromSerialPort();
     void uart_init(QString COM,uint8_t Enable);
     void Delay_MSec(unsigned int msec);

private:
     void UartFrameProcess(QByteArray & d);
     void UartFrameParse(QByteArray & from);
     bool UartFrameParseByte(uint8_t d);
     void UartFrameReset();
     QByteArray FrameData;
     int FrameCount = 0;
     int FrameLen = 0;
};

extern struct UU_data1 SEC;
extern struct UU_dataana ANA;
extern struct UU_dataacc ACC;
extern struct UU_datadig DIG;
extern struct UU_datadig_compare DIGCOMPARE;
#endif
