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.

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