#ifndef CMODBUSSEIMGR_H
#define CMODBUSSEIMGR_H


#include <QObject>
#include "ModbusBackend.h"
#include <QTimer>
#include <QElapsedTimer>
#include <QHostAddress>

#define SEI_MODBUS_WATCHDOG_TIMEOUT 7000   //ms


class CModbusSEIMgr : public CModbusBackend
{
    Q_OBJECT
public:
    explicit CModbusSEIMgr(CModbusRepository *SEIRepo,CModbusRepository *CCRepo, QHostAddress ServerIP, int ModbusPort, int DevID);
    ~CModbusSEIMgr();

    int ConnectToSlave(QHostAddress SlaveIP, int SlavePort);
    int DisconnectFromSlave();
    int ReadModbusRegisters();
    int StartSEICommunication();
    int SEISettingsChanged(QHostAddress ServerIP, int ModbusPort, int DevID);
    int SendZTRegistersToSEI();


    virtual void RegistersDatabaseUpdated(quint16 StartAddress, quint16 Length);
    virtual void ModbusResponseException(quint8 ExceptionCode, quint8 FctCode);
    virtual void ModbusWriteMultipleRegsSuccess();
    virtual void ModbusWriteSingleRegsSuccess();
    virtual void ModbusRequestMaxRetryReached();

    QTimer *mConnectionTimer;
    QTimer *mSEIWatchdogTimer;
    QTimer *mSEIModbusUpdateTimer;

private:
    int mModbusPort;
    QHostAddress mSEIIPAddress;
    CModbusRepository *mCCRepoHandle;
    bool mZTWatchdog;
    bool mSEILinkState;

    int mSEIWatchdogState;



    
signals:
    void ModbusMasterConnected(qint32 LocalIP, qint32 RemoteIP);
    void ModbusMasterDisconnected();
    void SEIAlarmClearUpdate(bool ZT1Clear, bool ZT2Clear);
    void SEIModbusLinkLost();
    void SEIModbusLinkRecovered();

public slots:
    void SocketConnected();
    void SocketDisconnected();
    void ConnectionTimerExpired();
    void SEIModbusUpdateTimerExpired();
    void ZTTKUpdated();
    void SEIModbusWatchdogtimerExpired();

};

#endif // CMODBUSSEIMGR_H
