#include "PIHistorianManager.h"
#include "ZTLog.h"
#include "ModbusRepository.h"
#include "PIHistorianDefs.h"
#include "EngLog.h"
#include <QDebug>

CPIHistorianManager::CPIHistorianManager(CModbusRepository *PIHistorianRepo, int ModbusPort, int ModbusDevID)
{
    mModbusPort = ModbusPort;
    mModbusServer = new QTcpServer();
    connect(mModbusServer,SIGNAL(newConnection()),this,SLOT(NewModbusConnection()));
    mPIHistorianRepo = PIHistorianRepo;
    mModbusDevID = ModbusDevID;

    mHistorianTimer = new QTimer;
    mHistorianTimer->setSingleShot(true);
    connect(mHistorianTimer,SIGNAL(timeout()),this,SLOT(HistorianTimerExpired()));


    mTodaysDate = QDate::currentDate();
    mNbPassagesToday = 0;
    mNbDeclToday = 0;
    mNbDeclTotal = 0;
    mNbFNToday = 0;
    mNbPGToday = 0;
    mNbPPIZT1Today = 0;
    mNbPPEZT1Today = 0;
    mNbPPIZT2Today = 0;
    mNbPPEZT2Today = 0;
}

CPIHistorianManager::~CPIHistorianManager()
{
    StopPIHistorian();
    delete mModbusServer;
    delete mHistorianTimer;
}

void CPIHistorianManager::NewModbusConnection()
{
    QTcpSocket* SessionSocket = mModbusServer->nextPendingConnection();
    if(SessionSocket != 0)
    {
        CPIHistorianSession *NewSession = new CPIHistorianSession(mPIHistorianRepo,mModbusDevID);
        connect(NewSession,SIGNAL(PIHistorianSessionClosed(CPIHistorianSession*)),this,SLOT(HistorianSessionClosed(CPIHistorianSession*)));
        mHistorianSessionsList.append(NewSession);
        NewSession->OpenSession(SessionSocket);
        emit HistorianConnected();
    }

}

void CPIHistorianManager::HistorianSessionClosed(CPIHistorianSession *SessionPtr)
{
    for(int i = 0; i < mHistorianSessionsList.size(); i++)
    {
        if(mHistorianSessionsList.at(i) == SessionPtr)
        {
            CPIHistorianSession *Session = mHistorianSessionsList.takeAt(i);
            Session->CloseSession();
            delete Session;

            if(mHistorianSessionsList.size() == 0)
            {
                emit HistorianDisconnected();
            }
            return;
        }
    }
    CZTLog::instance()->AddLogString(QString("Erreur de logique dans PIHistorianManager::HistorianSessionClosed.  [Session fermée inconnue] "));
}

int CPIHistorianManager::StartPIHistorian()
{
    mModbusServer->listen(QHostAddress::Any,mModbusPort);
    CZTLog::instance()->AddLogString(QString("Gestionnaire de l'historien PI démarré sur le port %1").arg(mModbusPort),true);
    StartHistorianTimer();
    return RET_OK;
}

int CPIHistorianManager::StopPIHistorian()
{
    mModbusServer->close();
    for(int i = 0; i < mHistorianSessionsList.size(); i++)
    {
        CPIHistorianSession *Session = mHistorianSessionsList.takeFirst();
        Session->CloseSession();
        delete Session;

    }
    mHistorianTimer->stop();
    return RET_OK;
}

int CPIHistorianManager::UpdateDetectionFunctions(CZTDetectionFunctionConfig *DetectionCfg)
{
    bool OK;
    quint16 StatusReg = mPIHistorianRepo->GetSingleReg(HISTORIAN_ZT_STATUS_FLAGS_REG_ADD,&OK);
    if(OK == false)
        return RET_ERROR;

    if(DetectionCfg->mZTDetectionConfig[DETECTION_FCT_FN].AnalysisActive == true && DetectionCfg->mZTDetectionConfig[DETECTION_FCT_FN].TKActive == true)
    {
        StatusReg |= HISTORIAN_ZT1_FN_ENABLED_FLAG_MASK;
    }
    else
    {
        StatusReg &= ~HISTORIAN_ZT1_FN_ENABLED_FLAG_MASK;
    }

    if(DetectionCfg->mZTDetectionConfig[DETECTION_FCT_PG].AnalysisActive == true && DetectionCfg->mZTDetectionConfig[DETECTION_FCT_PG].TKActive == true)
    {
        StatusReg |= HISTORIAN_ZT1_PG_ENABLED_FLAG_MASK;
    }
    else
    {
        StatusReg &= ~HISTORIAN_ZT1_PG_ENABLED_FLAG_MASK;
    }

    if(DetectionCfg->mZTDetectionConfig[DETECTION_FCT_PP].AnalysisActive == true && DetectionCfg->mZTDetectionConfig[DETECTION_FCT_PP].TKActive == true)
    {
        StatusReg |= HISTORIAN_ZT1_PP_ENABLED_FLAG_MASK;
    }
    else
    {
        StatusReg &= ~HISTORIAN_ZT1_PP_ENABLED_FLAG_MASK;
    }

    if(DetectionCfg->mZTDetectionConfig[DETECTION_FCT_PP2].AnalysisActive == true && DetectionCfg->mZTDetectionConfig[DETECTION_FCT_PP2].TKActive == true)
    {
        StatusReg |= HISTORIAN_ZT2_PP_ENABLED_FLAG_MASK;
    }
    else
    {
        StatusReg &= ~HISTORIAN_ZT2_PP_ENABLED_FLAG_MASK;
    }

    if(DetectionCfg->mZTDetectionConfig[DETECTION_FCT_ZT1].AnalysisActive == true && DetectionCfg->mZTDetectionConfig[DETECTION_FCT_ZT1].TKActive == true)
    {
        StatusReg |= HISTORIAN_ZT_ZT1_ENABLED_FLAG_MASK;
    }
    else
    {
        StatusReg &= ~HISTORIAN_ZT_ZT1_ENABLED_FLAG_MASK;
    }

    if(DetectionCfg->mZTDetectionConfig[DETECTION_FCT_ZT2].AnalysisActive == true && DetectionCfg->mZTDetectionConfig[DETECTION_FCT_ZT2].TKActive == true)
    {
        StatusReg |= HISTORIAN_ZT_ZT2_ENABLED_FLAG_MASK;
    }
    else
    {
        StatusReg &= ~HISTORIAN_ZT_ZT2_ENABLED_FLAG_MASK;
    }

    return mPIHistorianRepo->WriteSingleReg(HISTORIAN_ZT_STATUS_FLAGS_REG_ADD,StatusReg);

}


int CPIHistorianManager::UpdatePGTreshold(int Treshold)
{
    return mPIHistorianRepo->WriteSingleReg(HISTORIAN_PG_CALIBRATION_REG_ADD,(quint16)Treshold);

}

int CPIHistorianManager::UpdateMaintenanceMode(bool MaintenanceON)
{
    bool OK;
    quint16 StatusReg = mPIHistorianRepo->GetSingleReg(HISTORIAN_ZT_STATUS_FLAGS_REG_ADD,&OK);
    if(OK == false)
        return RET_ERROR;

    if(MaintenanceON)
    {
        StatusReg |= HISTORIAN_ZT_MAINTENANCE_MODE_FLAG_MASK;
    }
    else
    {
        StatusReg &= ~HISTORIAN_ZT_MAINTENANCE_MODE_FLAG_MASK;
    }

    return mPIHistorianRepo->WriteSingleReg(HISTORIAN_ZT_STATUS_FLAGS_REG_ADD,StatusReg);

}

int CPIHistorianManager::SetSystemStartDateTime(QDateTime StartDateTime)
{
    quint16 year, month, day, hour, minute, seconds;
    year = (quint16)StartDateTime.date().year();
    day = (quint16)StartDateTime.date().day();
    month = (quint16)StartDateTime.date().month();
    hour = (quint16)StartDateTime.time().hour();
    minute = (quint16)StartDateTime.time().minute();
    seconds = (quint16)StartDateTime.time().second();

    int ret;

    ret = mPIHistorianRepo->WriteSingleReg(HISTORIAN_BOOT_DATE_YEAR_REG_ADD,year);
    ret |= mPIHistorianRepo->WriteSingleReg(HISTORIAN_BOOT_DATE_DAY_REG_ADD,day);
    ret |= mPIHistorianRepo->WriteSingleReg(HISTORIAN_BOOT_DATE_MONTH_REG_ADD,month);
    ret |= mPIHistorianRepo->WriteSingleReg(HISTORIAN_BOOT_DATE_HOUR_REG_ADD,hour);
    ret |= mPIHistorianRepo->WriteSingleReg(HISTORIAN_BOOT_DATE_MINUTES_REG_ADD,minute);
    ret |= mPIHistorianRepo->WriteSingleReg(HISTORIAN_BOOT_DATE_SECONDS_REG_ADD,seconds);

    return ret;
}

void CPIHistorianManager::HPCDisconnected()
{
    bool OK;
    quint16 StatusReg = mPIHistorianRepo->GetSingleReg(HISTORIAN_ZT_STATUS_FLAGS_REG_ADD,&OK);
    if(OK == false)
        return;

    StatusReg &= ~HISTORIAN_ZT_HPC_COMM_HEALTH_FLAG_MASK;


    mPIHistorianRepo->WriteSingleReg(HISTORIAN_ZT_STATUS_FLAGS_REG_ADD,StatusReg);
}

void CPIHistorianManager::HPCConnected(qint32 IP1,qint32 IP2)
{
    Q_UNUSED(IP1)
    Q_UNUSED(IP2)

    bool OK;
    quint16 StatusReg = mPIHistorianRepo->GetSingleReg(HISTORIAN_ZT_STATUS_FLAGS_REG_ADD,&OK);
    if(OK == false)
        return;

    StatusReg |= HISTORIAN_ZT_HPC_COMM_HEALTH_FLAG_MASK;


    mPIHistorianRepo->WriteSingleReg(HISTORIAN_ZT_STATUS_FLAGS_REG_ADD,StatusReg);
}

void CPIHistorianManager::SEIConnected(qint32 IP1,qint32 IP2)
{
    Q_UNUSED(IP1)
    Q_UNUSED(IP2)

    bool OK;
    quint16 StatusReg = mPIHistorianRepo->GetSingleReg(HISTORIAN_ZT_STATUS_FLAGS_REG_ADD,&OK);
    if(OK == false)
        return;

    StatusReg |= HISTORIAN_ZT_SEI_COMM_FLAG_MASK;


    mPIHistorianRepo->WriteSingleReg(HISTORIAN_ZT_STATUS_FLAGS_REG_ADD,StatusReg);

}

void CPIHistorianManager::SEIDisconnected()
{    bool OK;
     quint16 StatusReg = mPIHistorianRepo->GetSingleReg(HISTORIAN_ZT_STATUS_FLAGS_REG_ADD,&OK);
     if(OK == false)
         return;

     StatusReg &= ~HISTORIAN_ZT_SEI_COMM_FLAG_MASK;


     mPIHistorianRepo->WriteSingleReg(HISTORIAN_ZT_STATUS_FLAGS_REG_ADD,StatusReg);

}

int CPIHistorianManager::NewZT1Passage(CZTPassageInfo ZT1PassageInfo)
{
    QByteArray EmptyList;   //create an empty buffer to reset modbus table values
    EmptyList.fill(0x00, 18*2);
    //Reset last declenchements values in the modbus table
    mPIHistorianRepo->WriteHRData(HISTORIAN_LAST_TRAIN_PPI_RK_1_REG_ADD,18,EmptyList);

    mPIHistorianRepo->WriteSingleReg(HISTORIAN_TRAINS_TOTAL_REG_ADD,ZT1PassageInfo.mNbPassages);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_ZONE_REG_ADD,1);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_TYPE_REG_ADD,ZT1PassageInfo.mTrainType);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_ID1_REG_ADD,ZT1PassageInfo.mTrainCompo1);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_ID2_REG_ADD,ZT1PassageInfo.mTrainCompo2);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_ID3_REG_ADD,ZT1PassageInfo.mTrainCompo3);

    mNbPassagesToday ++;
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_TRAINS_TODAY_REG_ADD,mNbPassagesToday);


    QDateTime PassageDateTime = ZT1PassageInfo.mPassageDateTime;
    quint16 year, month, day, hour, minute, seconds;
    year = (quint16)PassageDateTime.date().year();
    day = (quint16)PassageDateTime.date().day();
    month = (quint16)PassageDateTime.date().month();
    hour = (quint16)PassageDateTime.time().hour();
    minute = (quint16)PassageDateTime.time().minute();
    seconds = (quint16)PassageDateTime.time().second();

    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_YEAR_REG_ADD,year);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_DAY_REG_ADD,day);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_MONTH_REG_ADD,month);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_HOUR_REG_ADD,hour);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_MINUTE_REG_ADD,minute);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_SECONDS_REG_ADD,seconds);


    quint16 NbDecl = ZT1PassageInfo.mDetections->size();

    quint16 NbDeclFN = 0, NbDeclPG = 0, NbDeclPPI = 0, NbDeclPPE = 0;
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_NB_DETECTS_REG_ADD,NbDecl);
    if(NbDecl != 0)
    {
        for(int i = 0; i < NbDecl; i++)
        {
            CZTDetectionData *CurDetection = ZT1PassageInfo.mDetections->at(i);

            switch(CurDetection->mDetectionID)
            {
                case DETECTION_MAGNETIC_SENSOR_COUNT:
                {
                    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_COMPTAGE_DET_REG_ADD,1);
                    break;
                }
                case DETECTION_FN_DETECTION:
                {
                    NbDeclFN++;
                    if(NbDeclFN == 1)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_FN_RK_1_REG_ADD,CurDetection->mRank);
                    }
                    else if(NbDeclFN == 2)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_FN_RK_2_REG_ADD,CurDetection->mRank);
                    }
                    else if(NbDeclFN == 3)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_FN_RK_3_REG_ADD,CurDetection->mRank);
                    }
                    else if(NbDeclFN == 4)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_FN_RK_4_REG_ADD,CurDetection->mRank);
                    }
                    else
                    {
                        //Ignore if more than 4
                    }

                    break;
                }
                case DETECTION_PG_DETECTION:
                {
                    NbDeclPG++;
                    if(NbDeclPG == 1)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PG_RK_1_REG_ADD,CurDetection->mRank);
                    }
                    else if(NbDeclPG == 2)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PG_RK_2_REG_ADD,CurDetection->mRank);
                    }
                    else if(NbDeclPG == 3)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PG_RK_3_REG_ADD,CurDetection->mRank);
                    }
                    else if(NbDeclPG == 4)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PG_RK_4_REG_ADD,CurDetection->mRank);
                    }
                    else
                    {
                        //Ignore if more than 4
                    }
                    break;
                }
                case DETECTION_PPI_DETECTION:
                {
                    NbDeclPPI++;
                    if(NbDeclPPI == 1)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PPI_RK_1_REG_ADD,CurDetection->mRank);
                    }
                    else if(NbDeclPPI == 2)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PPI_RK_2_REG_ADD,CurDetection->mRank);
                    }
                    else if(NbDeclPPI == 3)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PPI_RK_3_REG_ADD,CurDetection->mRank);
                    }
                    else if(NbDeclPPI == 4)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PPI_RK_4_REG_ADD,CurDetection->mRank);
                    }
                    else
                    {
                        //Ignore if more than 4
                    }
                    break;
                }
                case DETECTION_PPE_DETECTION:
                {
                    NbDeclPPE++;
                    if(NbDeclPPE == 1)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PPE_RK_1_REG_ADD,CurDetection->mRank);
                    }
                    else if(NbDeclPPE == 2)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PPE_RK_2_REG_ADD,CurDetection->mRank);
                    }
                    else if(NbDeclPPE == 3)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PPE_RK_3_REG_ADD,CurDetection->mRank);
                    }
                    else if(NbDeclPPE == 4)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PPE_RK_4_REG_ADD,CurDetection->mRank);
                    }
                    else
                    {
                        //Ignore if more than 4
                    }
                    break;
                }
                case DETECTION_PEQ1_DETECTION:
                {
                    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PEQ_REG_ADD,1);
                    break;
                }
            }
        }



        mNbDeclToday += NbDecl;
        mNbDeclTotal += NbDecl;
        mNbFNToday += NbDeclFN;
        mNbPGToday += NbDeclPG;
        mNbPPIZT1Today += NbDeclPPI;
        mNbPPEZT1Today += NbDeclPPE;

    }

    mPIHistorianRepo->WriteSingleReg(HISTORIAN_DETECTIONS_TODAY_REG_ADD,mNbDeclToday);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_DETECTIONS_TOTAL_REG_ADD,ZT1PassageInfo.mNbDeclenchements);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_FN_DETECTS_TODAY_REG_ADD,mNbFNToday);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_PPI_ZT1_DETECTS_TODAY_REG_ADD,mNbPPIZT1Today);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_PPE_ZT1_DETECTS_TODAY_REG_ADD,mNbPPEZT1Today);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_PG_DETECTS_TODAY_REG_ADD,mNbPGToday);


    return RET_OK;
}


int CPIHistorianManager::NewZT2Passage(CZTPassageInfo ZT2PassageInfo)
{
    QByteArray EmptyList;   //create an empty buffer to reset modbus table values
    EmptyList.fill(0x00, 18*2);
    //Reset last declenchements values in the modbus table
    mPIHistorianRepo->WriteHRData(HISTORIAN_LAST_TRAIN_PPI_RK_1_REG_ADD,18,EmptyList);

    mPIHistorianRepo->WriteSingleReg(HISTORIAN_TRAINS_TOTAL_REG_ADD,ZT2PassageInfo.mNbPassages);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_ZONE_REG_ADD,2);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_TYPE_REG_ADD,ZT2PassageInfo.mTrainType);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_ID1_REG_ADD,ZT2PassageInfo.mTrainCompo1);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_ID2_REG_ADD,ZT2PassageInfo.mTrainCompo2);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_ID3_REG_ADD,ZT2PassageInfo.mTrainCompo3);

    QDateTime PassageDateTime = ZT2PassageInfo.mPassageDateTime;
    quint16 year, month, day, hour, minute, seconds;
    year = (quint16)PassageDateTime.date().year();
    day = (quint16)PassageDateTime.date().day();
    month = (quint16)PassageDateTime.date().month();
    hour = (quint16)PassageDateTime.time().hour();
    minute = (quint16)PassageDateTime.time().minute();
    seconds = (quint16)PassageDateTime.time().second();

    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_YEAR_REG_ADD,year);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_DAY_REG_ADD,day);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_MONTH_REG_ADD,month);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_HOUR_REG_ADD,hour);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_MINUTE_REG_ADD,minute);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_SECONDS_REG_ADD,seconds);


    quint16 NbDecl = ZT2PassageInfo.mDetections->size();

    quint16 NbDeclPPI = 0, NbDeclPPE = 0;
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_NB_DETECTS_REG_ADD,NbDecl);
    if(NbDecl != 0)
    {
        for(int i = 0; i < NbDecl; i++)
        {
            CZTDetectionData *CurDetection = ZT2PassageInfo.mDetections->at(i);

            switch(CurDetection->mDetectionID)
            {
                case DETECTION_ZT2_MAGNETIC_SENSOR_COUNT:
                {
                    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_COMPTAGE_DET_REG_ADD,1);
                    break;
                }
                case DETECTION_ZT2_PPI_DETECTION:
                {
                    NbDeclPPI++;
                    if(NbDeclPPI == 1)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PPI_RK_1_REG_ADD,CurDetection->mRank);
                    }
                    else if(NbDeclPPI == 2)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PPI_RK_2_REG_ADD,CurDetection->mRank);
                    }
                    else if(NbDeclPPI == 3)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PPI_RK_3_REG_ADD,CurDetection->mRank);
                    }
                    else if(NbDeclPPI == 4)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PPI_RK_4_REG_ADD,CurDetection->mRank);
                    }
                    else
                    {
                        //Ignore if more than 4
                    }
                    break;
                }
                case DETECTION_ZT2_PPE_DETECTION:
                {
                    NbDeclPPE++;
                    if(NbDeclPPE == 1)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PPE_RK_1_REG_ADD,CurDetection->mRank);
                    }
                    else if(NbDeclPPE == 2)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PPE_RK_2_REG_ADD,CurDetection->mRank);
                    }
                    else if(NbDeclPPE == 3)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PPE_RK_3_REG_ADD,CurDetection->mRank);
                    }
                    else if(NbDeclPPE == 4)
                    {
                        mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PPE_RK_4_REG_ADD,CurDetection->mRank);
                    }
                    else
                    {
                        //Ignore if more than 4
                    }
                    break;
                }
                case DETECTION_PEQ2_DETECTION:
                {
                    mPIHistorianRepo->WriteSingleReg(HISTORIAN_LAST_TRAIN_PEQ_REG_ADD,1);
                    break;
                }
            }
        }


        mNbDeclToday += NbDecl;
        mNbDeclTotal += NbDecl;
        mNbPPIZT2Today += NbDeclPPI;
        mNbPPEZT2Today += NbDeclPPE;



    }

    mPIHistorianRepo->WriteSingleReg(HISTORIAN_TRAINS_TODAY_REG_ADD,mNbPassagesToday);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_DETECTIONS_TOTAL_REG_ADD,ZT2PassageInfo.mNbDeclenchements);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_DETECTIONS_TODAY_REG_ADD,mNbDeclToday);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_PPI_ZT2_DETECTS_TODAY_REG_ADD,mNbPPIZT2Today);
    mPIHistorianRepo->WriteSingleReg(HISTORIAN_PPE_ZT2_DETECTS_TODAY_REG_ADD,mNbPPEZT2Today);

    return RET_OK;
}
void CPIHistorianManager::StartHistorianTimer()
{

    mHistorianTimer->start(PI_HISTORIAN_DAILY_RESET_CHECK_TIMEOUT);

}

void CPIHistorianManager::HistorianTimerExpired()
{
  //  qDebug(qPrintable(QString("Vérification du changement de date: Today:%1, Current:%2").arg(mTodaysDate.toString("yyyy-MM-dd")).arg(QDate::currentDate().toString("yyyy-MM-dd"))));
    if(mTodaysDate != QDate::currentDate() &&
       QTime::currentTime() >= QTime(3,0,0,0))
    {
        mNbFNToday = 0;
        mNbPGToday = 0;
        mNbPPIZT1Today = 0;
        mNbPPEZT1Today = 0;
        mNbPPIZT2Today = 0;
        mNbPPEZT2Today = 0;
        mNbDeclToday = 0;
        mNbPassagesToday = 0;

        mTodaysDate = QDate::currentDate();

        mPIHistorianRepo->WriteSingleReg(HISTORIAN_DETECTIONS_TODAY_REG_ADD,mNbDeclToday);
        mPIHistorianRepo->WriteSingleReg(HISTORIAN_FN_DETECTS_TODAY_REG_ADD,mNbFNToday);
        mPIHistorianRepo->WriteSingleReg(HISTORIAN_PPI_ZT1_DETECTS_TODAY_REG_ADD,mNbPPIZT1Today);
        mPIHistorianRepo->WriteSingleReg(HISTORIAN_PPE_ZT1_DETECTS_TODAY_REG_ADD,mNbPPEZT1Today);
        mPIHistorianRepo->WriteSingleReg(HISTORIAN_PG_DETECTS_TODAY_REG_ADD,mNbPGToday);
        mPIHistorianRepo->WriteSingleReg(HISTORIAN_PPI_ZT2_DETECTS_TODAY_REG_ADD,mNbPPIZT2Today);
        mPIHistorianRepo->WriteSingleReg(HISTORIAN_PPE_ZT2_DETECTS_TODAY_REG_ADD,mNbPPEZT2Today);
        mPIHistorianRepo->WriteSingleReg(HISTORIAN_TRAINS_TODAY_REG_ADD,mNbPassagesToday);

        CEngLog::instance()->AddLogString("Reset des stats historien du jour",2);

    }

    StartHistorianTimer();
}
