From 572376dd4a2dbd6d3858abbf7f406643f4b2122d Mon Sep 17 00:00:00 2001 From: Evgeny Gagua Date: Tue, 8 Sep 2026 03:40:22 +0300 Subject: [PATCH] Fix of Midnight Session Problem, and also Fix for continuing to old report file(wrong date/folder) for report history --- lib/App/common/SQLite_dataBinder.cpp | 142 ++++++++-------------- lib/App/common/SQLite_dataBinder.h | 2 +- lib/App/common/kernelModuleEmbedVideo.cpp | 15 ++- lib/App/common/kernelModuleEmbedVideo.h | 1 + lib/App/common/streamHandler.h | 7 ++ lib/App/mojo/mojoKernel.cpp | 44 ++++++- lib/App/mojo/mojoKernel.h | 2 + 7 files changed, 114 insertions(+), 99 deletions(-) diff --git a/lib/App/common/SQLite_dataBinder.cpp b/lib/App/common/SQLite_dataBinder.cpp index f336ee4d..b3209261 100644 --- a/lib/App/common/SQLite_dataBinder.cpp +++ b/lib/App/common/SQLite_dataBinder.cpp @@ -91,7 +91,7 @@ sqliteDBC::sqliteDBC(config_setting_t * Settings) : m_bMemIsConnected = false; m_bRepIsConnected = false; m_bStaIsConnected = false; - m_bLastEventWasClosed = false; + //m_bLastEventWasClosed = false; m_pDataStorage = NULL; m_pDataMemory = NULL; m_pDataReports = NULL; @@ -137,7 +137,8 @@ bool sqliteDBC::connect(void) m_bMemIsConnected = false; m_bRepIsConnected = false; m_bStaIsConnected = false; - m_bLastEventWasClosed = false; + //m_bLastEventWasClosed = false; + bool bLastEventWasClosed = true; // Workaround // Apply New Config #ifdef DEBUG @@ -293,22 +294,17 @@ bool sqliteDBC::connect(void) }; sprintf(m_pDbStat, "%s/stat.db", m_pDbReportBase); m_pDataStatistics = new SQLite_Connector(); - if (m_pDataStatistics) - { - if (fileExist(m_pDbStat)) - { + if (m_pDataStatistics){ + if (fileExist(m_pDbStat)){ m_bStaIsCreated = false; - if (m_pDataStatistics->Connect(m_pDbStat, 0, true)) - { + if (m_pDataStatistics->Connect(m_pDbStat, 0, true)){ #ifdef DEBUG LOG(m_pDbStat); LOG("DB Statistics Opened"); #endif m_pDataStatistics->SilentQuery("PRAGMA journal_mode=WAL"); m_bStaIsConnected = true; - } - else - { + } else { LOG_ERROR("Cannot Connect to DB Statistics"); delete(m_pDataStorage); delete(m_pDataMemory); @@ -320,12 +316,9 @@ bool sqliteDBC::connect(void) m_pDataStatistics = NULL; return false; }; - } - else - { + } else { m_bStaIsCreated = true; - if (m_pDataStatistics->Connect(m_pDbStat, 0, true)) - { + if (m_pDataStatistics->Connect(m_pDbStat, 0, true)){ #ifdef DEBUG LOG("NEW DB Statistics Creating"); #endif @@ -336,9 +329,7 @@ bool sqliteDBC::connect(void) "CREATE TABLE IF NOT EXISTS \"days\" (\"year\" INTEGER,\"month\" INTEGER,\"day\" INTEGER,\"evt_count\" INTEGER DEFAULT 0,\"session_count\" INTEGER DEFAULT 0,\"videos_count\" INTEGER DEFAULT 0,\"session_closed\" INTEGER " "DEFAULT 0, CONSTRAINT \"date_uni\" UNIQUE(\"year\",\"month\",\"day\") );"); m_bStaIsConnected = true; - } - else - { + } else { LOG_ERROR("Cannot Connect to DB Statistics"); delete(m_pDataStorage); delete(m_pDataMemory); @@ -352,42 +343,34 @@ bool sqliteDBC::connect(void) }; }; }; - m_pDataReports = new SQLite_Connector(); - if (m_pDataReports) - { + m_pDataReports = new SQLite_Connector(); + if (m_pDataReports){ +/* memset(&m_sPRepInfo, 0, sizeof(REPORT_DIRINFO)); - if (! m_bStaIsCreated) - { + if (! m_bStaIsCreated ){ #ifdef DEBUG LOG("OLD Statistics PRESENT Report Created!"); #endif AAssoc * result = m_pDataStatistics->Query("SELECT * FROM `days` ORDER BY `year` DESC, `month` DESC, `day` DESC LIMIT 1"); - if (result) - { - if (result->size() > 0) - { + if (result){ + if (result->size() > 0){ Assoc * cArr; Assoc::iterator it; cArr = result->at(0); it = cArr->find("year"); - if (it != cArr->end()) - { + if (it != cArr->end()){ strncpy(m_sPRepInfo.YEAR, it->second.c_str(), 4); }; it = cArr->find("month"); - if (it != cArr->end()) - { + if (it != cArr->end()){ sprintf(m_sPRepInfo.MONTH, "%02d", atoi(it->second.c_str())); }; it = cArr->find("day"); - if (it != cArr->end()) - { + if (it != cArr->end()){ sprintf(m_sPRepInfo.DAY, "%02d", atoi(it->second.c_str())); }; m_sPRepInfo.ts = getTsFromRDF(m_sPRepInfo); - } - else - { + } else { #ifdef DEBUG LOG("No Rows Fetched from STAT!"); #endif @@ -396,94 +379,71 @@ bool sqliteDBC::connect(void) m_pDataMemory->deleteResult(result); }; - if (m_sPRepInfo.ts != 0) - { + if( m_sPRepInfo.ts != 0 ){ #ifdef DEBUG LOG("Have Old Reports!"); #endif - if (checkRDFDir(m_pDbReportBase, m_sPRepInfo, false) == 0) - { + if (checkRDFDir(m_pDbReportBase, m_sPRepInfo, false) == 0){ LOG_ERROR("Error Connecting to Prev Report!"); - m_bLastEventWasClosed = true; - } - else - { + bLastEventWasClosed = true; + } else { #ifdef DEBUG LOG("Connect to old Report Database"); #endif sprintf(m_pDbReport, "%s/%s/%s/%s/report.db", m_pDbReportBase, m_sPRepInfo.YEAR, m_sPRepInfo.MONTH, m_sPRepInfo.DAY); LOG(m_pDbReport); - if (fileExist(m_pDbReport)) - { + if (fileExist(m_pDbReport)) { SQLite_Connector * tDBC = new SQLite_Connector(); - if (tDBC) - { - if (tDBC->Connect(m_pDbReport, 0, true)) - { + if (tDBC) { + if (tDBC->Connect(m_pDbReport, 0, true)){ char tQBuf[CTEXT]; AAssoc * result; - sprintf(tQBuf, "SELECT `evt_type` FROM `events` WHERE `evt_type`=%d OR `evt_type`=%d OR `evt_type`=%d ORDER BY `ts` DESC, `ts_ms` DESC LIMIT 1", EVT_LRF_START_SESSION, EVT_LRF_END_SESSION_MA, EVT_LRF_END_SESSION_TO); + //sprintf(tQBuf, "SELECT `evt_type` FROM `events` WHERE `evt_type`=%d OR `evt_type`=%d OR `evt_type`=%d ORDER BY `ts` DESC, `ts_ms` DESC LIMIT 1", EVT_LRF_START_SESSION, EVT_LRF_END_SESSION_MA, EVT_LRF_END_SESSION_TO); + sprintf( tQBuf, "SELECT `evt_type`, `ts`, `ts_ms` FROM `events` ORDER BY `ts` DESC, `ts_ms` DESC LIMIT 1" ); #ifdef DEBUG_ALL LOG(tQBuf); #endif result = tDBC->Query(tQBuf); - if (result != NULL) - { - if (result->size() > 0) - { + if (result != NULL){ + if (result->size() > 0){ Assoc * cArr; Assoc::iterator it; cArr = result->at(0); it = cArr->find("evt_type"); - if (it != cArr->end()) - { - if (atoi(it->second.c_str()) != EVT_LRF_START_SESSION) - { - m_bLastEventWasClosed = true; + if (it != cArr->end()){ + if (atoi(it->second.c_str()) != EVT_LRF_START_SESSION){ + bLastEventWasClosed = true; } }; - } - else - { - m_bLastEventWasClosed = true; - } + } else { + bLastEventWasClosed = true; + }; }; deleteResult(result); - } - else - { + } else { LOG_ERROR("Error Connecting to Prev Report!"); - m_bLastEventWasClosed = true; + bLastEventWasClosed = true; }; delete(tDBC); - } - else - { + } else { LOG_ERROR("Error Create DB Connection Object!"); - m_bLastEventWasClosed = true; + bLastEventWasClosed = true; }; - } - else - { + } else { LOG_ERROR("Error No Prev Report Database!"); - m_bLastEventWasClosed = true; + bLastEventWasClosed = true; }; }; - } - else - { - m_bLastEventWasClosed = true; + } else { + bLastEventWasClosed = true; }; - - if (m_bLastEventWasClosed) - { +*/ + if( bLastEventWasClosed ){ #ifdef DEBUG LOG("Last Session is Closed!"); #endif fillRDF(time(NULL), m_sCRepInfo); - } - else - { + } else { #ifdef DEBUG LOG("Last Session is NOT Closed!"); #endif @@ -492,8 +452,7 @@ bool sqliteDBC::connect(void) #ifdef DEBUG LOG(m_pDbReportBase); #endif - if (! checkRDFDir(m_pDbReportBase, m_sCRepInfo, true, getUserUid(web_access_user), getUserGid(web_access_group))) - { + if (! checkRDFDir(m_pDbReportBase, m_sCRepInfo, true, getUserUid(web_access_user), getUserGid(web_access_group))){ LOG_ERROR("Cannot Open Report!!"); return false; }; @@ -501,8 +460,7 @@ bool sqliteDBC::connect(void) #ifdef DEBUG LOG(m_pDbReport); #endif - if (m_pDataReports->Connect(m_pDbReport, 0, true)) - { + if (m_pDataReports->Connect(m_pDbReport, 0, true)){ #ifdef DEBUG LOG("Report File Opened"); #endif diff --git a/lib/App/common/SQLite_dataBinder.h b/lib/App/common/SQLite_dataBinder.h index a6632f37..b9452b52 100644 --- a/lib/App/common/SQLite_dataBinder.h +++ b/lib/App/common/SQLite_dataBinder.h @@ -32,7 +32,7 @@ class sqliteDBC : public dataBinder bool m_bRepIsConnected; bool m_bStaIsConnected; bool m_bStaIsCreated; - bool m_bLastEventWasClosed; + //bool m_bLastEventWasClosed; bool m_bRecreateMemStructures; char m_pDbFile[CLOG]; diff --git a/lib/App/common/kernelModuleEmbedVideo.cpp b/lib/App/common/kernelModuleEmbedVideo.cpp index 4e1f8af2..63adc285 100644 --- a/lib/App/common/kernelModuleEmbedVideo.cpp +++ b/lib/App/common/kernelModuleEmbedVideo.cpp @@ -314,7 +314,20 @@ void signal_pipe_handler(int SIG) basicLogCollection::getInstance()->Log("SIG_PIPE", NULL, _LOG_ERROR, "Sig Pipe Received Web Client Disconnected!"); #endif } - +bool kernelModuleEmbedVideo::isProcessingVideoStream(){ + bool RV = false; + m_mStreamHandlersProtector.lock(); + if (m_mapHANDLERS.size() ){ + for (mapSTREAMHANDLERS::iterator idx = m_mapHANDLERS.begin(); idx != m_mapHANDLERS.end(); idx++){ + if( (*idx).second->preservingCurrentRecording() ){ + RV = true; + break; + }; + }; + }; + m_mStreamHandlersProtector.unlock(); + return RV;( m_mapHANDLERS.size() > 0 ); +}; uint8_t kernelModuleEmbedVideo::getMediaMode(void) { return m_sEmbedVideoData.output_mode; diff --git a/lib/App/common/kernelModuleEmbedVideo.h b/lib/App/common/kernelModuleEmbedVideo.h index ef91e007..81bca47d 100644 --- a/lib/App/common/kernelModuleEmbedVideo.h +++ b/lib/App/common/kernelModuleEmbedVideo.h @@ -141,6 +141,7 @@ class kernelModuleEmbedVideo : public kernelModule bool registerStream(uint32_t streamID, uint8_t flip_mode = 0); bool unregisterStream(uint32_t streamID); uint8_t getMediaMode(void); + bool isProcessingVideoStream(); friend class streamHandler; }; #else //LRF_PROXY_ONLY diff --git a/lib/App/common/streamHandler.h b/lib/App/common/streamHandler.h index 2fd3f347..355d94a9 100644 --- a/lib/App/common/streamHandler.h +++ b/lib/App/common/streamHandler.h @@ -365,6 +365,13 @@ class streamHandler : bool triggerEvent(uint32_t eid, uint32_t & dbid, uint32_t & frame); bool triggerCamMove(void); bool validate(void); + bool preservingCurrentRecording(){ +#ifdef EVE_SEQUENTAL_OUTPUT + return !m_bSegDelete; +#else + return !( m_bSegADelete || m_bSegBDelete ); +#endif + }; }; #endif // !LRF_PROXY_ONLY diff --git a/lib/App/mojo/mojoKernel.cpp b/lib/App/mojo/mojoKernel.cpp index e245c610..1a38bf06 100644 --- a/lib/App/mojo/mojoKernel.cpp +++ b/lib/App/mojo/mojoKernel.cpp @@ -72,6 +72,8 @@ mojoKernel::mojoKernel(config_setting_t * ptr) : m_bKeepLocalReport = true; m_bMediaMode = 0; m_bApplicationFirstRun = false; + m_bForceCloseSessionOnEndOfTheDay = false; + m_mDateOfToday = getTimeValue() / 86400000; // round to a Day m_pRPC = NULL; #ifndef LRF_PROXY_ONLY m_pMediaHelper = NULL; @@ -810,6 +812,17 @@ 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(); //?? @@ -1316,7 +1329,25 @@ bool mojoKernel::dispatchEvent(EVENT_CONTAINER * Evt) { return false; } - switch (Evt->m_eClass) + + if( m_bForceCloseSessionOnEndOfTheDay && m_bSessionStarted ) { +#ifndef LRF_PROXY_ONLY + kernelModuleEmbedVideo * tMO = (kernelModuleEmbedVideo *) m_pModuleFactory->getModule(KERNEL_MODULE_EMBED_VIDEO); + if( tMO ){ + if( !tMO->isProcessingVideoStream() ) { + stopSession(); + m_bForceCloseSessionOnEndOfTheDay = false; + }; + } else { + LOG_ERROR("No Embed Video Processing Module!"); + }; +#else + stopSession(); + m_bForceCloseSessionOnEndOfTheDay = false; +#endif + } + + switch (Evt->m_eClass) { case EVT_CLASS_SYSTEM: switch (Evt->m_eType) @@ -1902,11 +1933,14 @@ void mojoKernel::handleVideo(VIDEO_SEGMENT_INFO * ptr) LOG("Video Scratch Deleted!"); #endif }; - } - else - { + } else { LOG_ERROR("Video File is Missing!"); - } + }; + //In case end of the day Force Session Close after Video File is saved + if ( m_bForceCloseSessionOnEndOfTheDay ) { + stopSession( m_bForceCloseSessionOnEndOfTheDay ); + m_bForceCloseSessionOnEndOfTheDay = false; + }; } bool mojoKernel::validateReports(void) diff --git a/lib/App/mojo/mojoKernel.h b/lib/App/mojo/mojoKernel.h index 32d651ca..0dd49630 100644 --- a/lib/App/mojo/mojoKernel.h +++ b/lib/App/mojo/mojoKernel.h @@ -79,6 +79,8 @@ class mojoKernel : public kernelPrototype kernelHelperMediaMTX * m_pMediaHelper; #endif basicMutex m_mSysInfoProtector; + unsigned long long m_mDateOfToday; + bool m_bForceCloseSessionOnEndOfTheDay; bool m_bDebugMode; bool m_bKeepLocalReport; bool m_bWorkWithRemoteServer;