#include "ModbusDisplayPage.h"
#include "Zonetest.h"
#include <QGraphicsProxyWidget>
#include "ModbusCCDefs.h"
#include "ModbusSEIDefs.h"
#include "ModbusRepository.h"
#include <QGraphicsTextItem>
#include "ZTData.h"

CModbusDisplayPage::CModbusDisplayPage(QGraphicsWidget *parent)
{
    setParentItem(parent);

    mProgramHandle = 0;
    mCCRepoHandle = 0;
    mSEIRepoHandle = 0;


    mBackgroundRect = new QGraphicsRectItem(boundingRect(), this);
    QBrush BackgroundBrush(QColor(245, 245, 255,200));
    mBackgroundRect->setBrush(BackgroundBrush);

    QGraphicsTextItem *TableTitle = new QGraphicsTextItem("Table Modbus Commande Centralisée",this);
    QFont font;
    font.setPointSize(18);
    TableTitle->setFont(font);
    TableTitle->setPos(40,10);

    TableTitle = new QGraphicsTextItem("Table Modbus SEI",this);
    TableTitle->setFont(font);
    TableTitle->setPos(650,10);


    mCloseBtn = new CTextButtonWidget("Fermer");
    mCloseBtn->setParentItem(this);
    mCloseBtn->setPos(600,610);
    connect(mCloseBtn,SIGNAL(TxtButtonClicked(CTextButtonWidget*)),this,SLOT(ButtonClicked(CTextButtonWidget*)));

    mModbusCCDataTree = new QTreeWidget;
    QGraphicsProxyWidget *Proxy = new QGraphicsProxyWidget(this);
    Proxy->setWidget(mModbusCCDataTree);
    Proxy->setPos(0,0);

    mModbusCCDataTree->resize(350,750);

    mModbusSEIDataTree = new QTreeWidget;
    Proxy = new QGraphicsProxyWidget(this);
    Proxy->setWidget(mModbusSEIDataTree);
    Proxy->setPos(400,0);

    mModbusSEIDataTree->resize(350,750);

    mRefreshTimer = new QTimer();
    mRefreshTimer->setSingleShot(false);
    mRefreshTimer->setInterval(300);
    mRefreshTimer->stop();
    connect(mRefreshTimer,SIGNAL(timeout()),this,SLOT(RefreshTimerExpired()));


    QList<QTreeWidgetItem *> CCTableItems;
    mModbusCCDataTree->setColumnCount(3);
    mModbusCCDataTree->setHeaderItem(new QTreeWidgetItem(QStringList()<<"Flag/Alarme CC"<<"Valeur CC"<<"Rang / Donnée "));

    //Items.append(new QTreeWidgetItem(QStringList(QString().fromUtf8("Pneu De Guidage ZT1 #2") << QString().fromUtf8("Pneu De Guidage ZT1 #2") << QString().fromUtf8("Pneu De Guidage ZT1 #2"))));
    CCTableItems.append(new QTreeWidgetItem(QStringList()<<"ZT1"));
    CCTableItems.append(new QTreeWidgetItem(QStringList()<<"ZT2"));
    CCTableItems.append(new QTreeWidgetItem(QStringList()<<"ZT"));
    CCTableItems.append(new QTreeWidgetItem(QStringList()<<"CC"));

    QTreeWidgetItem* Item;
    //  Item = new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Alarmes"));
    //  CCTableItems.at(0)->addChild(Item);

    Item = CCTableItems.at(0);  //ZT1

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT1 #1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT1 #2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT1 #3")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT1 #4")<<"0"<<"---"));

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT1 #1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT1 #2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT1 #3")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT1 #4")<<"0"<<"---"));

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu De Guidage ZT1 #1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu De Guidage ZT1 #2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu De Guidage ZT1 #3")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu De Guidage ZT1 #4")<<"0"<<"---"));

    Item ->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Frotteur Négatif ZT1 #1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Frotteur Négatif ZT1 #2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Frotteur Négatif ZT1 #3")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Frotteur Négatif ZT1 #4")<<"0"<<"---"));

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Itinéraire")<<"0"<<"---"));

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Alarme V00 ZT1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Panne Équipement ZT1")<<"0"<<"---"));

    Item = CCTableItems.at(1);  //ZT2

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT2 #1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT2 #2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT2 #3")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT2 #4")<<"0"<<"---"));

    // Items.at(1)->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("  ")));

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT2 #1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT2 #2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT2 #3")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT2 #4")<<"0"<<"---"));

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Alarme V02 ZT2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Panne Équipement ZT2")<<"0"<<"---"));

    Item = CCTableItems.at(2);  //ZT
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Watchdog ZT")<<"0"<<"---"));

    Item = CCTableItems.at(3);  //CC
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Watchdog CC")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("AN1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("AN2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Inhib. ZT1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Inhib ZT2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Type MR ZT1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Type MR ZT2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("ID1 MR ZT1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("ID2 MR ZT1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("ID3 MR ZT1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("ID1 MR ZT2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("ID2 MR ZT2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("ID3 MR ZT2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("MAJ Date")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Secondes")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Heure Minutes")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Mois Jour")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Année")<<"0"<<"---"));

    mModbusCCDataTree->insertTopLevelItems(0,CCTableItems);
    mModbusCCDataTree->resizeColumnToContents(1);

    mModbusCCDataTree->expandAll();
    mModbusCCDataTree->resizeColumnToContents(0);





    QList<QTreeWidgetItem *> SEITableItems;

    mModbusSEIDataTree->setColumnCount(3);
    mModbusSEIDataTree->setHeaderItem(new QTreeWidgetItem(QStringList()<<"Flag/Alarme SEI"<<"Valeur SEI"<<"Rang SEI"));


    //Items.append(new QTreeWidgetItem(QStringList(QString().fromUtf8("Pneu De Guidage ZT1 #2") << QString().fromUtf8("Pneu De Guidage ZT1 #2") << QString().fromUtf8("Pneu De Guidage ZT1 #2"))));
    SEITableItems.append(new QTreeWidgetItem(QStringList()<<"ZT1"));
    SEITableItems.append(new QTreeWidgetItem(QStringList()<<"ZT2"));
    SEITableItems.append(new QTreeWidgetItem(QStringList()<<"ZT"));
    SEITableItems.append(new QTreeWidgetItem(QStringList() << "SEI"));

  //  QTreeWidgetItem* Item;
    //  Item = new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Alarmes"));
    //  SEITableItems.at(0)->addChild(Item);

    Item = SEITableItems.at(0); //ZT1

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT1 #1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT1 #2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT1 #3")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT1 #4")<<"0"<<"---"));

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT1 #1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT1 #2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT1 #3")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT1 #4")<<"0"<<"---"));

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu De Guidage ZT1 #1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu De Guidage ZT1 #2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu De Guidage ZT1 #3")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu De Guidage ZT1 #4")<<"0"<<"---"));

    Item ->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Frotteur Négatif ZT1 #1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Frotteur Négatif ZT1 #2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Frotteur Négatif ZT1 #3")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Frotteur Négatif ZT1 #4")<<"0"<<"---"));

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Itinéraire")<<"0"<<"---"));

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Alarme V00 ZT1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Panne Équipement ZT1")<<"0"<<"---"));

    Item = SEITableItems.at(1); //ZT2

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT2 #1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT2 #2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT2 #3")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT2 #4")<<"0"<<"---"));

    // Items.at(1)->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("  ")));

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT2 #1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT2 #2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT2 #3")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT2 #4")<<"0"<<"---"));

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Alarme V02 ZT2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Panne Équipement ZT2")<<"0"<<"---"));

    Item = SEITableItems.at(2); //ZT
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Watchdog ZT")<<"0"<<"---"));

    Item = SEITableItems.at(3); //SEI
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Watchdog SEI")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("AN1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("AN2")<<"0"<<"---"));


    mModbusSEIDataTree->insertTopLevelItems(0,SEITableItems);
    mModbusSEIDataTree->resizeColumnToContents(1);

    mModbusSEIDataTree->expandAll();
    mModbusSEIDataTree->resizeColumnToContents(0);

   }


void CModbusDisplayPage::resizeEvent(QGraphicsSceneResizeEvent *event)
{
    Q_UNUSED(event)
    mBackgroundRect->setRect(boundingRect());
    mCloseBtn->setPos(50,boundingRect().height()-50);
    mModbusCCDataTree->setGeometry(0,50,((boundingRect().width()/2) - 10),boundingRect().height()-100);
    mModbusSEIDataTree->setGeometry((boundingRect().width()/2),50,(boundingRect().width()/2),boundingRect().height()-100);

}

CModbusDisplayPage::~CModbusDisplayPage()
{
    delete mModbusCCDataTree;
}

void CModbusDisplayPage::ButtonClicked(CTextButtonWidget *button)
{
    if(button == mCloseBtn)
    {
        mRefreshTimer->stop();
        mProgramHandle->CloseModbusDataPageRequest();
    }
}

int CModbusDisplayPage::UpdateTable(int Source)
{
    return RET_OK;
}

void CModbusDisplayPage::UpdateCCModbusTable()
{
    if(mCCRepoHandle == 0  ||
       mSEIRepoHandle == 0 ||
       mProgramHandle == 0)
    {
        return;
    }

    CZTInfo *DataStruct = new CZTInfo;
    bool OK = false;

    quint16 data = mCCRepoHandle->GetSingleReg(MODBUS_ZT1_ALARM_DATA_BASE_REG_ADD,&OK);

    if(OK == true)
    {

        DataStruct->mZT1PPIAlarmFlag1 = (data & ZT1_PP_INT_FLAG_MASK_1) != 0;
        DataStruct->mZT1PPIAlarmFlag2 = (data & ZT1_PP_INT_FLAG_MASK_2) != 0;
        DataStruct->mZT1PPIAlarmFlag3 = (data & ZT1_PP_INT_FLAG_MASK_3) != 0;
        DataStruct->mZT1PPIAlarmFlag4 = (data & ZT1_PP_INT_FLAG_MASK_4) != 0;
        DataStruct->mZT1PPEAlarmFlag1 = (data & ZT1_PP_EXT_FLAG_MASK_1) != 0;
        DataStruct->mZT1PPEAlarmFlag2 = (data & ZT1_PP_EXT_FLAG_MASK_2) != 0;
        DataStruct->mZT1PPEAlarmFlag3 = (data & ZT1_PP_EXT_FLAG_MASK_3) != 0;
        DataStruct->mZT1PPEAlarmFlag4 = (data & ZT1_PP_EXT_FLAG_MASK_4) != 0;
        DataStruct->mZT1PGAlarmFlag1 = (data & ZT1_PG_FLAG_MASK_1) != 0;
        DataStruct->mZT1PGAlarmFlag2 = (data & ZT1_PG_FLAG_MASK_2) != 0;
        DataStruct->mZT1PGAlarmFlag3 = (data & ZT1_PG_FLAG_MASK_3) != 0;
        DataStruct->mZT1PGAlarmFlag4 = (data & ZT1_PG_FLAG_MASK_4) != 0;
        DataStruct->mZT1FNAlarmFlag1 = (data & ZT1_FN_FLAG_MASK_1) != 0;
        DataStruct->mZT1FNAlarmFlag2 = (data & ZT1_FN_FLAG_MASK_2) != 0;
        DataStruct->mZT1FNAlarmFlag3 = (data & ZT1_FN_FLAG_MASK_3) != 0;
        DataStruct->mZT1FNAlarmFlag4 = (data & ZT1_FN_FLAG_MASK_4) != 0;

    }
    else
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_ZT1_ALARM_DATA_BASE_REG_ADD)");
        return;
    }

    QList<qint16> RankData;
    RankData = mCCRepoHandle->GetRegs(MODBUS_ZT1_ALARM_RANKS_BASE_ADD,16,&OK);
    if(OK == true)
    {
        DataStruct->mZT1Ranks = RankData;
    }
    else
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_ZT1_ALARM_RANKS_BASE_ADD)");
        return;
    }

    data = mCCRepoHandle->GetSingleReg(MODBUS_ZT2_ALARM_DATA_BASE_REG_ADD,&OK);
    if(OK == true)
    {
        DataStruct->mZT2PPIAlarmFlag1 = (data & ZT2_PP_INT_FLAG_MASK_1) != 0;
        DataStruct->mZT2PPIAlarmFlag2 = (data & ZT2_PP_INT_FLAG_MASK_2) != 0;
        DataStruct->mZT2PPIAlarmFlag3 = (data & ZT2_PP_INT_FLAG_MASK_3) != 0;
        DataStruct->mZT2PPIAlarmFlag4 = (data & ZT2_PP_INT_FLAG_MASK_4) != 0;
        DataStruct->mZT2PPEAlarmFlag1 = (data & ZT2_PP_EXT_FLAG_MASK_1) != 0;
        DataStruct->mZT2PPEAlarmFlag2 = (data & ZT2_PP_EXT_FLAG_MASK_2) != 0;
        DataStruct->mZT2PPEAlarmFlag3 = (data & ZT2_PP_EXT_FLAG_MASK_3) != 0;
        DataStruct->mZT2PPEAlarmFlag4 = (data & ZT2_PP_EXT_FLAG_MASK_4) != 0;
    }
    else
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_ZT2_ALARM_DATA_BASE_REG_ADD)");
        return;
    }

    RankData.clear();
    RankData = mCCRepoHandle->GetRegs(MODBUS_ZT2_ALARM_RANKS_BASE_ADD,8,&OK);
    if(OK == true)
    {
        DataStruct->mZT2Ranks = RankData;
    }
    else
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_ZT2_ALARM_RANKS_BASE_ADD)");
        return;
    }


    data = mCCRepoHandle->GetSingleReg(MODBUS_MISC_DATA_BASE_REG_ADD,&OK);

    if(OK == true)
    {

        DataStruct->mZT1V00AlarmFlag = (data & ZT1_V00_ALARM_FLAG_MASK) != 0;
        DataStruct->mZT1PEQ1AlarmFlag = (data & ZT1_PEQ1_ALARM_FLAG_MASK) != 0;
        DataStruct->mZT2V02AlarmFlag = (data & ZT2_V02_ALARM_FLAG_MASK) != 0;
        DataStruct->mZT2PEQ2AlarmFlag = (data & ZT2_PEQ2_ALARM_FLAG_MASK) != 0;
        DataStruct->mITI10_12Flag = (data & ZT1_ALARM_ITI_FLAG_MASK) != 0;
    }
    else
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_PANNES_DATA_BASE_REG_ADD)");
        return;
    }

    DataStruct->mWatchdogZT = mCCRepoHandle->GetSingleReg(MODBUS_ZT_WATCHDOG_REG_ADD,&OK);
    if(OK == false)
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_ZT_WATCHDOG_REG_ADD)");
        return;
    }

    data = mCCRepoHandle->GetSingleReg(MODBUS_CC_WATCHDOG_BASE_REG_ADD,&OK);

    if(OK == true)
    {
        DataStruct->mWatchdogRemote = (int)data;
    }
    else
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (SEI_MODBUS_SEI_WATCHDOG_REG)");
        return;
    }

    data = mCCRepoHandle->GetSingleReg(MODBUS_CC_AN_BASE_REG_ADD, &OK);
    if(OK == true)
    {
        DataStruct->mAN1 = ((data & MODBUS_CC_FCYCLE_ZT1_FLAG_MASK) != 0);
        DataStruct->mAN2 = ((data & MODBUS_CC_FCYCLE_ZT2_FLAG_MASK) != 0);
        DataStruct->mInhibitZT1 = ((data & MODBUS_CC_INHIBIT_ZT1_FLAG_MASK) != 0);
        DataStruct->mInhibitZT2 = ((data & MODBUS_CC_INHIBIT_ZT2_FLAG_MASK) != 0);
    }
    else
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (SEI_MODBUS_SEI_ALARMS_RESET_REG)");
        return;
    }


    DataStruct->mZT1TrainType = mCCRepoHandle->GetSingleReg(MODBUS_CC_ZT1_TRAIN_TYPE_REG_ADD,&OK);
    if(OK == false)
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_CC_ZT1_TRAIN_TYPE_REG_ADD)");
        return;
    }

    DataStruct->mZT2TrainType = mCCRepoHandle->GetSingleReg(MODBUS_CC_ZT2_TRAIN_TYPE_REG_ADD,&OK);
    if(OK == false)
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_CC_ZT2_TRAIN_TYPE_REG_ADD)");
        return;
    }

    DataStruct->mZT1TrainID1 = mCCRepoHandle->GetSingleReg(MODBUS_CC_ZT1_TRAIN_ID_1_REG_ADD,&OK);
    if(OK == false)
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_CC_ZT1_TRAIN_ID_1_REG_ADD)");
        return;
    }
    DataStruct->mZT1TrainID2 = mCCRepoHandle->GetSingleReg(MODBUS_CC_ZT1_TRAIN_ID_2_REG_ADD,&OK);
    if(OK == false)
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_CC_ZT1_TRAIN_ID_2_REG_ADD)");
        return;
    }
    DataStruct->mZT1TrainID3 = mCCRepoHandle->GetSingleReg(MODBUS_CC_ZT1_TRAIN_ID_3_REG_ADD,&OK);
    if(OK == false)
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_CC_ZT1_TRAIN_ID_3_REG_ADD)");
        return;
    }

    DataStruct->mZT2TrainID1 = mCCRepoHandle->GetSingleReg(MODBUS_CC_ZT2_TRAIN_ID_1_REG_ADD,&OK);
    if(OK == false)
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_CC_ZT2_TRAIN_ID_1_REG_ADD)");
        return;
    }

    DataStruct->mZT2TrainID2 = mCCRepoHandle->GetSingleReg(MODBUS_CC_ZT2_TRAIN_ID_2_REG_ADD,&OK);
    if(OK == false)
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_CC_ZT2_TRAIN_ID_2_REG_ADD)");
        return;
    }

    DataStruct->mZT2TrainID3 = mCCRepoHandle->GetSingleReg(MODBUS_CC_ZT2_TRAIN_ID_3_REG_ADD,&OK);
    if(OK == false)
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_CC_ZT2_TRAIN_ID_3_REG_ADD)");
        return;
    }

    DataStruct->mZT2TrainID3 = mCCRepoHandle->GetSingleReg(MODBUS_CC_ZT2_TRAIN_ID_3_REG_ADD,&OK);
    if(OK == false)
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_CC_ZT2_TRAIN_ID_3_REG_ADD)");
        return;
    }


    DataStruct->mUpdateDateTimeFlag = mCCRepoHandle->GetSingleReg(MODBUS_CC_CLK_UPDATE_BASE_REG_ADD,&OK);
    if(OK == false)
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_CC_CLK_UPDATE_BASE_REG_ADD)");
        return;
    }

    DataStruct->mNTPSeconds = mCCRepoHandle->GetSingleReg(MODUBS_CC_CLK_SEC_BASE_REG_ADD,&OK);
    if(OK == false)
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODUBS_CC_CLK_SEC_BASE_REG_ADD)");
        return;
    }
    DataStruct->mNTPHourMinutes = mCCRepoHandle->GetSingleReg(MODUBS_CC_CLK_HR_MIN_BASE_REG_ADD,&OK);
    if(OK == false)
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODUBS_CC_CLK_HR_MIN_BASE_REG_ADD)");
        return;
    }
    DataStruct->mNTPMonthDay = mCCRepoHandle->GetSingleReg(MODUBS_CC_CLK_MNT_DAY_BASE_REG_ADD,&OK);
    if(OK == false)
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODUBS_CC_CLK_MNT_DAY_BASE_REG_ADD)");
        return;
    }
    DataStruct->mNTPYear = mCCRepoHandle->GetSingleReg(MODUBS_CC_CLK_YEAR_BASE_REG_ADD,&OK);
    if(OK == false)
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODUBS_CC_CLK_YEAR_BASE_REG_ADD)");
        return;
    }

    QTreeWidgetItem* Item;
    QString TempStr;

    //ZT1
    Item = mModbusCCDataTree->topLevelItem(0);

    Item->child(0)->setText(1,QString("%1").arg(DataStruct->mZT1PPIAlarmFlag1));
    Item->child(1)->setText(1,QString("%1").arg(DataStruct->mZT1PPIAlarmFlag2));
    Item->child(2)->setText(1,QString("%1").arg(DataStruct->mZT1PPIAlarmFlag3));
    Item->child(3)->setText(1,QString("%1").arg(DataStruct->mZT1PPIAlarmFlag4));

    Item->child(4)->setText(1,QString("%1").arg(DataStruct->mZT1PPEAlarmFlag1));
    Item->child(5)->setText(1,QString("%1").arg(DataStruct->mZT1PPEAlarmFlag2));
    Item->child(6)->setText(1,QString("%1").arg(DataStruct->mZT1PPEAlarmFlag3));
    Item->child(7)->setText(1,QString("%1").arg(DataStruct->mZT1PPEAlarmFlag4));

    Item->child(8)->setText(1,QString("%1").arg(DataStruct->mZT1PGAlarmFlag1));
    Item->child(9)->setText(1,QString("%1").arg(DataStruct->mZT1PGAlarmFlag2));
    Item->child(10)->setText(1,QString("%1").arg(DataStruct->mZT1PGAlarmFlag3));
    Item->child(11)->setText(1,QString("%1").arg(DataStruct->mZT1PGAlarmFlag4));

    Item->child(12)->setText(1,QString("%1").arg(DataStruct->mZT1FNAlarmFlag1));
    Item->child(13)->setText(1,QString("%1").arg(DataStruct->mZT1FNAlarmFlag2));
    Item->child(14)->setText(1,QString("%1").arg(DataStruct->mZT1FNAlarmFlag3));
    Item->child(15)->setText(1,QString("%1").arg(DataStruct->mZT1FNAlarmFlag4));

    if(DataStruct->mITI10_12Flag == 0)
    {
        TempStr = "Principal";
    }
    else
    {
        TempStr = "Secondaire";
    }
    Item->child(16)->setText(1,QString("%1").arg(DataStruct->mITI10_12Flag));
    Item->child(16)->setText(2,TempStr);

    Item->child(17)->setText(1,QString("%1").arg(DataStruct->mZT1V00AlarmFlag));
    Item->child(18)->setText(1,QString("%1").arg(DataStruct->mZT1PEQ1AlarmFlag));

    for(int i = 0; i < 16; i++)
    {
        Item->child(i)->setText(2,QString("%1").arg(DataStruct->mZT1Ranks.at(i)));
    }

    //ZT2
    Item = mModbusCCDataTree->topLevelItem(1);

    Item->child(0)->setText(1,QString("%1").arg(DataStruct->mZT2PPIAlarmFlag1));
    Item->child(1)->setText(1,QString("%1").arg(DataStruct->mZT2PPIAlarmFlag2));
    Item->child(2)->setText(1,QString("%1").arg(DataStruct->mZT2PPIAlarmFlag3));
    Item->child(3)->setText(1,QString("%1").arg(DataStruct->mZT2PPIAlarmFlag4));

    Item->child(4)->setText(1,QString("%1").arg(DataStruct->mZT2PPEAlarmFlag1));
    Item->child(5)->setText(1,QString("%1").arg(DataStruct->mZT2PPEAlarmFlag2));
    Item->child(6)->setText(1,QString("%1").arg(DataStruct->mZT2PPEAlarmFlag3));
    Item->child(7)->setText(1,QString("%1").arg(DataStruct->mZT2PPEAlarmFlag4));

    Item->child(8)->setText(1,QString("%1").arg(DataStruct->mZT2V02AlarmFlag));
    Item->child(9)->setText(1,QString("%1").arg(DataStruct->mZT2PEQ2AlarmFlag));

    for(int i = 0; i < 8; i++)
    {
        Item->child(i)->setText(2,QString("%1").arg(DataStruct->mZT2Ranks.at(i)));
    }

    //Watchdog
    Item = mModbusCCDataTree->topLevelItem(2);
    Item->child(0)->setText(1,QString("%1").arg(DataStruct->mWatchdogZT));

    //CC
    Item = mModbusCCDataTree->topLevelItem(3);
    Item->child(0)->setText(1,QString("%1").arg(DataStruct->mWatchdogRemote));
    Item->child(1)->setText(1,QString("%1").arg(DataStruct->mAN1));
    Item->child(2)->setText(1,QString("%1").arg(DataStruct->mAN2));
    Item->child(3)->setText(1,QString("%1").arg(DataStruct->mInhibitZT1));
    Item->child(4)->setText(1,QString("%1").arg(DataStruct->mInhibitZT2));
    Item->child(5)->setText(1,QString("%1").arg(DataStruct->mZT1TrainType));
    Item->child(5)->setText(2,CZTData::GetModbusTrainTypeString(DataStruct->mZT1TrainType));
    Item->child(6)->setText(1,QString("%1").arg(DataStruct->mZT2TrainType));
    Item->child(6)->setText(2,CZTData::GetModbusTrainTypeString(DataStruct->mZT2TrainType));
    Item->child(7)->setText(1,QString("%1").arg(DataStruct->mZT1TrainID1));
    Item->child(8)->setText(1,QString("%1").arg(DataStruct->mZT1TrainID2));
    Item->child(9)->setText(1,QString("%1").arg(DataStruct->mZT1TrainID3));
    Item->child(10)->setText(1,QString("%1").arg(DataStruct->mZT2TrainID1));
    Item->child(11)->setText(1,QString("%1").arg(DataStruct->mZT2TrainID2));
    Item->child(12)->setText(1,QString("%1").arg(DataStruct->mZT2TrainID3));
    Item->child(13)->setText(1,QString("%1").arg(DataStruct->mUpdateDateTimeFlag));
    Item->child(14)->setText(1,QString("0x%1").arg(DataStruct->mNTPSeconds,0,16));
    Item->child(15)->setText(1,QString("0x%1").arg(DataStruct->mNTPHourMinutes,0,16));
    Item->child(16)->setText(1,QString("0x%1").arg(DataStruct->mNTPMonthDay,0,16));
    Item->child(17)->setText(1,QString("0x%1").arg(DataStruct->mNTPYear,0,16));
    QDateTime NetworkTime;
    quint8 Secs, Minutes, Hours, Month, Day;
    quint8 temp;
    temp = (quint8)(DataStruct->mNTPSeconds >> 8);
    Secs = BCDToDecByte(temp);

    temp = (quint8)(DataStruct->mNTPHourMinutes & 0x00FF);
    Minutes = BCDToDecByte(temp);

    temp = (qint8)(DataStruct->mNTPHourMinutes >> 8);
    Hours = BCDToDecByte(temp);

    temp = (quint8)(DataStruct->mNTPMonthDay & 0x00FF);
    Day = BCDToDecByte(temp);

    temp = (quint8)(DataStruct->mNTPMonthDay >> 8);
    Month = BCDToDecByte(temp);

    quint16 Year = DataStruct->mNTPYear;
    Year = BCDToDecWord(Year);

    NetworkTime.setTimeSpec(Qt::UTC);
    NetworkTime.setDate(QDate(Year,Month,Day));
    NetworkTime.setTime(QTime(Hours,Minutes,Secs));

    Item->child(17)->setText(2,NetworkTime.toLocalTime().toString("yyyy-MM-dd hh:mm:ss"));



    delete DataStruct;
}

void CModbusDisplayPage::UpdateSEIModbusTable()
{
    if(mCCRepoHandle == 0  ||
       mSEIRepoHandle == 0 ||
       mProgramHandle == 0)
    {
        return;
    }

    CZTInfo *DataStruct = new CZTInfo;
    bool OK = false;

    quint16 data = mSEIRepoHandle->GetSingleReg(MODBUS_ZT1_ALARM_DATA_BASE_REG_ADD,&OK);

    if(OK == true)
    {

        DataStruct->mZT1PPIAlarmFlag1 = (data & ZT1_PP_INT_FLAG_MASK_1) != 0;
        DataStruct->mZT1PPIAlarmFlag2 = (data & ZT1_PP_INT_FLAG_MASK_2) != 0;
        DataStruct->mZT1PPIAlarmFlag3 = (data & ZT1_PP_INT_FLAG_MASK_3) != 0;
        DataStruct->mZT1PPIAlarmFlag4 = (data & ZT1_PP_INT_FLAG_MASK_4) != 0;
        DataStruct->mZT1PPEAlarmFlag1 = (data & ZT1_PP_EXT_FLAG_MASK_1) != 0;
        DataStruct->mZT1PPEAlarmFlag2 = (data & ZT1_PP_EXT_FLAG_MASK_2) != 0;
        DataStruct->mZT1PPEAlarmFlag3 = (data & ZT1_PP_EXT_FLAG_MASK_3) != 0;
        DataStruct->mZT1PPEAlarmFlag4 = (data & ZT1_PP_EXT_FLAG_MASK_4) != 0;
        DataStruct->mZT1PGAlarmFlag1 = (data & ZT1_PG_FLAG_MASK_1) != 0;
        DataStruct->mZT1PGAlarmFlag2 = (data & ZT1_PG_FLAG_MASK_2) != 0;
        DataStruct->mZT1PGAlarmFlag3 = (data & ZT1_PG_FLAG_MASK_3) != 0;
        DataStruct->mZT1PGAlarmFlag4 = (data & ZT1_PG_FLAG_MASK_4) != 0;
        DataStruct->mZT1FNAlarmFlag1 = (data & ZT1_FN_FLAG_MASK_1) != 0;
        DataStruct->mZT1FNAlarmFlag2 = (data & ZT1_FN_FLAG_MASK_2) != 0;
        DataStruct->mZT1FNAlarmFlag3 = (data & ZT1_FN_FLAG_MASK_3) != 0;
        DataStruct->mZT1FNAlarmFlag4 = (data & ZT1_FN_FLAG_MASK_4) != 0;

    }
    else
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_ZT1_ALARM_DATA_BASE_REG_ADD)");
        return;
    }

    QList<qint16> RankData;
    RankData = mSEIRepoHandle->GetRegs(MODBUS_ZT1_ALARM_RANKS_BASE_ADD,16,&OK);
    if(OK == true)
    {
        DataStruct->mZT1Ranks = RankData;
    }
    else
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_ZT1_ALARM_RANKS_BASE_ADD)");
        return;
    }

    data = mSEIRepoHandle->GetSingleReg(MODBUS_ZT2_ALARM_DATA_BASE_REG_ADD,&OK);
    if(OK == true)
    {
        DataStruct->mZT2PPIAlarmFlag1 = (data & ZT2_PP_INT_FLAG_MASK_1) != 0;
        DataStruct->mZT2PPIAlarmFlag2 = (data & ZT2_PP_INT_FLAG_MASK_2) != 0;
        DataStruct->mZT2PPIAlarmFlag3 = (data & ZT2_PP_INT_FLAG_MASK_3) != 0;
        DataStruct->mZT2PPIAlarmFlag4 = (data & ZT2_PP_INT_FLAG_MASK_4) != 0;
        DataStruct->mZT2PPEAlarmFlag1 = (data & ZT2_PP_EXT_FLAG_MASK_1) != 0;
        DataStruct->mZT2PPEAlarmFlag2 = (data & ZT2_PP_EXT_FLAG_MASK_2) != 0;
        DataStruct->mZT2PPEAlarmFlag3 = (data & ZT2_PP_EXT_FLAG_MASK_3) != 0;
        DataStruct->mZT2PPEAlarmFlag4 = (data & ZT2_PP_EXT_FLAG_MASK_4) != 0;
    }
    else
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_ZT2_ALARM_DATA_BASE_REG_ADD)");
        return;
    }

    RankData.clear();
    RankData = mSEIRepoHandle->GetRegs(MODBUS_ZT2_ALARM_RANKS_BASE_ADD,8,&OK);
    if(OK == true)
    {
        DataStruct->mZT2Ranks = RankData;
    }
    else
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_ZT2_ALARM_RANKS_BASE_ADD)");
        return;
    }


    data = mSEIRepoHandle->GetSingleReg(MODBUS_MISC_DATA_BASE_REG_ADD,&OK);

    if(OK == true)
    {

        DataStruct->mZT1V00AlarmFlag = (data & ZT1_V00_ALARM_FLAG_MASK) != 0;
        DataStruct->mZT1PEQ1AlarmFlag = (data & ZT1_PEQ1_ALARM_FLAG_MASK) != 0;
        DataStruct->mZT2V02AlarmFlag = (data & ZT2_V02_ALARM_FLAG_MASK) != 0;
        DataStruct->mZT2PEQ2AlarmFlag = (data & ZT2_PEQ2_ALARM_FLAG_MASK) != 0;
        DataStruct->mITI10_12Flag = (data & ZT1_ALARM_ITI_FLAG_MASK) != 0;
    }
    else
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_PANNES_DATA_BASE_REG_ADD)");
        return;
    }

    DataStruct->mWatchdogZT = mSEIRepoHandle->GetSingleReg(MODBUS_ZT_WATCHDOG_REG_ADD,&OK);
    if(OK == false)
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (MODBUS_ZT_WATCHDOG_REG_ADD)");
        return;
    }

    data = mSEIRepoHandle->GetSingleReg(SEI_MODBUS_SEI_WATCHDOG_REG,&OK);

    if(OK == true)
    {
        DataStruct->mWatchdogRemote = (int)data;
    }
    else
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (SEI_MODBUS_SEI_WATCHDOG_REG)");
        return;
    }

    data = mSEIRepoHandle->GetSingleReg(SEI_MODBUS_SEI_ALARMS_RESET_REG, &OK);
    if(OK == true)
    {
        DataStruct->mAN1 = ((data & SEI_MODBUS_SEI_ZT1_ALARM_RESET_MASK) != 0);
        DataStruct->mAN2 = ((data & SEI_MODBUS_SEI_ZT2_ALARM_RESET_MASK) != 0);
    }
    else
    {
        qDebug("GetSingleReg failed in UpdateZTInfo (SEI_MODBUS_SEI_ALARMS_RESET_REG)");
        return;
    }


    QTreeWidgetItem* Item;
    QString TempStr;

    //ZT1
    Item = mModbusSEIDataTree->topLevelItem(0);

    Item->child(0)->setText(1,QString("%1").arg(DataStruct->mZT1PPIAlarmFlag1));
    Item->child(1)->setText(1,QString("%1").arg(DataStruct->mZT1PPIAlarmFlag2));
    Item->child(2)->setText(1,QString("%1").arg(DataStruct->mZT1PPIAlarmFlag3));
    Item->child(3)->setText(1,QString("%1").arg(DataStruct->mZT1PPIAlarmFlag4));

    Item->child(4)->setText(1,QString("%1").arg(DataStruct->mZT1PPEAlarmFlag1));
    Item->child(5)->setText(1,QString("%1").arg(DataStruct->mZT1PPEAlarmFlag2));
    Item->child(6)->setText(1,QString("%1").arg(DataStruct->mZT1PPEAlarmFlag3));
    Item->child(7)->setText(1,QString("%1").arg(DataStruct->mZT1PPEAlarmFlag4));

    Item->child(8)->setText(1,QString("%1").arg(DataStruct->mZT1PGAlarmFlag1));
    Item->child(9)->setText(1,QString("%1").arg(DataStruct->mZT1PGAlarmFlag2));
    Item->child(10)->setText(1,QString("%1").arg(DataStruct->mZT1PGAlarmFlag3));
    Item->child(11)->setText(1,QString("%1").arg(DataStruct->mZT1PGAlarmFlag4));

    Item->child(12)->setText(1,QString("%1").arg(DataStruct->mZT1FNAlarmFlag1));
    Item->child(13)->setText(1,QString("%1").arg(DataStruct->mZT1FNAlarmFlag2));
    Item->child(14)->setText(1,QString("%1").arg(DataStruct->mZT1FNAlarmFlag3));
    Item->child(15)->setText(1,QString("%1").arg(DataStruct->mZT1FNAlarmFlag4));

    if(DataStruct->mITI10_12Flag == 0)
    {
        TempStr = "Principal";
    }
    else
    {
        TempStr = "Secondaire";
    }
    Item->child(16)->setText(1,QString("%1").arg(DataStruct->mITI10_12Flag));
    Item->child(16)->setText(2,TempStr);

    Item->child(17)->setText(1,QString("%1").arg(DataStruct->mZT1V00AlarmFlag));
    Item->child(18)->setText(1,QString("%1").arg(DataStruct->mZT1PEQ1AlarmFlag));

    for(int i = 0; i < 16; i++)
    {
        Item->child(i)->setText(2,QString("%1").arg(DataStruct->mZT1Ranks.at(i)));
    }

    //ZT2
    Item = mModbusSEIDataTree->topLevelItem(1);

    Item->child(0)->setText(1,QString("%1").arg(DataStruct->mZT2PPIAlarmFlag1));
    Item->child(1)->setText(1,QString("%1").arg(DataStruct->mZT2PPIAlarmFlag2));
    Item->child(2)->setText(1,QString("%1").arg(DataStruct->mZT2PPIAlarmFlag3));
    Item->child(3)->setText(1,QString("%1").arg(DataStruct->mZT2PPIAlarmFlag4));

    Item->child(4)->setText(1,QString("%1").arg(DataStruct->mZT2PPEAlarmFlag1));
    Item->child(5)->setText(1,QString("%1").arg(DataStruct->mZT2PPEAlarmFlag2));
    Item->child(6)->setText(1,QString("%1").arg(DataStruct->mZT2PPEAlarmFlag3));
    Item->child(7)->setText(1,QString("%1").arg(DataStruct->mZT2PPEAlarmFlag4));

    Item->child(8)->setText(1,QString("%1").arg(DataStruct->mZT2V02AlarmFlag));
    Item->child(9)->setText(1,QString("%1").arg(DataStruct->mZT2PEQ2AlarmFlag));

    for(int i = 0; i < 8; i++)
    {
        Item->child(i)->setText(2,QString("%1").arg(DataStruct->mZT2Ranks.at(i)));
    }

    //Watchdog
    Item = mModbusSEIDataTree->topLevelItem(2);
    Item->child(0)->setText(1,QString("%1").arg(DataStruct->mWatchdogZT));

    Item = mModbusSEIDataTree->topLevelItem(3); //SEI
    Item->child(0)->setText(1,QString("%1").arg(DataStruct->mWatchdogRemote));
    Item->child(1)->setText(1,QString("%1").arg(DataStruct->mAN1));
    Item->child(2)->setText(1,QString("%1").arg(DataStruct->mAN2));

    delete DataStruct;
}

void CModbusDisplayPage::RefreshTimerExpired()
{
    UpdateCCModbusTable();
    UpdateSEIModbusTable();
//    UpdateTable(CC_MODBUS_TABLE_SOURCE);
//    UpdateTable(SEI_MODBUS_TABLE_SOURCE);
}

void CModbusDisplayPage::showEvent(QShowEvent *event)
{
    if(mCCRepoHandle == 0  ||
       mSEIRepoHandle == 0 ||
       mProgramHandle == 0)
    {
        return;
    }


    mRefreshTimer->start();
}


quint8 CModbusDisplayPage::BCDToDecByte(const quint8 byte)
{
    quint8 out = 0;
    quint8 temp = 0;

    out += byte & 0x0F;
    temp = (byte >> 4) & 0x0F;
    out += (temp * 10);

    return out;

}

quint16 CModbusDisplayPage::BCDToDecWord(const quint16 word)
{
    quint16 out = 0;
    quint16 temp = 0;

    out += word & 0x000F;
    temp = (word >> 4) & 0x000F;
    out += (temp * 10);
    temp = (word >> 8) & 0x000F;
    out += (temp * 100);
    temp = (word >> 12) & 0x000F;
    out += (temp * 1000);

    return out;

}
