#ifndef COUNTERDEVICE_H
#define COUNTERDEVICE_H

#include <QObject>
#include <QUdpSocket>

#include "CounterChannel.h"
#include "common/utils/QSerialPortUtil.h"
#include "common/utils/QKafkaUtil.h"
#include "common/utils/QByteUtil.h"
#include "common/utils/QLogUtil.h"
#include "common/utils/SettingConfig.h"
#include "common/ConstCache.h"
#include "common/LinearRegression.h"
#include "protocol/CounterProtocolXH.h"

class CounterDevice : public QObject
{
    Q_OBJECT
public:
    explicit CounterDevice(QObject *parent = nullptr);
    ~CounterDevice();

    void initSerialPort();

    void setComName(QString comName);
    void setBaudRate(int baudRate);
    QString getDevCode();
    void setDevCode(QString devCode);
    void setDeviceId(QString deviceId);
    QString getDeviceName();
    void setDeviceName(QString deviceName);

    int getFormColIndex();
    void setFormColIndex(int idx);

    int getIsUse();
    void setIsUse(int isUse);

    bool isSerialOpen();

private:
    QString deviceId; // 设备ID
    QString devCode; // 设备编号
    QString deviceName;
    QString comName; // 串口名
    int baudRate; // 波特率
    int isUse;
    int refChannelNo;

    // 计数器所在的列索引
    int formColIndex;

    QSerialPortUtil serialUtil;
    QKafkaUtil kafkaUtil;
    QByteArray dataBuff;

    void afterFramePhase(CounterDataDto * counterData);
    void calcResidualClockData(qlonglong tm);

signals:
    void sendDataToDraw(QJsonArray messageArray);

public slots:
    void dataReceivedHandler(QByteArray data);

};

#endif // COUNTERDEVICE_H
