You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
12 KiB

​
​
​
​
​
​
​
​
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
4 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
4 hours ago
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
4 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __SQLITE_DATA_BINDER_HEADER__
  2. #define __SQLITE_DATA_BINDER_HEADER__
  3. #include "../../alg/generic/md5.h"
  4. #include "../../alg/lgs/mask.h"
  5. #include "../../components/base/basicEvent.h"
  6. #include "../../components/base/basicSettingsProvider.h"
  7. #include "../data/sqlite/sqlite_class.h"
  8. #include "dataBinder.h"
  9. #include "dataBinder_ReportAux.h"
  10. #include <pthread.h>
  11. #define SESSION_CONTINUE 0x00
  12. #define SESSION_START 0x01
  13. #define SESSION_END 0x02
  14. #define SESSION_DROP 0x03
  15. #define REMOVE_ONLY_SESSION_MARKS
  16. // One report database per local hour inside the day folder: data/reports/YYYY/MM/DD/report_HH.db
  17. #define REPORT_DB_NAME_FMT "report_%02d.db"
  18. class sqliteDBC : public dataBinder
  19. {
  20. private:
  21. basicLogCollection * m_pLog;
  22. config_setting_t * m_pSettings;
  23. SQLite_Connector * m_pDataStorage;
  24. SQLite_Connector * m_pDataMemory;
  25. SQLite_Connector * m_pDataReports;
  26. SQLite_Connector * m_pDataStatistics;
  27. bool m_bStoIsConnected;
  28. bool m_bMemIsConnected;
  29. bool m_bRepIsConnected;
  30. bool m_bStaIsConnected;
  31. bool m_bStaIsCreated;
  32. //bool m_bLastEventWasClosed;
  33. bool m_bRecreateMemStructures;
  34. char m_pDbFile[CLOG];
  35. char m_pDbMemory[CLOG];
  36. char m_pDbReportBase[CLOG];
  37. char m_pDbReport[CLOG];
  38. char m_pDbStat[CLOG];
  39. char m_pDbUsername[CLOG];
  40. char m_pDbPassword[CLOG];
  41. char m_pDbDatabase[CLOG];
  42. char web_access_user[64];
  43. char web_access_group[64];
  44. REPORT_DIRINFO m_sCRepInfo;
  45. REPORT_DIRINFO m_sPRepInfo;
  46. int m_nCRepHour; // local hour of the open report_HH.db (-1: none)
  47. // Protects the current day report (m_pDataReports / m_sCRepInfo / m_bRepIsConnected) against
  48. // a day switch from the kernel thread while the event thread writes to it. Recursive because
  49. // report methods call each other (e.g. addEvent -> updateStats -> switchReportToCurrentHour).
  50. pthread_mutex_t m_mReportLock;
  51. class ReportGuard
  52. {
  53. public:
  54. explicit ReportGuard(pthread_mutex_t & m) : m_rM(m) { pthread_mutex_lock(&m_rM); }
  55. ~ReportGuard() { pthread_mutex_unlock(&m_rM); }
  56. private:
  57. pthread_mutex_t & m_rM;
  58. };
  59. bool openHourReport(REPORT_DIRINFO & RDF, int hour);
  60. uint32_t m_nSiteID;
  61. uint32_t m_nServerID;
  62. uint32_t m_nServerNodeID;
  63. void initMemDB(uint32_t sid = 1);
  64. // uint32_t getTsFromRDF( REPORT_DIRINFO &RDF );
  65. void updateStats(bool evt = true, bool video = false, uint8_t ses_status = SESSION_CONTINUE);
  66. // bool checkRDFDir( CSTR basePath, REPORT_DIRINFO &RDF, bool recreate=true );
  67. bool addLRFDraw(uint32_t node_id, CSTR uniq_id, uint8_t lrf_id, uint8_t type);
  68. static sqliteDBC * _instance;
  69. explicit sqliteDBC(config_setting_t * Settings);
  70. public:
  71. // virtual void fillRDF( uint32_t ts, REPORT_DIRINFO &RDF );
  72. static sqliteDBC * getInstance(config_setting_t * Settings = NULL);
  73. virtual ~sqliteDBC();
  74. /* Functionality */
  75. bool connect(void);
  76. virtual bool cleanResources(bool all = false);
  77. virtual bool isLocal(void) const
  78. {
  79. return true;
  80. }
  81. virtual bool addDeviceDraw(uint32_t DBID, uint8_t type, uint8_t lrf, void * th_data, uint32_t th_size);
  82. virtual uint32_t addEvent(DBC_EVT_RECORD * ptr);
  83. virtual bool dropSession();
  84. virtual bool addIPCam(OBSERVER_RESULT * tPtr);
  85. virtual uint32_t addLocalCam(CSTR name, CSTR hardware, CSTR path, uint32_t width, uint32_t height, uint16_t fps, uint16_t den = 1);
  86. virtual uint32_t addLocalNetwork(uint32_t NIF_ID, NETWORK_TYPE net_type, CSTR address, CSTR mask, CSTR gateway, CSTR broadcast);
  87. virtual uint32_t addLocalNetworkInfrastructure(uint32_t NIF_ID, uint32_t OUTBOUND_NIF, bool ena_dhcp = false, CSTR BRANG = NULL, CSTR ERANG = NULL, CSTR MASK = NULL, CSTR LEASE_TIME = NULL);
  88. virtual uint32_t addLocalNIF(CSTR device, CSTR mac, NIF_TYPE dtype, NIF_MODE dmode, uint32_t aux_id = 0);
  89. virtual uint32_t addLocalUART(uint8_t utype, uint32_t speed, uint8_t bit, char parity, float stop_bit, CSTR PATH, uint8_t mode = 0, uint16_t port = 0);
  90. virtual uint32_t addLocalWiFi(uint32_t NIF_ID, WIFI_MODE mode, CSTR SSID, CSTR password, WIFI_ENCRIPTION encryption, uint32_t router_id = 0);
  91. virtual uint32_t addLRFMask(uint32_t devID, uint8_t lrfID = 0, uint8_t layerID = 0);
  92. virtual uint32_t addLRFMaskPolygone(uint32_t maskID, uint8_t vertexID, uint8_t r, uint8_t g, uint8_t b);
  93. virtual bool addLRFMaskPolygonePoint(uint32_t polyID, int16_t x, int16_t y);
  94. virtual bool addLRFWidthTable(uint32_t sensorID, PWIDTH_TABLE ptr);
  95. virtual bool addPicture(DBC_SNAPSHOT_DATA * ptr, CSTR ext);
  96. virtual uint32_t addResource(uint8_t type, CSTR devName, uint32_t auxID = 0);
  97. virtual bool addVideoEvent(uint32_t evtID, uint32_t videoID, uint32_t frame, uint32_t tss, uint32_t tms);
  98. virtual uint32_t addVideoRecord(uint32_t streamID, CSTR md5, CSTR ext, uint32_t ts, uint32_t tsms, uint32_t fps);
  99. virtual bool cleanLRFMask(uint32_t maskID, bool deleteMask = false);
  100. virtual bool cleanLRFWidthTable(uint32_t sensorID);
  101. virtual bool cleanNodePushedCMD(void);
  102. virtual bool cleanRegisteredBubbles(void);
  103. // virtual bool createAccessibleDir( CSTR path );
  104. virtual void deleteNIF(uint32_t NifID);
  105. virtual void deleteNIS(uint32_t NisID);
  106. virtual void deleteNetwork(uint32_t NetID);
  107. // virtual bool deleteRDFDir( CSTR Base, REPORT_DIRINFO &RDF );
  108. virtual bool deleteReportDate(uint32_t ts);
  109. virtual bool deleteReportStat(REPORT_DIRINFO & RDF);
  110. virtual void deleteResource(uint32_t ResourceID);
  111. virtual void deleteSensor(uint32_t SensorID);
  112. virtual void deleteUART(uint32_t UARTID);
  113. virtual bool fillShadow2D(uint32_t ID, SHADOW & SH);
  114. virtual bool getAppDebugMode();
  115. virtual DBC_ATC_MODULE_DATA * getAttentionModuleData( uint32_t id );
  116. virtual DBC_CAM_DATA * getCamData(uint32_t CamID);
  117. virtual uint32_t getCamId(CSTR UUID);
  118. virtual CAM_PRESET * getCamPreset(uint32_t SETID);
  119. virtual DBC_PRESETS_INFO * getCamPresetInfo(uint32_t CamID);
  120. virtual CSTR getCurrentReportDir(void);
  121. virtual REST_SETTINGS * getBoschData( uint32_t id );
  122. virtual BUBBLE_FILTER_DATA * getBubbleFilterData(uint32_t id);
  123. virtual DBC_MODULE_EMBED_VIDEO_DATA * getEmbedVideoData(uint32_t ID);
  124. virtual DBC_LRF_INI * getINIData(uint32_t lrfID);
  125. virtual bool getEventSessionStatus();
  126. virtual bool switchReportToCurrentHour(void);
  127. virtual uint32_t getLastEventTS();
  128. virtual uint32_t getFirstEventTS();
  129. virtual DBC_LRF_DATA * getLRFData(uint32_t ID);
  130. virtual uint8_t getLRFDataStatus(uint32_t ID);
  131. virtual PLGS_2D_PARAMETERS getLRFDetectionProfile(uint32_t lrfID, uint8_t profileID, bool master);
  132. virtual RELAY_PARAMETERS * getLRFDryContact(uint32_t lrfID);
  133. virtual uint32_t getLRFMaskID(uint32_t deviceID, uint8_t lrfID, uint8_t layer);
  134. virtual uint32_t getLRFMaskPolygoneID(uint32_t maskID, uint8_t vertecID);
  135. virtual uint8_t getLRFMasksStatus(uint32_t deviceID);
  136. // virtual simplePolygone * getLRFMaskVertex( uint32_t vid );
  137. virtual DBC_LRF_UPLOAD_DATA * getLRFUploadData(uint32_t upID);
  138. virtual DBC_WIDTH_TABLE * getLRFWidthTable(uint32_t lrfID);
  139. virtual DBC_MODEM_MANAGER_DATA * getModemManagerData(uint32_t ID);
  140. virtual DBC_MODEM_PHONEBOOK_RECORD * getModemPhonebookRecord(uint32_t ID);
  141. virtual DBC_MODULE_DATA * getModuleData(uint32_t ID);
  142. virtual DBC_MEDIA_STREAM_DATA * getMediaStreamData(uint32_t ID);
  143. virtual uint32_t getMediaStreamID(uint32_t source_id, CSTR token);
  144. virtual DBC_NET_IF_DATA * getNIF_Data(uint32_t ID);
  145. virtual uint32_t getNIF_ID(CSTR key);
  146. virtual uint32_t getNIF_IDbyDevice(CSTR device);
  147. virtual DBC_NETWORK_DATA * getNetworkData(uint32_t net_id);
  148. virtual uint32_t getNetworkInfrastructureID(uint32_t nif_id);
  149. virtual uint32_t getNodeAuxParam(uint32_t NodeID);
  150. virtual DBC_NODE_DATA * getNodeData(uint32_t ID);
  151. virtual uint32_t getNodeIdByTypeAndAux(uint8_t type, uint32_t aux_id);
  152. virtual CSTR getNodeName(uint32_t NodeID);
  153. virtual DBC_PUSHED_CMD * getNodePushedCMD(uint32_t NodeID);
  154. virtual TARGET_SEGMENT * getRegisteredBubble(uint32_t ID);
  155. virtual uint32_t getRegisteredBubbleId(float X, float Y, float Z);
  156. virtual REPORT_DIRINFO * getReportDirInfo(uint32_t id);
  157. virtual DBC_RESOURCE_DATA * getResourceData(uint32_t ResourceID);
  158. virtual uint32_t getResourceID(uint32_t NodeID, uint8_t type, uint32_t auxID);
  159. virtual DBC_SITE_DATA * getSiteData(uint32_t ID);
  160. virtual uint32_t getServerID(CSTR identity);
  161. virtual uint32_t getServerNode(uint32_t SID);
  162. virtual DBC_TARGET_FLITER_DATA * getTargetFilterData(uint32_t ID);
  163. virtual TRAIN_FILTER_DATA * getTrainFilterData(uint32_t ID);
  164. virtual DBC_UART_DATA * getUartData(uint32_t UartID);
  165. virtual uint32_t getUartID(uint32_t NodeID, uint8_t Type, CSTR path);
  166. virtual uint32_t getWiFi_ID(uint32_t nif_id);
  167. virtual bool isDatabaseNew();
  168. virtual DBC_INT_LIST * listCameras();
  169. virtual DBC_INT_LIST * listCamPresets(uint32_t SETID);
  170. virtual DBC_INT_LIST * listFilters(uint32_t ServerID);
  171. virtual DBC_INT_LIST * listLRFSensors();
  172. virtual DBC_INT_LIST * listLRFUploads(uint32_t deviceID);
  173. virtual DBC_INT_LIST * listMediaStreams(uint32_t SiteId, uint8_t listingType = LIST_ALL_RECORDS);
  174. virtual DBC_INT_LIST * listModules();
  175. virtual DBC_INT_LIST * listNetworks(uint32_t nif_id);
  176. virtual DBC_INT_LIST * listNewModules(DBC_INT_LIST * old_ids);
  177. virtual DBC_INT_LIST * listNewNodes(DBC_INT_LIST * old_ids);
  178. virtual DBC_INT_LIST * listNodeFilters(uint32_t NodeID = 0);
  179. virtual DBC_INT_LIST * listNodes(bool all = false);
  180. virtual DBC_INT_LIST * listOlderReportStat(CSTR Y, CSTR M, CSTR D);
  181. virtual DBC_INT_LIST * listOlderReportStat(uint32_t TS);
  182. virtual DBC_INT_LIST * listPhonebook();
  183. virtual DBC_INT_LIST * listFrozenBubbles(uint8_t cc = 0);
  184. virtual DBC_INT_LIST * listResources(uint32_t NodeID = 0);
  185. virtual bool pushMemCMD(uint32_t NODE_ID, uint8_t CMD, CSTR DATA);
  186. virtual void registerServer(uint32_t SiteID, CSTR identity, int PID, uint32_t * ServID, uint32_t * ServNode);
  187. virtual bool setLRFDetectionProfile(uint32_t lrfID, uint8_t profileID);
  188. virtual bool setLRFDataStatus(uint32_t lrfID, uint8_t ST);
  189. virtual bool setLRFAltSerial(uint32_t lrfID, CSTR Serial);
  190. virtual bool setLRFAltVerison(uint32_t lrfID, CSTR version);
  191. virtual bool setLRFMaskStatus(uint32_t maskID, bool status);
  192. virtual bool setNodeOffline(uint32_t NodeID);
  193. virtual bool setNodeOnline(uint32_t NodeID, uint8_t status = NODE_STATUS_WORK);
  194. virtual bool setRessourceActive(uint32_t RessourceID, bool Status = true);
  195. virtual bool setStreamValid(uint32_t streamID, bool status = true);
  196. virtual void unregisterServer(void);
  197. virtual bool updateCamRange(uint32_t camID, float range);
  198. virtual bool updateCamOnlineStatus(uint32_t camID, bool online);
  199. virtual bool updateIPCam(uint32_t CamID, CSTR Addr);
  200. virtual bool updateIPCam(CSTR uuid, CSTR Addr);
  201. virtual bool updateLocalCam(CSTR uuid);
  202. virtual bool updateLocalCam(uint32_t CamID);
  203. virtual bool updateLRFComStat(uint32_t lrfID, uint32_t ErrorPerMinute);
  204. virtual bool updateLRFFrozenStatus(uint32_t lrfID, bool FFT, bool SFT);
  205. virtual bool updateLRFDetectionProfile(uint32_t lrfID, uint8_t profileID, bool master, uint16_t nOrder, uint16_t nOffset, uint16_t nDelta, uint16_t nWidth, uint16_t nFlip, uint16_t nSun, uint8_t bSST, uint32_t GP, uint32_t spikes);
  206. virtual bool updateLRFDryContact(uint32_t lrfID, bool bAuto, bool bNO, uint16_t wWindow);
  207. virtual bool updateLRFSerial(uint32_t lrfID, CSTR Serial);
  208. virtual bool updateLRFUpload(uint32_t id, uint8_t status);
  209. virtual bool updateLRFUnfinishedUploads(uint32_t deviceID);
  210. virtual bool updateLRFVerison(uint32_t lrfID, CSTR version);
  211. virtual bool updateModemManagerActiveCall(uint32_t MMID, int32_t callID);
  212. virtual bool updateModemManagerLTS(uint32_t MMID, uint32_t ts);
  213. virtual bool updateModemManagerActivePhoneBookID(uint32_t MMID, uint32_t pbID);
  214. virtual bool updateModemManagerActiveSMS(uint32_t MMID, int32_t smsID);
  215. virtual bool updateNodePushedCMD(uint32_t CmdID, uint8_t status);
  216. virtual uint8_t updateRegisteredBubble(TARGET_SEGMENT * ptr);
  217. virtual bool updateServerIdentity(uint32_t m_nServerID, CSTR m_sIdentity);
  218. virtual bool updateServerUtilisation(DBC_SYS_LOAD_INFO * ptr);
  219. virtual bool updateSystemInfo(DBC_SYS_INFO * ptr);
  220. virtual bool wipeServerResources();
  221. virtual CSTR getDataParam(uint8_t pid = 0);
  222. };
  223. #endif