#include "ModbusHistorienMaster.h"
#include "HistorianPage.h"
#include "HistorienData.h"
#include "PIHistorianDefs.h"

CModbusHistorienMaster::CModbusHistorienMaster(CModbusRepository *HistorienRepo) :
    CModbusBackend(HistorienRepo)
{

    mPageHandle = 0;
    mModbusMode = MODBUS_MASTER_MODE;


    mModbusTCPSocketHandle = new QTcpSocket();
    connect(mModbusTCPSocketHandle,&QTcpSocket::readyRead,this,&CModbusBackend::ModbusDataReady);
    connect(mModbusTCPSocketHandle,&QTcpSocket::disconnected,this,&CModbusHistorienMaster::SocketDisconnected);
    connect(mModbusTCPSocketHandle,&QTcpSocket::connected,this,&CModbusHistorienMaster::SocketConnected);

    mHistorienPollTimer = new QTimer;
    mHistorienPollTimer->setInterval(1000);
    mHistorienPollTimer->setSingleShot(false);
    connect(mHistorienPollTimer,&QTimer::timeout,this,&CModbusHistorienMaster::HistorienPollTimerExpired);

}

CModbusHistorienMaster::~CModbusHistorienMaster()
{
 //   delete mHistorianZTSocket;
    delete mHistorienPollTimer;
}

int CModbusHistorienMaster::ConnectToZTRequest(QString IP, int port)
{
    if(mModbusTCPSocketHandle->state() != QAbstractSocket::UnconnectedState)
        return RET_ERROR;

    mModbusTCPSocketHandle->connectToHost(IP,port);
    return RET_OK;
}

int CModbusHistorienMaster::DisconnectFromZTRequest()
{
    mModbusTCPSocketHandle->disconnectFromHost();
    return RET_OK;
}

void CModbusHistorienMaster::SocketConnected()
{
    qDebug("Historian module connected to ZT");
    mPageHandle->HistorianConnexionStatus(true);
    mHistorienPollTimer->start();
}

void CModbusHistorienMaster::SocketDisconnected()
{
    qDebug("Historian module disconnected");
    mPageHandle->HistorianConnexionStatus(false);
    mHistorienPollTimer->stop();
    ModbusLinkDisconnected();
}

void CModbusHistorienMaster::RegistersDatabaseUpdated(quint16 StartAddress, quint16 Length)
{
    qDebug("New Historien data received.  StartAdd:%d, Length:%d",StartAddress,Length);

    CHistorienData NewData;
    bool OK;
    mModbusRepo->GetSingleReg(HISTORIAN_ZT_STATUS_FLAGS_REG_ADD,&OK);

    NewData.mFNEnabled = 0;
    NewData.mPPEnabled = 0;
    NewData.mPGEnabled = 0;
    NewData.mPPZT2Enabled = 0;
    NewData.mMaintenanceMode = 0;
    NewData.mHPCHealth = 0;
    NewData.mSEIHealth = 0;
    NewData.mZT1Enabled = 0;
    NewData.mZT2Enabled = 0;
    NewData.mCalibPG = 0;



    quint16 reg = mModbusRepo->GetSingleReg(HISTORIAN_ZT_STATUS_FLAGS_REG_ADD,&OK);
    NewData.mFNEnabled = ((reg & HISTORIAN_ZT1_FN_ENABLED_FLAG_MASK) != 0);
    NewData.mPPEnabled = ((reg & HISTORIAN_ZT1_PP_ENABLED_FLAG_MASK) != 0);
    NewData.mPGEnabled = ((reg & HISTORIAN_ZT1_PG_ENABLED_FLAG_MASK) != 0);
    NewData.mPPZT2Enabled = ((reg & HISTORIAN_ZT2_PP_ENABLED_FLAG_MASK) != 0);
    NewData.mMaintenanceMode = ((reg & HISTORIAN_ZT_MAINTENANCE_MODE_FLAG_MASK) != 0);
    NewData.mHPCHealth = ((reg & HISTORIAN_ZT_HPC_COMM_HEALTH_FLAG_MASK) != 0);
    NewData.mSEIHealth = ((reg & HISTORIAN_ZT_SEI_COMM_FLAG_MASK) != 0);
    NewData.mZT1Enabled = ((reg & HISTORIAN_ZT_ZT1_ENABLED_FLAG_MASK) != 0);
    NewData.mZT2Enabled = ((reg & HISTORIAN_ZT_ZT2_ENABLED_FLAG_MASK) != 0);
    NewData.mCalibPG = ((reg & HISTORIAN_PG_CALIBRATION_REG_ADD) != 0);



    NewData.mBootDateYr = mModbusRepo->GetSingleReg(HISTORIAN_BOOT_DATE_YEAR_REG_ADD,&OK);
    NewData.mBootDateMonth = mModbusRepo->GetSingleReg(HISTORIAN_BOOT_DATE_MONTH_REG_ADD,&OK);
    NewData.mBootDateDay = mModbusRepo->GetSingleReg(HISTORIAN_BOOT_DATE_DAY_REG_ADD,&OK);
    NewData.mBootDateHour = mModbusRepo->GetSingleReg(HISTORIAN_BOOT_DATE_HOUR_REG_ADD,&OK);
    NewData.mBootDateMinutes = mModbusRepo->GetSingleReg(HISTORIAN_BOOT_DATE_MINUTES_REG_ADD,&OK);
    NewData.mBootDateSeconds = mModbusRepo->GetSingleReg(HISTORIAN_BOOT_DATE_SECONDS_REG_ADD,&OK);
    NewData.mTotalTrains = mModbusRepo->GetSingleReg(HISTORIAN_TRAINS_TOTAL_REG_ADD,&OK);
    NewData.mTotalDetections = mModbusRepo->GetSingleReg(HISTORIAN_DETECTIONS_TOTAL_REG_ADD,&OK);
    NewData.mTrainsToday = mModbusRepo->GetSingleReg(HISTORIAN_TRAINS_TODAY_REG_ADD ,&OK);
    NewData.mDetectToday = mModbusRepo->GetSingleReg(HISTORIAN_DETECTIONS_TODAY_REG_ADD ,&OK);
    NewData.mFNDetectToday = mModbusRepo->GetSingleReg(HISTORIAN_FN_DETECTS_TODAY_REG_ADD,&OK);
    NewData.mPPIDetectToday = mModbusRepo->GetSingleReg(HISTORIAN_PPI_ZT1_DETECTS_TODAY_REG_ADD,&OK);
    NewData.mPPEDetectToday = mModbusRepo->GetSingleReg(HISTORIAN_PPE_ZT1_DETECTS_TODAY_REG_ADD,&OK);
    NewData.mPGDetectToday = mModbusRepo->GetSingleReg(HISTORIAN_PG_DETECTS_TODAY_REG_ADD,&OK);
    NewData.mPPIZT2DetectToday = mModbusRepo->GetSingleReg(HISTORIAN_PPI_ZT2_DETECTS_TODAY_REG_ADD,&OK);
    NewData.mPPEZT2DetectToday = mModbusRepo->GetSingleReg(HISTORIAN_PPE_ZT2_DETECTS_TODAY_REG_ADD,&OK);
    NewData.mLastTrainYear = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_YEAR_REG_ADD,&OK);
    NewData.mLastTrainMonth = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_MONTH_REG_ADD,&OK);
    NewData.mLastTrainDay = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_DAY_REG_ADD,&OK);
    NewData.mLastTrainHour = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_HOUR_REG_ADD,&OK);
    NewData.mLastTrainMinutes = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_MINUTE_REG_ADD,&OK);
    NewData.mLastTrainSeconds = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_SECONDS_REG_ADD,&OK);
    NewData.mLastZone = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_ZONE_REG_ADD ,&OK);
    NewData.mNbDetectLasTrain = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_NB_DETECTS_REG_ADD ,&OK);
    NewData.mLastTrainPPI1 = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_PPI_RK_1_REG_ADD,&OK);
    NewData.mLastTrainPPI2 = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_PPI_RK_2_REG_ADD,&OK);
    NewData.mLastTrainPPI3 = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_PPI_RK_3_REG_ADD,&OK);
    NewData.mLastTrainPPI4 = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_PPI_RK_4_REG_ADD,&OK);
    NewData.mLastTrainPPE1 = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_PPE_RK_1_REG_ADD,&OK);
    NewData.mLastTrainPPE2 = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_PPE_RK_2_REG_ADD,&OK);
    NewData.mLastTrainPPE3 = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_PPE_RK_3_REG_ADD,&OK);
    NewData.mLastTrainPPE4 = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_PPE_RK_4_REG_ADD,&OK);
    NewData.mLastTrainPG1 = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_PG_RK_1_REG_ADD,&OK);
    NewData.mLastTrainPG2 = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_PG_RK_2_REG_ADD,&OK);
    NewData.mLastTrainPG3 = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_PG_RK_3_REG_ADD,&OK);
    NewData.mLastTrainPG4 = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_PG_RK_4_REG_ADD,&OK);
    NewData.mLastTrainFN1 = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_FN_RK_1_REG_ADD,&OK);
    NewData.mLastTrainFN2 = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_FN_RK_2_REG_ADD,&OK);
    NewData.mLastTrainFN3 = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_FN_RK_3_REG_ADD,&OK);
    NewData.mLastTrainFN4 = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_FN_RK_4_REG_ADD,&OK);
    NewData.mLastTrainCountErr = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_COMPTAGE_DET_REG_ADD,&OK);
    NewData.mLastTrainPEQ = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_PEQ_REG_ADD,&OK);
    NewData.mLastTrainType = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_TYPE_REG_ADD,&OK);
    NewData.mLastTrainCompo1 = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_ID1_REG_ADD,&OK);
    NewData.mLastTrainCompo2 = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_ID2_REG_ADD,&OK);
    NewData.mLastTrainCompo3 = mModbusRepo->GetSingleReg(HISTORIAN_LAST_TRAIN_ID3_REG_ADD,&OK);

    mPageHandle->UpdateDataTable(&NewData);

}

void CModbusHistorienMaster::ModbusResponseException(quint8 ExceptionCode, quint8 FctCode)
{

}

void CModbusHistorienMaster::HistorienPollTimerExpired()
{
    SendReadHoldingRegistersRequest(2000,50);    //Read all registers
}
