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.

2090 lines
57 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 months ago
4 years ago
​
​
​
​
​
12 months ago
​
​
​
​
​
4 years ago
​
​
​
​
​
4 years ago
4 years ago
​
4 years ago
​
​
​
​
​
​
​
4 years ago
​
​
4 years 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
1 day ago
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
4 years ago
​
​
​
4 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years 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
1 day ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
4 years ago
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 months ago
​
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years 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
1 day 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
1 day ago
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
  1. //----------------------------------------------------------------------------
  2. // Dr.Frucht Systems Ltd.
  3. // Copyright � 2000-2022. All Rights Reserved.
  4. // Version 1.0 11/19/2022
  5. // Last modification: 11/19/2022
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: mojoKernel.cpp
  9. // AUTHOR: gagua
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // LRF_Kernel.cpp
  13. //----------------------------------------------------------------------------
  14. //
  15. #include "mojoKernel.h"
  16. #include "../common/kernelEventFilterAPF.h"
  17. #include "../common/kernelEventFilterBubble.h"
  18. #include "../common/kernelEventFilterTrain.h"
  19. #include "../common/kernelModuleEmbedVideo.h"
  20. #include "../data/json/json_tool.h"
  21. #include <json/reader.h>
  22. #include <json/value.h>
  23. #include <json/writer.h>
  24. #include <strings.h>
  25. #include <sys/time.h>
  26. #include <time.h>
  27. #ifdef USE_MYSQL
  28. #include "../common/MySQL_dataBinder.h"
  29. #else
  30. #include "../common/SQLite_dataBinder.h"
  31. #endif
  32. #define LOG(X) kernelPrototype::m_pLog->Log("KRNL", this, X)
  33. #ifdef DEBUG
  34. #define LOG_ERROR(X) \
  35. { \
  36. char _tBfrErrMSG234[256]; \
  37. sprintf(_tBfrErrMSG234, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  38. kernelPrototype::m_pLog->Log("KRNL", this, _LOG_ERROR, _tBfrErrMSG234); \
  39. }
  40. #else
  41. #define LOG_ERROR(X) kernelPrototype::m_pLog->Log("KRNL", this, _LOG_ERROR, X);
  42. #endif
  43. mojoKernel * mojoKernel::_instance = NULL;
  44. mojoKernel * mojoKernel::getInstance(config_setting_t * ptr)
  45. {
  46. if (_instance == NULL)
  47. {
  48. _instance = new mojoKernel(ptr);
  49. };
  50. return _instance;
  51. }
  52. mojoKernel::mojoKernel(config_setting_t * ptr) :
  53. kernelPrototype(ptr)
  54. {
  55. m_pEventHeap = basicEventHeap::getInstance();
  56. memset(&m_sSysInfo, 0, sizeof(DBC_SYS_LOAD_INFO));
  57. m_pShadow = NULL;
  58. m_pSystemAnchor = NULL;
  59. m_bSessionStarted = false;
  60. m_bSessionMustPreserve = false;
  61. m_bFirstSession = true;
  62. m_bWorkWithRemoteServer = false;
  63. m_bKeepLocalReport = true;
  64. m_bMediaMode = 0;
  65. m_bApplicationFirstRun = false;
  66. m_bForceCloseSessionOnEndOfTheDay = false;
  67. m_mDateOfToday = getLocalDay();
  68. m_bDayChangePending = false;
  69. m_tDayChangeDetected = 0;
  70. m_nSessionDay = -1;
  71. m_pRPC = NULL;
  72. #ifndef LRF_PROXY_ONLY
  73. m_pMediaHelper = NULL;
  74. #endif
  75. #ifdef EMBED_AUTORESET
  76. m_bEnableAutoReset = false;
  77. m_nSessionCounter = 0;
  78. m_nAutoResetTime = 0;
  79. #endif
  80. #ifdef PEREODIC_SENSORS_RESET
  81. m_bEnablePereodicReset = false;
  82. m_nPereodicResetTime = 0;
  83. m_nPereodicResetCounter = 0;
  84. #endif
  85. // setWM( WM_NONE );
  86. }
  87. mojoKernel::~mojoKernel()
  88. {
  89. WaitFinish(true);
  90. stopSession(false);
  91. #ifndef LRF_PROXY_ONLY
  92. if ( m_pMediaHelper ) {
  93. delete( m_pMediaHelper );
  94. }
  95. #endif
  96. #ifdef DEBUG
  97. LOG(" Deleting Node Factory...");
  98. #endif
  99. if (m_pNodeFactory != NULL)
  100. {
  101. delete(m_pNodeFactory);
  102. }
  103. #ifdef DEBUG
  104. LOG(" Deleting Module Factory...");
  105. #endif
  106. if (m_pModuleFactory != NULL)
  107. {
  108. delete(m_pModuleFactory);
  109. }
  110. #ifdef DEBUG
  111. LOG(" Deleting Target Filters...");
  112. #endif
  113. unloadFilters();
  114. if (EXIT_CODE_KERNEL_CLEAR == getExitCause())
  115. {
  116. #ifdef DEBUG
  117. LOG("Unregister Server");
  118. #endif
  119. m_pDBC->unregisterServer();
  120. #ifdef DEBUG
  121. // LOG("Close DataBase Connection...");
  122. #endif
  123. // if( m_pDBC != NULL ) delete( m_pDBC );//Deleting in Backend
  124. // m_pDBC = NULL;
  125. }
  126. else
  127. {
  128. if (EXIT_CODE_DETECT_HARDWARE == getExitCause())
  129. {
  130. #ifdef DEBUG
  131. LOG("HW PNP Detection Requested...");
  132. #endif
  133. // m_pDBC->updateNodePushedCMD( tCMD->id, 30 );
  134. m_pHardwareManager->enumerateHardware();
  135. // m_pDBC->updateNodePushedCMD( tCMD->id, 60 );
  136. };
  137. #ifdef DEBUG
  138. // LOG("Close DataBase Connection...");
  139. #endif
  140. // if( m_pDBC != NULL ) delete( m_pDBC );
  141. // m_pDBC = NULL;
  142. };
  143. delete(m_pHardwareManager);
  144. // while(1){
  145. // char tB[200];
  146. // sprintf( tB, "[%d] Seconds Till System Restart", timeout );
  147. // LOG( "BopBop!" );
  148. // SLEEP(1000);
  149. // };
  150. if (m_pRPC)
  151. {
  152. #ifdef DEBUG
  153. LOG("Deleting Remote Server Connection...");
  154. #endif
  155. delete(m_pRPC);
  156. #ifdef DEBUG
  157. LOG("Remote Server Connection Destroyed!");
  158. #endif
  159. }
  160. #ifdef LINUX_KERNEL_WATCHDOG
  161. LOG("Disable WD!");
  162. if (m_iWatchdogFd > -1)
  163. {
  164. char tB[64];
  165. sprintf(tB, "-1\n");
  166. // ToDo: Make Error Handling here!
  167. write(m_iWatchdogFd, &tB, strlen(tB));
  168. };
  169. LOG("WatchDog turned Off!");
  170. #endif
  171. _instance = NULL;
  172. }
  173. bool mojoKernel::create()
  174. {
  175. m_bSessionStarted = false;
  176. m_bSessionMustPreserve = false;
  177. m_bFirstSession = true;
  178. if (m_pSettings != NULL)
  179. {
  180. config_setting_t * pSetting;
  181. int tVal;
  182. const char * tBuf;
  183. char tBuffer[128];
  184. LOG("Getting Settings ...");
  185. if (! config_setting_lookup_string(m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, &tBuf))
  186. {
  187. char * tVar;
  188. char * tTS;
  189. tTS = getTStr(NULL);
  190. tVar = MD5String(tTS);
  191. free(tTS);
  192. sprintf(m_sIdentity, tVar);
  193. pSetting = config_setting_add(m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING);
  194. config_setting_set_string(pSetting, tVar);
  195. free(tVar);
  196. basicSettingsProvider::getInstance()->compromise();
  197. LOG("Server Identity not defined - generating new!");
  198. }
  199. else
  200. {
  201. sprintf(m_sIdentity, tBuf);
  202. sprintf(tBuffer, "Server Identity Read From Settings: %s", tBuf);
  203. LOG(tBuffer);
  204. };
  205. if (! config_setting_lookup_int(m_pSettings, MOJO_KERNEL_TAG_SITE_ID, &tVal))
  206. {
  207. tVal = m_nInstanceID = 1;
  208. pSetting = config_setting_add(m_pSettings, MOJO_KERNEL_TAG_SITE_ID, CONFIG_TYPE_INT);
  209. config_setting_set_int(pSetting, tVal);
  210. basicSettingsProvider::getInstance()->compromise();
  211. LOG("Site ID not defined - using [1] as number!");
  212. }
  213. else
  214. {
  215. m_nInstanceID = tVal;
  216. sprintf(tBuffer, "App Instance ID Read From Settings: %d", m_nInstanceID);
  217. LOG(tBuffer);
  218. };
  219. #ifdef EMBED_AUTORESET
  220. if (! config_setting_lookup_int(m_pSettings, "AUTORESET", &tVal))
  221. {
  222. m_bEnableAutoReset = false;
  223. tVal = m_nAutoResetTime = 0;
  224. pSetting = config_setting_add(m_pSettings, "AUTORESET", CONFIG_TYPE_INT);
  225. config_setting_set_int(pSetting, tVal);
  226. basicSettingsProvider::getInstance()->compromise();
  227. LOG("Autoreset not defined - Set Disabled!");
  228. }
  229. else
  230. {
  231. m_nAutoResetTime = tVal;
  232. m_bEnableAutoReset = (m_nAutoResetTime > 0);
  233. sprintf(tBuffer, "App Autoreset Read From Settings: %d", m_nAutoResetTime);
  234. LOG(tBuffer);
  235. };
  236. #endif
  237. #ifdef PEREODIC_SENSORS_RESET
  238. if (! config_setting_lookup_int(m_pSettings, "PEREODICRESET", &tVal))
  239. {
  240. m_bEnablePereodicReset = false;
  241. tVal = m_nPereodicResetTime = 0;
  242. pSetting = config_setting_add(m_pSettings, "PEREODICRESET", CONFIG_TYPE_INT);
  243. config_setting_set_int(pSetting, tVal);
  244. basicSettingsProvider::getInstance()->compromise();
  245. LOG("Pereodic Sensor Reset not defined - Set Disabled!");
  246. }
  247. else
  248. {
  249. m_nPereodicResetTime = tVal;
  250. m_bEnablePereodicReset = (m_nPereodicResetTime > 0);
  251. sprintf(tBuffer, "Pereodic Sensor Reset Read From Settings: %d", m_nPereodicResetTime);
  252. LOG(tBuffer);
  253. };
  254. #endif
  255. }
  256. else
  257. {
  258. LOG_ERROR("Failed Read Config Settings!");
  259. return false;
  260. };
  261. basicSettingsProvider::getInstance()->validate();
  262. LOG("Register Server ... ");
  263. m_pDBC->registerServer(m_nInstanceID, m_sIdentity, getpid(), &m_nServerID, &m_nServerNodeID);
  264. char tBuffer[128];
  265. sprintf(tBuffer, "Server Registered as [%d] nodeID[%d]", m_nServerID, m_nServerNodeID);
  266. LOG(tBuffer);
  267. DBC_SITE_DATA * tSiteData = m_pDBC->getSiteData(1);
  268. if (tSiteData)
  269. {
  270. C3D_WGS84_LOC t;
  271. t.lat = tSiteData->lat;
  272. t.lon = tSiteData->lon;
  273. t.alt = tSiteData->alt;
  274. m_pSystemAnchor = systemAnchor::getInstance( &t, tSiteData->local_binding );
  275. memcpy(&m_sSiteData, tSiteData, sizeof(DBC_SITE_DATA));
  276. free(tSiteData);
  277. }
  278. else
  279. {
  280. LOG_ERROR("Error Loading Site DATA!");
  281. C3D_WGS84_LOC t;
  282. t.lat = 0;
  283. t.lon = 0;
  284. t.alt = 0;
  285. memset(&m_sSiteData, 0, sizeof(DBC_SITE_DATA));
  286. m_pSystemAnchor = systemAnchor::getInstance( &t, true );
  287. };
  288. m_bKeepLocalReport = ! m_sSiteData.skip_local_report;
  289. m_bDebugMode = m_pDBC->getAppDebugMode();
  290. if (m_bDebugMode)
  291. {
  292. m_bKeepLocalReport = true; // Forced in case of debug mode
  293. }
  294. /// m_bWorkWithRemoteServer = m_sSiteData.use_remote_server; ->
  295. /// since Connection is delayed - must be not set at beginning
  296. // Check date/Time if correct!
  297. // Just in case that we have local reports
  298. if (m_bKeepLocalReport)
  299. {
  300. #ifdef DEBUG
  301. LOG("Working With Local Reports");
  302. #endif
  303. // On some systems the System Date may be in past cause last session was interrupted by power loose
  304. uint32_t tTS = m_pDBC->getLastEventTS();
  305. if (tTS)
  306. {
  307. // Have last TS
  308. struct timespec tS;// = {0}; will filed by gettime function
  309. if (clock_gettime(CLOCK_REALTIME, &tS) == 0)
  310. {
  311. // time_t cTS = time(NULL);
  312. if (tTS > tS.tv_sec)
  313. {
  314. // System Time is in the Past
  315. LOG("Correcting System Time!");
  316. tS.tv_sec = tTS;
  317. tS.tv_nsec = tTS * 1000000;
  318. if (clock_settime(CLOCK_REALTIME, &tS) != 0)
  319. {
  320. LOG_ERROR("Time Correction Failed!");
  321. }
  322. };
  323. };
  324. #ifdef DEBUG
  325. }
  326. else
  327. {
  328. LOG("No Last TS fetched from DB!");
  329. #endif
  330. };
  331. m_bSessionStarted = m_pDBC->getEventSessionStatus();//m_nServerNodeID);
  332. stopSession(false);
  333. };
  334. m_pHardwareManager = hardwareManager::getInstance();
  335. loadFilters();
  336. m_pModuleFactory = kernelModuleFactory::getInstance(m_nInstanceID);
  337. if (! m_pModuleFactory)
  338. {
  339. LOG_ERROR("Module Factory Failure!");
  340. return false;
  341. };
  342. m_pModuleFactory->loadModules();
  343. // Reald Media Mode ( Video/Pictures )
  344. #ifndef LRF_PROXY_ONLY
  345. m_pMediaHelper = new kernelHelperMediaMTX();
  346. if ( m_pMediaHelper ) {
  347. m_pMediaHelper->create();
  348. };
  349. kernelModuleEmbedVideo * tMO = (kernelModuleEmbedVideo *) m_pModuleFactory->getModule(KERNEL_MODULE_EMBED_VIDEO);
  350. if (tMO)
  351. {
  352. m_bMediaMode = tMO->getMediaMode();
  353. }
  354. else
  355. {
  356. LOG_ERROR("No Embed Video Processing Module!");
  357. };
  358. #else
  359. LOG("MOJO is DATA PROXY Processing Module!");
  360. m_bMediaMode = 0;
  361. #endif
  362. m_pNodeFactory = deviceNodeFactory::getInstance();
  363. if (! m_pNodeFactory)
  364. {
  365. LOG_ERROR("Node Factory Failure!");
  366. return false;
  367. };
  368. if ((m_bApplicationFirstRun = m_pDBC->isDatabaseNew()))
  369. {
  370. LOG("DATABASE IS NEW - Will Detect Hardware!");
  371. }
  372. else
  373. {
  374. LOG("DATABASE IS OLD - Will SKIP Detection of Hardware!");
  375. }
  376. // Must Be moved elsewhere To be connected after all Nodes are Loadded ?? at Node's Validation ??
  377. /*
  378. if ( m_bWorkWithRemoteServer ) {
  379. #ifdef DEBUG
  380. LOG("Working With Remote Server");
  381. #endif
  382. if ( m_pRPC = new mojoRemoteClient( this ) ) {
  383. if( m_bWorkWithRemoteServer = m_pRPC->create() ){
  384. } else LOG_ERROR("Cannot Connect to Remote Server!");
  385. } else m_bWorkWithRemoteServer = false;
  386. };
  387. */
  388. return init();
  389. }
  390. #include "../common/deviceNode2D.h"
  391. void mojoKernel::releaseExplicitChannel( uint32_t node_id, uint32_t channel_id ){
  392. if ( m_pNodeFactory ) {
  393. deviceNode ** tA = NULL;
  394. uint32_t tCNT = m_pNodeFactory->fetchNodes(ARGUS_ALS_SENSOR, tA );
  395. for ( uint32_t i = 0; i < tCNT; i++ ) {
  396. if ( tA[i]->getNodeID() != node_id ) ( (device2DSensor *)tA[i] )->setChannelReady( channel_id );
  397. };
  398. if (tCNT){
  399. free(tA);
  400. tA = NULL;
  401. };
  402. tCNT = m_pNodeFactory->fetchNodes(ARGUS_LFS_SENSOR, tA );
  403. for ( uint32_t i = 0; i < tCNT; i++ ) {
  404. if ( tA[i]->getNodeID() != node_id ) ( (device2DSensor *)tA[i] )->setChannelReady( channel_id );
  405. };
  406. if (tCNT){
  407. free(tA);
  408. tA = NULL;
  409. };
  410. tCNT = m_pNodeFactory->fetchNodes(ARGUS_PLS_200_SENSOR, tA );
  411. for ( uint32_t i = 0; i < tCNT; i++ ) {
  412. if ( tA[i]->getNodeID() != node_id ) ( (device2DSensor *)tA[i] )->setChannelReady( channel_id );
  413. };
  414. if (tCNT){
  415. free(tA);
  416. tA = NULL;
  417. };
  418. tCNT = m_pNodeFactory->fetchNodes(ARGUS_PLS_360_SENSOR, tA );
  419. for ( uint32_t i = 0; i < tCNT; i++ ) {
  420. if ( tA[i]->getNodeID() != node_id ) ( (device2DSensor *)tA[i] )->setChannelReady( channel_id );
  421. };
  422. if (tCNT){
  423. free(tA);
  424. tA = NULL;
  425. };
  426. };
  427. }
  428. void mojoKernel::requestExplicitChannel( uint32_t node_id, uint32_t channel_id, uint8_t seconds_to ){
  429. if ( m_pNodeFactory ) {
  430. deviceNode ** tA = NULL;
  431. uint32_t tCNT = m_pNodeFactory->fetchNodes(ARGUS_ALS_SENSOR, tA );
  432. for ( uint32_t i = 0; i < tCNT; i++ ) {
  433. if ( tA[i]->getNodeID() != node_id ) ( (device2DSensor *)tA[i] )->setChannelBusy( channel_id, seconds_to );
  434. };
  435. if (tCNT){
  436. free(tA);
  437. tA = NULL;
  438. };
  439. tCNT = m_pNodeFactory->fetchNodes(ARGUS_LFS_SENSOR, tA );
  440. for ( uint32_t i = 0; i < tCNT; i++ ) {
  441. if ( tA[i]->getNodeID() != node_id ) ( (device2DSensor *)tA[i] )->setChannelBusy( channel_id, seconds_to );
  442. };
  443. if (tCNT){
  444. free(tA);
  445. tA = NULL;
  446. };
  447. tCNT = m_pNodeFactory->fetchNodes(ARGUS_PLS_200_SENSOR, tA );
  448. for ( uint32_t i = 0; i < tCNT; i++ ) {
  449. if ( tA[i]->getNodeID() != node_id ) ( (device2DSensor *)tA[i] )->setChannelBusy( channel_id, seconds_to );
  450. };
  451. if (tCNT){
  452. free(tA);
  453. tA = NULL;
  454. };
  455. tCNT = m_pNodeFactory->fetchNodes(ARGUS_PLS_360_SENSOR, tA );
  456. for ( uint32_t i = 0; i < tCNT; i++ ) {
  457. if ( tA[i]->getNodeID() != node_id ) ( (device2DSensor *)tA[i] )->setChannelBusy( channel_id, seconds_to );
  458. };
  459. if (tCNT){
  460. free(tA);
  461. tA = NULL;
  462. };
  463. };
  464. }
  465. void mojoKernel::startSession()
  466. {
  467. m_mSessionProtector.lock();
  468. m_nNoAlarmCounter = 0;
  469. if (! m_bSessionStarted)
  470. {
  471. if (! m_bFirstSession)
  472. {
  473. if (! m_bSessionMustPreserve)
  474. {
  475. m_pDBC->dropSession();
  476. }
  477. m_bSessionMustPreserve = false;
  478. }
  479. else
  480. {
  481. m_bFirstSession = false;
  482. }
  483. uint64_t TS_64 = getMilliseconds() - 20; // -20 msec before
  484. uint32_t ts_s = TS_64 / 1000;
  485. uint16_t ts_ms = TS_64 % 1000;
  486. EVENT_CONTAINER * bEVT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_LRF_START_SESSION, NULL, NULL, NULL, ts_s, ts_ms);
  487. if (bEVT)
  488. {
  489. m_pEventHeap->broadcast(bEVT);
  490. };
  491. m_bSessionStarted = true;
  492. m_nSessionDay = getLocalDay();
  493. #ifdef EMBED_AUTORESET
  494. m_nSessionCounter = 0;
  495. #endif
  496. };
  497. m_mSessionProtector.unlock();
  498. }
  499. void mojoKernel::stopSession(bool forced)
  500. {
  501. m_mSessionProtector.lock();
  502. if (m_bSessionStarted)
  503. {
  504. uint64_t TS_64 = getMilliseconds() + 20; // +20 msec after
  505. uint32_t ts_s = TS_64 / 1000;
  506. uint16_t ts_ms = TS_64 % 1000;
  507. EVENT_CONTAINER * bEVT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, forced ? EVT_LRF_END_SESSION_MA : EVT_LRF_END_SESSION_TO, NULL, NULL, NULL, ts_s, ts_ms);
  508. if (bEVT)
  509. {
  510. m_pEventHeap->broadcast(bEVT);
  511. };
  512. m_bSessionStarted = false;
  513. #ifdef EMBED_AUTORESET
  514. m_nSessionCounter = 0;
  515. #endif
  516. };
  517. m_mSessionProtector.unlock();
  518. }
  519. void mojoKernel::validateSession(){
  520. m_mSessionProtector.lock();
  521. if( m_bSessionStarted ){
  522. if( m_nNoAlarmCounter < NOTARGET_SESSION_CLOSE ){
  523. m_nNoAlarmCounter++;
  524. } else {
  525. m_mSessionProtector.unlock();
  526. stopSession( m_bForceCloseSessionOnEndOfTheDay );
  527. m_mSessionProtector.lock();
  528. };
  529. #ifdef EMBED_AUTORESET
  530. if (m_bEnableAutoReset) {
  531. if (m_nSessionCounter < m_nAutoResetTime) {
  532. m_nSessionCounter++;
  533. } else {
  534. m_nSessionCounter = 0;
  535. // Reset All Sensors
  536. DBC_INT_LIST * tL = m_pDBC->listNodes(false);
  537. if (tL){
  538. for (DBC_INT_LIST::iterator idx = tL->begin(); idx < tL->end(); idx++){
  539. m_pDBC->pushMemCMD(*idx, 1, "'A'");
  540. };
  541. delete(tL);
  542. };
  543. m_nNoAlarmCounter = NOTARGET_SESSION_CLOSE; // Session will be closed on next validation
  544. };
  545. };
  546. #endif
  547. };
  548. m_mSessionProtector.unlock();
  549. #ifdef PEREODIC_SENSORS_RESET
  550. if ( m_bEnablePereodicReset ) {
  551. m_nPereodicResetCounter++;
  552. if ( m_nPereodicResetCounter > m_nPereodicResetTime ) {
  553. m_nPereodicResetCounter = 0;
  554. // Reset All Sensors
  555. #ifdef DEBUG
  556. LOG("Pereodic Reset to All Sensors");
  557. #endif
  558. DBC_INT_LIST * tL = m_pDBC->listNodes(false);
  559. if (tL){
  560. for (DBC_INT_LIST::iterator idx = tL->begin(); idx < tL->end(); idx++){
  561. m_pDBC->pushMemCMD(*idx, 1, "'A'");
  562. };
  563. delete(tL);
  564. };
  565. };
  566. };
  567. #endif
  568. }
  569. #ifdef __MOJO_REMOTE_CLIENT_HEADER__
  570. bool mojoKernel::getTrainPresence()
  571. {
  572. bool RetValue = false;
  573. if (m_vTargetFilters.size())
  574. {
  575. for (vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++)
  576. {
  577. if ((*idx)->getType() == TF_LA_METRO_TRAIN_FILTER)
  578. {
  579. kernelEventFilterTrain * tFT = (kernelEventFilterTrain *) (*idx);
  580. RetValue = tFT->getTrainStatus();
  581. break;
  582. };
  583. };
  584. };
  585. return RetValue;
  586. }
  587. bool mojoKernel::fillMojoInfo(ARGUS_DATA_MOJO_INFO * ptr)
  588. {
  589. if (ptr == NULL)
  590. {
  591. return false;
  592. }
  593. memset(ptr, 0, sizeof(ARGUS_DATA_MOJO_INFO));
  594. ptr->MOJO_WORK_MODE = (m_bKeepLocalReport ? 4 : 0) | (m_bMediaMode ? 2 : 0) | (m_bDebugMode ? 1 : 0);
  595. if (m_vTargetFilters.size())
  596. {
  597. for (vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++)
  598. {
  599. #ifdef DEBUG_EXTRA
  600. LOG("check Filter");
  601. #endif
  602. if ((*idx)->getType() == TF_LA_METRO_TRAIN_FILTER)
  603. {
  604. #ifdef DEBUG_EXTRA
  605. LOG("T Filter Found!");
  606. #endif
  607. kernelEventFilterTrain * tFT = (kernelEventFilterTrain *) (*idx);
  608. ptr->UPPER_ID = tFT->getUpperId();
  609. ptr->BOTTOM_ID = tFT->getBottomId();
  610. ptr->MOJO_WORK_MODE |= (tFT->getMode() << 3);
  611. break;
  612. };
  613. };
  614. };
  615. #ifndef LRF_PROXY_ONLY
  616. if ( m_pMediaHelper ) {
  617. m_pMediaHelper->validateService();
  618. };
  619. #endif
  620. deviceNode ** tA = NULL;
  621. uint32_t tCNT = m_pNodeFactory->fetchNodes(ARGUS_ANALOG_STATIC_CAMERA, tA);
  622. if (tCNT)
  623. {
  624. // deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[0];
  625. // ptr->CAM_ID = tCAM->getNodeID();
  626. // ptr->CAM_STREAM_ID = tCAM->getCamStream();
  627. // ptr->CAM_ID = tA[0]->getDeviceID();
  628. ptr->CAM_ID = tA[0]->getNodeID();
  629. ptr->CAM_STREAM_ID = tA[0]->getDeviceParam();
  630. }
  631. else
  632. {
  633. LOG("FIMO Cannot Get ARGUS_ANALOG_STATIC_CAMERA Camera!");
  634. tCNT = m_pNodeFactory->fetchNodes(ARGUS_ANALOG_PTZ_CAMERA, tA);
  635. if (tCNT)
  636. {
  637. // deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[0];
  638. // ptr->CAM_ID = tCAM->getNodeID();
  639. // ptr->CAM_STREAM_ID = tCAM->getCamStream();
  640. // ptr->CAM_ID = tA[0]->getDeviceID();
  641. ptr->CAM_ID = tA[0]->getNodeID();
  642. ptr->CAM_STREAM_ID = tA[0]->getDeviceParam();
  643. }
  644. else
  645. {
  646. LOG("FIMO Cannot Get ARGUS_ANALOG_PTZ_CAMERA Camera!");
  647. tCNT = m_pNodeFactory->fetchNodes(ARGUS_IP_STATIC_CAMERA, tA);
  648. if (tCNT)
  649. {
  650. // deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[0];
  651. // ptr->CAM_ID = tCAM->getNodeID();
  652. // ptr->CAM_STREAM_ID = tCAM->getCamStream();
  653. // ptr->CAM_ID = tA[0]->getDeviceID();
  654. ptr->CAM_ID = tA[0]->getNodeID();
  655. ptr->CAM_STREAM_ID = tA[0]->getDeviceParam();
  656. }
  657. else
  658. {
  659. LOG("FIMO Cannot Get ARGUS_IP_STATIC_CAMERA Camera!");
  660. tCNT = m_pNodeFactory->fetchNodes(ARGUS_IP_PTZ_CAMERA, tA);
  661. if (tCNT)
  662. {
  663. // deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[0];
  664. // ptr->CAM_ID = tCAM->getNodeID();
  665. // ptr->CAM_STREAM_ID = tCAM->getCamStream();
  666. // ptr->CAM_ID = tA[0]->getDeviceID();
  667. ptr->CAM_ID = tA[0]->getNodeID();
  668. ptr->CAM_STREAM_ID = tA[0]->getDeviceParam();
  669. }
  670. else
  671. {
  672. LOG("FIMO Cannot Get ARGUS_IP_PTZ_CAMERA Camera!");
  673. tCNT = m_pNodeFactory->fetchNodes(ARGUS_IP_PRESET_CAMERA, tA);
  674. if (tCNT)
  675. {
  676. // deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[0];
  677. // ptr->CAM_ID = tCAM->getNodeID();
  678. // ptr->CAM_STREAM_ID = tCAM->getCamStream();
  679. // ptr->CAM_ID = tA[0]->getDeviceID();
  680. ptr->CAM_ID = tA[0]->getNodeID();
  681. ptr->CAM_STREAM_ID = tA[0]->getDeviceParam();
  682. }
  683. else
  684. {
  685. LOG("FIMO Cannot Get ARGUS_IP_PRESET_CAMERA Camera!");
  686. tCNT = m_pNodeFactory->fetchNodes(ARGUS_ANALOG_PRESET_CAMERA, tA);
  687. if (tCNT)
  688. {
  689. // deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[0];
  690. // ptr->CAM_ID = tCAM->getNodeID();
  691. // ptr->CAM_STREAM_ID = tCAM->getCamStream();
  692. // ptr->CAM_ID = tA[0]->getDeviceID();
  693. ptr->CAM_ID = tA[0]->getNodeID();
  694. ptr->CAM_STREAM_ID = tA[0]->getDeviceParam();
  695. }
  696. else
  697. {
  698. LOG("FIMO Cannot Get ARGUS_ANALOG_PRESET_CAMERA Camera!");
  699. LOG("No Camera Defined In the system!");
  700. };
  701. };
  702. };
  703. };
  704. };
  705. };
  706. if (tCNT)
  707. {
  708. free(tA);
  709. tA = NULL;
  710. };
  711. ptr->CPU_COUNT = m_sSysInfo.CPU_CORES;
  712. ptr->TOT_DISK = m_sSysInfo.T_DISK;
  713. ptr->TOT_MEMO = m_sSysInfo.T_MEM;
  714. strncpy(ptr->TLS_VERSION, _BUILD_STRING_, 64);
  715. ptr->TLS_VERSION[63] = (char) 0;
  716. strncpy(ptr->LLS_VERSION, m_pHardwareManager->getSysInfo()->OS_INFO, 64);
  717. ptr->LLS_VERSION[63] = (char) 0;
  718. LOG("FIMO Overall info");
  719. return true;
  720. }
  721. #endif
  722. void mojoKernel::getSystemUtilization(DBC_SYS_LOAD_INFO * ptr)
  723. {
  724. if (ptr == NULL)
  725. {
  726. return;
  727. }
  728. m_mSysInfoProtector.lock();
  729. memcpy(ptr, &m_sSysInfo, sizeof(DBC_SYS_LOAD_INFO));
  730. m_mSysInfoProtector.unlock();
  731. }
  732. void mojoKernel::m_fThreadProc(void){
  733. time_t tTimeTSeconds = 0;
  734. m_bThreadStatus = TH_WORKING;
  735. pthread_setname_np( pthread_self(), "Kernel" );
  736. while (m_bThreadStatus < TH_ENDED){
  737. m_tTimeTSeconds = time(NULL);
  738. // Each Second
  739. if (m_tTimeTSeconds != tTimeTSeconds){
  740. tTimeTSeconds = m_tTimeTSeconds;
  741. if (m_bApplicationFirstRun){
  742. if (m_pHardwareManager != NULL){
  743. LOG_ERROR("Restart and Detect HardWare!");
  744. // Must be shutdowned all service that uses hardware prior to detection!
  745. m_bThreadStatus = TH_ENDED;
  746. setExitCause(EXIT_CODE_DETECT_HARDWARE); // Go -> Detect Hardware in BACKEND
  747. continue;
  748. };
  749. };
  750. #ifdef LINUX_KERNEL_WATCHDOG
  751. if (m_iWatchdogFd > -1){
  752. char tB[64];
  753. sprintf(tB, "4000\n");
  754. // ToDo: Make Error Handling here!
  755. write(m_iWatchdogFd, &tB, strlen(tB));
  756. };
  757. #endif
  758. m_bDebugMode = m_pDBC->getAppDebugMode();
  759. validateFilters();
  760. validateSession();
  761. validateReports();
  762. //??
  763. if (! m_pHardwareManager->validate()){
  764. LOG("Reload Mojo Node Due to Memory Consumption!");
  765. m_bThreadStatus = TH_ENDED;
  766. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  767. continue;
  768. };
  769. m_mSysInfoProtector.lock();
  770. m_pHardwareManager->updateSysInfo(&m_sSysInfo);
  771. m_mSysInfoProtector.unlock();
  772. #ifndef LRF_PROXY_ONLY
  773. if ( m_pMediaHelper ) {
  774. m_pMediaHelper->validateService();
  775. };
  776. #endif
  777. if (m_sSiteData.use_remote_server){
  778. if (m_pRPC){
  779. m_pRPC->validate();
  780. m_bWorkWithRemoteServer = (m_pRPC->getConnectionStatus() == CS_CONNECTED);
  781. } else {
  782. // Checking if Nodes are Loadded
  783. if (m_pNodeFactory->nodesAreLoaded()) {
  784. #ifdef DEBUG
  785. LOG("Initial Connect to Remote Server");
  786. #endif
  787. if ((m_pRPC = new mojoRemoteClient(this))) {
  788. if (! (m_bWorkWithRemoteServer = m_pRPC->create())) {
  789. LOG_ERROR("Cannot Connect to Remote Server!");
  790. }
  791. } else {
  792. m_bWorkWithRemoteServer = false;
  793. }
  794. };
  795. };
  796. };
  797. DBC_PUSHED_CMD * tCMD = m_pDBC->getNodePushedCMD(m_nServerNodeID); // Kernel
  798. if (tCMD != NULL) {
  799. m_pDBC->updateNodePushedCMD(tCMD->id, DBC_CMD_STARTED);
  800. switch (tCMD->CMD) {
  801. case CMD_KRNL_RELOAD:{
  802. LOG("Reload Mojo Node!");
  803. m_bThreadStatus = TH_ENDED;
  804. setExitCause(EXIT_CODE_RELOAD_SERVICES);
  805. break;
  806. };
  807. case CMD_KRNL_RESTART:{
  808. LOG("Restart Mojo Node!");
  809. m_bThreadStatus = TH_ENDED;
  810. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  811. break;
  812. };
  813. case CMD_KRNL_FACTORY_RESET:{
  814. LOG("Restart Mojo -> Factory Reset!");
  815. m_bThreadStatus = TH_ENDED;
  816. setExitCause(EXIT_CODE_FACTORY_RESET);
  817. break;
  818. };
  819. case CMD_NODES_RELOAD:{
  820. if (m_pNodeFactory != NULL){
  821. m_pNodeFactory->reloadNodes();
  822. };
  823. break;
  824. };
  825. case CMD_NODE_RELOAD:{
  826. uint32_t tNodeID = atoi(tCMD->PARAM);
  827. if (m_pNodeFactory != NULL){
  828. m_pNodeFactory->reloadNode(tNodeID);
  829. };
  830. break;
  831. };
  832. case CMD_KRNL_PNP:{
  833. if (m_pHardwareManager != NULL){
  834. LOG_ERROR("Restart and Detect HardWare!");
  835. // Must be shutdowned all service that uses hardware prior to detection!
  836. m_bThreadStatus = TH_ENDED;
  837. setExitCause(EXIT_CODE_DETECT_HARDWARE); // Go -> Detect Hardware in BACKEND
  838. };
  839. break;
  840. };
  841. case CMD_KRNL_UPDATE_IDENT:{
  842. LOG("Restart Mojo -> Update Ident!");
  843. sprintf(m_sIdentity, tCMD->PARAM);
  844. config_setting_remove(m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY);
  845. config_setting_t * pSetting = config_setting_add(m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING);
  846. config_setting_set_string(pSetting, tCMD->PARAM);
  847. basicSettingsProvider::getInstance()->compromise();
  848. basicSettingsProvider::getInstance()->validate();
  849. m_pDBC->updateServerIdentity(m_nServerID, m_sIdentity);
  850. m_bThreadStatus = TH_ENDED;
  851. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  852. break;
  853. };
  854. case CMD_KRNL_REPLACE_DATABASE:{
  855. LOG("Restart Mojo -> Replace Database!");
  856. CSTR tNAME = m_pDBC->getDataParam(0);
  857. CSTR tUSER = m_pDBC->getDataParam(1);
  858. CSTR tGROUP = m_pDBC->getDataParam(2);
  859. if (tNAME){
  860. char tB[512];
  861. sprintf(tB, "./data/%s.db", tCMD->PARAM);
  862. if (fileExist(tB)){
  863. LOG("Restart Mojo -> Close Database!");
  864. // Closing Database
  865. delete(m_pDBC);
  866. m_pDBC = NULL;
  867. LOG("Restart Mojo -> Delete Database!");
  868. // unlink( tNAME );
  869. LOG("Restart Mojo -> Copy Database!");
  870. fileCopy(tB, tNAME);
  871. LOG("Restart Mojo -> Chown Database!");
  872. ch_chown(tNAME, tUSER, tGROUP);
  873. LOG("Restart Mojo -> Delete Old Database!");
  874. unlink(tB);
  875. m_bThreadStatus = TH_ENDED;
  876. LOG("Restart Mojo -> Exit!");
  877. setExitCause(EXIT_CODE_NO_DATABASE); // EXIT_CODE_RELOAD_SERVICES );
  878. m_bThreadStatus = TH_ENDED;
  879. } else {
  880. LOG_ERROR("New Database Does not Exists!");
  881. };
  882. // free( tNAME );
  883. // free( tUSER );
  884. // free( tGROUP );
  885. };
  886. break;
  887. };
  888. case CMD_KRNL_SYNC_TIME:{
  889. uint32_t uts = atoi(tCMD->PARAM);
  890. if (uts){
  891. #if (__GLIBC__ < 3) && (__GLIBC_MINOR__ < 31)
  892. time_t ts = (time_t) uts;
  893. if (stime(&ts) == 0){
  894. // Restert Server;
  895. m_bThreadStatus = TH_ENDED;
  896. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  897. };
  898. #else
  899. timespec ts;
  900. ts.tv_sec = (time_t) uts;
  901. ts.tv_nsec = 0;
  902. if (clock_settime(CLOCK_REALTIME, &ts) == 0){
  903. // Restert Server;
  904. m_bThreadStatus = TH_ENDED;
  905. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  906. };
  907. #endif
  908. };
  909. break;
  910. };
  911. case CMD_KRNL_SYSTEM_TASK:{
  912. break;
  913. };
  914. case NODE_FORCE_FINISH_ALARM:{
  915. stopSession();
  916. break;
  917. };
  918. case CMD_FORCE_FREEZE_BUBBLES:{
  919. LOG("Freeze Bubbles NCQ");
  920. EVENT_CONTAINER * tTT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_FLT_BUBBLES_BLOCK);
  921. if (tTT){
  922. m_pEventHeap->broadcast(tTT);
  923. };
  924. break;
  925. };
  926. case CMD_FORCE_POP_BUBBLE:{
  927. LOG("POP Bubbles NCQ");
  928. EVENT_CONTAINER * tTT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_FLT_BUBBLES_DEL_FB);
  929. if (tTT){
  930. m_pEventHeap->broadcast(tTT);
  931. };
  932. break;
  933. };
  934. case CMD_FORCE_RELOAD_BUBBLES:{
  935. LOG("Reload Bubbles NCQ");
  936. EVENT_CONTAINER * tTT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_FLT_BUBBLES_RELOAD);
  937. if (tTT){
  938. m_pEventHeap->broadcast(tTT);
  939. };
  940. break;
  941. };
  942. case CMD_FORCE_SENORS_RESET:{
  943. LOG("Reset All Sensors NCQ");
  944. if ( m_pNodeFactory ) {
  945. deviceNode ** tA = NULL;
  946. uint32_t tCNT = m_pNodeFactory->fetchNodes(ARGUS_ALS_SENSOR, tA );
  947. for ( uint32_t i = 0; i < tCNT; i++ ) ( (device2DSensor *)tA[i] )->reset();
  948. if (tCNT){
  949. free(tA);
  950. tA = NULL;
  951. };
  952. tCNT = m_pNodeFactory->fetchNodes(ARGUS_LFS_SENSOR, tA );
  953. for ( uint32_t i = 0; i < tCNT; i++ ) ( (device2DSensor *)tA[i] )->reset();
  954. if (tCNT){
  955. free(tA);
  956. tA = NULL;
  957. };
  958. tCNT = m_pNodeFactory->fetchNodes(ARGUS_PLS_200_SENSOR, tA );
  959. for ( uint32_t i = 0; i < tCNT; i++ ) ( (device2DSensor *)tA[i] )->reset();
  960. if (tCNT){
  961. free(tA);
  962. tA = NULL;
  963. };
  964. tCNT = m_pNodeFactory->fetchNodes(ARGUS_PLS_360_SENSOR, tA );
  965. for ( uint32_t i = 0; i < tCNT; i++ ) ( (device2DSensor *)tA[i] )->reset();
  966. if (tCNT){
  967. free(tA);
  968. tA = NULL;
  969. };
  970. };
  971. stopSession();
  972. break;
  973. };
  974. };
  975. free(tCMD);
  976. };
  977. if ( m_pDBC ){
  978. m_pDBC->cleanNodePushedCMD();
  979. }
  980. };
  981. SLEEP(SLEEP_KERNEL_INTERVAL);
  982. };
  983. }
  984. void mojoKernel::loadFilters()
  985. {
  986. if (m_pDBC == NULL)
  987. {
  988. return;
  989. }
  990. #ifdef DEBUG
  991. LOG("Loadding Filters ...");
  992. #endif
  993. DBC_INT_LIST * tList = m_pDBC->listFilters(m_nServerID);
  994. if (tList != NULL)
  995. {
  996. if (tList->size() > 0)
  997. {
  998. for (DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++)
  999. {
  1000. #ifdef DEBUG_EXTRA
  1001. LOG("Load Filter!");
  1002. #endif
  1003. DBC_TARGET_FLITER_DATA * tData = NULL;
  1004. tData = m_pDBC->getTargetFilterData(*idx);
  1005. if (tData != NULL)
  1006. {
  1007. #ifdef DEBUG_EXTRA
  1008. LOG("Filter Data OK!");
  1009. #endif
  1010. switch (tData->type)
  1011. {
  1012. case TF_LA_METRO_TRAIN_FILTER:
  1013. {
  1014. #ifdef DEBUG
  1015. LOG("Create Train Filter");
  1016. #endif
  1017. kernelEventFilterTrain * tFilter = new kernelEventFilterTrain(*idx);
  1018. if (tFilter != NULL)
  1019. {
  1020. if (tFilter->initFilter())
  1021. {
  1022. #ifdef DEBUG
  1023. LOG("Add Train Filter");
  1024. #endif
  1025. m_vTargetFilters.push_back((kernelEventFilter *) tFilter);
  1026. }
  1027. else
  1028. {
  1029. #ifdef DEBUG
  1030. LOG_ERROR("Error in Filter Init!");
  1031. #endif
  1032. delete(tFilter);
  1033. };
  1034. };
  1035. break;
  1036. };
  1037. case TF_AUTHORISED_PEOPLE_FILTER:
  1038. {
  1039. LOG("Create APF");
  1040. kernelEventFilterAPF * tFilter = new kernelEventFilterAPF(*idx);
  1041. if (tFilter != NULL)
  1042. {
  1043. if (tFilter->initFilter())
  1044. {
  1045. #ifdef DEBUG
  1046. LOG("Add APF");
  1047. #endif
  1048. m_vTargetFilters.push_back((kernelEventFilter *) tFilter);
  1049. }
  1050. else
  1051. {
  1052. #ifdef DEBUG
  1053. LOG_ERROR("Error in Filter Init!");
  1054. #endif
  1055. delete(tFilter);
  1056. };
  1057. };
  1058. break;
  1059. };
  1060. case TF_BUBBLE_SEGMENTATION_FILTER:
  1061. {
  1062. #ifdef DEBUG
  1063. LOG("Create Bubble Filter");
  1064. #endif
  1065. kernelEventFilterBubble * tFilter = new kernelEventFilterBubble(*idx);
  1066. if (tFilter != NULL)
  1067. {
  1068. #ifdef DEBUG_EXTRA
  1069. LOG("Init Bubble Filter");
  1070. #endif
  1071. if (tFilter->initFilter())
  1072. {
  1073. #ifdef DEBUG
  1074. LOG("Add Bubble Filter");
  1075. #endif
  1076. m_vTargetFilters.push_back((kernelEventFilter *) tFilter);
  1077. }
  1078. else
  1079. {
  1080. #ifdef DEBUG
  1081. LOG_ERROR("Error in Filter Init!");
  1082. #endif
  1083. delete(tFilter);
  1084. };
  1085. };
  1086. break;
  1087. };
  1088. case TF_YOLO_FILTER:
  1089. {
  1090. break;
  1091. };
  1092. default: LOG("Unknoun Filter"); break;
  1093. };
  1094. #ifdef DEBUG_EXTRA
  1095. LOG("Delete Filter Data");
  1096. #endif
  1097. free(tData);
  1098. };
  1099. };
  1100. #ifdef DEBUG_EXTRA
  1101. LOG("Clear Filter List");
  1102. #endif
  1103. tList->clear();
  1104. };
  1105. #ifdef DEBUG_EXTRA
  1106. LOG("Delete Filter List");
  1107. #endif
  1108. delete(tList);
  1109. #ifdef DEBUG
  1110. }
  1111. else
  1112. {
  1113. LOG("No Target Filters Assigned!");
  1114. #endif
  1115. };
  1116. #ifdef DEBUG
  1117. LOG("Done Load Filters!");
  1118. #endif
  1119. }
  1120. void mojoKernel::unloadFilters()
  1121. {
  1122. if (m_vTargetFilters.size() > 0)
  1123. {
  1124. #ifdef DEBUG
  1125. LOG("Have Filters to Unload");
  1126. #endif
  1127. for (vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++)
  1128. {
  1129. switch ((*idx)->getType())
  1130. {
  1131. case TF_LA_METRO_TRAIN_FILTER:
  1132. {
  1133. delete((kernelEventFilterTrain *) (*idx));
  1134. break;
  1135. };
  1136. case TF_AUTHORISED_PEOPLE_FILTER:
  1137. {
  1138. delete((kernelEventFilterAPF *) (*idx));
  1139. break;
  1140. };
  1141. case TF_BUBBLE_SEGMENTATION_FILTER:
  1142. {
  1143. delete((kernelEventFilterBubble *) (*idx));
  1144. break;
  1145. };
  1146. case TF_YOLO_FILTER:
  1147. {
  1148. // ToDo: Must be changed according to Steph's code
  1149. // delete( (kernelEventFilterYOLO*)(*idx) );
  1150. break;
  1151. };
  1152. default:
  1153. {
  1154. char tBuffer[64];
  1155. sprintf(tBuffer, "Unknown Filter: %d", (*idx)->getType());
  1156. LOG(tBuffer);
  1157. };
  1158. };
  1159. };
  1160. m_vTargetFilters.clear();
  1161. LOG("Unload Filters Done!");
  1162. #ifdef DEBUG
  1163. }
  1164. else
  1165. {
  1166. LOG("No Filters to Unload!");
  1167. #endif
  1168. };
  1169. }
  1170. void mojoKernel::validateFilters()
  1171. {
  1172. if (m_vTargetFilters.size() > 0)
  1173. {
  1174. #ifdef DEBUG_EXTRA
  1175. LOG("Filters Validation");
  1176. #endif
  1177. for (vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++)
  1178. {
  1179. if (! (*idx)->validate())
  1180. {
  1181. LOG_ERROR("Filter is Invalid!");
  1182. /*switch ((*idx)->getType()) {
  1183. case TF_LA_METRO_TRAIN_FILTER:{
  1184. delete( (kernelEventFilterTrain*)(*idx) );
  1185. break;
  1186. };
  1187. case TF_AUTHORISED_PEOPLE_FILTER:{
  1188. delete( (kernelEventFilterAPF*)(*idx) );
  1189. break;
  1190. };
  1191. default:{
  1192. char tBuffer[64];
  1193. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  1194. LOG( tBuffer );
  1195. };
  1196. };
  1197. */
  1198. };
  1199. };
  1200. };
  1201. }
  1202. bool mojoKernel::dispatchEvent(EVENT_CONTAINER * Evt)
  1203. {
  1204. if (Evt == NULL)
  1205. {
  1206. return false;
  1207. }
  1208. if (m_bWorkWithRemoteServer && (m_pRPC != NULL))
  1209. {
  1210. m_pRPC->addEvent(Evt);
  1211. }
  1212. if (! m_bKeepLocalReport)
  1213. {
  1214. return true;
  1215. }
  1216. if (m_pDBC == NULL)
  1217. {
  1218. return false;
  1219. }
  1220. if( m_bForceCloseSessionOnEndOfTheDay && m_bSessionStarted ) {
  1221. #ifndef LRF_PROXY_ONLY
  1222. kernelModuleEmbedVideo * tMO = (kernelModuleEmbedVideo *) m_pModuleFactory->getModule(KERNEL_MODULE_EMBED_VIDEO);
  1223. if( tMO ){
  1224. if( !tMO->isProcessingVideoStream() ) {
  1225. stopSession();
  1226. m_bForceCloseSessionOnEndOfTheDay = false;
  1227. };
  1228. } else {
  1229. LOG_ERROR("No Embed Video Processing Module!");
  1230. };
  1231. #else
  1232. stopSession();
  1233. m_bForceCloseSessionOnEndOfTheDay = false;
  1234. #endif
  1235. }
  1236. switch (Evt->m_eClass)
  1237. {
  1238. case EVT_CLASS_SYSTEM:
  1239. switch (Evt->m_eType)
  1240. {
  1241. case EVT_LRF_START_SESSION:
  1242. case EVT_LRF_END_SESSION_TO:
  1243. case EVT_LRF_END_SESSION_MA:
  1244. {
  1245. DBC_EVT_RECORD tRecord;
  1246. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1247. tRecord.ts_s = Evt->m_UMSTS / 1000;
  1248. tRecord.ts_ms = Evt->m_UMSTS % 1000;
  1249. tRecord.node_id = m_nServerNodeID;
  1250. tRecord.type = Evt->m_eType;
  1251. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1252. m_pDBC->addEvent(&tRecord);
  1253. break;
  1254. };
  1255. default: break;
  1256. };
  1257. break;
  1258. /*
  1259. case EVT_CLASS_EXTERNAL_CMD:
  1260. switch( Evt->m_eType ){
  1261. case EVT_FH_JAMER_ON:{
  1262. uint32_t TS_A, TS_B;
  1263. TS_A = Evt->m_UMSTS / 1000;
  1264. TS_B = Evt->m_UMSTS % 1000;
  1265. #ifdef SIMULATE_KERNEL_TARGETS
  1266. m_bJamingTO = SIMULATE_JAMER_TO;
  1267. #endif
  1268. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  1269. if( tData ) {
  1270. if( m_pDBC->addEvent(
  1271. 0,
  1272. Evt->m_UUID,
  1273. TS_A,
  1274. TS_B,
  1275. EVT_FH_JAMER_ON,
  1276. 0,
  1277. 0,
  1278. 0,
  1279. 0,
  1280. 0,
  1281. 0,
  1282. 0,
  1283. 0,
  1284. 0,
  1285. 0,
  1286. tData->evtUUID
  1287. ) ){
  1288. //Simulate FH Module Event;
  1289. EVENT_CONTAINER *ttt;
  1290. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  1291. if( nnn ){
  1292. nnn->location.lat = 0;
  1293. nnn->location.lon = 0;
  1294. nnn->location.alt = 0;
  1295. sprintf( nnn->missionID, tData->evtUUID );
  1296. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_JAMER_ON, NULL, NULL, nnn );
  1297. if( ttt ) {
  1298. m_pEventHeap->broadcast( ttt );
  1299. } else {
  1300. free( nnn );
  1301. };
  1302. };
  1303. };
  1304. free(tData);
  1305. };
  1306. break;
  1307. };
  1308. case EVT_FH_MISSION_END:{
  1309. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  1310. if( tData ) {
  1311. //DBC_EVT_DATA * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  1312. if( 1 / *tEVT* / ) {
  1313. uint32_t TS_A, TS_B;
  1314. TS_A = Evt->m_UMSTS / 1000;
  1315. TS_B = Evt->m_UMSTS % 1000;
  1316. if( m_pDBC->addEvent(
  1317. 0,
  1318. Evt->m_UUID,
  1319. TS_A,
  1320. TS_B,
  1321. EVT_FH_LANDED ) ){
  1322. //Simulate FH Module Event;
  1323. EVENT_CONTAINER *ttt;
  1324. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  1325. if( nnn ){
  1326. nnn->location.lat = 0;
  1327. nnn->location.lon = 0;
  1328. nnn->location.alt = 0;
  1329. sprintf( nnn->missionID, tData->evtUUID );
  1330. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_LANDED, NULL, NULL, nnn );
  1331. if( ttt ) {
  1332. m_pEventHeap->broadcast( ttt );
  1333. } else {
  1334. free( nnn );
  1335. };
  1336. };
  1337. };
  1338. //free( tEVT );
  1339. } else {
  1340. LOG_ERROR("Linked Event Was Not Found!");
  1341. };
  1342. };
  1343. break;
  1344. }
  1345. case EVT_FH_MISSION_BEGIN:{
  1346. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  1347. #ifdef SIMULATE_KERNEL_TARGETS
  1348. m_bLandingTO = SIMULATE_LANDING_TO;
  1349. #endif
  1350. if( tData ) {
  1351. #ifdef SIMULATE_KERNEL_TARGETS
  1352. sprintf( m_cCurrentMission, tData->evtUUID );
  1353. #endif
  1354. DBC_EVT_RECORD * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  1355. if( tEVT ) {
  1356. uint32_t TS_A, TS_B;
  1357. TS_A = Evt->m_UMSTS / 1000;
  1358. TS_B = Evt->m_UMSTS % 1000;
  1359. if( Evt->m_db_id = m_pDBC->addEvent(
  1360. 0,
  1361. Evt->m_UUID,
  1362. TS_A,
  1363. TS_B,
  1364. EVT_FH_FLY_TO,
  1365. 0,
  1366. 0,
  1367. 0,
  1368. 0,
  1369. 0,
  1370. 0,
  1371. tEVT->lat,
  1372. tEVT->lon,
  1373. tEVT->alt,
  1374. tEVT->id,
  1375. tEVT->uuid
  1376. ) ){
  1377. //Simulate FH Module Event;
  1378. EVENT_CONTAINER * ttt;
  1379. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  1380. if( nnn ){
  1381. nnn->location.lat = tEVT->lat;
  1382. nnn->location.lon = tEVT->lon;
  1383. nnn->location.alt = tEVT->alt;
  1384. sprintf( nnn->missionID, tData->evtUUID );
  1385. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_MISSION_BEGIN, NULL, NULL, nnn );
  1386. if( ttt ) {
  1387. m_pEventHeap->broadcast( ttt );
  1388. } else {
  1389. free( nnn );
  1390. };
  1391. };
  1392. };
  1393. free( tEVT );
  1394. } else {
  1395. LOG_ERROR("Linked Event Was Not Found!");
  1396. };
  1397. };
  1398. break;
  1399. };
  1400. };
  1401. break;
  1402. */
  1403. case EVT_CLASS_VIDEO:
  1404. {
  1405. switch (Evt->m_eType)
  1406. {
  1407. case EVT_CAPTURE_DONE:
  1408. case EVT_CAPTURE_END:
  1409. {
  1410. if (Evt->m_pEvtData)
  1411. {
  1412. handleVideo((VIDEO_SEGMENT_INFO *) Evt->m_pEvtData);
  1413. // Dont free Data - it points to static struct of Encoder!!
  1414. memset(Evt->m_pEvtData, 0, sizeof(VIDEO_SEGMENT_INFO));
  1415. Evt->m_pEvtData = NULL;
  1416. #ifdef DEBUG_EXTRA
  1417. LOG("VIDEO_SEGMENT_INFO Cleared!");
  1418. #endif
  1419. };
  1420. break;
  1421. };
  1422. case EVT_VIDEO_FPS_DEGRADED:
  1423. case EVT_VIDEO_FPS_RAISED:
  1424. {
  1425. if (! m_bDebugMode)
  1426. {
  1427. break;
  1428. }
  1429. DBC_EVT_RECORD tRecord;
  1430. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1431. uint32_t TS_A, TS_B;
  1432. TS_A = Evt->m_UMSTS / 1000;
  1433. TS_B = Evt->m_UMSTS % 1000;
  1434. tRecord.node_id = Evt->m_nSourceNodeID;
  1435. tRecord.type = Evt->m_eType;
  1436. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1437. tRecord.ts_s = TS_A;
  1438. tRecord.ts_ms = TS_B;
  1439. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1440. break;
  1441. };
  1442. default: break;
  1443. };
  1444. break;
  1445. };
  1446. case EVT_CLASS_SENSOR_CLD:
  1447. case EVT_CLASS_SENSOR_LGS:
  1448. case EVT_CLASS_SENSOR_LFS:
  1449. case EVT_CLASS_SENSOR_MND:
  1450. case EVT_CLASS_SENSOR_PLS_210:
  1451. case EVT_CLASS_SENSOR_PLS_360:
  1452. case EVT_CLASS_SENSOR_ALS:
  1453. case EVT_CLASS_SENSOR_3D:
  1454. case EVT_CLASS_SENSOR_ACC:
  1455. #ifdef DEBUG_EXTRA
  1456. char tB[128];
  1457. sprintf(tB, "Evt Class: %d Evt Type: %d ", Evt->m_eClass, Evt->m_eType);
  1458. LOG(tB);
  1459. #endif
  1460. switch (Evt->m_eType)
  1461. {
  1462. case EVT_TARGET_1D:
  1463. {
  1464. // startSession();
  1465. preserveSession();
  1466. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1467. if (tDATA != NULL)
  1468. {
  1469. DBC_EVT_RECORD tRecord;
  1470. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1471. uint32_t TS_A, TS_B;
  1472. TS_A = Evt->m_UMSTS / 1000;
  1473. TS_B = Evt->m_UMSTS % 1000;
  1474. tRecord.node_id = Evt->m_nSourceNodeID;
  1475. tRecord.type = Evt->m_eType;
  1476. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1477. tRecord.ts_s = TS_A;
  1478. tRecord.ts_ms = TS_B;
  1479. tRecord.range_digital = tDATA->digital;
  1480. tRecord.ra = tDATA->local_target.r;
  1481. tRecord.area = tDATA->area;
  1482. tRecord.lat = tDATA->global_target.lat;
  1483. tRecord.lon = tDATA->global_target.lon;
  1484. tRecord.alt = tDATA->global_target.alt;
  1485. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1486. };
  1487. break;
  1488. };
  1489. // While Camera Always Records Video on each Absolute Move
  1490. // an Event Must be generated to see video in report
  1491. case EVT_ROI_ACTIVE: // if( !m_bDebugMode ) break;
  1492. case EVT_TARGET_2D_BLACK:
  1493. case EVT_TARGET_2D_WHITE:
  1494. {
  1495. // startSession();
  1496. preserveSession();
  1497. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1498. if (tDATA != NULL)
  1499. {
  1500. DBC_EVT_RECORD tRecord;
  1501. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1502. uint32_t TS_A, TS_B;
  1503. TS_A = Evt->m_UMSTS / 1000;
  1504. TS_B = Evt->m_UMSTS % 1000;
  1505. tRecord.node_id = Evt->m_nSourceNodeID;
  1506. tRecord.type = Evt->m_eType;
  1507. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1508. tRecord.ts_s = TS_A;
  1509. tRecord.ts_ms = TS_B;
  1510. tRecord.lrf_id = tDATA->lrf_id;
  1511. tRecord.range_digital = tDATA->digital;
  1512. tRecord.az = tDATA->local_target.fi;
  1513. tRecord.ra = tDATA->local_target.r;
  1514. tRecord.area = tDATA->area;
  1515. tRecord.lat = tDATA->global_target.lat;
  1516. tRecord.lon = tDATA->global_target.lon;
  1517. tRecord.alt = tDATA->global_target.alt;
  1518. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1519. };
  1520. break;
  1521. };
  1522. case EVT_TARGET_3D_BLACK:
  1523. case EVT_TARGET_3D_WHITE:
  1524. {
  1525. // startSession();
  1526. preserveSession();
  1527. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1528. if (tDATA != NULL)
  1529. {
  1530. DBC_EVT_RECORD tRecord;
  1531. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1532. uint32_t TS_A, TS_B;
  1533. TS_A = Evt->m_UMSTS / 1000;
  1534. TS_B = Evt->m_UMSTS % 1000;
  1535. tRecord.node_id = Evt->m_nSourceNodeID;
  1536. tRecord.type = Evt->m_eType;
  1537. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1538. tRecord.ts_s = TS_A;
  1539. tRecord.ts_ms = TS_B;
  1540. tRecord.lrf_id = tDATA->lrf_id;
  1541. tRecord.range_digital = tDATA->digital;
  1542. tRecord.az = tDATA->local_target.fi;
  1543. tRecord.el = tDATA->local_target.tetta;
  1544. tRecord.ra = tDATA->local_target.r;
  1545. tRecord.area = tDATA->area;
  1546. tRecord.lat = tDATA->global_target.lat;
  1547. tRecord.lon = tDATA->global_target.lon;
  1548. tRecord.alt = tDATA->global_target.alt;
  1549. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1550. };
  1551. break;
  1552. };
  1553. case EVT_ERR_LRF_NO_IRQ:
  1554. case EVT_ERR_LRF_NO_ZERO:
  1555. case EVT_ERR_LRF_ANGLE:
  1556. case EVT_ERR_LRF_VOLTAGE:
  1557. case EVT_ERR_LRF_BROKEN:
  1558. case EVT_LRF_VIS_CHANGED:
  1559. case EVT_ERR_OVERFLOW:
  1560. case EVT_SST_OVERFLOW:
  1561. case EVT_COMMUNICATION_JAMED:
  1562. if (! m_bDebugMode)
  1563. {
  1564. break;
  1565. }
  1566. /* fall through */
  1567. case EVT_OP_ACK_MASK:
  1568. case EVT_OP_NACK_MASK:
  1569. case EVT_OP_ON_MASK:
  1570. case EVT_OP_OFF_MASK:
  1571. case EVT_CONNECTED:
  1572. case EVT_DISCONNECTED:
  1573. {
  1574. DBC_EVT_RECORD tRecord;
  1575. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1576. uint32_t TS_A, TS_B;
  1577. TS_A = Evt->m_UMSTS / 1000;
  1578. TS_B = Evt->m_UMSTS % 1000;
  1579. tRecord.node_id = Evt->m_nSourceNodeID;
  1580. tRecord.type = Evt->m_eType;
  1581. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1582. tRecord.ts_s = TS_A;
  1583. tRecord.ts_ms = TS_B;
  1584. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1585. break;
  1586. };
  1587. case EVT_FLIP:
  1588. {
  1589. if (! m_bDebugMode)
  1590. {
  1591. break;
  1592. }
  1593. DBC_EVT_RECORD tRecord;
  1594. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1595. uint32_t TS_A, TS_B;
  1596. TS_A = Evt->m_UMSTS / 1000;
  1597. TS_B = Evt->m_UMSTS % 1000;
  1598. tRecord.node_id = Evt->m_nSourceNodeID;
  1599. tRecord.type = Evt->m_eType;
  1600. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1601. tRecord.ts_s = TS_A;
  1602. tRecord.ts_ms = TS_B;
  1603. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1604. break;
  1605. };
  1606. default: break;
  1607. };
  1608. break;
  1609. case EVT_CLASS_KRNL_FILTER_LATF:
  1610. {
  1611. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1612. if ((Evt->m_eType > EVT_FLT_LATF_DIFF_DET) || (tDATA == NULL))
  1613. {
  1614. // AUX messages (no additional target data)
  1615. DBC_EVT_RECORD tRecord;
  1616. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1617. uint32_t TS_A, TS_B;
  1618. TS_A = Evt->m_UMSTS / 1000;
  1619. TS_B = Evt->m_UMSTS % 1000;
  1620. tRecord.node_id = m_nServerNodeID;
  1621. tRecord.type = Evt->m_eType;
  1622. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1623. tRecord.ts_s = TS_A;
  1624. tRecord.ts_ms = TS_B;
  1625. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1626. }
  1627. else
  1628. {
  1629. // If First Suspiciouse -> Start session to collect Fliter Messages
  1630. // if( Evt->m_eType == EVT_FLT_LATF_SUSP_START ) startSession();
  1631. // Regular LATF with target Info
  1632. DBC_EVT_RECORD tRecord;
  1633. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1634. uint32_t TS_A, TS_B;
  1635. TS_A = Evt->m_UMSTS / 1000;
  1636. TS_B = Evt->m_UMSTS % 1000;
  1637. tRecord.node_id = Evt->m_nSourceNodeID; // m_nServerNodeID;//??
  1638. tRecord.type = Evt->m_eType;
  1639. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1640. tRecord.ts_s = TS_A;
  1641. tRecord.ts_ms = TS_B;
  1642. tRecord.lrf_id = tDATA->lrf_id;
  1643. tRecord.range_digital = tDATA->digital;
  1644. tRecord.az = tDATA->local_target.fi;
  1645. tRecord.el = tDATA->local_target.tetta;
  1646. tRecord.ra = tDATA->local_target.r;
  1647. tRecord.area = tDATA->area;
  1648. tRecord.lat = 0;
  1649. tRecord.lon = 0;
  1650. tRecord.alt = 0;
  1651. #ifdef DEBUG_EXTRA
  1652. char tB[243];
  1653. sprintf(tB, "Az: %0.3f El: %0.3f Ra: %0.3f Dig: %d Width: %0.3f", tRecord.az, tRecord.el, tRecord.ra, tRecord.range_digital, tRecord.area);
  1654. LOG(tB);
  1655. #endif
  1656. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1657. };
  1658. break;
  1659. };
  1660. case EVT_CLASS_KRNL_FILTER_BUBBLE:
  1661. {
  1662. if (Evt->m_eType == EVT_FLT_BUBBLE_POPED || Evt->m_eType == EVT_FLT_BUBBLE_CREATED || Evt->m_eType == EVT_FLT_BUBBLE_BLOCK_T || Evt->m_eType == EVT_FLT_BUBBLE_BLOCK_P)
  1663. {
  1664. preserveSession();
  1665. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1666. DBC_EVT_RECORD tRecord;
  1667. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1668. uint32_t TS_A, TS_B;
  1669. TS_A = Evt->m_UMSTS / 1000;
  1670. TS_B = Evt->m_UMSTS % 1000;
  1671. tRecord.node_id = Evt->m_nSourceNodeID; // m_nServerNodeID;//??
  1672. tRecord.type = Evt->m_eType;
  1673. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1674. tRecord.ts_s = TS_A;
  1675. tRecord.ts_ms = TS_B;
  1676. tRecord.lrf_id = tDATA->lrf_id;
  1677. tRecord.range_digital = tDATA->digital;
  1678. tRecord.az = tDATA->local_target.fi;
  1679. tRecord.el = tDATA->local_target.tetta;
  1680. tRecord.ra = tDATA->local_target.r;
  1681. tRecord.area = tDATA->area;
  1682. tRecord.lat = 0;
  1683. tRecord.lon = 0;
  1684. tRecord.alt = 0;
  1685. #ifdef DEBUG_EXTRA
  1686. char tB[243];
  1687. sprintf(tB, "Az: %0.3f El: %0.3f Ra: %0.3f Dig: %d Width: %0.3f", tRecord.az, tRecord.el, tRecord.ra, tRecord.range_digital, tRecord.area);
  1688. LOG(tB);
  1689. #endif
  1690. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1691. };
  1692. break;
  1693. };
  1694. case EVT_CLASS_REMOTE_SERVER_CMD:
  1695. {
  1696. switch (Evt->m_eType)
  1697. {
  1698. case EVT_RPC_FRIZE_ACTIVE_B:
  1699. {
  1700. // getFilterId
  1701. break;
  1702. };
  1703. default: break;
  1704. };
  1705. break;
  1706. };
  1707. default: break;
  1708. };
  1709. return true;
  1710. }
  1711. /*
  1712. typedef struct S_VIDEO_SEGMENT_INFO{
  1713. uint32_t stream_id;
  1714. uint32_t streamTSS;
  1715. uint32_t streamTMS;
  1716. DBC_INT_LIST eventIDS;
  1717. DBC_INT_LIST eventFrames;
  1718. DBC_INT_LIST eventTSS;
  1719. DBC_INT_LIST eventTMS;
  1720. char recordMD5[33];
  1721. char recordEXT[6];
  1722. uint8_t streamFPS;
  1723. char scratchPath[PATH_MAX];
  1724. uint32_t dataSize;
  1725. uint32_t db_id;
  1726. }VIDEO_SEGMENT_INFO;
  1727. */
  1728. void mojoKernel::handleVideo(VIDEO_SEGMENT_INFO * ptr)
  1729. {
  1730. if (fileExist(ptr->scratchPath))
  1731. {
  1732. bool bKeepVideoFile = false;
  1733. if (ptr->recordMD5[0])
  1734. {
  1735. if (m_bKeepLocalReport)
  1736. {
  1737. // if wm is debug -> add all to database, else send to server
  1738. ptr->db_id = m_pDBC->addVideoRecord(ptr->stream_id, ptr->recordMD5, ptr->recordEXT, ptr->streamTSS, ptr->streamTMS, ptr->streamFPS);
  1739. if (ptr->db_id)
  1740. {
  1741. #ifdef DEBUG_EXTRA
  1742. LOG("Video File Registered");
  1743. #endif
  1744. for (size_t idx = 0; idx < ptr->eventIDS.size(); idx++)
  1745. {
  1746. #ifdef DEBUG_EXTRA
  1747. LOG("Binding Event to a Video File!");
  1748. #endif
  1749. if (m_pDBC->addVideoEvent(ptr->eventIDS[idx], ptr->db_id, ptr->eventFrames[idx], ptr->eventTSS[idx], ptr->eventTMS[idx]))
  1750. {
  1751. #ifdef DEBUG_EXTRA
  1752. LOG("OK");
  1753. #endif
  1754. };
  1755. };
  1756. CSTR tP = m_pDBC->getCurrentReportDir();
  1757. if (tP)
  1758. {
  1759. char tB[MAX_PATH];
  1760. char dB[MAX_PATH];
  1761. // sprintf( tB, "%s/data/videos/%s.%s", getcwd(dB,MAX_PATH), ptr->recordMD5, ptr->recordEXT );
  1762. sprintf(tB, "%s/data/reports/%s/%s.%s", getcwd(dB, MAX_PATH), tP, ptr->recordMD5, ptr->recordEXT);
  1763. fileCopy(ptr->scratchPath, tB);
  1764. ch_chown(tB, "nobody", "nogroup");
  1765. chmod(tB, 0664);
  1766. #ifdef DEBUG_EXTRA
  1767. LOG("Video Saved");
  1768. #endif
  1769. free(tP);
  1770. }
  1771. else
  1772. {
  1773. LOG_ERROR("Cannot Get Report Path from DB!");
  1774. };
  1775. }
  1776. else
  1777. {
  1778. LOG_ERROR("Failure in registration video!");
  1779. };
  1780. };
  1781. if (m_bWorkWithRemoteServer && (m_pRPC != NULL))
  1782. {
  1783. bKeepVideoFile = m_pRPC->addVideo(ptr);
  1784. }
  1785. };
  1786. if (! bKeepVideoFile)
  1787. {
  1788. unlink(ptr->scratchPath);
  1789. #ifdef DEBUG_EXTRA
  1790. LOG("Video Scratch Deleted!");
  1791. #endif
  1792. };
  1793. } else {
  1794. LOG_ERROR("Video File is Missing!");
  1795. };
  1796. //In case end of the day Force Session Close after Video File is saved
  1797. if ( m_bForceCloseSessionOnEndOfTheDay ) {
  1798. stopSession( m_bForceCloseSessionOnEndOfTheDay );
  1799. m_bForceCloseSessionOnEndOfTheDay = false;
  1800. };
  1801. }
  1802. /*
  1803. * Called every second from the kernel thread.
  1804. * At local midnight the current day report must be closed and the next day's report opened
  1805. * (data/reports/YYYY/MM/DD). Previously the report only moved to the new day when the next
  1806. * session started, so everything after midnight was written into the previous day's report
  1807. * until the next alarm.
  1808. *
  1809. * Order of operations:
  1810. * 1. an open session that started on the previous day is closed (END event goes into the old
  1811. * report) - if a video segment with events is still recording, wait for it at most
  1812. * DAY_CHANGE_MAX_VIDEO_WAIT seconds so the video is stored next to its events;
  1813. * 2. once the END event is written (event thread), the report is switched to the new day.
  1814. * A session started after midnight already switched the report itself (SESSION_START).
  1815. */
  1816. void mojoKernel::handleDayChange(void){
  1817. int cd = getLocalDay();
  1818. if ( cd < 0 ) return;
  1819. time_t now = time(NULL);
  1820. if ( !m_bDayChangePending ) {
  1821. if ( m_mDateOfToday == (unsigned long long) cd ) return;
  1822. m_bDayChangePending = true;
  1823. m_tDayChangeDetected = now;
  1824. LOG("Local day changed - closing day report");
  1825. };
  1826. bool bTimeOut = ( now - m_tDayChangeDetected ) >= DAY_CHANGE_MAX_VIDEO_WAIT;
  1827. if ( m_bSessionStarted && ( m_nSessionDay != cd ) ) {
  1828. // Session belongs to the previous day - close it in the previous day's report
  1829. m_bForceCloseSessionOnEndOfTheDay = true;
  1830. #ifndef LRF_PROXY_ONLY
  1831. kernelModuleEmbedVideo * tMO = (kernelModuleEmbedVideo *) m_pModuleFactory->getModule(KERNEL_MODULE_EMBED_VIDEO);
  1832. if ( tMO && tMO->isProcessingVideoStream() ) {
  1833. if ( !bTimeOut ) return; // let the running recording finish into the old day
  1834. LOG_ERROR("Day change: video still recording - forcing session close");
  1835. };
  1836. #endif
  1837. stopSession( true );
  1838. return; // switch on a next call, after the END event is written by the event thread
  1839. };
  1840. // Wait until the END of the previous session is stored in the old report
  1841. if ( !m_bSessionStarted && m_pDBC->getEventSessionStatus() && !bTimeOut ) return;
  1842. if ( m_pDBC->switchReportToCurrentDay() ) {
  1843. m_mDateOfToday = cd;
  1844. m_bDayChangePending = false;
  1845. m_bForceCloseSessionOnEndOfTheDay = false;
  1846. LOG("Day report switched to the new day");
  1847. } else {
  1848. LOG_ERROR("Day report switch failed - will retry");
  1849. };
  1850. }
  1851. bool mojoKernel::validateReports(void){
  1852. /// @todo Stephane needs to follow this function...
  1853. if ( m_bKeepLocalReport ){
  1854. // Local midnight: close the day report and open the next day's report directory
  1855. handleDayChange();
  1856. DBC_SITE_DATA * tSiteData = m_pDBC->getSiteData(1);
  1857. if (tSiteData){
  1858. // Check if settings has been changed in DB
  1859. if (tSiteData->report_cleanup != m_sSiteData.report_cleanup || tSiteData->preserve_empty != m_sSiteData.preserve_empty){
  1860. memcpy(&m_sSiteData, tSiteData, sizeof(DBC_SITE_DATA));
  1861. };
  1862. free(tSiteData);
  1863. } else {
  1864. LOG_ERROR("Error Loading Site DATA!");
  1865. return false;
  1866. };
  1867. if (m_sSiteData.report_cleanup) {
  1868. cleanUpReportDir(m_sSiteData.report_cleanup);
  1869. };
  1870. if (m_sSiteData.preserve_empty) {
  1871. float tT = 100.0f * (1.0f - (1.0f / ((float) m_sSiteData.preserve_empty)));
  1872. #ifdef DEBUG_EXTRA
  1873. char tB[128];
  1874. sprintf(tB, "Check Disk Size: %0.3f %0.3f %d [%d]", m_sSysInfo.disk, tT, m_sSiteData.preserve_empty);
  1875. LOG(tB);
  1876. #endif
  1877. if (m_sSysInfo.disk > tT) {
  1878. #ifdef DEBUG_EXTRA
  1879. LOG("Delete Some Reports!");
  1880. #endif
  1881. // delete some oldest report date
  1882. m_pDBC->deleteReportDate(m_pDBC->getFirstEventTS());
  1883. };
  1884. };
  1885. };
  1886. return true;
  1887. }
  1888. void mojoKernel::backupReports(uint32_t days)
  1889. {
  1890. if (! m_bKeepLocalReport)
  1891. {
  1892. return;
  1893. }
  1894. REPORT_DIRINFO tRDI;// = {0};
  1895. //memset( &tRDI, 0, sizeof(REPORT_DIRINFO)); will be initialized by fillRDF
  1896. uint32_t cTS = (uint32_t) time(NULL);
  1897. cTS -= 86400 * days;
  1898. fillRDF(cTS, tRDI);
  1899. DBC_INT_LIST * TL = m_pDBC->listOlderReportStat(tRDI.YEAR, tRDI.MONTH, tRDI.DAY);
  1900. if (TL)
  1901. {
  1902. for (DBC_INT_LIST::iterator idx = TL->begin(); idx < TL->end(); idx++)
  1903. {
  1904. REPORT_DIRINFO * tRDI = m_pDBC->getReportDirInfo(*idx);
  1905. if (tRDI)
  1906. {
  1907. char tB[MAX_PATH];
  1908. char dB[MAX_PATH];
  1909. sprintf(tB, "%s/data/reports/%s/%s/%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY);
  1910. char tPATH[MAX_PATH];
  1911. sprintf(tPATH, "%s/data/archive/%s_%s_%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY);
  1912. if (fileExist(tB))
  1913. {
  1914. if (! fileExist(tPATH))
  1915. {
  1916. // Try Create Job if sucess then create dir to skip next validation
  1917. if (createCronJob(0, tPATH, cTS))
  1918. {
  1919. // Create Backup Dir
  1920. uid_t uid = getUserUid(m_pDBC->getDataParam(1));
  1921. gid_t gid = getUserGid(m_pDBC->getDataParam(2));
  1922. if (createAccessibleDir(tPATH, STORAGE_DIR_MODE, uid, gid))
  1923. {
  1924. // Create Just One Backup Per Time!
  1925. free(tRDI);
  1926. break;
  1927. };
  1928. };
  1929. }; // else proceed next
  1930. };
  1931. free(tRDI);
  1932. };
  1933. };
  1934. delete(TL);
  1935. };
  1936. }
  1937. void mojoKernel::cleanUpReportDir(uint32_t days)
  1938. {
  1939. // Cleanup Current reports no Backup
  1940. uint32_t oTS = m_pDBC->getFirstEventTS();
  1941. uint32_t cTS = (uint32_t) time(NULL);
  1942. cTS -= 86400 * days;
  1943. #ifdef DEBUG_EXTRA
  1944. char tB[128];
  1945. sprintf(tB, "Oldest %d border %d", oTS, cTS);
  1946. LOG(tB);
  1947. #endif
  1948. if (oTS <= cTS)
  1949. {
  1950. #ifdef DEBUG_EXTRA
  1951. LOG("Deleting Old Reports");
  1952. #endif
  1953. /// @todo Stephane needs to see when this gets called...
  1954. m_pDBC->deleteReportDate(oTS);
  1955. };
  1956. }
  1957. bool mojoKernel::createCronJob(uint8_t job, CSTR param_str, int param_int)
  1958. {
  1959. if (! fileExist("/srv/etc/cron.lock"))
  1960. {
  1961. Json::Value root;
  1962. // Json::Value data;
  1963. switch (job)
  1964. {
  1965. case CRON_JOB_CREATE_BACKUP:
  1966. {
  1967. root["operation"] = CRON_JOB_CREATE_BACKUP;
  1968. // data["number"] = 1;
  1969. root["ts"] = param_int;
  1970. root["backup_dst"] = std::string(param_str);
  1971. break;
  1972. };
  1973. // No default JOB!
  1974. default: return false;
  1975. };
  1976. FILE * fd = fopen("/srv/etc/cron_.json", "w+");
  1977. if (fd)
  1978. {
  1979. if (HUMAN_FRIENDLY_JSON)
  1980. {
  1981. //Json::FastWriter writer;
  1982. //const std::string json_file = writer.write(root);
  1983. //fprintf(fd, json_file.c_str());
  1984. Json::StreamWriterBuilder builder;
  1985. const std::string json_file = Json::writeString(builder, root);
  1986. fprintf(fd, json_file.c_str());
  1987. }
  1988. else
  1989. {
  1990. Json::StreamWriterBuilder builder;
  1991. const std::string json_file = Json::writeString(builder, root);
  1992. fprintf(fd, json_file.c_str());
  1993. };
  1994. fclose(fd);
  1995. return true;
  1996. };
  1997. };
  1998. return false;
  1999. }
  2000. #undef LOG
  2001. #undef LOG_ERROR