#ifndef LOCK_H
#define LOCK_H
#include <QObject>
#include <QThread>
#include <QDebug>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QHash>
#include <QMutex>
#include "define.h"

enum class lockWorkStat {
    init,
    readLockStat,
    controlLock,
    controlLight,
};

class lock : public QThread
{
    Q_OBJECT
public:
    explicit lock(QObject *parent = nullptr);

public slots:
    void slot_uartinfo_change(void);
    bool slot_openLock(void);           // 开锁
    void slot_set_thread_stop(void);

private:
    bool threadstatus = true;
    QSerialPort *devserial;
    bool openPort = false;
    lockWorkStat workstat = lockWorkStat::init;
    bool rightLockStat = true;
    bool leftLockStat = true;
    bool setRightlightStat = false;
    bool setLeftlightStat = false;

    bool openSerial(void);
    void sendData(QByteArray &sendData);
    bool readData(QByteArray *data, quint16 timeout);
    quint16 uiCrc16Cal(QByteArray &data, quint8 count);
    void getBoardType(void);
    void openLock(void);
    void openLight(void);
    void readLockStat(void);

signals:
    void signal_lock_run(bool stat);
    void signal_lockStat(bool leftStat, bool rightStat);    // 锁状态改变

protected:
    void run() override;
};

#endif // LOCK_H
