#ifndef OUTILMODBUS_H
#define OUTILMODBUS_H

#include <QObject>
#include "ModbusSlave.h"
#include "ModbusMaster.h"
#include "MainWindow.h"
#include "ModbusRepository.h"
#include "ZTInfo.h"
#include "ZTAlarmGenerator.h"
#include "ModbusHistorienMaster.h"

class CMainPage;

class COutilModbus : public QObject
{
    Q_OBJECT
public:
    explicit COutilModbus(QObject *parent = 0);
    ~COutilModbus();
    int Start();


    MainWindow *w;
    CModbusSlave *mModbusSEIEmulator;
    CModbusMaster *mModbusCCEmulator;
    CModbusRepository mSEIModbusRepo,mCCModbusRepo, mHistorienRepo;
    CZTInfo mCurZTInfo, mSEIZTInfo;

    CModbusPage *mModbusPageHandle;
    CSEIModbusPage *mSEIModbusPageHandle;

    CZTAlarmGenerator *mZTAlarmGenerator;
    CModbusHistorienMaster *mHistorienMaster;




    //Modules interfaces
    //CMainPage
    int ConnectCCToZTButtonPressed(QString IP, int Port);
    int DisconnectCCFromZTButtonPressed();
    int SetCCReqDelay(int delay);
    int SendMasterReadCommand();
    int UpdateZTInfo(CZTInfo *DataStruct, CModbusRepository* SourceRepo);
    int SendAN1Request();
    int SendAN2Request();
    int SendZTCZT1Request();
    int SendZTCZT2Request();
    int SetAutoRequestMode(bool Auto, int Delay);
    int EnableWatchdog(bool Enabled);
    int SendDateTime();
    int SendTrainInfo(QList<qint16> TrainInfo);
    int SendInhibitZT(qint16 InhibZT1, qint16 InhibZT2);

    int ToggleSEIServerState();
    bool IsSEIServerOpened();
    int SEIWatchdogEnable(bool Enabled);
    int SEIAN1Toggle(bool Enabled);
    int SEIAN2Toggle(bool Enabled);

   private:
    QTimer *mAutoRequestTimer;
    QTimer *mWatchdogTimer;
    bool mWatchdogEnabled;
    qint16 mWatchdog;

    QTimer *mSEIWatchdogTimer;
    qint16 mSEIWatchdogValue;

    quint16 DecToBCDWord(const quint16 word);
    quint8 DecToBCDByte(const quint8 byte);
signals:
    
public slots:

    void ModbusMasterConnected(CModbusMaster *);
    void ModbusMasterDisconnected(CModbusMaster *);
    void ModbusMasterRepositoryUpdated();

    void SEIModbusConnected(CModbusSlave *);
    void SEIModbusDisconnected(CModbusSlave *);
    void SEIRepositoryUpdated();

    void AutoRequestTimeExpired();
    void WatchdogTimerExpired();

    void SEIWatchdogExpired();
    
};

#endif // OUTILMODBUS_H
