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.

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