diff --git a/Distro/pp_cc/rootCA.srl b/Distro/pp_cc/rootCA.srl index 6c5425df..548cbdbb 100644 --- a/Distro/pp_cc/rootCA.srl +++ b/Distro/pp_cc/rootCA.srl @@ -1 +1 @@ -58EF3FD140413B2CA6B811223EDBB6B0CB926FAD +58EF3FD140413B2CA6B811223EDBB6B0CB926FAE diff --git a/lib/App/common/SQLite_dataBinder.cpp b/lib/App/common/SQLite_dataBinder.cpp index e1e65ede..935c529d 100644 --- a/lib/App/common/SQLite_dataBinder.cpp +++ b/lib/App/common/SQLite_dataBinder.cpp @@ -344,7 +344,7 @@ bool sqliteDBC::connect(void) }; }; m_pDataReports = new SQLite_Connector(); - if (m_pDataReports){ + if( m_pDataReports ){ /* memset(&m_sPRepInfo, 0, sizeof(REPORT_DIRINFO)); if (! m_bStaIsCreated ){ @@ -438,7 +438,7 @@ bool sqliteDBC::connect(void) bLastEventWasClosed = true; }; */ - if( bLastEventWasClosed ){ + if( bLastEventWasClosed || true ){ #ifdef DEBUG LOG("Last Session is Closed!"); #endif @@ -480,9 +480,7 @@ bool sqliteDBC::connect(void) "INTEGER NOT NULL, \"flength\" INTEGER, PRIMARY KEY(\"id\" AUTOINCREMENT) )"); //}; m_bRepIsConnected = true; - } - else - { + } else { LOG_ERROR("Cannot Connect to DB File"); delete(m_pDataReports); m_pDataReports = NULL; @@ -490,7 +488,6 @@ bool sqliteDBC::connect(void) return false; }; }; - return (m_bRepIsConnected && m_bMemIsConnected && m_bStoIsConnected && m_bStaIsConnected); } @@ -882,24 +879,20 @@ void sqliteDBC::updateStats(bool evt, bool video, uint8_t ses_status) // Check New Day Reports REPORT_DIRINFO tRR;// = {0}; initialized by fillRDF fillRDF(time(NULL), tRR); - if (tRR.ts > m_sCRepInfo.ts) - { + if( tRR.ts > m_sCRepInfo.ts ){ // New Day Report - if (m_bRepIsConnected) - { + if (m_bRepIsConnected){ m_bRepIsConnected = false; LOG("Disconnect Report!"); m_pDataReports->Disconnect(); }; - memcpy(&m_sPRepInfo, &m_sCRepInfo, sizeof(REPORT_DIRINFO)); - memcpy(&m_sCRepInfo, &tRR, sizeof(REPORT_DIRINFO)); - if (checkRDFDir(m_pDbReportBase, m_sCRepInfo, true, getUserUid(web_access_user), getUserGid(web_access_group))) - { - sprintf(m_pDbReport, "%s/%s/%s/%s/report.db", m_pDbReportBase, m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY); + memcpy( &m_sPRepInfo, &m_sCRepInfo, sizeof(REPORT_DIRINFO)); + memcpy( &m_sCRepInfo, &tRR, sizeof(REPORT_DIRINFO)); + if( checkRDFDir( m_pDbReportBase, m_sCRepInfo, true, getUserUid(web_access_user), getUserGid(web_access_group)) ){ + sprintf( m_pDbReport, "%s/%s/%s/%s/report.db", m_pDbReportBase, m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY ); LOG("Connect Report!"); - LOG(m_pDbReport); - if (m_pDataReports->Connect(m_pDbReport, 0, true)) - { + LOG( m_pDbReport ); + if( m_pDataReports->Connect( m_pDbReport, 0, true ) ){ chown(m_pDbReport, getUserUid(web_access_user), getUserGid(web_access_group)); chmod(m_pDbReport, DB_FILE_MODE); m_pDataReports->SilentQuery( @@ -924,7 +917,8 @@ void sqliteDBC::updateStats(bool evt, bool video, uint8_t ses_status) }; }; }; - // Create New record + + // Create New STAT record sprintf(tQBuf, "INSERT INTO `days`(`year`,`month`,`day`,`session_count`,`session_closed`) VALUES(%s,%s,%s,1,0) ON CONFLICT(`year`,`month`,`day`) DO UPDATE SET `session_count`=`session_count`+1, `session_closed`=0", m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY); #ifdef DEBUG_ALL diff --git a/lib/App/common/dataBinder_ReportAux.cpp b/lib/App/common/dataBinder_ReportAux.cpp index bea92d41..49d150d7 100644 --- a/lib/App/common/dataBinder_ReportAux.cpp +++ b/lib/App/common/dataBinder_ReportAux.cpp @@ -1,14 +1,11 @@ #include "dataBinder_ReportAux.h" -void fillRDF(uint32_t ts, REPORT_DIRINFO & RDF) -{ +void fillRDF(uint32_t ts, REPORT_DIRINFO & RDF){ memset(&RDF, 0, sizeof(REPORT_DIRINFO)); - if (ts) - { + if( ts ){ time_t tt = ts; struct tm * tmp = localtime(&tt); - if (tmp) - { + if (tmp){ sprintf(RDF.DAY, "%02d", tmp->tm_mday); sprintf(RDF.MONTH, "%02d", 1 + tmp->tm_mon); sprintf(RDF.YEAR, "%04d", 1900 + tmp->tm_year); @@ -17,99 +14,74 @@ void fillRDF(uint32_t ts, REPORT_DIRINFO & RDF) }; } -bool checkRDFDir(CSTR basePath, REPORT_DIRINFO & RDF, bool recreate, uid_t uid, gid_t gid, mode_t mode) -{ +bool checkRDFDir(CSTR basePath, REPORT_DIRINFO & RDF, bool recreate, uid_t uid, gid_t gid, mode_t mode){ char tBuff[MAX_PATH]; sprintf(tBuff, "%s/%s", basePath, RDF.YEAR); // If recreate GID and UID must be supplied! - if (recreate) - { - if (uid == 0 || gid == 0 ) - { + if (recreate){ + if (uid == 0 || gid == 0 ){ return false; }; }; - if (fileExist(tBuff) == 0) - { - if (recreate) - { - if (! createAccessibleDir(tBuff, mode, uid, gid)) - { + if (fileExist(tBuff) == 0){ + if (recreate){ + if (! createAccessibleDir(tBuff, mode, uid, gid)){ return false; } - } - else - { + }else{ return false; } }; sprintf(tBuff, "%s/%s/%s", basePath, RDF.YEAR, RDF.MONTH); - if (fileExist(tBuff) == 0) - { - if (recreate) - { - if (! createAccessibleDir(tBuff, mode, uid, gid)) - { + if (fileExist(tBuff) == 0){ + if (recreate){ + if (! createAccessibleDir(tBuff, mode, uid, gid)){ return false; } - } - else - { + }else{ return false; } }; sprintf(tBuff, "%s/%s/%s/%s", basePath, RDF.YEAR, RDF.MONTH, RDF.DAY); - if (fileExist(tBuff) == 0) - { - if (recreate) - { - if (! createAccessibleDir(tBuff, mode, uid, gid)) - { + if (fileExist(tBuff) == 0){ + if (recreate){ + if (! createAccessibleDir(tBuff, mode, uid, gid)){ return false; } - } - else - { + }else{ return false; } }; return true; } -uint32_t getTsFromRDF(REPORT_DIRINFO & RDF) -{ +uint32_t getTsFromRDF(REPORT_DIRINFO & RDF){ struct tm ltm; memset(<m, 0, sizeof(struct tm)); ltm.tm_mday = atoi(RDF.DAY); ltm.tm_mon = atoi(RDF.MONTH) - 1; ltm.tm_year = atoi(RDF.YEAR) - 1900; - return (uint32_t) timelocal(<m); + return (uint32_t) timelocal( <m ); } -bool deleteRDFDir(CSTR Base, REPORT_DIRINFO & RDF) -{ +bool deleteRDFDir(CSTR Base, REPORT_DIRINFO & RDF){ char tBuff[MAX_PATH]; sprintf(tBuff, "%s/%s/%s/%s", Base, RDF.YEAR, RDF.MONTH, RDF.DAY); // No Checking if dir is empty - dir contents all we need to delete! - if (fileExist(tBuff)) - { + if (fileExist(tBuff)){ sprintf(tBuff, "rm -f -R \"%s/%s/%s/%s\"", Base, RDF.YEAR, RDF.MONTH, RDF.DAY); system(tBuff); }; sprintf(tBuff, "%s/%s/%s", Base, RDF.YEAR, RDF.MONTH); - if (fileExist(tBuff)) - { - if (dirEmpty(tBuff)) - { + if (fileExist(tBuff)){ + if (dirEmpty(tBuff)){ sprintf(tBuff, "rm -f -R \"%s/%s/%s\"", Base, RDF.YEAR, RDF.MONTH); system(tBuff); }; }; sprintf(tBuff, "%s/%s", Base, RDF.YEAR); - if (fileExist(tBuff)) - { - if (dirEmpty(tBuff)) - { + if (fileExist(tBuff)){ + if (dirEmpty(tBuff)){ sprintf(tBuff, "rm -f -R \"%s/%s\"", Base, RDF.YEAR); system(tBuff); }; diff --git a/lib/App/common/serverDataBinder.h b/lib/App/common/serverDataBinder.h index cf314a3d..6511b373 100644 --- a/lib/App/common/serverDataBinder.h +++ b/lib/App/common/serverDataBinder.h @@ -542,6 +542,10 @@ class serverDataBinder return false; } + virtual bool updateSessionSource(uint64_t id, char BB ){ + return false; + } + virtual bool updateSessionStatus(uint64_t id, uint8_t status) { return false; diff --git a/lib/App/common/serverMySQL_dataBinder.cpp b/lib/App/common/serverMySQL_dataBinder.cpp index 7cc8debc..d759d8e3 100644 --- a/lib/App/common/serverMySQL_dataBinder.cpp +++ b/lib/App/common/serverMySQL_dataBinder.cpp @@ -2236,6 +2236,21 @@ bool servaMySQLDBC::updateSessionFinal(uint64_t id, char BB ) return false; } +bool servaMySQLDBC::updateSessionSource(uint64_t id, char BB ) +{ + if (m_bIsConnected) + { + char tQBuf[MAX_PATH_LEN]; + sprintf(tQBuf, "UPDATE `sessions` SET `src`='%c' WHERE `id`=%" PRIu64, BB, id); +#ifdef DEBUG_ALL + LOG(tQBuf); +#endif + m_pDatabase->SilentQuery(tQBuf); + return true; + }; + return false; +} + bool servaMySQLDBC::updateSessionStatus(uint64_t id, uint8_t status) { if (m_bIsConnected) diff --git a/lib/App/common/serverMySQL_dataBinder.h b/lib/App/common/serverMySQL_dataBinder.h index e8136800..3d5baca4 100644 --- a/lib/App/common/serverMySQL_dataBinder.h +++ b/lib/App/common/serverMySQL_dataBinder.h @@ -100,6 +100,7 @@ class servaMySQLDBC : public serverDataBinder virtual bool updateModuleData(uint64_t m_dMojoDBID, uint32_t UPPER_ID, uint32_t BOTTOM_ID, uint32_t CAM_ID, uint32_t CAM_STREAM_ID, CSTR vstr, CSTR lstr); virtual bool updateServerUtilisation(DBC_SYS_LOAD_INFO * ptr); virtual bool updateSessionFinal(uint64_t id, char BB ); + virtual bool updateSessionSource(uint64_t id, char BB ); virtual bool updateSessionStatus(uint64_t id, uint8_t status); virtual bool updateWebCmdStatus(uint64_t id, uint8_t status); }; diff --git a/lib/App/mojo/mojoKernel.cpp b/lib/App/mojo/mojoKernel.cpp index 1a38bf06..62c87295 100644 --- a/lib/App/mojo/mojoKernel.cpp +++ b/lib/App/mojo/mojoKernel.cpp @@ -73,7 +73,7 @@ mojoKernel::mojoKernel(config_setting_t * ptr) : m_bMediaMode = 0; m_bApplicationFirstRun = false; m_bForceCloseSessionOnEndOfTheDay = false; - m_mDateOfToday = getTimeValue() / 86400000; // round to a Day + m_mDateOfToday = getLocalDay(); m_pRPC = NULL; #ifndef LRF_PROXY_ONLY m_pMediaHelper = NULL; @@ -547,37 +547,26 @@ void mojoKernel::stopSession(bool forced) m_mSessionProtector.unlock(); } -void mojoKernel::validateSession() -{ +void mojoKernel::validateSession(){ m_mSessionProtector.lock(); - if (m_bSessionStarted) - { - if (m_nNoAlarmCounter < NOTARGET_SESSION_CLOSE) - { + if( m_bSessionStarted ){ + if( m_nNoAlarmCounter < NOTARGET_SESSION_CLOSE ){ m_nNoAlarmCounter++; - } - else - { + } else { m_mSessionProtector.unlock(); - stopSession(false); + stopSession( m_bForceCloseSessionOnEndOfTheDay ); m_mSessionProtector.lock(); }; #ifdef EMBED_AUTORESET - if (m_bEnableAutoReset) - { - if (m_nSessionCounter < m_nAutoResetTime) - { + if (m_bEnableAutoReset) { + if (m_nSessionCounter < m_nAutoResetTime) { m_nSessionCounter++; - } - else - { + } else { m_nSessionCounter = 0; // Reset All Sensors DBC_INT_LIST * tL = m_pDBC->listNodes(false); - if (tL) - { - for (DBC_INT_LIST::iterator idx = tL->begin(); idx < tL->end(); idx++) - { + if (tL){ + for (DBC_INT_LIST::iterator idx = tL->begin(); idx < tL->end(); idx++){ m_pDBC->pushMemCMD(*idx, 1, "'A'"); }; delete(tL); @@ -598,10 +587,8 @@ void mojoKernel::validateSession() LOG("Pereodic Reset to All Sensors"); #endif DBC_INT_LIST * tL = m_pDBC->listNodes(false); - if (tL) - { - for (DBC_INT_LIST::iterator idx = tL->begin(); idx < tL->end(); idx++) - { + if (tL){ + for (DBC_INT_LIST::iterator idx = tL->begin(); idx < tL->end(); idx++){ m_pDBC->pushMemCMD(*idx, 1, "'A'"); }; delete(tL); @@ -777,23 +764,17 @@ void mojoKernel::getSystemUtilization(DBC_SYS_LOAD_INFO * ptr) m_mSysInfoProtector.unlock(); } -void mojoKernel::m_fThreadProc(void) -{ +void mojoKernel::m_fThreadProc(void){ time_t tTimeTSeconds = 0; m_bThreadStatus = TH_WORKING; pthread_setname_np( pthread_self(), "Kernel" ); - while (m_bThreadStatus < TH_ENDED) - { - + while (m_bThreadStatus < TH_ENDED){ m_tTimeTSeconds = time(NULL); // Each Second - if (m_tTimeTSeconds != tTimeTSeconds) - { + if (m_tTimeTSeconds != tTimeTSeconds){ tTimeTSeconds = m_tTimeTSeconds; - if (m_bApplicationFirstRun) - { - if (m_pHardwareManager != NULL) - { + if (m_bApplicationFirstRun){ + if (m_pHardwareManager != NULL){ LOG_ERROR("Restart and Detect HardWare!"); // Must be shutdowned all service that uses hardware prior to detection! m_bThreadStatus = TH_ENDED; @@ -802,8 +783,7 @@ void mojoKernel::m_fThreadProc(void) }; }; #ifdef LINUX_KERNEL_WATCHDOG - if (m_iWatchdogFd > -1) - { + if (m_iWatchdogFd > -1){ char tB[64]; sprintf(tB, "4000\n"); // ToDo: Make Error Handling here! @@ -812,22 +792,10 @@ void mojoKernel::m_fThreadProc(void) #endif m_bDebugMode = m_pDBC->getAppDebugMode(); validateFilters(); - if ( !m_bForceCloseSessionOnEndOfTheDay ) { - unsigned long long tD = getTimeValue() / 86400000; - if ( m_mDateOfToday != tD ) { - //New Day Started - if( !m_bSessionStarted ){ - m_mDateOfToday = tD; - } else { - m_bForceCloseSessionOnEndOfTheDay = true; - }; - }; - }; validateSession(); validateReports(); //?? - if (! m_pHardwareManager->validate()) - { + if (! m_pHardwareManager->validate()){ LOG("Reload Mojo Node Due to Memory Consumption!"); m_bThreadStatus = TH_ENDED; setExitCause(EXIT_CODE_KERNEL_CLEAR); @@ -842,83 +810,63 @@ void mojoKernel::m_fThreadProc(void) m_pMediaHelper->validateService(); }; #endif - if (m_sSiteData.use_remote_server) - { - if (m_pRPC) - { + if (m_sSiteData.use_remote_server){ + if (m_pRPC){ m_pRPC->validate(); m_bWorkWithRemoteServer = (m_pRPC->getConnectionStatus() == CS_CONNECTED); - } - else - { + } else { // Checking if Nodes are Loadded - if (m_pNodeFactory->nodesAreLoaded()) - { + if (m_pNodeFactory->nodesAreLoaded()) { #ifdef DEBUG LOG("Initial Connect to Remote Server"); #endif - if ((m_pRPC = new mojoRemoteClient(this))) - { - if (! (m_bWorkWithRemoteServer = m_pRPC->create())) - { + if ((m_pRPC = new mojoRemoteClient(this))) { + if (! (m_bWorkWithRemoteServer = m_pRPC->create())) { LOG_ERROR("Cannot Connect to Remote Server!"); } - } - else - { + } else { m_bWorkWithRemoteServer = false; } }; }; }; DBC_PUSHED_CMD * tCMD = m_pDBC->getNodePushedCMD(m_nServerNodeID); // Kernel - if (tCMD != NULL) - { + if (tCMD != NULL) { m_pDBC->updateNodePushedCMD(tCMD->id, DBC_CMD_STARTED); - switch (tCMD->CMD) - { - case CMD_KRNL_RELOAD: - { + switch (tCMD->CMD) { + case CMD_KRNL_RELOAD:{ LOG("Reload Mojo Node!"); m_bThreadStatus = TH_ENDED; setExitCause(EXIT_CODE_RELOAD_SERVICES); break; }; - case CMD_KRNL_RESTART: - { + case CMD_KRNL_RESTART:{ LOG("Restart Mojo Node!"); m_bThreadStatus = TH_ENDED; setExitCause(EXIT_CODE_KERNEL_CLEAR); break; }; - case CMD_KRNL_FACTORY_RESET: - { + case CMD_KRNL_FACTORY_RESET:{ LOG("Restart Mojo -> Factory Reset!"); m_bThreadStatus = TH_ENDED; setExitCause(EXIT_CODE_FACTORY_RESET); break; }; - case CMD_NODES_RELOAD: - { - if (m_pNodeFactory != NULL) - { + case CMD_NODES_RELOAD:{ + if (m_pNodeFactory != NULL){ m_pNodeFactory->reloadNodes(); }; break; }; - case CMD_NODE_RELOAD: - { + case CMD_NODE_RELOAD:{ uint32_t tNodeID = atoi(tCMD->PARAM); - if (m_pNodeFactory != NULL) - { + if (m_pNodeFactory != NULL){ m_pNodeFactory->reloadNode(tNodeID); }; break; }; - case CMD_KRNL_PNP: - { - if (m_pHardwareManager != NULL) - { + case CMD_KRNL_PNP:{ + if (m_pHardwareManager != NULL){ LOG_ERROR("Restart and Detect HardWare!"); // Must be shutdowned all service that uses hardware prior to detection! m_bThreadStatus = TH_ENDED; @@ -926,8 +874,7 @@ void mojoKernel::m_fThreadProc(void) }; break; }; - case CMD_KRNL_UPDATE_IDENT: - { + case CMD_KRNL_UPDATE_IDENT:{ LOG("Restart Mojo -> Update Ident!"); sprintf(m_sIdentity, tCMD->PARAM); config_setting_remove(m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY); @@ -941,18 +888,15 @@ void mojoKernel::m_fThreadProc(void) setExitCause(EXIT_CODE_KERNEL_CLEAR); break; }; - case CMD_KRNL_REPLACE_DATABASE: - { + case CMD_KRNL_REPLACE_DATABASE:{ LOG("Restart Mojo -> Replace Database!"); CSTR tNAME = m_pDBC->getDataParam(0); CSTR tUSER = m_pDBC->getDataParam(1); CSTR tGROUP = m_pDBC->getDataParam(2); - if (tNAME) - { + if (tNAME){ char tB[512]; sprintf(tB, "./data/%s.db", tCMD->PARAM); - if (fileExist(tB)) - { + if (fileExist(tB)){ LOG("Restart Mojo -> Close Database!"); // Closing Database delete(m_pDBC); @@ -969,9 +913,7 @@ void mojoKernel::m_fThreadProc(void) LOG("Restart Mojo -> Exit!"); setExitCause(EXIT_CODE_NO_DATABASE); // EXIT_CODE_RELOAD_SERVICES ); m_bThreadStatus = TH_ENDED; - } - else - { + } else { LOG_ERROR("New Database Does not Exists!"); }; // free( tNAME ); @@ -980,15 +922,12 @@ void mojoKernel::m_fThreadProc(void) }; break; }; - case CMD_KRNL_SYNC_TIME: - { + case CMD_KRNL_SYNC_TIME:{ uint32_t uts = atoi(tCMD->PARAM); - if (uts) - { + if (uts){ #if (__GLIBC__ < 3) && (__GLIBC_MINOR__ < 31) time_t ts = (time_t) uts; - if (stime(&ts) == 0) - { + if (stime(&ts) == 0){ // Restert Server; m_bThreadStatus = TH_ENDED; setExitCause(EXIT_CODE_KERNEL_CLEAR); @@ -997,8 +936,7 @@ void mojoKernel::m_fThreadProc(void) timespec ts; ts.tv_sec = (time_t) uts; ts.tv_nsec = 0; - if (clock_settime(CLOCK_REALTIME, &ts) == 0) - { + if (clock_settime(CLOCK_REALTIME, &ts) == 0){ // Restert Server; m_bThreadStatus = TH_ENDED; setExitCause(EXIT_CODE_KERNEL_CLEAR); @@ -1007,41 +945,33 @@ void mojoKernel::m_fThreadProc(void) }; break; }; - case CMD_KRNL_SYSTEM_TASK: - { + case CMD_KRNL_SYSTEM_TASK:{ break; }; - case NODE_FORCE_FINISH_ALARM: - { + case NODE_FORCE_FINISH_ALARM:{ stopSession(); break; }; - case CMD_FORCE_FREEZE_BUBBLES: - { + case CMD_FORCE_FREEZE_BUBBLES:{ LOG("Freeze Bubbles NCQ"); EVENT_CONTAINER * tTT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_FLT_BUBBLES_BLOCK); - if (tTT) - { + if (tTT){ m_pEventHeap->broadcast(tTT); }; break; }; - case CMD_FORCE_POP_BUBBLE: - { + case CMD_FORCE_POP_BUBBLE:{ LOG("POP Bubbles NCQ"); EVENT_CONTAINER * tTT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_FLT_BUBBLES_DEL_FB); - if (tTT) - { + if (tTT){ m_pEventHeap->broadcast(tTT); }; break; }; - case CMD_FORCE_RELOAD_BUBBLES: - { + case CMD_FORCE_RELOAD_BUBBLES:{ LOG("Reload Bubbles NCQ"); EVENT_CONTAINER * tTT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_FLT_BUBBLES_RELOAD); - if (tTT) - { + if (tTT){ m_pEventHeap->broadcast(tTT); }; break; @@ -1081,8 +1011,7 @@ void mojoKernel::m_fThreadProc(void) }; free(tCMD); }; - if (m_pDBC) - { + if ( m_pDBC ){ m_pDBC->cleanNodePushedCMD(); } }; @@ -1943,41 +1872,43 @@ void mojoKernel::handleVideo(VIDEO_SEGMENT_INFO * ptr) }; } -bool mojoKernel::validateReports(void) -{ +bool mojoKernel::validateReports(void){ /// @todo Stephane needs to follow this function... - if (m_bKeepLocalReport) - { + if ( m_bKeepLocalReport ){ + if ( !m_bForceCloseSessionOnEndOfTheDay ) { + uint8_t cd = getLocalDay(); + if ( m_mDateOfToday != cd ) { + //New Day Started + m_mDateOfToday = cd; + if( m_bSessionStarted ){ + m_bForceCloseSessionOnEndOfTheDay = true; + }; + }; + }; + DBC_SITE_DATA * tSiteData = m_pDBC->getSiteData(1); - if (tSiteData) - { + if (tSiteData){ // Check if settings has been changed in DB - if (tSiteData->report_cleanup != m_sSiteData.report_cleanup || tSiteData->preserve_empty != m_sSiteData.preserve_empty) - { + if (tSiteData->report_cleanup != m_sSiteData.report_cleanup || tSiteData->preserve_empty != m_sSiteData.preserve_empty){ memcpy(&m_sSiteData, tSiteData, sizeof(DBC_SITE_DATA)); }; free(tSiteData); - } - else - { + } else { LOG_ERROR("Error Loading Site DATA!"); return false; }; - if (m_sSiteData.report_cleanup) - { + if (m_sSiteData.report_cleanup) { cleanUpReportDir(m_sSiteData.report_cleanup); }; - if (m_sSiteData.preserve_empty) - { + if (m_sSiteData.preserve_empty) { float tT = 100.0f * (1.0f - (1.0f / ((float) m_sSiteData.preserve_empty))); #ifdef DEBUG_EXTRA char tB[128]; sprintf(tB, "Check Disk Size: %0.3f %0.3f %d [%d]", m_sSysInfo.disk, tT, m_sSiteData.preserve_empty); LOG(tB); #endif - if (m_sSysInfo.disk > tT) - { + if (m_sSysInfo.disk > tT) { #ifdef DEBUG_EXTRA LOG("Delete Some Reports!"); #endif @@ -1985,7 +1916,7 @@ bool mojoKernel::validateReports(void) m_pDBC->deleteReportDate(m_pDBC->getFirstEventTS()); }; }; - } + }; return true; } diff --git a/lib/App/serva/servaModBusManager.cpp b/lib/App/serva/servaModBusManager.cpp index 769b2776..ec7c4889 100644 --- a/lib/App/serva/servaModBusManager.cpp +++ b/lib/App/serva/servaModBusManager.cpp @@ -221,10 +221,10 @@ void servaModBusManager::m_fThreadProc( void ){ for ( uint8_t bi = 0; bi < 4; bi++ ) { if ( ((*idx)->c_InBits & ( 1 << bi ) ) == ( 1 << bi ) ) { //Bit is set - if ( (*idx)->crossbar[4+bi] ) { // Assigned Action For Bit + if( (*idx)->crossbar[4+bi] ) { // Assigned Action For Bit // check functionality switch ( (*idx)->crossbar[4 + bi] ) { - case 1:{//Upper Track + case MBUS_FUNC_ACK_UPPER_TRACK:{ uint64_t cTID = m_pDBC->getTrackIdByPlatform( (*idx)->platform_id, 'U' ); if ( cTID ) { SESSION_DATA *tSData = servaSessionManager::getInstance()->getActiveSessionByTrack( cTID ); @@ -237,8 +237,8 @@ void servaModBusManager::m_fThreadProc( void ){ for ( DBC_UINT64_LIST::iterator idd=tIDS->begin(); idd != tIDS->end(); idd++ ) { MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idd ); if ( tRTU ) { - uint8_t bON = ( 1 << tRTU->crossbar[MBUS_CROSSBAR_UPPER_TRACK] ); - uint8_t bOFF = ~( 1 << tRTU->crossbar[MBUS_CROSSBAR_UPPER_TRACK] ); + uint8_t bON = ( 1 << ( tRTU->crossbar[MBUS_CROSSBAR_UPPER_TRACK] - 1 ) ); + uint8_t bOFF = ~( 1 << ( tRTU->crossbar[MBUS_CROSSBAR_UPPER_TRACK] - 1 ) ); //Remove Scheduled Signal change m_mProtector.lock(); for ( V_MODBUS_ACION_ITEMS::iterator idz=m_vActioItems.begin(); idz!=m_vActioItems.end(); ){ @@ -272,7 +272,7 @@ void servaModBusManager::m_fThreadProc( void ){ }; break; }; - case 2:{//Bottom Track + case MBUS_FUNC_ACK_BOTTOM_TRACK:{//Bottom Track uint64_t cTID = m_pDBC->getTrackIdByPlatform( (*idx)->platform_id, 'B' ); if ( cTID ) { SESSION_DATA *tSData = servaSessionManager::getInstance()->getActiveSessionByTrack( cTID ); @@ -320,7 +320,7 @@ void servaModBusManager::m_fThreadProc( void ){ }; break; }; - case 3:{//Both Tracks + case MBUS_FUNC_ACK_BOTH_TRACKS:{//Both Tracks uint64_t cTID = m_pDBC->getTrackIdByPlatform( (*idx)->platform_id, 'U' ); if ( cTID ) { SESSION_DATA *tSData = servaSessionManager::getInstance()->getActiveSessionByTrack( cTID ); @@ -413,7 +413,7 @@ void servaModBusManager::m_fThreadProc( void ){ }; break; }; - case 4:{ // Set TCC AUX signal + case MBUS_FUNC_SIG_STROBE_LIGHT:{ // Set TCC AUX signal // ?? and create Scheduled mitigation of the signal ?? DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( (*idx)->platform_id ); if ( tIDS ) { @@ -422,8 +422,8 @@ void servaModBusManager::m_fThreadProc( void ){ if ( tRTU ) { // check if TCC Module if ( tRTU->func == 3 ) { - uint8_t bON = (1 << tRTU->crossbar[MBUS_CROSSBAR_AUX]); - uint8_t bOFF = ~( 1 << tRTU->crossbar[MBUS_CROSSBAR_AUX] ); + uint8_t bON = (1 << ( tRTU->crossbar[MBUS_CROSSBAR_AUX] - 1 ) ); + uint8_t bOFF = ~( 1 << ( tRTU->crossbar[MBUS_CROSSBAR_AUX] - 1 ) ); //Remove Scheduled Signal change m_mProtector.lock(); for ( V_MODBUS_ACION_ITEMS::iterator idz=m_vActioItems.begin(); idz!=m_vActioItems.end(); ){ @@ -452,7 +452,7 @@ void servaModBusManager::m_fThreadProc( void ){ //?? How long tAI->scheduled_on = getMilliseconds() + ( m_sManagerData.automaticAlarmRisingTO * 1000 ); tAI->module_id = tRTU->id; - tAI->value = ( 1 << tRTU->crossbar[MBUS_CROSSBAR_AUX] ); + tAI->mask = (1 << (tRTU->crossbar[MBUS_CROSSBAR_AUX] - 1)); m_mProtector.lock(); m_vActioItems.push_back( tAI ); m_mProtector.unlock(); @@ -467,7 +467,7 @@ void servaModBusManager::m_fThreadProc( void ){ //?? How long tAI->scheduled_on = getMilliseconds() + ( m_sManagerData.automaticAlarmRisingTO * 1000 ); tAI->module_id = tRTU->id; - tAI->value = ( 1 << tRTU->crossbar[MBUS_CROSSBAR_AUX] ); + tAI->mask = ( 1 << ( tRTU->crossbar[MBUS_CROSSBAR_AUX] - 1 ) ); m_mProtector.lock(); m_vActioItems.push_back( tAI ); m_mProtector.unlock(); @@ -508,8 +508,9 @@ void servaModBusManager::m_fThreadProc( void ){ #ifdef DEBUG_EXTRA LOG("Sync ModBus Modules Done"); #endif + oss = css; // Give 1 Second for STAGE_WORK ?? m_bStage = MBUS_STAGE_WORK; - break; + [[fallthrough]]; }; case MBUS_STAGE_WORK:{ uint64_t cts = getMilliseconds(); @@ -517,34 +518,9 @@ void servaModBusManager::m_fThreadProc( void ){ if ( m_vActioItems.size() ) { for( V_MODBUS_ACION_ITEMS::iterator idx = m_vActioItems.begin(); idx != m_vActioItems.end(); ){ if( (*idx)->scheduled_on < cts ) { - MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( (*idx)->module_id ); - if ( tRTU ) { - if( ( tRTU->c_OutBits & (*idx)->value ) == 0 ) { - //Set - tRTU->c_OutBits |= (*idx)->value; - int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits ); - if ( rv < 0 ) { - m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 ); - } else { - m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits ); - }; - }; - if( ( tRTU->c_OutBits & (*idx)->mask ) != 0 ) { - //Clear - tRTU->c_OutBits &= ~(*idx)->mask; - int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits ); - if ( rv < 0 ) { - m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 ); - } else { - m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits ); - }; - }; - free(tRTU); - }; + handleRTU( (*idx)->module_id, (*idx)->value, (*idx)->mask ); free( *idx ); m_vActioItems.erase( idx ); - //Make Just One SCADA per loop - //break; } else idx++; }; }; @@ -557,14 +533,48 @@ void servaModBusManager::m_fThreadProc( void ){ }; }; if ( oss != css ) { - //Each Second + //Each Second or as it takes .... Scan All RTUs may take longer! oss = css; - m_bStage = MBUS_STAGE_SYNC; + m_bStage = MBUS_STAGE_SYNC;; }; SLEEP(DEFAULT_THREAD_SLEEP_TIME); }; }; +bool servaModBusManager::handleRTU( uint64_t rtu_id, uint8_t value, uint8_t mask ){ + bool rVal = false; + if ( m_pDBC == NULL ) return rVal; + MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( rtu_id ); + if ( tRTU ) { + if( ( tRTU->c_OutBits & value ) == 0 ) { + //Set + tRTU->c_OutBits |= value; + int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits ); + if ( rv < 0 ) { + // Do we need clear Database values on failure?? + //m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 ); + } else { + m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits ); + rVal = true; + }; + }; + if( ( tRTU->c_OutBits & mask ) != 0 ) { + //Clear + tRTU->c_OutBits &= ~mask; + int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits ); + if ( rv < 0 ) { + // Do we need clear Database values on failure?? + //m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 ); + } else { + m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits ); + rVal = true; + }; + }; + free(tRTU); + }; + return rVal; +}; + bool servaModBusManager::validate(){ MODBUS_MANAGER_DATA * tData = m_pDBC->getModBusManagerData(); if ( tData ) { diff --git a/lib/App/serva/servaModBusManager.h b/lib/App/serva/servaModBusManager.h index d4903f78..7ec8e617 100644 --- a/lib/App/serva/servaModBusManager.h +++ b/lib/App/serva/servaModBusManager.h @@ -19,14 +19,29 @@ #define MBUS_STATUS_OFFLINE 0 #define MBUS_STATUS_ONLINE 1 -#define MBUS_CROSSBAR_UPPER_TRACK 1 -#define MBUS_CROSSBAR_BOTTOM_TRACK 2 -#define MBUS_CROSSBAR_HEALTH 3 -#define MBUS_CROSSBAR_AUX 4 +// Crossbar IDs +// DO NOT BE CONFUSED BY BITS of CB ... +// Bits of Crossbar must be decreased by 1, +// to have ability to be disabled by value of 0 +// then the Value of crossbas is a bit position +// It is not a power of 2!! it is a pow( 2, (crossbar_value-1) ) or ( 1 << (crossbar_value-1) ) + +#define MBUS_CROSSBAR_UPPER_TRACK 0 +#define MBUS_CROSSBAR_BOTTOM_TRACK 1 +#define MBUS_CROSSBAR_HEALTH 2 +#define MBUS_CROSSBAR_AUX 3 + +#define MBUS_FUNC_ACK_UPPER_TRACK 1 +#define MBUS_FUNC_ACK_BOTTOM_TRACK 2 +#define MBUS_FUNC_ACK_BOTH_TRACKS 3 +#define MBUS_FUNC_SIG_STROBE_LIGHT 4 +#define MBUS_FUNC_ACK_HEALTH_TROUBLE 5 +#define MBUS_FUNC_ACK_STROBE_TROUBLE 6 +#define MBUS_FUNC_ACK_HEALTH_N_STROBE_TROUBLE 7 class servaKernel; -#define IMMIDIATE_ACTION 0 +#define IMMIDIATE_ACTION 0 typedef struct S_MODBUS_MODULE_ITEM{ uint64_t module_id; @@ -62,6 +77,8 @@ class servaModBusManager: public basicEventHandler, basicThread, basicValidatabl int modbus_get_rtu( CSTR ip, uint16_t port, uint8_t addr, uint8_t &status );//port=502 int modbus_set_rtu( CSTR ip, uint16_t port, uint8_t addr, uint8_t data ); int modbus_set_rtu_bit( CSTR ip, uint16_t port, uint8_t addr, uint8_t bit ); + + bool handleRTU( uint64_t rtu_id, uint8_t set, uint8_t mask ); public: virtual ~servaModBusManager(); bool create(); diff --git a/lib/components/base/basicFunctionality.cpp b/lib/components/base/basicFunctionality.cpp index 8142f89d..b24b8223 100644 --- a/lib/components/base/basicFunctionality.cpp +++ b/lib/components/base/basicFunctionality.cpp @@ -41,6 +41,18 @@ int ch_chown(const char * file_path, const char * user_name, const char * group_ } #endif +int getLocalDay(){ + time_t LocalTime; + time(&LocalTime); + tm local_time; + tm * gmt_time = &local_time; + if( localtime_r(&LocalTime, gmt_time) == NULL ){ + return -1; + } else { + return gmt_time->tm_mday; + }; +} + int checkCmdExists(CSTR cmd) { FILE * fd = popen(cmd, "r"); diff --git a/lib/components/base/basicFunctionality.h b/lib/components/base/basicFunctionality.h index 95a40e02..2e5b154a 100644 --- a/lib/components/base/basicFunctionality.h +++ b/lib/components/base/basicFunctionality.h @@ -71,6 +71,7 @@ void getUniqID_UUID(CSTR bu, uint32_t SEED = 0); CSTR strReplace(CSTR orig, CSTR rep, CSTR with); int checkCmdExists(CSTR cmd); +int getLocalDay(); ssize_t readLine(int fd, char * buffer, size_t n); ssize_t readLine(FILE * fd, char * buffer, size_t n); diff --git a/lib/components/base/basicLogCollection.cpp b/lib/components/base/basicLogCollection.cpp index 41aee956..ab93734a 100644 --- a/lib/components/base/basicLogCollection.cpp +++ b/lib/components/base/basicLogCollection.cpp @@ -78,19 +78,18 @@ void basicLogCollection::Log(const char * Mod, void * ptr, _LOG_LEVEL Level, CST time_t LocalTime; time(&LocalTime); #ifdef USE_LOCAL_TIME_IN_THE_LOG - + tm local_time; + tm * gmt_time = &local_time; + if (localtime_r(&LocalTime, gmt_time) == NULL){ + return; + } #else - #ifndef __linux__ - tm * gmt_time = NULL; - gmt_time = gmtime(&LocalTime); - #else - tm gmtt; - tm * gmt_time = &gmtt; - if (gmtime_r(&LocalTime, gmt_time) == NULL) - { - return; - } - #endif + tm gmtt; + tm * gmt_time = &gmtt; + if (gmtime_r(&LocalTime, gmt_time) == NULL) + { + return; + } #endif switch (m_bLogSettings) {