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.

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