From 428ef3bfb211fd4fdac76176779a8f178a1873e3 Mon Sep 17 00:00:00 2001 From: jfmartel Date: Fri, 7 Jun 2024 14:20:16 -0400 Subject: [PATCH] Dev Historien PI --- Configuration/ZT.cfg | 2 +- Configuration/ZTSettings.ztc | Bin 41 -> 41 bytes ZT.pro | 3 +- sftpbatch | 2 +- sources/Modbus/PIHistorianDefs.h | 203 ++++------ sources/Modbus/PIHistorianManager.cpp | 527 +++++++++++++++++++++++++- sources/Modbus/PIHistorianManager.h | 33 +- sources/Modbus/PIHistorianSession.cpp | 4 +- sources/ZTData.h | 14 + sources/ZTStateMachine.cpp | 26 ++ sources/ZTStateMachine.h | 2 + sources/Zonetest.cpp | 142 +++---- sources/Zonetest.h | 2 + 13 files changed, 730 insertions(+), 230 deletions(-) diff --git a/Configuration/ZT.cfg b/Configuration/ZT.cfg index 5bc6b89..0894139 100644 --- a/Configuration/ZT.cfg +++ b/Configuration/ZT.cfg @@ -72,7 +72,7 @@ ACTIVER_HISTORIEN_PI=OUI #Adresse modbus de la ZT HISTORIEN_PI_DEVID=1 #Port Modbus/TCP du serveur historien -HISTORIEN_PI_PORT=503 +HISTORIEN_PI_PORT=2183 #---------------------------------- diff --git a/Configuration/ZTSettings.ztc b/Configuration/ZTSettings.ztc index d7191e9c232797e572a1119ffeafe29553d8f0fc..d4476a56d4d00519ab4bbaca6ff9d0338fd98e77 100644 GIT binary patch delta 14 VcmdPYsetSingleShot(true); + + + 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() @@ -20,7 +38,7 @@ void CPIHistorianManager::NewModbusConnection() QTcpSocket* SessionSocket = mModbusServer->nextPendingConnection(); if(SessionSocket != 0) { - CPIHistorianSession *NewSession = new CPIHistorianSession(mPIHistorianRepo,PI_HISTORIAN_MODBUS_DEVICE_ID); + CPIHistorianSession *NewSession = new CPIHistorianSession(mPIHistorianRepo,mModbusDevID); connect(NewSession,SIGNAL(PIHistorianSessionClosed(CPIHistorianSession*)),this,SLOT(HistorianSessionClosed(CPIHistorianSession*))); mHistorianSessionsList.append(NewSession); NewSession->OpenSession(SessionSocket); @@ -35,6 +53,7 @@ void CPIHistorianManager::HistorianSessionClosed(CPIHistorianSession *SessionPtr if(mHistorianSessionsList.at(i) == SessionPtr) { CPIHistorianSession *Session = mHistorianSessionsList.takeAt(i); + Session->CloseSession(); delete Session; return; } @@ -46,7 +65,8 @@ 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); - return 1; + StartHistorianTimer(); + return RET_OK; } int CPIHistorianManager::StopPIHistorian() @@ -59,5 +79,506 @@ int CPIHistorianManager::StopPIHistorian() 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); + + 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; + quint16 PEQ = 0, ErrComptage = 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; + } + } + } + + + mNbPassagesToday ++; + mNbDeclToday += NbDecl; + mNbDeclTotal += NbDecl; + mNbFNToday += NbDeclFN; + mNbPGToday += NbDeclPG; + mNbPPIZT1Today += NbDeclPPI; + mNbPPEZT1Today += NbDeclPPE; + + mPIHistorianRepo->WriteSingleReg(HISTORIAN_TRAINS_TODAY_REG_ADD,mNbPassagesToday); + mPIHistorianRepo->WriteSingleReg(HISTORIAN_DETECTIONS_TOTAL_REG_ADD,ZT1PassageInfo.mNbDeclenchements); + 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); + + } + + 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() +{ + QTime Time = QTime::currentTime(); + int Delay = Time.msecsTo(QTime(23,59,59)); + if(Delay < 0) + { + Delay += MSECS_IN_A_DAY; + } + + mHistorianTimer->start(Delay); +} + +void CPIHistorianManager::HistorianTimerExpired() +{ + mNbFNToday = 0; + mNbPGToday = 0; + mNbPPIZT1Today = 0; + mNbPPEZT1Today = 0; + mNbPPIZT2Today = 0; + mNbPPEZT2Today = 0; + + StartHistorianTimer(); } diff --git a/sources/Modbus/PIHistorianManager.h b/sources/Modbus/PIHistorianManager.h index 7d83c73..23534f4 100644 --- a/sources/Modbus/PIHistorianManager.h +++ b/sources/Modbus/PIHistorianManager.h @@ -5,28 +5,59 @@ #include "PIHistorianSession.h" #include #include +#include "ZTData.h" #define PI_HISTORIAN_MODBUS_DEVICE_ID 1 +#define MSECS_IN_A_DAY (24 * 60 * 60 * 1000) class CPIHistorianManager : public QObject { Q_OBJECT public: - CPIHistorianManager(CModbusRepository *PIHistorianRepo, int ModbusPort); + 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(); + private: QList 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(); + + + }; #endif // PIHISTORIANMANAGER_H diff --git a/sources/Modbus/PIHistorianSession.cpp b/sources/Modbus/PIHistorianSession.cpp index 9902b8b..0388957 100644 --- a/sources/Modbus/PIHistorianSession.cpp +++ b/sources/Modbus/PIHistorianSession.cpp @@ -17,7 +17,7 @@ int CPIHistorianSession::OpenSession(QTcpSocket *ModbusSocket) { mModbusTCPSocketHandle = ModbusSocket; connect(mModbusTCPSocketHandle,SIGNAL(readyRead()),this,SLOT(ModbusDataReady())); - connect(mModbusTCPSocketHandle,SIGNAL(disconnected()),this,SLOT(ConnectionLost())); + connect(mModbusTCPSocketHandle,SIGNAL(disconnected()),this,SLOT(HistorianConnectionLost())); CZTLog::instance()->AddLogString(QString("Session avec Historien PI établie. IP locale [%1] - IP HPC [%2]").arg(mModbusTCPSocketHandle->localAddress().toString()).arg(mModbusTCPSocketHandle->peerAddress().toString()),true); @@ -28,7 +28,7 @@ int CPIHistorianSession::CloseSession() { disconnect(mModbusTCPSocketHandle,SIGNAL(readyRead()),this,SLOT(ModbusDataReady())); mModbusTCPSocketHandle->close(); - CZTLog::instance()->AddLogString(QString("Session avec Historien PI fermée avec succès.")); + CZTLog::instance()->AddLogString(QString("Session avec Historien PI fermée avec succès."),true); return RET_OK; } diff --git a/sources/ZTData.h b/sources/ZTData.h index 4a897f1..086e71b 100644 --- a/sources/ZTData.h +++ b/sources/ZTData.h @@ -379,4 +379,18 @@ class CZTPGCalibrationData qint32 mPGIntValue; }; +class CZTPassageInfo +{ +public: + QVector *mDetections; + quint16 mNbPassages; + quint16 mNbDeclenchements; + qint16 mTrainCompo1; + qint16 mTrainCompo2; + qint16 mTrainCompo3; + qint16 mTrainType; + QDateTime mPassageDateTime; + +}; + #endif // ZTDATA_H diff --git a/sources/ZTStateMachine.cpp b/sources/ZTStateMachine.cpp index 6a3aa27..e109ca9 100755 --- a/sources/ZTStateMachine.cpp +++ b/sources/ZTStateMachine.cpp @@ -377,6 +377,7 @@ unsigned int CZTStateMachine::ZT1StateMachine(unsigned int Event, unsigned int S mZT1ActiveStatus = SB_ZT_ACTIVE_STATUS; mZTPagePTr->SetZTStatus(mNbPassages,mNbTriggers,mZT1ActiveStatus,mZT2ActiveStatus); mZTPagePTr->SetZT1ActivationState(true); + mZTActivationDateTime = QDateTime::currentDateTime(); mZTPagePTr->ResetZT1Stats(); if(mModbusCCMgr != 0) @@ -1168,6 +1169,19 @@ unsigned int CZTStateMachine::AnalyzeZT1PostDetection() CZTLog::instance()->ClearBufferString(); //2015-03-03 To avoid potention memory leak. Computer Crash problem resolution tentative... } + CZTPassageInfo PassageInfo; + PassageInfo.mDetections = &mZT1DetectionsLog; + PassageInfo.mNbPassages = mNbPassages; + PassageInfo.mNbDeclenchements = mNbTriggers; + PassageInfo.mTrainType = mZT1Log.mZT1Flags.mModbusTrainType; + PassageInfo.mTrainCompo1 = mZT1Log.mZT1Flags.mTrainCompo1; + PassageInfo.mTrainCompo2 = mZT1Log.mZT1Flags.mTrainCompo2; + PassageInfo.mTrainCompo3 = mZT1Log.mZT1Flags.mTrainCompo3; + PassageInfo.mPassageDateTime = mZTActivationDateTime; + + + mProgramPtr->NewZT1Passage(PassageInfo); + mZTPagePTr->SetZTStatus(mNbPassages,mNbTriggers,mZT1ActiveStatus,mZT2ActiveStatus); return RET_POST_DETECTION_VALID; @@ -1240,6 +1254,7 @@ unsigned int CZTStateMachine::ZT2StateMachine(unsigned int Event, unsigned int S mEventsRefTimer.start(); mZTPagePTr->SetZT2ActivationSTate(true); mZT2ActiveStatus = SB_ZT_ACTIVE_STATUS; + mZTActivationDateTime = QDateTime::currentDateTime(); mZTPagePTr->SetZTStatus(mNbPassages,mNbTriggers,mZT1ActiveStatus,mZT2ActiveStatus); mZTPagePTr->ResetZT2Stats(); if(mModbusCCMgr != 0) @@ -1442,6 +1457,17 @@ unsigned int CZTStateMachine::AnalyzeZT2PostDetection() CZTLog::instance()->ClearBufferString(); //2015-03-03 To avoid potention memory leak. Computer Crash problem resolution tentative... } + CZTPassageInfo PassageInfo; + PassageInfo.mDetections = &mZT2DetectionsLog; + PassageInfo.mNbPassages = mNbPassages; + PassageInfo.mNbDeclenchements = mNbTriggers; + PassageInfo.mTrainType = mZT2Log.mZT2Flags.mModbusTrainType; + PassageInfo.mTrainCompo1 = mZT2Log.mZT2Flags.mTrainCompo1; + PassageInfo.mTrainCompo2 = mZT2Log.mZT2Flags.mTrainCompo2; + PassageInfo.mTrainCompo3 = mZT2Log.mZT2Flags.mTrainCompo3; + PassageInfo.mPassageDateTime = mZTActivationDateTime; + mProgramPtr->NewZT2Passage(PassageInfo); + //mZTPagePTr->SetZTStatus(mNbPassages,mNbTriggers,mZT1Active,mZT2Active); mZTPagePTr->SetZTStatus(mNbPassages,mNbTriggers,mZT1ActiveStatus,mZT2ActiveStatus); diff --git a/sources/ZTStateMachine.h b/sources/ZTStateMachine.h index 5e5cd8f..b77cc25 100644 --- a/sources/ZTStateMachine.h +++ b/sources/ZTStateMachine.h @@ -230,6 +230,8 @@ private: QElapsedTimer mFSSyncTimer; bool mTimeToForceSyncFS; + QDateTime mZTActivationDateTime; + signals: void PGCalibrationFinished(int); //Signal envoyé lorsque la calibration est terminée. Le paramètre est la valeur calculée. void PGCalibrationStatus(int,int); //Signal envoyé pour informer de l'état de la calibration. Le paramètre 1 est le nombre de passages à date et le 2è est le nombre requis. diff --git a/sources/Zonetest.cpp b/sources/Zonetest.cpp index f750119..e9d0af3 100644 --- a/sources/Zonetest.cpp +++ b/sources/Zonetest.cpp @@ -60,6 +60,7 @@ #include "ModbusTKTransport.h" #include "ModbusSEIDefs.h" #include +#include "PIHistorianDefs.h" @@ -424,44 +425,6 @@ unsigned int CZoneTest::InitZT() mAnalogModule = 0; -// //Open the DataQ datalogger module if present in config file -// QString DataQIP = CZTConfigMgr::instance()->GetDataQIPAddress(); -// if(DataQIP.isEmpty()) -// { -// CZTLog::instance()->AddLogString("Module DataQ non trouvé dans le fichier de configuration.",true); -// } -// else -// { -// mDI710Module = new CDI710Driver; -// if(mDI710Module->OpenDevice(DataQIP,DI710_TCP_PORT) != DI710_RET_OK) -// { -// mWelcomePagePtr->InsertTextBoxString(QString("Impossible d'initialiser le module DataQ")); -// mWelcomePagePtr->InsertTextBoxString(QString("Vérifier le fichier de configuration et la connexion.")); -// CZTLog::instance()->AddLogString("Impossible d'initialiser le module DataQ",true); -// delete mDI710Module; -// mDI710Module = 0; -// } -// else -// { -// mDataQThread = new QThread; -// mDI710Module->moveToThread(mDataQThread); -// connect(mDataQThread,SIGNAL(started()),mDI710Module,SLOT(Run())); -// mDataQThread->start(); -// mWelcomePagePtr->InsertTextBoxString(QString("Initialisation du module DataQ: OK")); -// } -// } - - //Now, determine which of the analog module is detected. - -// if((mIOManager->GetModule(IO_MODULE_MIXED_TYPE,1) != 0)) -// { -// mAnalogModule = (CMixedModule*)mIOManager->GetModule(IO_MODULE_MIXED_TYPE,1); -// } -// else if(mDI710Module != 0) -// { -// mAnalogModule = mDI710Module; -// } - //Open advantech USB-4704 analog acquisition module. CUSB4704Interface *USBAnalogModule = new CUSB4704Interface(); @@ -517,57 +480,6 @@ unsigned int CZoneTest::InitZT() mExtIOThread->start(QThread::NormalPriority); bool UseModbusInterface = mZTSettings->mUseModbusCC; -// if(CZTConfigMgr::instance()->GetModbusCCEnabled() != ZT_CONFIG_MODBUS_ENABLE_FORCE_OFF) -// { -// if(CZTConfigMgr::instance()->GetModbusCCEnabled() == ZT_CONFIG_MODBUS_ENABLE_FORCE_ON) -// { -// CEngLog::instance()->AddLogString("Interface Modbus activée (forcée)",1); -// UseModbusInterface = true; -// } -// else -// { -//// //autodetect modbus interface... -// int ModbusPort = CZTConfigMgr::instance()->GetModbusCCPort(); -// mWelcomePagePtr->InsertTextBoxString(QString("Détection Modbus CC...")); -// if(CModbusCCMgr::CheckForCCPartner(3000,ModbusPort) == true) -// { -// UseModbusInterface = true; -// mWelcomePagePtr->InsertTextBoxString(QString("Modbus CC détecté et activé")); -// } -// else -// { -// mWelcomePagePtr->InsertTextBoxString(QString("Modbus CC non-détecté")); -// } - -//// CEngLog::instance()->AddLogString("Auto-Détection de l'interface Modbus",1); -//// QString IFName = CZTConfigMgr::instance()->GetModbusCCIFName(); -//// if(IFName != "INVALID") -//// { -//// QString CarrierFile = QString("/sys/class/net/%1/carrier").arg(IFName); -//// QFile *EthIfStateFile = new QFile(CarrierFile); -//// if(EthIfStateFile) -//// { -//// CEngLog::instance()->AddLogString(QString("Ouverture du fichier %1 ...").arg(IFName),3); -//// if(EthIfStateFile->open(QIODevice::ReadOnly | QIODevice::Text) == true) -//// { -//// QByteArray val = EthIfStateFile->readAll(); -//// QString State(val); -//// if(State.contains("1")) -//// { -//// CZTLog::instance()->AddLogString(QString("Connexion Ethernet CC détectée sur %1").arg(IFName),true); -//// UseModbusInterface = true; -//// } -//// else -//// { -//// CZTLog::instance()->AddLogString(QString("Connexion Ethernet CC inactive ou non-détectée sur %1").arg(IFName),true); -//// } -//// EthIfStateFile->close(); -//// } -//// delete EthIfStateFile; -//// } -//// } -// } -// } if(UseModbusInterface == true) { @@ -690,8 +602,23 @@ unsigned int CZoneTest::InitZT() if(EnableHistorian == true) { mPIHistorianRepository = new CModbusRepository; - mPIHistorianRepository->AddHRDataMap(MODBUS_ZT_DATA_BASE_REG,MODBUS_ZT_TABLE_DATA_SIZE); //Add the ZT data map - mPIHistorianManager = new CPIHistorianManager(mPIHistorianRepository,PIHistorianPort); + mPIHistorianRepository->AddHRDataMap(HISTORIAN_ZT_DATA_BASE_REG,HISTORIAN_MODBUS_ZT_TABLE_DATA_SIZE); //Add the ZT data map + mPIHistorianManager = new CPIHistorianManager(mPIHistorianRepository,PIHistorianPort,PIHistorianDevID); + + if(UseModbusInterface) + { + connect(mModbusCCMgr,SIGNAL(ModbusCCConnected(qint32,qint32)),mPIHistorianManager,SLOT(HPCConnected(qint32,qint32))); + connect(mModbusCCMgr,SIGNAL(ModbusCCDisconnected()),mPIHistorianManager,SLOT(HPCDisconnected())); + connect(mModbusSEIMgr,SIGNAL(ModbusMasterConnected(qint32,qint32)),mPIHistorianManager,SLOT(SEIConnected(qint32,qint32))); + connect(mModbusSEIMgr,SIGNAL(ModbusMasterDisconnected()),mPIHistorianManager,SLOT(SEIDisconnected())); + } + + mPIHistorianManager->UpdateDetectionFunctions(mZTSettings->mDetectionFunctionSettings); + mPIHistorianManager->UpdatePGTreshold(mZTSettings->mPGTreshold); + mPIHistorianManager->UpdateMaintenanceMode(false); + mPIHistorianManager->SetSystemStartDateTime(mZTStartDateTime); + + mPIHistorianManager->StartPIHistorian(); } @@ -1036,6 +963,9 @@ unsigned int CZoneTest::DetectionFunctionsConfigChanged(CZTDetectionFunctionConf mZTSettingsFileMgr.SaveSettings(mZTSettings); mEventMgr->UpdateEvents(mZTSettings->mDetectionFunctionSettings); + if(mPIHistorianManager != 0) + mPIHistorianManager->UpdateDetectionFunctions(NewConfig); + return RET_OK; } @@ -1166,6 +1096,9 @@ unsigned int CZoneTest::SetPGTreshold(int NewTreshold) mZTSettings->mPGTreshold = NewTreshold; mZTSettingsFileMgr.SaveSettings(mZTSettings); + if(mPIHistorianManager != 0) + mPIHistorianManager->UpdatePGTreshold(NewTreshold); + return RET_OK; } @@ -1175,6 +1108,11 @@ bool CZoneTest::EnterMaintenanceModeRequest() if(mZTStateMachine->EnterMaintenanceMode() == RET_OK) { mEventMgr->AddSingleEvent(EVENT_MAINTENANCE); + + if(mPIHistorianManager != 0) + { + mPIHistorianManager->UpdateMaintenanceMode(true); + } return true; } else @@ -1193,6 +1131,11 @@ void CZoneTest::ExitMaintenanceModeRequest() } mZTStateMachine->QuitMaintenanceMode(); mEventMgr->RemoveSingleEvent(EVENT_MAINTENANCE); + + if(mPIHistorianManager != 0) + { + mPIHistorianManager->UpdateMaintenanceMode(false); + } } unsigned int CZoneTest::ClearMaintenanceCurTKRequest() @@ -1514,6 +1457,23 @@ int CZoneTest::SetZT2InhibitionState(bool InhibitionON) return RET_OK; } +int CZoneTest::NewZT1Passage(CZTPassageInfo ZT1PassageInfo) +{ + if(mPIHistorianManager != 0) + { + mPIHistorianManager->NewZT1Passage(ZT1PassageInfo); + } + return RET_OK; +} + +int CZoneTest::NewZT2Passage(CZTPassageInfo ZT2PassageInfo) +{ + if(mPIHistorianManager != 0) + { + mPIHistorianManager->NewZT2Passage(ZT2PassageInfo); + } + return RET_OK; +} void CZoneTest::ApplicationQuit(int Reason) { diff --git a/sources/Zonetest.h b/sources/Zonetest.h index c4e4f86..280340a 100644 --- a/sources/Zonetest.h +++ b/sources/Zonetest.h @@ -170,6 +170,8 @@ public: //ZTStateMachine requests int SetZT1InhibitionState(bool InhibitionON); int SetZT2InhibitionState(bool InhibitionON); + int NewZT1Passage(CZTPassageInfo ZT1PassageInfo); + int NewZT2Passage(CZTPassageInfo ZT2PassageInfo);