@ -99,6 +99,7 @@ sqliteDBC::sqliteDBC(config_setting_t * Settings) :
m_nServerID = 1 ;
memset ( & m_sCRepInfo , 0 , sizeof ( REPORT_DIRINFO ) ) ;
memset ( & m_sPRepInfo , 0 , sizeof ( REPORT_DIRINFO ) ) ;
m_nCRepHour = - 1 ;
pthread_mutexattr_t tAttr ;
pthread_mutexattr_init ( & tAttr ) ;
pthread_mutexattr_settype ( & tAttr , PTHREAD_MUTEX_RECURSIVE ) ;
@ -450,7 +451,9 @@ bool sqliteDBC::connect(void)
# ifdef DEBUG
LOG ( " Last Session is Closed! " ) ;
# endif
fillRDF ( time ( NULL ) , m_sCRepInfo ) ;
time_t tNow = time ( NULL ) ;
fillRDF ( tNow , m_sCRepInfo ) ;
m_nCRepHour = getLocalHour ( tNow ) ;
} else {
# ifdef DEBUG
LOG ( " Last Session is NOT Closed! " ) ;
@ -464,7 +467,9 @@ bool sqliteDBC::connect(void)
LOG_ERROR ( " Cannot Open Report!! " ) ;
return false ;
} ;
sprintf ( m_pDbReport , " %s/%s/%s/%s/report.db " , m_pDbReportBase , m_sCRepInfo . YEAR , m_sCRepInfo . MONTH , m_sCRepInfo . DAY ) ;
char tRepName [ 32 ] ;
snprintf ( tRepName , sizeof ( tRepName ) , REPORT_DB_NAME_FMT , m_nCRepHour < 0 ? 0 : m_nCRepHour ) ;
sprintf ( m_pDbReport , " %s/%s/%s/%s/%s " , m_pDbReportBase , m_sCRepInfo . YEAR , m_sCRepInfo . MONTH , m_sCRepInfo . DAY , tRepName ) ;
# ifdef DEBUG
LOG ( m_pDbReport ) ;
# endif
@ -570,7 +575,7 @@ void sqliteDBC::initMemDB(uint32_t sid)
CSTR sqliteDBC : : getCurrentReportDir ( void )
{
ReportGuard tReportGuard ( m_mReportLock ) ; // see switchReportToCurrentDay ()
ReportGuard tReportGuard ( m_mReportLock ) ; // see switchReportToCurrentHour ()
CSTR tV = NULL ;
if ( m_sCRepInfo . ts )
{
@ -861,12 +866,14 @@ bool sqliteDBC::addDeviceDraw(uint32_t DBID, uint8_t type, uint8_t lrf, void * t
}
/*
* Opens ( creates if needed ) the report of the given day : data / reports / YYYY / MM / DD / report . db
* SQLite_Connector : : Connect ( ) closes the currently open report under its own lock first .
* Caller must hold m_mReportLock .
* Opens ( creates if needed ) the report of the given day and local hour :
* data / reports / YYYY / MM / DD / report_HH . db ( REPORT_DB_NAME_FMT )
* Pictures and videos stay in the day folder . SQLite_Connector : : Connect ( ) closes the
* currently open report under its own lock first . Caller must hold m_mReportLock .
*/
bool sqliteDBC : : openDay Report ( REPORT_DIRINFO & RDF )
bool sqliteDBC : : openHour Report ( REPORT_DIRINFO & RDF , int hour )
{
char tName [ 32 ] ;
char tPath [ CLOG ] ;
char tQBuf [ 1024 ] ;
if ( ! checkRDFDir ( m_pDbReportBase , RDF , true , getUserUid ( web_access_user ) , getUserGid ( web_access_group ) ) )
@ -874,7 +881,13 @@ bool sqliteDBC::openDayReport(REPORT_DIRINFO & RDF)
LOG_ERROR ( " Cannot create report directory! " ) ;
return false ;
} ;
snprintf ( tPath , sizeof ( tPath ) , " %s/%s/%s/%s/report.db " , m_pDbReportBase , RDF . YEAR , RDF . MONTH , RDF . DAY ) ;
if ( hour < 0 | | hour > 23 )
{
LOG_ERROR ( " Invalid report hour! " ) ;
return false ;
} ;
snprintf ( tName , sizeof ( tName ) , REPORT_DB_NAME_FMT , hour ) ;
snprintf ( tPath , sizeof ( tPath ) , " %s/%s/%s/%s/%s " , m_pDbReportBase , RDF . YEAR , RDF . MONTH , RDF . DAY , tName ) ;
LOG ( " Connect Report! " ) ;
LOG ( tPath ) ;
if ( m_bRepIsConnected )
@ -884,7 +897,7 @@ bool sqliteDBC::openDayReport(REPORT_DIRINFO & RDF)
m_bRepIsConnected = false ;
if ( ! m_pDataReports - > Connect ( tPath , 0 , true ) )
{
LOG_ERROR ( " Cannot open day report! " ) ;
LOG_ERROR ( " Cannot open hour report! " ) ;
return false ;
} ;
strncpy ( m_pDbReport , tPath , CLOG - 1 ) ;
@ -905,6 +918,7 @@ bool sqliteDBC::openDayReport(REPORT_DIRINFO & RDF)
" NULL, \" fps \" INTEGER NOT NULL, \" flength \" INTEGER, PRIMARY KEY( \" id \" AUTOINCREMENT) ) " ) ;
memcpy ( & m_sPRepInfo , & m_sCRepInfo , sizeof ( REPORT_DIRINFO ) ) ;
memcpy ( & m_sCRepInfo , & RDF , sizeof ( REPORT_DIRINFO ) ) ;
m_nCRepHour = hour ;
m_bRepIsConnected = true ;
LOG ( " Report Connected! " ) ;
// Create the day record (no session yet) - session counters are updated on SESSION_START / SESSION_END
@ -919,31 +933,39 @@ bool sqliteDBC::openDayReport(REPORT_DIRINFO & RDF)
}
/*
* Called by the kernel at local midnight ( and on every SESSION_START ) : if the local day changed
* since the current report was opened , close it and open the new day ' s report directory .
* Called by the kernel when the local hour changes ( and on every SESSION_START ) : if the local
* day or hour changed since the current report was opened , close it and open the current
* hour ' s report_HH . db ( in a new day folder at midnight ) .
*/
bool sqliteDBC : : switchReportToCurrentDay ( void )
bool sqliteDBC : : switchReportToCurrentHour ( void )
{
ReportGuard tGuard ( m_mReportLock ) ;
if ( m_pDataReports = = NULL )
{
return false ;
} ;
time_t tNow = time ( NULL ) ;
REPORT_DIRINFO tRR ; // initialized by fillRDF
fillRDF ( time ( NULL ) , tRR ) ;
if ( m_bRepIsConnected & & ( tRR . ts < = m_sCRepInfo . ts ) )
fillRDF ( tNow , tRR ) ;
int tHour = getLocalHour ( tNow ) ;
// Same day and same hour (also when the hour repeats at the end of daylight saving time)
if ( m_bRepIsConnected & & ( tRR . ts < = m_sCRepInfo . ts ) & & ( tHour = = m_nCRepHour ) )
{
return true ; // already the current hour's report
} ;
if ( m_bRepIsConnected & & ( tRR . ts < m_sCRepInfo . ts ) )
{
return true ; // already today's report
return true ; // clock moved back to a previous day - keep the open report
} ;
char tB [ 128 ] ;
snprintf ( tB , sizeof ( tB ) , " Day report switch %s-%s-%s -> %s-%s-%s " , m_sCRepInfo . YEAR , m_sCRepInfo . MONTH , m_sCRepInfo . DAY , tRR . YEAR , tRR . MONTH , tRR . DAY ) ;
snprintf ( tB , sizeof ( tB ) , " Report switch %s-%s-%s %02d -> %s-%s-%s %02d " , m_sCRepInfo . YEAR , m_sCRepInfo . MONTH , m_sCRepInfo . DAY , m_nCRepHour , tRR . YEAR , tRR . MONTH , tRR . DAY , tHour ) ;
LOG ( tB ) ;
return openDay Report ( tRR ) ;
return openHour Report ( tRR , tHour ) ;
}
void sqliteDBC : : updateStats ( bool evt , bool video , uint8_t ses_status )
{
ReportGuard tReportGuard ( m_mReportLock ) ; // see switchReportToCurrentDay ()
ReportGuard tReportGuard ( m_mReportLock ) ; // see switchReportToCurrentHour ()
// Session status changes are called as updateStats(false, false, SESSION_xxx): they must not be
// filtered out by the evt/video condition (they were, so the session counters and the new-day
// report switch on SESSION_START never ran).
@ -970,8 +992,8 @@ void sqliteDBC::updateStats(bool evt, bool video, uint8_t ses_status)
} ;
case SESSION_START :
{
// Close the previous day report and open today's one (if the local day changed)
switchReportToCurrentDay ( ) ;
// Close the previous report and open the current hour's one (if the hour changed)
switchReportToCurrentHour ( ) ;
// 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 ,
@ -1008,7 +1030,7 @@ void sqliteDBC::updateStats(bool evt, bool video, uint8_t ses_status)
bool sqliteDBC : : dropSession ( )
{
ReportGuard tReportGuard ( m_mReportLock ) ; // see switchReportToCurrentDay ()
ReportGuard tReportGuard ( m_mReportLock ) ; // see switchReportToCurrentHour ()
if ( m_bRepIsConnected )
{
char tQBuf [ 1024 ] ;
@ -1067,7 +1089,7 @@ bool sqliteDBC::dropSession()
uint32_t sqliteDBC : : addEvent ( DBC_EVT_RECORD * ptr )
{
ReportGuard tReportGuard ( m_mReportLock ) ; // see switchReportToCurrentDay ()
ReportGuard tReportGuard ( m_mReportLock ) ; // see switchReportToCurrentHour ()
if ( ptr = = NULL )
{
return 0 ;
@ -1611,7 +1633,7 @@ bool sqliteDBC::addLRFWidthTable(uint32_t sensorID, PWIDTH_TABLE ptr)
bool sqliteDBC : : addPicture ( DBC_SNAPSHOT_DATA * ptr , CSTR ext )
{
ReportGuard tReportGuard ( m_mReportLock ) ; // see switchReportToCurrentDay ()
ReportGuard tReportGuard ( m_mReportLock ) ; // see switchReportToCurrentHour ()
bool rVal = false ;
if ( ptr ! = NULL )
{
@ -1679,7 +1701,7 @@ uint32_t sqliteDBC::addResource(uint8_t type, CSTR devName, uint32_t auxID)
bool sqliteDBC : : addVideoEvent ( uint32_t evtID , uint32_t videoID , uint32_t frame , uint32_t tss , uint32_t tms )
{
ReportGuard tReportGuard ( m_mReportLock ) ; // see switchReportToCurrentDay ()
ReportGuard tReportGuard ( m_mReportLock ) ; // see switchReportToCurrentHour ()
if ( m_bRepIsConnected )
{
char tQBuf [ 2048 ] ;
@ -1694,7 +1716,7 @@ bool sqliteDBC::addVideoEvent(uint32_t evtID, uint32_t videoID, uint32_t frame,
uint32_t sqliteDBC : : addVideoRecord ( uint32_t streamID , CSTR md5 , CSTR ext , uint32_t tss , uint32_t tms , uint32_t fps )
{
ReportGuard tReportGuard ( m_mReportLock ) ; // see switchReportToCurrentDay ()
ReportGuard tReportGuard ( m_mReportLock ) ; // see switchReportToCurrentHour ()
uint32_t RetVal = 0 ;
if ( m_bRepIsConnected )
{
@ -2274,7 +2296,7 @@ DBC_LRF_INI * sqliteDBC::getINIData(uint32_t lrfID)
bool sqliteDBC : : getEventSessionStatus ( )
{
ReportGuard tReportGuard ( m_mReportLock ) ; // see switchReportToCurrentDay ()
ReportGuard tReportGuard ( m_mReportLock ) ; // see switchReportToCurrentHour ()
bool rv = false ;
if ( m_bRepIsConnected )
{
@ -2315,7 +2337,7 @@ bool sqliteDBC::getEventSessionStatus()
*/
uint32_t sqliteDBC : : getLastEventTS ( )
{
ReportGuard tReportGuard ( m_mReportLock ) ; // see switchReportToCurrentDay ()
ReportGuard tReportGuard ( m_mReportLock ) ; // see switchReportToCurrentHour ()
uint32_t rv = 0 ;
if ( m_bRepIsConnected )
{