#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QSerialPort>
#include <QSettings>
#include <stdint.h>
#include "common.h"

namespace Ui {   //新增的
 class form;
}

typedef struct _ST_DEV_INFO
{
	
} stDevInfo;

#define USART_BUF_SIZE  1088
class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
private slots:
    void on_btnConn_clicked();

    void on_btnReset_clicked();

    void on_btnRefresh_clicked();

    void on_configButton_clicked();

    void on_defaultButton_clicked();

    void on_fileButton_clicked();

    void on_updateButton_clicked();

    void on_configSetButton_clicked();

    void on_configGetButton_clicked();

    void on_csgConfigGetButton_clicked();

    void on_csgConfigSetButton_clicked();

    void on_dataRefreshButton_clicked();

    void on_dataRefreshAutoButton_clicked();

    void on_stateRefreshButton_clicked();

    void on_freRefreshButton_clicked();

    void on_waveColButton_clicked();

    void on_waveRefreshButton_clicked();

    void on_waveCalButton_clicked();

    void on_historyDataButton_clicked();

    void on_stateLogButton_clicked();

    void on_waveButton_clicked();

private:

    QString toHexadecimal(const QByteArray &byteArray);
    int searchUsablePort();
    void serialErrHandle(QSerialPort::SerialPortError error);
    void serialDisconnect();
    int initSerial();
    int deinitSerial();
    int readData();
    int writeData(char *buf, int len);
    
    int protoHeaderInit(proto_head_t * header, uint16_t len, uint32_t id, uint8_t cmd_type, uint8_t cmd);
	
	void buttonEnable(bool flag);
	
	QString devStateWaveStateGet(uint8_t state);
	
	QString devStateWlStateGet(uint8_t state);
    void uiDevInfoRefresh();
    void uiDevConfigRefresh();
    void uiDevCsgConfigRefresh();
	void uiDevRealDataRefresh();
	
	void uiDevStateRefresh();
	
	void protoContact(char *data);
	
	void protoConfig(char *data);
	
	void protoCsgConfig(char *data);
	
	void protoRealdata(char *data);
	
	int protoUpdateReply(char *data, int fileType);

	int protoWaveDataUploadReply(char *data);

	int protoState(char *data);
    int paraseProtocols(char *data);
    int snedMsgDeviceInfoGet();
    int sendMsgDeviceInfoSet();
    int sendMsgDeviceReset();
	int sendMsgDeviceCfgDefault();
    int sendMsgDeviceConfigSet();
    int sendMsgDeviceConfigGet();
    int sendMsgDeviceCsgConfigGet();
	int sendMsgDeviceCsgConfigSet();
	int sendMsgDeviceRealDataGet();
    int sendMsgDeviceUpdate(int fileType);
	int sendMsgDeviceWaveDataGet();
	
	int sendMsgDeviceStateGet();
    
	int sendMsgWaveCol();
	
	int senMsgWaveCal();

    Ui::form *ui;   //新增的
    QSerialPort *serialPort;
    QSettings *settings;
    QString filePath;    
    QString adjFilePath;
    QString waveFilePath;
    //QThread		*newTask;
    //Protocol	*usartProtocol;
    
    char rxbuf[USART_BUF_SIZE];
    int 	rxbufLen;

    devInfo clientDevInfo;
    devConfig clientDevConfig;
    csgConfig_t csgConfig;
    realData_t clientRealData;
    devState clientDevState;
    QString energyMode[4];
    int isConnected;	// serial connect state
    int64_t upFileSize;
    uint32_t upFileRecvSize;
    uint32_t upPackIndex;
    int isDataRefresh;
    int16_t powerFre[CFG_ADC_CH_CNT][ADC_COLLECT_CNT];
    uint32_t freIndex;
    uint32_t waveIndex;
    uint16_t waveParam;
    uint8_t is_wave_up;
    uint8_t is_log_up;
    uint32_t logIndex;
    uint8_t is_data_up;
    uint32_t dataIndex;
};

#endif // WIDGET_H
