#ifndef PIHISTORIANMANAGER_H
#define PIHISTORIANMANAGER_H

#include <QObject>
#include "PIHistorianSession.h"
#include <QList>
#include <QTcpServer>
#include "ZTData.h"

#define PI_HISTORIAN_MODBUS_DEVICE_ID 1
#define PI_HISTORIAN_DAILY_RESET_CHECK_TIMEOUT 60000    //60 seconds

class CPIHistorianManager : public QObject
{
    Q_OBJECT
public:
    CPIHistorianManager(CModbusRepository *PIHistorianRepo, int ModbusPort, int ModbusDevID);
    virtual ~CPIHistorianManager();
    int StartPIHistorian();
    int StopPIHistorian();

    int UpdateDetectionFunctions(CZTDetectionFunctionConfig* DetectionCfg);
    int UpdatePGTreshold(int Treshold);
    int UpdateMaintenanceMode(bool MaintenanceON);
    int SetSystemStartDateTime(QDateTime StartDateTime);
    int NewZT1Passage(CZTPassageInfo ZT1PassageInfo);
    int NewZT2Passage(CZTPassageInfo ZT2PassageInfo);

    QTcpServer *mModbusServer;                      //L'instance du serveur TCP
    int mModbusPort;
    int mModbusDevID;
    CModbusRepository *mPIHistorianRepo;

    QTimer *mHistorianTimer;
    void StartHistorianTimer();
    QDate mTodaysDate;

private:
    QList<CPIHistorianSession*> mHistorianSessionsList; //Les sessions établies par le serveur sont gardées dans cette liste
    quint16 mNbPassagesToday;
    quint16 mNbDeclToday;
    quint16 mNbDeclTotal;
    quint16 mNbFNToday;
    quint16 mNbPGToday;
    quint16 mNbPPIZT1Today;
    quint16 mNbPPEZT1Today;
    quint16 mNbPPIZT2Today;
    quint16 mNbPPEZT2Today;

public slots:
    void NewModbusConnection();
    void HistorianSessionClosed(CPIHistorianSession*);

    void HPCDisconnected();
    void HPCConnected(qint32 IP1, qint32 IP2);
    void SEIConnected(qint32, qint32 IP2);
    void SEIDisconnected();
    void HistorianTimerExpired();

signals:
    void HistorianConnected();
    void HistorianDisconnected();


};

#endif // PIHISTORIANMANAGER_H
