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.

2041 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 = getLocalDay();
  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. m_mSessionProtector.lock();
  517. if( m_bSessionStarted ){
  518. if( m_nNoAlarmCounter < NOTARGET_SESSION_CLOSE ){
  519. m_nNoAlarmCounter++;
  520. } else {
  521. m_mSessionProtector.unlock();
  522. stopSession( m_bForceCloseSessionOnEndOfTheDay );
  523. m_mSessionProtector.lock();
  524. };
  525. #ifdef EMBED_AUTORESET
  526. if (m_bEnableAutoReset) {
  527. if (m_nSessionCounter < m_nAutoResetTime) {
  528. m_nSessionCounter++;
  529. } else {
  530. m_nSessionCounter = 0;
  531. // Reset All Sensors
  532. DBC_INT_LIST * tL = m_pDBC->listNodes(false);
  533. if (tL){
  534. for (DBC_INT_LIST::iterator idx = tL->begin(); idx < tL->end(); idx++){
  535. m_pDBC->pushMemCMD(*idx, 1, "'A'");
  536. };
  537. delete(tL);
  538. };
  539. m_nNoAlarmCounter = NOTARGET_SESSION_CLOSE; // Session will be closed on next validation
  540. };
  541. };
  542. #endif
  543. };
  544. m_mSessionProtector.unlock();
  545. #ifdef PEREODIC_SENSORS_RESET
  546. if ( m_bEnablePereodicReset ) {
  547. m_nPereodicResetCounter++;
  548. if ( m_nPereodicResetCounter > m_nPereodicResetTime ) {
  549. m_nPereodicResetCounter = 0;
  550. // Reset All Sensors
  551. #ifdef DEBUG
  552. LOG("Pereodic Reset to All Sensors");
  553. #endif
  554. DBC_INT_LIST * tL = m_pDBC->listNodes(false);
  555. if (tL){
  556. for (DBC_INT_LIST::iterator idx = tL->begin(); idx < tL->end(); idx++){
  557. m_pDBC->pushMemCMD(*idx, 1, "'A'");
  558. };
  559. delete(tL);
  560. };
  561. };
  562. };
  563. #endif
  564. }
  565. #ifdef __MOJO_REMOTE_CLIENT_HEADER__
  566. bool mojoKernel::getTrainPresence()
  567. {
  568. bool RetValue = false;
  569. if (m_vTargetFilters.size())
  570. {
  571. for (vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++)
  572. {
  573. if ((*idx)->getType() == TF_LA_METRO_TRAIN_FILTER)
  574. {
  575. kernelEventFilterTrain * tFT = (kernelEventFilterTrain *) (*idx);
  576. RetValue = tFT->getTrainStatus();
  577. break;
  578. };
  579. };
  580. };
  581. return RetValue;
  582. }
  583. bool mojoKernel::fillMojoInfo(ARGUS_DATA_MOJO_INFO * ptr)
  584. {
  585. if (ptr == NULL)
  586. {
  587. return false;
  588. }
  589. memset(ptr, 0, sizeof(ARGUS_DATA_MOJO_INFO));
  590. ptr->MOJO_WORK_MODE = (m_bKeepLocalReport ? 4 : 0) | (m_bMediaMode ? 2 : 0) | (m_bDebugMode ? 1 : 0);
  591. if (m_vTargetFilters.size())
  592. {
  593. for (vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++)
  594. {
  595. #ifdef DEBUG_EXTRA
  596. LOG("check Filter");
  597. #endif
  598. if ((*idx)->getType() == TF_LA_METRO_TRAIN_FILTER)
  599. {
  600. #ifdef DEBUG_EXTRA
  601. LOG("T Filter Found!");
  602. #endif
  603. kernelEventFilterTrain * tFT = (kernelEventFilterTrain *) (*idx);
  604. ptr->UPPER_ID = tFT->getUpperId();
  605. ptr->BOTTOM_ID = tFT->getBottomId();
  606. ptr->MOJO_WORK_MODE |= (tFT->getMode() << 3);
  607. break;
  608. };
  609. };
  610. };
  611. #ifndef LRF_PROXY_ONLY
  612. if ( m_pMediaHelper ) {
  613. m_pMediaHelper->validateService();
  614. };
  615. #endif
  616. deviceNode ** tA = NULL;
  617. uint32_t tCNT = m_pNodeFactory->fetchNodes(ARGUS_ANALOG_STATIC_CAMERA, tA);
  618. if (tCNT)
  619. {
  620. // deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[0];
  621. // ptr->CAM_ID = tCAM->getNodeID();
  622. // ptr->CAM_STREAM_ID = tCAM->getCamStream();
  623. // ptr->CAM_ID = tA[0]->getDeviceID();
  624. ptr->CAM_ID = tA[0]->getNodeID();
  625. ptr->CAM_STREAM_ID = tA[0]->getDeviceParam();
  626. }
  627. else
  628. {
  629. LOG("FIMO Cannot Get ARGUS_ANALOG_STATIC_CAMERA Camera!");
  630. tCNT = m_pNodeFactory->fetchNodes(ARGUS_ANALOG_PTZ_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_PTZ_CAMERA Camera!");
  643. tCNT = m_pNodeFactory->fetchNodes(ARGUS_IP_STATIC_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_IP_STATIC_CAMERA Camera!");
  656. tCNT = m_pNodeFactory->fetchNodes(ARGUS_IP_PTZ_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_PTZ_CAMERA Camera!");
  669. tCNT = m_pNodeFactory->fetchNodes(ARGUS_IP_PRESET_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_PRESET_CAMERA Camera!");
  682. tCNT = m_pNodeFactory->fetchNodes(ARGUS_ANALOG_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_ANALOG_PRESET_CAMERA Camera!");
  695. LOG("No Camera Defined In the system!");
  696. };
  697. };
  698. };
  699. };
  700. };
  701. };
  702. if (tCNT)
  703. {
  704. free(tA);
  705. tA = NULL;
  706. };
  707. ptr->CPU_COUNT = m_sSysInfo.CPU_CORES;
  708. ptr->TOT_DISK = m_sSysInfo.T_DISK;
  709. ptr->TOT_MEMO = m_sSysInfo.T_MEM;
  710. strncpy(ptr->TLS_VERSION, _BUILD_STRING_, 64);
  711. ptr->TLS_VERSION[63] = (char) 0;
  712. strncpy(ptr->LLS_VERSION, m_pHardwareManager->getSysInfo()->OS_INFO, 64);
  713. ptr->LLS_VERSION[63] = (char) 0;
  714. LOG("FIMO Overall info");
  715. return true;
  716. }
  717. #endif
  718. void mojoKernel::getSystemUtilization(DBC_SYS_LOAD_INFO * ptr)
  719. {
  720. if (ptr == NULL)
  721. {
  722. return;
  723. }
  724. m_mSysInfoProtector.lock();
  725. memcpy(ptr, &m_sSysInfo, sizeof(DBC_SYS_LOAD_INFO));
  726. m_mSysInfoProtector.unlock();
  727. }
  728. void mojoKernel::m_fThreadProc(void){
  729. time_t tTimeTSeconds = 0;
  730. m_bThreadStatus = TH_WORKING;
  731. pthread_setname_np( pthread_self(), "Kernel" );
  732. while (m_bThreadStatus < TH_ENDED){
  733. m_tTimeTSeconds = time(NULL);
  734. // Each Second
  735. if (m_tTimeTSeconds != tTimeTSeconds){
  736. tTimeTSeconds = m_tTimeTSeconds;
  737. if (m_bApplicationFirstRun){
  738. if (m_pHardwareManager != NULL){
  739. LOG_ERROR("Restart and Detect HardWare!");
  740. // Must be shutdowned all service that uses hardware prior to detection!
  741. m_bThreadStatus = TH_ENDED;
  742. setExitCause(EXIT_CODE_DETECT_HARDWARE); // Go -> Detect Hardware in BACKEND
  743. continue;
  744. };
  745. };
  746. #ifdef LINUX_KERNEL_WATCHDOG
  747. if (m_iWatchdogFd > -1){
  748. char tB[64];
  749. sprintf(tB, "4000\n");
  750. // ToDo: Make Error Handling here!
  751. write(m_iWatchdogFd, &tB, strlen(tB));
  752. };
  753. #endif
  754. m_bDebugMode = m_pDBC->getAppDebugMode();
  755. validateFilters();
  756. validateSession();
  757. validateReports();
  758. //??
  759. if (! m_pHardwareManager->validate()){
  760. LOG("Reload Mojo Node Due to Memory Consumption!");
  761. m_bThreadStatus = TH_ENDED;
  762. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  763. continue;
  764. };
  765. m_mSysInfoProtector.lock();
  766. m_pHardwareManager->updateSysInfo(&m_sSysInfo);
  767. m_mSysInfoProtector.unlock();
  768. #ifndef LRF_PROXY_ONLY
  769. if ( m_pMediaHelper ) {
  770. m_pMediaHelper->validateService();
  771. };
  772. #endif
  773. if (m_sSiteData.use_remote_server){
  774. if (m_pRPC){
  775. m_pRPC->validate();
  776. m_bWorkWithRemoteServer = (m_pRPC->getConnectionStatus() == CS_CONNECTED);
  777. } else {
  778. // Checking if Nodes are Loadded
  779. if (m_pNodeFactory->nodesAreLoaded()) {
  780. #ifdef DEBUG
  781. LOG("Initial Connect to Remote Server");
  782. #endif
  783. if ((m_pRPC = new mojoRemoteClient(this))) {
  784. if (! (m_bWorkWithRemoteServer = m_pRPC->create())) {
  785. LOG_ERROR("Cannot Connect to Remote Server!");
  786. }
  787. } else {
  788. m_bWorkWithRemoteServer = false;
  789. }
  790. };
  791. };
  792. };
  793. DBC_PUSHED_CMD * tCMD = m_pDBC->getNodePushedCMD(m_nServerNodeID); // Kernel
  794. if (tCMD != NULL) {
  795. m_pDBC->updateNodePushedCMD(tCMD->id, DBC_CMD_STARTED);
  796. switch (tCMD->CMD) {
  797. case CMD_KRNL_RELOAD:{
  798. LOG("Reload Mojo Node!");
  799. m_bThreadStatus = TH_ENDED;
  800. setExitCause(EXIT_CODE_RELOAD_SERVICES);
  801. break;
  802. };
  803. case CMD_KRNL_RESTART:{
  804. LOG("Restart Mojo Node!");
  805. m_bThreadStatus = TH_ENDED;
  806. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  807. break;
  808. };
  809. case CMD_KRNL_FACTORY_RESET:{
  810. LOG("Restart Mojo -> Factory Reset!");
  811. m_bThreadStatus = TH_ENDED;
  812. setExitCause(EXIT_CODE_FACTORY_RESET);
  813. break;
  814. };
  815. case CMD_NODES_RELOAD:{
  816. if (m_pNodeFactory != NULL){
  817. m_pNodeFactory->reloadNodes();
  818. };
  819. break;
  820. };
  821. case CMD_NODE_RELOAD:{
  822. uint32_t tNodeID = atoi(tCMD->PARAM);
  823. if (m_pNodeFactory != NULL){
  824. m_pNodeFactory->reloadNode(tNodeID);
  825. };
  826. break;
  827. };
  828. case CMD_KRNL_PNP:{
  829. if (m_pHardwareManager != NULL){
  830. LOG_ERROR("Restart and Detect HardWare!");
  831. // Must be shutdowned all service that uses hardware prior to detection!
  832. m_bThreadStatus = TH_ENDED;
  833. setExitCause(EXIT_CODE_DETECT_HARDWARE); // Go -> Detect Hardware in BACKEND
  834. };
  835. break;
  836. };
  837. case CMD_KRNL_UPDATE_IDENT:{
  838. LOG("Restart Mojo -> Update Ident!");
  839. sprintf(m_sIdentity, tCMD->PARAM);
  840. config_setting_remove(m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY);
  841. config_setting_t * pSetting = config_setting_add(m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING);
  842. config_setting_set_string(pSetting, tCMD->PARAM);
  843. basicSettingsProvider::getInstance()->compromise();
  844. basicSettingsProvider::getInstance()->validate();
  845. m_pDBC->updateServerIdentity(m_nServerID, m_sIdentity);
  846. m_bThreadStatus = TH_ENDED;
  847. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  848. break;
  849. };
  850. case CMD_KRNL_REPLACE_DATABASE:{
  851. LOG("Restart Mojo -> Replace Database!");
  852. CSTR tNAME = m_pDBC->getDataParam(0);
  853. CSTR tUSER = m_pDBC->getDataParam(1);
  854. CSTR tGROUP = m_pDBC->getDataParam(2);
  855. if (tNAME){
  856. char tB[512];
  857. sprintf(tB, "./data/%s.db", tCMD->PARAM);
  858. if (fileExist(tB)){
  859. LOG("Restart Mojo -> Close Database!");
  860. // Closing Database
  861. delete(m_pDBC);
  862. m_pDBC = NULL;
  863. LOG("Restart Mojo -> Delete Database!");
  864. // unlink( tNAME );
  865. LOG("Restart Mojo -> Copy Database!");
  866. fileCopy(tB, tNAME);
  867. LOG("Restart Mojo -> Chown Database!");
  868. ch_chown(tNAME, tUSER, tGROUP);
  869. LOG("Restart Mojo -> Delete Old Database!");
  870. unlink(tB);
  871. m_bThreadStatus = TH_ENDED;
  872. LOG("Restart Mojo -> Exit!");
  873. setExitCause(EXIT_CODE_NO_DATABASE); // EXIT_CODE_RELOAD_SERVICES );
  874. m_bThreadStatus = TH_ENDED;
  875. } else {
  876. LOG_ERROR("New Database Does not Exists!");
  877. };
  878. // free( tNAME );
  879. // free( tUSER );
  880. // free( tGROUP );
  881. };
  882. break;
  883. };
  884. case CMD_KRNL_SYNC_TIME:{
  885. uint32_t uts = atoi(tCMD->PARAM);
  886. if (uts){
  887. #if (__GLIBC__ < 3) && (__GLIBC_MINOR__ < 31)
  888. time_t ts = (time_t) uts;
  889. if (stime(&ts) == 0){
  890. // Restert Server;
  891. m_bThreadStatus = TH_ENDED;
  892. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  893. };
  894. #else
  895. timespec ts;
  896. ts.tv_sec = (time_t) uts;
  897. ts.tv_nsec = 0;
  898. if (clock_settime(CLOCK_REALTIME, &ts) == 0){
  899. // Restert Server;
  900. m_bThreadStatus = TH_ENDED;
  901. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  902. };
  903. #endif
  904. };
  905. break;
  906. };
  907. case CMD_KRNL_SYSTEM_TASK:{
  908. break;
  909. };
  910. case NODE_FORCE_FINISH_ALARM:{
  911. stopSession();
  912. break;
  913. };
  914. case CMD_FORCE_FREEZE_BUBBLES:{
  915. LOG("Freeze Bubbles NCQ");
  916. EVENT_CONTAINER * tTT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_FLT_BUBBLES_BLOCK);
  917. if (tTT){
  918. m_pEventHeap->broadcast(tTT);
  919. };
  920. break;
  921. };
  922. case CMD_FORCE_POP_BUBBLE:{
  923. LOG("POP Bubbles NCQ");
  924. EVENT_CONTAINER * tTT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_FLT_BUBBLES_DEL_FB);
  925. if (tTT){
  926. m_pEventHeap->broadcast(tTT);
  927. };
  928. break;
  929. };
  930. case CMD_FORCE_RELOAD_BUBBLES:{
  931. LOG("Reload Bubbles NCQ");
  932. EVENT_CONTAINER * tTT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_FLT_BUBBLES_RELOAD);
  933. if (tTT){
  934. m_pEventHeap->broadcast(tTT);
  935. };
  936. break;
  937. };
  938. case CMD_FORCE_SENORS_RESET:{
  939. LOG("Reset All Sensors NCQ");
  940. if ( m_pNodeFactory ) {
  941. deviceNode ** tA = NULL;
  942. uint32_t tCNT = m_pNodeFactory->fetchNodes(ARGUS_ALS_SENSOR, tA );
  943. for ( uint32_t i = 0; i < tCNT; i++ ) ( (device2DSensor *)tA[i] )->reset();
  944. if (tCNT){
  945. free(tA);
  946. tA = NULL;
  947. };
  948. tCNT = m_pNodeFactory->fetchNodes(ARGUS_LFS_SENSOR, tA );
  949. for ( uint32_t i = 0; i < tCNT; i++ ) ( (device2DSensor *)tA[i] )->reset();
  950. if (tCNT){
  951. free(tA);
  952. tA = NULL;
  953. };
  954. tCNT = m_pNodeFactory->fetchNodes(ARGUS_PLS_200_SENSOR, tA );
  955. for ( uint32_t i = 0; i < tCNT; i++ ) ( (device2DSensor *)tA[i] )->reset();
  956. if (tCNT){
  957. free(tA);
  958. tA = NULL;
  959. };
  960. tCNT = m_pNodeFactory->fetchNodes(ARGUS_PLS_360_SENSOR, tA );
  961. for ( uint32_t i = 0; i < tCNT; i++ ) ( (device2DSensor *)tA[i] )->reset();
  962. if (tCNT){
  963. free(tA);
  964. tA = NULL;
  965. };
  966. };
  967. stopSession();
  968. break;
  969. };
  970. };
  971. free(tCMD);
  972. };
  973. if ( m_pDBC ){
  974. m_pDBC->cleanNodePushedCMD();
  975. }
  976. };
  977. SLEEP(SLEEP_KERNEL_INTERVAL);
  978. };
  979. }
  980. void mojoKernel::loadFilters()
  981. {
  982. if (m_pDBC == NULL)
  983. {
  984. return;
  985. }
  986. #ifdef DEBUG
  987. LOG("Loadding Filters ...");
  988. #endif
  989. DBC_INT_LIST * tList = m_pDBC->listFilters(m_nServerID);
  990. if (tList != NULL)
  991. {
  992. if (tList->size() > 0)
  993. {
  994. for (DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++)
  995. {
  996. #ifdef DEBUG_EXTRA
  997. LOG("Load Filter!");
  998. #endif
  999. DBC_TARGET_FLITER_DATA * tData = NULL;
  1000. tData = m_pDBC->getTargetFilterData(*idx);
  1001. if (tData != NULL)
  1002. {
  1003. #ifdef DEBUG_EXTRA
  1004. LOG("Filter Data OK!");
  1005. #endif
  1006. switch (tData->type)
  1007. {
  1008. case TF_LA_METRO_TRAIN_FILTER:
  1009. {
  1010. #ifdef DEBUG
  1011. LOG("Create Train Filter");
  1012. #endif
  1013. kernelEventFilterTrain * tFilter = new kernelEventFilterTrain(*idx);
  1014. if (tFilter != NULL)
  1015. {
  1016. if (tFilter->initFilter())
  1017. {
  1018. #ifdef DEBUG
  1019. LOG("Add Train Filter");
  1020. #endif
  1021. m_vTargetFilters.push_back((kernelEventFilter *) tFilter);
  1022. }
  1023. else
  1024. {
  1025. #ifdef DEBUG
  1026. LOG_ERROR("Error in Filter Init!");
  1027. #endif
  1028. delete(tFilter);
  1029. };
  1030. };
  1031. break;
  1032. };
  1033. case TF_AUTHORISED_PEOPLE_FILTER:
  1034. {
  1035. LOG("Create APF");
  1036. kernelEventFilterAPF * tFilter = new kernelEventFilterAPF(*idx);
  1037. if (tFilter != NULL)
  1038. {
  1039. if (tFilter->initFilter())
  1040. {
  1041. #ifdef DEBUG
  1042. LOG("Add APF");
  1043. #endif
  1044. m_vTargetFilters.push_back((kernelEventFilter *) tFilter);
  1045. }
  1046. else
  1047. {
  1048. #ifdef DEBUG
  1049. LOG_ERROR("Error in Filter Init!");
  1050. #endif
  1051. delete(tFilter);
  1052. };
  1053. };
  1054. break;
  1055. };
  1056. case TF_BUBBLE_SEGMENTATION_FILTER:
  1057. {
  1058. #ifdef DEBUG
  1059. LOG("Create Bubble Filter");
  1060. #endif
  1061. kernelEventFilterBubble * tFilter = new kernelEventFilterBubble(*idx);
  1062. if (tFilter != NULL)
  1063. {
  1064. #ifdef DEBUG_EXTRA
  1065. LOG("Init Bubble Filter");
  1066. #endif
  1067. if (tFilter->initFilter())
  1068. {
  1069. #ifdef DEBUG
  1070. LOG("Add Bubble Filter");
  1071. #endif
  1072. m_vTargetFilters.push_back((kernelEventFilter *) tFilter);
  1073. }
  1074. else
  1075. {
  1076. #ifdef DEBUG
  1077. LOG_ERROR("Error in Filter Init!");
  1078. #endif
  1079. delete(tFilter);
  1080. };
  1081. };
  1082. break;
  1083. };
  1084. case TF_YOLO_FILTER:
  1085. {
  1086. break;
  1087. };
  1088. default: LOG("Unknoun Filter"); break;
  1089. };
  1090. #ifdef DEBUG_EXTRA
  1091. LOG("Delete Filter Data");
  1092. #endif
  1093. free(tData);
  1094. };
  1095. };
  1096. #ifdef DEBUG_EXTRA
  1097. LOG("Clear Filter List");
  1098. #endif
  1099. tList->clear();
  1100. };
  1101. #ifdef DEBUG_EXTRA
  1102. LOG("Delete Filter List");
  1103. #endif
  1104. delete(tList);
  1105. #ifdef DEBUG
  1106. }
  1107. else
  1108. {
  1109. LOG("No Target Filters Assigned!");
  1110. #endif
  1111. };
  1112. #ifdef DEBUG
  1113. LOG("Done Load Filters!");
  1114. #endif
  1115. }
  1116. void mojoKernel::unloadFilters()
  1117. {
  1118. if (m_vTargetFilters.size() > 0)
  1119. {
  1120. #ifdef DEBUG
  1121. LOG("Have Filters to Unload");
  1122. #endif
  1123. for (vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++)
  1124. {
  1125. switch ((*idx)->getType())
  1126. {
  1127. case TF_LA_METRO_TRAIN_FILTER:
  1128. {
  1129. delete((kernelEventFilterTrain *) (*idx));
  1130. break;
  1131. };
  1132. case TF_AUTHORISED_PEOPLE_FILTER:
  1133. {
  1134. delete((kernelEventFilterAPF *) (*idx));
  1135. break;
  1136. };
  1137. case TF_BUBBLE_SEGMENTATION_FILTER:
  1138. {
  1139. delete((kernelEventFilterBubble *) (*idx));
  1140. break;
  1141. };
  1142. case TF_YOLO_FILTER:
  1143. {
  1144. // ToDo: Must be changed according to Steph's code
  1145. // delete( (kernelEventFilterYOLO*)(*idx) );
  1146. break;
  1147. };
  1148. default:
  1149. {
  1150. char tBuffer[64];
  1151. sprintf(tBuffer, "Unknown Filter: %d", (*idx)->getType());
  1152. LOG(tBuffer);
  1153. };
  1154. };
  1155. };
  1156. m_vTargetFilters.clear();
  1157. LOG("Unload Filters Done!");
  1158. #ifdef DEBUG
  1159. }
  1160. else
  1161. {
  1162. LOG("No Filters to Unload!");
  1163. #endif
  1164. };
  1165. }
  1166. void mojoKernel::validateFilters()
  1167. {
  1168. if (m_vTargetFilters.size() > 0)
  1169. {
  1170. #ifdef DEBUG_EXTRA
  1171. LOG("Filters Validation");
  1172. #endif
  1173. for (vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++)
  1174. {
  1175. if (! (*idx)->validate())
  1176. {
  1177. LOG_ERROR("Filter is Invalid!");
  1178. /*switch ((*idx)->getType()) {
  1179. case TF_LA_METRO_TRAIN_FILTER:{
  1180. delete( (kernelEventFilterTrain*)(*idx) );
  1181. break;
  1182. };
  1183. case TF_AUTHORISED_PEOPLE_FILTER:{
  1184. delete( (kernelEventFilterAPF*)(*idx) );
  1185. break;
  1186. };
  1187. default:{
  1188. char tBuffer[64];
  1189. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  1190. LOG( tBuffer );
  1191. };
  1192. };
  1193. */
  1194. };
  1195. };
  1196. };
  1197. }
  1198. bool mojoKernel::dispatchEvent(EVENT_CONTAINER * Evt)
  1199. {
  1200. if (Evt == NULL)
  1201. {
  1202. return false;
  1203. }
  1204. if (m_bWorkWithRemoteServer && (m_pRPC != NULL))
  1205. {
  1206. m_pRPC->addEvent(Evt);
  1207. }
  1208. if (! m_bKeepLocalReport)
  1209. {
  1210. return true;
  1211. }
  1212. if (m_pDBC == NULL)
  1213. {
  1214. return false;
  1215. }
  1216. if( m_bForceCloseSessionOnEndOfTheDay && m_bSessionStarted ) {
  1217. #ifndef LRF_PROXY_ONLY
  1218. kernelModuleEmbedVideo * tMO = (kernelModuleEmbedVideo *) m_pModuleFactory->getModule(KERNEL_MODULE_EMBED_VIDEO);
  1219. if( tMO ){
  1220. if( !tMO->isProcessingVideoStream() ) {
  1221. stopSession();
  1222. m_bForceCloseSessionOnEndOfTheDay = false;
  1223. };
  1224. } else {
  1225. LOG_ERROR("No Embed Video Processing Module!");
  1226. };
  1227. #else
  1228. stopSession();
  1229. m_bForceCloseSessionOnEndOfTheDay = false;
  1230. #endif
  1231. }
  1232. switch (Evt->m_eClass)
  1233. {
  1234. case EVT_CLASS_SYSTEM:
  1235. switch (Evt->m_eType)
  1236. {
  1237. case EVT_LRF_START_SESSION:
  1238. case EVT_LRF_END_SESSION_TO:
  1239. case EVT_LRF_END_SESSION_MA:
  1240. {
  1241. DBC_EVT_RECORD tRecord;
  1242. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1243. tRecord.ts_s = Evt->m_UMSTS / 1000;
  1244. tRecord.ts_ms = Evt->m_UMSTS % 1000;
  1245. tRecord.node_id = m_nServerNodeID;
  1246. tRecord.type = Evt->m_eType;
  1247. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1248. m_pDBC->addEvent(&tRecord);
  1249. break;
  1250. };
  1251. default: break;
  1252. };
  1253. break;
  1254. /*
  1255. case EVT_CLASS_EXTERNAL_CMD:
  1256. switch( Evt->m_eType ){
  1257. case EVT_FH_JAMER_ON:{
  1258. uint32_t TS_A, TS_B;
  1259. TS_A = Evt->m_UMSTS / 1000;
  1260. TS_B = Evt->m_UMSTS % 1000;
  1261. #ifdef SIMULATE_KERNEL_TARGETS
  1262. m_bJamingTO = SIMULATE_JAMER_TO;
  1263. #endif
  1264. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  1265. if( tData ) {
  1266. if( m_pDBC->addEvent(
  1267. 0,
  1268. Evt->m_UUID,
  1269. TS_A,
  1270. TS_B,
  1271. EVT_FH_JAMER_ON,
  1272. 0,
  1273. 0,
  1274. 0,
  1275. 0,
  1276. 0,
  1277. 0,
  1278. 0,
  1279. 0,
  1280. 0,
  1281. 0,
  1282. tData->evtUUID
  1283. ) ){
  1284. //Simulate FH Module Event;
  1285. EVENT_CONTAINER *ttt;
  1286. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  1287. if( nnn ){
  1288. nnn->location.lat = 0;
  1289. nnn->location.lon = 0;
  1290. nnn->location.alt = 0;
  1291. sprintf( nnn->missionID, tData->evtUUID );
  1292. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_JAMER_ON, NULL, NULL, nnn );
  1293. if( ttt ) {
  1294. m_pEventHeap->broadcast( ttt );
  1295. } else {
  1296. free( nnn );
  1297. };
  1298. };
  1299. };
  1300. free(tData);
  1301. };
  1302. break;
  1303. };
  1304. case EVT_FH_MISSION_END:{
  1305. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  1306. if( tData ) {
  1307. //DBC_EVT_DATA * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  1308. if( 1 / *tEVT* / ) {
  1309. uint32_t TS_A, TS_B;
  1310. TS_A = Evt->m_UMSTS / 1000;
  1311. TS_B = Evt->m_UMSTS % 1000;
  1312. if( m_pDBC->addEvent(
  1313. 0,
  1314. Evt->m_UUID,
  1315. TS_A,
  1316. TS_B,
  1317. EVT_FH_LANDED ) ){
  1318. //Simulate FH Module Event;
  1319. EVENT_CONTAINER *ttt;
  1320. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  1321. if( nnn ){
  1322. nnn->location.lat = 0;
  1323. nnn->location.lon = 0;
  1324. nnn->location.alt = 0;
  1325. sprintf( nnn->missionID, tData->evtUUID );
  1326. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_LANDED, NULL, NULL, nnn );
  1327. if( ttt ) {
  1328. m_pEventHeap->broadcast( ttt );
  1329. } else {
  1330. free( nnn );
  1331. };
  1332. };
  1333. };
  1334. //free( tEVT );
  1335. } else {
  1336. LOG_ERROR("Linked Event Was Not Found!");
  1337. };
  1338. };
  1339. break;
  1340. }
  1341. case EVT_FH_MISSION_BEGIN:{
  1342. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  1343. #ifdef SIMULATE_KERNEL_TARGETS
  1344. m_bLandingTO = SIMULATE_LANDING_TO;
  1345. #endif
  1346. if( tData ) {
  1347. #ifdef SIMULATE_KERNEL_TARGETS
  1348. sprintf( m_cCurrentMission, tData->evtUUID );
  1349. #endif
  1350. DBC_EVT_RECORD * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  1351. if( tEVT ) {
  1352. uint32_t TS_A, TS_B;
  1353. TS_A = Evt->m_UMSTS / 1000;
  1354. TS_B = Evt->m_UMSTS % 1000;
  1355. if( Evt->m_db_id = m_pDBC->addEvent(
  1356. 0,
  1357. Evt->m_UUID,
  1358. TS_A,
  1359. TS_B,
  1360. EVT_FH_FLY_TO,
  1361. 0,
  1362. 0,
  1363. 0,
  1364. 0,
  1365. 0,
  1366. 0,
  1367. tEVT->lat,
  1368. tEVT->lon,
  1369. tEVT->alt,
  1370. tEVT->id,
  1371. tEVT->uuid
  1372. ) ){
  1373. //Simulate FH Module Event;
  1374. EVENT_CONTAINER * ttt;
  1375. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  1376. if( nnn ){
  1377. nnn->location.lat = tEVT->lat;
  1378. nnn->location.lon = tEVT->lon;
  1379. nnn->location.alt = tEVT->alt;
  1380. sprintf( nnn->missionID, tData->evtUUID );
  1381. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_MISSION_BEGIN, NULL, NULL, nnn );
  1382. if( ttt ) {
  1383. m_pEventHeap->broadcast( ttt );
  1384. } else {
  1385. free( nnn );
  1386. };
  1387. };
  1388. };
  1389. free( tEVT );
  1390. } else {
  1391. LOG_ERROR("Linked Event Was Not Found!");
  1392. };
  1393. };
  1394. break;
  1395. };
  1396. };
  1397. break;
  1398. */
  1399. case EVT_CLASS_VIDEO:
  1400. {
  1401. switch (Evt->m_eType)
  1402. {
  1403. case EVT_CAPTURE_DONE:
  1404. case EVT_CAPTURE_END:
  1405. {
  1406. if (Evt->m_pEvtData)
  1407. {
  1408. handleVideo((VIDEO_SEGMENT_INFO *) Evt->m_pEvtData);
  1409. // Dont free Data - it points to static struct of Encoder!!
  1410. memset(Evt->m_pEvtData, 0, sizeof(VIDEO_SEGMENT_INFO));
  1411. Evt->m_pEvtData = NULL;
  1412. #ifdef DEBUG_EXTRA
  1413. LOG("VIDEO_SEGMENT_INFO Cleared!");
  1414. #endif
  1415. };
  1416. break;
  1417. };
  1418. case EVT_VIDEO_FPS_DEGRADED:
  1419. case EVT_VIDEO_FPS_RAISED:
  1420. {
  1421. if (! m_bDebugMode)
  1422. {
  1423. break;
  1424. }
  1425. DBC_EVT_RECORD tRecord;
  1426. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1427. uint32_t TS_A, TS_B;
  1428. TS_A = Evt->m_UMSTS / 1000;
  1429. TS_B = Evt->m_UMSTS % 1000;
  1430. tRecord.node_id = Evt->m_nSourceNodeID;
  1431. tRecord.type = Evt->m_eType;
  1432. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1433. tRecord.ts_s = TS_A;
  1434. tRecord.ts_ms = TS_B;
  1435. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1436. break;
  1437. };
  1438. default: break;
  1439. };
  1440. break;
  1441. };
  1442. case EVT_CLASS_SENSOR_CLD:
  1443. case EVT_CLASS_SENSOR_LGS:
  1444. case EVT_CLASS_SENSOR_LFS:
  1445. case EVT_CLASS_SENSOR_MND:
  1446. case EVT_CLASS_SENSOR_PLS_210:
  1447. case EVT_CLASS_SENSOR_PLS_360:
  1448. case EVT_CLASS_SENSOR_ALS:
  1449. case EVT_CLASS_SENSOR_3D:
  1450. case EVT_CLASS_SENSOR_ACC:
  1451. #ifdef DEBUG_EXTRA
  1452. char tB[128];
  1453. sprintf(tB, "Evt Class: %d Evt Type: %d ", Evt->m_eClass, Evt->m_eType);
  1454. LOG(tB);
  1455. #endif
  1456. switch (Evt->m_eType)
  1457. {
  1458. case EVT_TARGET_1D:
  1459. {
  1460. // startSession();
  1461. preserveSession();
  1462. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1463. if (tDATA != NULL)
  1464. {
  1465. DBC_EVT_RECORD tRecord;
  1466. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1467. uint32_t TS_A, TS_B;
  1468. TS_A = Evt->m_UMSTS / 1000;
  1469. TS_B = Evt->m_UMSTS % 1000;
  1470. tRecord.node_id = Evt->m_nSourceNodeID;
  1471. tRecord.type = Evt->m_eType;
  1472. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1473. tRecord.ts_s = TS_A;
  1474. tRecord.ts_ms = TS_B;
  1475. tRecord.range_digital = tDATA->digital;
  1476. tRecord.ra = tDATA->local_target.r;
  1477. tRecord.area = tDATA->area;
  1478. tRecord.lat = tDATA->global_target.lat;
  1479. tRecord.lon = tDATA->global_target.lon;
  1480. tRecord.alt = tDATA->global_target.alt;
  1481. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1482. };
  1483. break;
  1484. };
  1485. // While Camera Always Records Video on each Absolute Move
  1486. // an Event Must be generated to see video in report
  1487. case EVT_ROI_ACTIVE: // if( !m_bDebugMode ) break;
  1488. case EVT_TARGET_2D_BLACK:
  1489. case EVT_TARGET_2D_WHITE:
  1490. {
  1491. // startSession();
  1492. preserveSession();
  1493. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1494. if (tDATA != NULL)
  1495. {
  1496. DBC_EVT_RECORD tRecord;
  1497. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1498. uint32_t TS_A, TS_B;
  1499. TS_A = Evt->m_UMSTS / 1000;
  1500. TS_B = Evt->m_UMSTS % 1000;
  1501. tRecord.node_id = Evt->m_nSourceNodeID;
  1502. tRecord.type = Evt->m_eType;
  1503. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1504. tRecord.ts_s = TS_A;
  1505. tRecord.ts_ms = TS_B;
  1506. tRecord.lrf_id = tDATA->lrf_id;
  1507. tRecord.range_digital = tDATA->digital;
  1508. tRecord.az = tDATA->local_target.fi;
  1509. tRecord.ra = tDATA->local_target.r;
  1510. tRecord.area = tDATA->area;
  1511. tRecord.lat = tDATA->global_target.lat;
  1512. tRecord.lon = tDATA->global_target.lon;
  1513. tRecord.alt = tDATA->global_target.alt;
  1514. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1515. };
  1516. break;
  1517. };
  1518. case EVT_TARGET_3D_BLACK:
  1519. case EVT_TARGET_3D_WHITE:
  1520. {
  1521. // startSession();
  1522. preserveSession();
  1523. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1524. if (tDATA != NULL)
  1525. {
  1526. DBC_EVT_RECORD tRecord;
  1527. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1528. uint32_t TS_A, TS_B;
  1529. TS_A = Evt->m_UMSTS / 1000;
  1530. TS_B = Evt->m_UMSTS % 1000;
  1531. tRecord.node_id = Evt->m_nSourceNodeID;
  1532. tRecord.type = Evt->m_eType;
  1533. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1534. tRecord.ts_s = TS_A;
  1535. tRecord.ts_ms = TS_B;
  1536. tRecord.lrf_id = tDATA->lrf_id;
  1537. tRecord.range_digital = tDATA->digital;
  1538. tRecord.az = tDATA->local_target.fi;
  1539. tRecord.el = tDATA->local_target.tetta;
  1540. tRecord.ra = tDATA->local_target.r;
  1541. tRecord.area = tDATA->area;
  1542. tRecord.lat = tDATA->global_target.lat;
  1543. tRecord.lon = tDATA->global_target.lon;
  1544. tRecord.alt = tDATA->global_target.alt;
  1545. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1546. };
  1547. break;
  1548. };
  1549. case EVT_ERR_LRF_NO_IRQ:
  1550. case EVT_ERR_LRF_NO_ZERO:
  1551. case EVT_ERR_LRF_ANGLE:
  1552. case EVT_ERR_LRF_VOLTAGE:
  1553. case EVT_ERR_LRF_BROKEN:
  1554. case EVT_LRF_VIS_CHANGED:
  1555. case EVT_ERR_OVERFLOW:
  1556. case EVT_SST_OVERFLOW:
  1557. case EVT_COMMUNICATION_JAMED:
  1558. if (! m_bDebugMode)
  1559. {
  1560. break;
  1561. }
  1562. /* fall through */
  1563. case EVT_OP_ACK_MASK:
  1564. case EVT_OP_NACK_MASK:
  1565. case EVT_OP_ON_MASK:
  1566. case EVT_OP_OFF_MASK:
  1567. case EVT_CONNECTED:
  1568. case EVT_DISCONNECTED:
  1569. {
  1570. DBC_EVT_RECORD tRecord;
  1571. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1572. uint32_t TS_A, TS_B;
  1573. TS_A = Evt->m_UMSTS / 1000;
  1574. TS_B = Evt->m_UMSTS % 1000;
  1575. tRecord.node_id = Evt->m_nSourceNodeID;
  1576. tRecord.type = Evt->m_eType;
  1577. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1578. tRecord.ts_s = TS_A;
  1579. tRecord.ts_ms = TS_B;
  1580. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1581. break;
  1582. };
  1583. case EVT_FLIP:
  1584. {
  1585. if (! m_bDebugMode)
  1586. {
  1587. break;
  1588. }
  1589. DBC_EVT_RECORD tRecord;
  1590. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1591. uint32_t TS_A, TS_B;
  1592. TS_A = Evt->m_UMSTS / 1000;
  1593. TS_B = Evt->m_UMSTS % 1000;
  1594. tRecord.node_id = Evt->m_nSourceNodeID;
  1595. tRecord.type = Evt->m_eType;
  1596. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1597. tRecord.ts_s = TS_A;
  1598. tRecord.ts_ms = TS_B;
  1599. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1600. break;
  1601. };
  1602. default: break;
  1603. };
  1604. break;
  1605. case EVT_CLASS_KRNL_FILTER_LATF:
  1606. {
  1607. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1608. if ((Evt->m_eType > EVT_FLT_LATF_DIFF_DET) || (tDATA == NULL))
  1609. {
  1610. // AUX messages (no additional target data)
  1611. DBC_EVT_RECORD tRecord;
  1612. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1613. uint32_t TS_A, TS_B;
  1614. TS_A = Evt->m_UMSTS / 1000;
  1615. TS_B = Evt->m_UMSTS % 1000;
  1616. tRecord.node_id = m_nServerNodeID;
  1617. tRecord.type = Evt->m_eType;
  1618. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1619. tRecord.ts_s = TS_A;
  1620. tRecord.ts_ms = TS_B;
  1621. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1622. }
  1623. else
  1624. {
  1625. // If First Suspiciouse -> Start session to collect Fliter Messages
  1626. // if( Evt->m_eType == EVT_FLT_LATF_SUSP_START ) startSession();
  1627. // Regular LATF with target Info
  1628. DBC_EVT_RECORD tRecord;
  1629. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1630. uint32_t TS_A, TS_B;
  1631. TS_A = Evt->m_UMSTS / 1000;
  1632. TS_B = Evt->m_UMSTS % 1000;
  1633. tRecord.node_id = Evt->m_nSourceNodeID; // m_nServerNodeID;//??
  1634. tRecord.type = Evt->m_eType;
  1635. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1636. tRecord.ts_s = TS_A;
  1637. tRecord.ts_ms = TS_B;
  1638. tRecord.lrf_id = tDATA->lrf_id;
  1639. tRecord.range_digital = tDATA->digital;
  1640. tRecord.az = tDATA->local_target.fi;
  1641. tRecord.el = tDATA->local_target.tetta;
  1642. tRecord.ra = tDATA->local_target.r;
  1643. tRecord.area = tDATA->area;
  1644. tRecord.lat = 0;
  1645. tRecord.lon = 0;
  1646. tRecord.alt = 0;
  1647. #ifdef DEBUG_EXTRA
  1648. char tB[243];
  1649. 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);
  1650. LOG(tB);
  1651. #endif
  1652. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1653. };
  1654. break;
  1655. };
  1656. case EVT_CLASS_KRNL_FILTER_BUBBLE:
  1657. {
  1658. 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)
  1659. {
  1660. preserveSession();
  1661. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1662. DBC_EVT_RECORD tRecord;
  1663. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1664. uint32_t TS_A, TS_B;
  1665. TS_A = Evt->m_UMSTS / 1000;
  1666. TS_B = Evt->m_UMSTS % 1000;
  1667. tRecord.node_id = Evt->m_nSourceNodeID; // m_nServerNodeID;//??
  1668. tRecord.type = Evt->m_eType;
  1669. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1670. tRecord.ts_s = TS_A;
  1671. tRecord.ts_ms = TS_B;
  1672. tRecord.lrf_id = tDATA->lrf_id;
  1673. tRecord.range_digital = tDATA->digital;
  1674. tRecord.az = tDATA->local_target.fi;
  1675. tRecord.el = tDATA->local_target.tetta;
  1676. tRecord.ra = tDATA->local_target.r;
  1677. tRecord.area = tDATA->area;
  1678. tRecord.lat = 0;
  1679. tRecord.lon = 0;
  1680. tRecord.alt = 0;
  1681. #ifdef DEBUG_EXTRA
  1682. char tB[243];
  1683. 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);
  1684. LOG(tB);
  1685. #endif
  1686. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1687. };
  1688. break;
  1689. };
  1690. case EVT_CLASS_REMOTE_SERVER_CMD:
  1691. {
  1692. switch (Evt->m_eType)
  1693. {
  1694. case EVT_RPC_FRIZE_ACTIVE_B:
  1695. {
  1696. // getFilterId
  1697. break;
  1698. };
  1699. default: break;
  1700. };
  1701. break;
  1702. };
  1703. default: break;
  1704. };
  1705. return true;
  1706. }
  1707. /*
  1708. typedef struct S_VIDEO_SEGMENT_INFO{
  1709. uint32_t stream_id;
  1710. uint32_t streamTSS;
  1711. uint32_t streamTMS;
  1712. DBC_INT_LIST eventIDS;
  1713. DBC_INT_LIST eventFrames;
  1714. DBC_INT_LIST eventTSS;
  1715. DBC_INT_LIST eventTMS;
  1716. char recordMD5[33];
  1717. char recordEXT[6];
  1718. uint8_t streamFPS;
  1719. char scratchPath[PATH_MAX];
  1720. uint32_t dataSize;
  1721. uint32_t db_id;
  1722. }VIDEO_SEGMENT_INFO;
  1723. */
  1724. void mojoKernel::handleVideo(VIDEO_SEGMENT_INFO * ptr)
  1725. {
  1726. if (fileExist(ptr->scratchPath))
  1727. {
  1728. bool bKeepVideoFile = false;
  1729. if (ptr->recordMD5[0])
  1730. {
  1731. if (m_bKeepLocalReport)
  1732. {
  1733. // if wm is debug -> add all to database, else send to server
  1734. ptr->db_id = m_pDBC->addVideoRecord(ptr->stream_id, ptr->recordMD5, ptr->recordEXT, ptr->streamTSS, ptr->streamTMS, ptr->streamFPS);
  1735. if (ptr->db_id)
  1736. {
  1737. #ifdef DEBUG_EXTRA
  1738. LOG("Video File Registered");
  1739. #endif
  1740. for (size_t idx = 0; idx < ptr->eventIDS.size(); idx++)
  1741. {
  1742. #ifdef DEBUG_EXTRA
  1743. LOG("Binding Event to a Video File!");
  1744. #endif
  1745. if (m_pDBC->addVideoEvent(ptr->eventIDS[idx], ptr->db_id, ptr->eventFrames[idx], ptr->eventTSS[idx], ptr->eventTMS[idx]))
  1746. {
  1747. #ifdef DEBUG_EXTRA
  1748. LOG("OK");
  1749. #endif
  1750. };
  1751. };
  1752. CSTR tP = m_pDBC->getCurrentReportDir();
  1753. if (tP)
  1754. {
  1755. char tB[MAX_PATH];
  1756. char dB[MAX_PATH];
  1757. // sprintf( tB, "%s/data/videos/%s.%s", getcwd(dB,MAX_PATH), ptr->recordMD5, ptr->recordEXT );
  1758. sprintf(tB, "%s/data/reports/%s/%s.%s", getcwd(dB, MAX_PATH), tP, ptr->recordMD5, ptr->recordEXT);
  1759. fileCopy(ptr->scratchPath, tB);
  1760. ch_chown(tB, "nobody", "nogroup");
  1761. chmod(tB, 0664);
  1762. #ifdef DEBUG_EXTRA
  1763. LOG("Video Saved");
  1764. #endif
  1765. free(tP);
  1766. }
  1767. else
  1768. {
  1769. LOG_ERROR("Cannot Get Report Path from DB!");
  1770. };
  1771. }
  1772. else
  1773. {
  1774. LOG_ERROR("Failure in registration video!");
  1775. };
  1776. };
  1777. if (m_bWorkWithRemoteServer && (m_pRPC != NULL))
  1778. {
  1779. bKeepVideoFile = m_pRPC->addVideo(ptr);
  1780. }
  1781. };
  1782. if (! bKeepVideoFile)
  1783. {
  1784. unlink(ptr->scratchPath);
  1785. #ifdef DEBUG_EXTRA
  1786. LOG("Video Scratch Deleted!");
  1787. #endif
  1788. };
  1789. } else {
  1790. LOG_ERROR("Video File is Missing!");
  1791. };
  1792. //In case end of the day Force Session Close after Video File is saved
  1793. if ( m_bForceCloseSessionOnEndOfTheDay ) {
  1794. stopSession( m_bForceCloseSessionOnEndOfTheDay );
  1795. m_bForceCloseSessionOnEndOfTheDay = false;
  1796. };
  1797. }
  1798. bool mojoKernel::validateReports(void){
  1799. /// @todo Stephane needs to follow this function...
  1800. if ( m_bKeepLocalReport ){
  1801. if ( !m_bForceCloseSessionOnEndOfTheDay ) {
  1802. uint8_t cd = getLocalDay();
  1803. if ( m_mDateOfToday != cd ) {
  1804. //New Day Started
  1805. m_mDateOfToday = cd;
  1806. if( m_bSessionStarted ){
  1807. m_bForceCloseSessionOnEndOfTheDay = true;
  1808. };
  1809. };
  1810. };
  1811. DBC_SITE_DATA * tSiteData = m_pDBC->getSiteData(1);
  1812. if (tSiteData){
  1813. // Check if settings has been changed in DB
  1814. if (tSiteData->report_cleanup != m_sSiteData.report_cleanup || tSiteData->preserve_empty != m_sSiteData.preserve_empty){
  1815. memcpy(&m_sSiteData, tSiteData, sizeof(DBC_SITE_DATA));
  1816. };
  1817. free(tSiteData);
  1818. } else {
  1819. LOG_ERROR("Error Loading Site DATA!");
  1820. return false;
  1821. };
  1822. if (m_sSiteData.report_cleanup) {
  1823. cleanUpReportDir(m_sSiteData.report_cleanup);
  1824. };
  1825. if (m_sSiteData.preserve_empty) {
  1826. float tT = 100.0f * (1.0f - (1.0f / ((float) m_sSiteData.preserve_empty)));
  1827. #ifdef DEBUG_EXTRA
  1828. char tB[128];
  1829. sprintf(tB, "Check Disk Size: %0.3f %0.3f %d [%d]", m_sSysInfo.disk, tT, m_sSiteData.preserve_empty);
  1830. LOG(tB);
  1831. #endif
  1832. if (m_sSysInfo.disk > tT) {
  1833. #ifdef DEBUG_EXTRA
  1834. LOG("Delete Some Reports!");
  1835. #endif
  1836. // delete some oldest report date
  1837. m_pDBC->deleteReportDate(m_pDBC->getFirstEventTS());
  1838. };
  1839. };
  1840. };
  1841. return true;
  1842. }
  1843. void mojoKernel::backupReports(uint32_t days)
  1844. {
  1845. if (! m_bKeepLocalReport)
  1846. {
  1847. return;
  1848. }
  1849. REPORT_DIRINFO tRDI;// = {0};
  1850. //memset( &tRDI, 0, sizeof(REPORT_DIRINFO)); will be initialized by fillRDF
  1851. uint32_t cTS = (uint32_t) time(NULL);
  1852. cTS -= 86400 * days;
  1853. fillRDF(cTS, tRDI);
  1854. DBC_INT_LIST * TL = m_pDBC->listOlderReportStat(tRDI.YEAR, tRDI.MONTH, tRDI.DAY);
  1855. if (TL)
  1856. {
  1857. for (DBC_INT_LIST::iterator idx = TL->begin(); idx < TL->end(); idx++)
  1858. {
  1859. REPORT_DIRINFO * tRDI = m_pDBC->getReportDirInfo(*idx);
  1860. if (tRDI)
  1861. {
  1862. char tB[MAX_PATH];
  1863. char dB[MAX_PATH];
  1864. sprintf(tB, "%s/data/reports/%s/%s/%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY);
  1865. char tPATH[MAX_PATH];
  1866. sprintf(tPATH, "%s/data/archive/%s_%s_%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY);
  1867. if (fileExist(tB))
  1868. {
  1869. if (! fileExist(tPATH))
  1870. {
  1871. // Try Create Job if sucess then create dir to skip next validation
  1872. if (createCronJob(0, tPATH, cTS))
  1873. {
  1874. // Create Backup Dir
  1875. uid_t uid = getUserUid(m_pDBC->getDataParam(1));
  1876. gid_t gid = getUserGid(m_pDBC->getDataParam(2));
  1877. if (createAccessibleDir(tPATH, STORAGE_DIR_MODE, uid, gid))
  1878. {
  1879. // Create Just One Backup Per Time!
  1880. free(tRDI);
  1881. break;
  1882. };
  1883. };
  1884. }; // else proceed next
  1885. };
  1886. free(tRDI);
  1887. };
  1888. };
  1889. delete(TL);
  1890. };
  1891. }
  1892. void mojoKernel::cleanUpReportDir(uint32_t days)
  1893. {
  1894. // Cleanup Current reports no Backup
  1895. uint32_t oTS = m_pDBC->getFirstEventTS();
  1896. uint32_t cTS = (uint32_t) time(NULL);
  1897. cTS -= 86400 * days;
  1898. #ifdef DEBUG_EXTRA
  1899. char tB[128];
  1900. sprintf(tB, "Oldest %d border %d", oTS, cTS);
  1901. LOG(tB);
  1902. #endif
  1903. if (oTS <= cTS)
  1904. {
  1905. #ifdef DEBUG_EXTRA
  1906. LOG("Deleting Old Reports");
  1907. #endif
  1908. /// @todo Stephane needs to see when this gets called...
  1909. m_pDBC->deleteReportDate(oTS);
  1910. };
  1911. }
  1912. bool mojoKernel::createCronJob(uint8_t job, CSTR param_str, int param_int)
  1913. {
  1914. if (! fileExist("/srv/etc/cron.lock"))
  1915. {
  1916. Json::Value root;
  1917. // Json::Value data;
  1918. switch (job)
  1919. {
  1920. case CRON_JOB_CREATE_BACKUP:
  1921. {
  1922. root["operation"] = CRON_JOB_CREATE_BACKUP;
  1923. // data["number"] = 1;
  1924. root["ts"] = param_int;
  1925. root["backup_dst"] = std::string(param_str);
  1926. break;
  1927. };
  1928. // No default JOB!
  1929. default: return false;
  1930. };
  1931. FILE * fd = fopen("/srv/etc/cron_.json", "w+");
  1932. if (fd)
  1933. {
  1934. if (HUMAN_FRIENDLY_JSON)
  1935. {
  1936. //Json::FastWriter writer;
  1937. //const std::string json_file = writer.write(root);
  1938. //fprintf(fd, json_file.c_str());
  1939. Json::StreamWriterBuilder builder;
  1940. const std::string json_file = Json::writeString(builder, root);
  1941. fprintf(fd, json_file.c_str());
  1942. }
  1943. else
  1944. {
  1945. Json::StreamWriterBuilder builder;
  1946. const std::string json_file = Json::writeString(builder, root);
  1947. fprintf(fd, json_file.c_str());
  1948. };
  1949. fclose(fd);
  1950. return true;
  1951. };
  1952. };
  1953. return false;
  1954. }
  1955. #undef LOG
  1956. #undef LOG_ERROR