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.

1443 lines
56 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
12 months ago
3 years ago
4 years ago
12 months ago
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
4 years ago
​
​
​
​
​
​
​
​
​
4 years ago
​
3 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
​
​
​
​
​
​
​
​
3 years ago
​
​
3 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
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
2 years ago
4 years ago
2 years ago
2 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
​
​
​
​
​
​
​
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
2 years ago
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
4 years ago
​
2 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
3 years ago
4 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
3 years ago
​
3 years ago
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
2 years ago
2 years ago
2 years ago
​
​
​
​
​
​
​
​
2 years ago
​
​
​
4 years ago
​
4 years ago
​
2 years ago
4 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 "../data/json/json_tool.h"
  17. #include <strings.h>
  18. #include <sys/time.h>
  19. #include <time.h>
  20. #include <json/value.h>
  21. #include <json/writer.h>
  22. #include <json/reader.h>
  23. #include "../common/kernelEventFilterAPF.h"
  24. #include "../common/kernelEventFilterTrain.h"
  25. #include "../common/kernelEventFilterBubble.h"
  26. #include "../common/kernelModuleEmbedVideo.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. char _tBfrErrMSG234[256];\
  36. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  37. kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, _tBfrErrMSG234 );\
  38. }
  39. #else
  40. #define LOG_ERROR( X ) kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, X );
  41. #endif
  42. mojoKernel *mojoKernel::_instance = NULL;
  43. mojoKernel *mojoKernel::getInstance( config_setting_t *ptr ){
  44. if ( _instance == NULL ) {
  45. _instance = new mojoKernel( ptr );
  46. };
  47. return _instance;
  48. };
  49. mojoKernel::mojoKernel( config_setting_t *ptr ):kernelPrototype( ptr ){
  50. m_pEventHeap = basicEventHeap::getInstance();
  51. memset( &m_sSysInfo, 0, sizeof( DBC_SYS_LOAD_INFO ));
  52. m_pShadow = NULL;
  53. m_pSystemAnchor = NULL;
  54. m_bSessionStarted = false;
  55. m_bSessionMustPreserve = false;
  56. m_bFirstSession = true;
  57. m_bWorkWithRemoteServer = false;
  58. m_bKeepLocalReport = true;
  59. m_bMediaMode = 0;
  60. m_pRPC = NULL;
  61. #ifdef EMBED_AUTORESET
  62. m_bEnableAutoReset = false;
  63. m_nSessionCounter = 0;
  64. m_nAutoResetTime = 0;
  65. #endif
  66. //setWM( WM_NONE );
  67. };
  68. mojoKernel::~mojoKernel(){
  69. WaitFinish( true );
  70. stopSession( false );
  71. #ifdef DEBUG
  72. LOG(" Deleting Node Factory...");
  73. #endif
  74. if( m_pNodeFactory != NULL ) delete( m_pNodeFactory );
  75. #ifdef DEBUG
  76. LOG(" Deleting Module Factory...");
  77. #endif
  78. if( m_pModuleFactory != NULL ) delete( m_pModuleFactory );
  79. #ifdef DEBUG
  80. LOG(" Deleting Target Filters...");
  81. #endif
  82. unloadFilters();
  83. if( EXIT_CODE_KERNEL_CLEAR == getExitCause() ) {
  84. #ifdef DEBUG
  85. LOG("Unregister Server");
  86. #endif
  87. m_pDBC->unregisterServer();
  88. #ifdef DEBUG
  89. //LOG("Close DataBase Connection...");
  90. #endif
  91. //if( m_pDBC != NULL ) delete( m_pDBC );//Deleting in Backend
  92. //m_pDBC = NULL;
  93. } else {
  94. if( EXIT_CODE_DETECT_HARDWARE == getExitCause() ) {
  95. #ifdef DEBUG
  96. LOG("HW PNP Detection Requested...");
  97. #endif
  98. //m_pDBC->updateNodePushedCMD( tCMD->id, 30 );
  99. m_pHardwareManager->enumerateHardware();
  100. //m_pDBC->updateNodePushedCMD( tCMD->id, 60 );
  101. };
  102. #ifdef DEBUG
  103. //LOG("Close DataBase Connection...");
  104. #endif
  105. //if( m_pDBC != NULL ) delete( m_pDBC );
  106. //m_pDBC = NULL;
  107. };
  108. delete( m_pHardwareManager );
  109. //while(1){
  110. // char tB[200];
  111. // sprintf( tB, "[%d] Seconds Till System Restart", timeout );
  112. // LOG( "BopBop!" );
  113. // SLEEP(1000);
  114. //};
  115. if ( m_pRPC ) delete( m_pRPC );
  116. #ifdef LINUX_KERNEL_WATCHDOG
  117. LOG("Disable WD!");
  118. if( m_iWatchdogFd > -1 ) {
  119. char tB[64];
  120. sprintf( tB, "-1\n" );
  121. //ToDo: Make Error Handling here!
  122. write( m_iWatchdogFd, &tB, strlen( tB ) );
  123. };
  124. LOG("WatchDog turned Off!");
  125. #endif
  126. _instance = NULL;
  127. };
  128. bool mojoKernel::create(){
  129. m_bSessionStarted = false;
  130. m_bSessionMustPreserve = false;
  131. m_bFirstSession = true;
  132. if( m_pSettings != NULL ) {
  133. config_setting_t * pSetting;
  134. int tVal;
  135. const char * tBuf;
  136. char tBuffer[128];
  137. LOG("Getting Settings ...");
  138. if( !config_setting_lookup_string( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, &tBuf ) ){
  139. char * tVar;
  140. char * tTS;
  141. tTS = getTStr(NULL);
  142. tVar = MD5String(tTS);
  143. free(tTS);
  144. sprintf( m_sIdentity, tVar );
  145. pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  146. config_setting_set_string( pSetting, tVar );
  147. free( tVar );
  148. basicSettingsProvider::getInstance()->compromise();
  149. LOG("Server Identity not defined - generating new!");
  150. } else {
  151. sprintf( m_sIdentity, tBuf );
  152. sprintf( tBuffer, "Server Identity Read From Settings: %s", tBuf );
  153. LOG( tBuffer );
  154. };
  155. if( !config_setting_lookup_int( m_pSettings, MOJO_KERNEL_TAG_SITE_ID, &tVal ) ){
  156. tVal = m_nInstanceID = 1;
  157. pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SITE_ID, CONFIG_TYPE_INT );
  158. config_setting_set_int( pSetting, tVal );
  159. basicSettingsProvider::getInstance()->compromise();
  160. LOG("Site ID not defined - using [1] as number!");
  161. } else {
  162. m_nInstanceID = tVal;
  163. sprintf( tBuffer, "App Instance ID Read From Settings: %d", m_nInstanceID );
  164. LOG( tBuffer );
  165. };
  166. #ifdef EMBED_AUTORESET
  167. if( !config_setting_lookup_int( m_pSettings, "AUTORESET", &tVal ) ){
  168. m_bEnableAutoReset = false;
  169. tVal = m_nAutoResetTime = 0;
  170. pSetting = config_setting_add( m_pSettings, "AUTORESET", CONFIG_TYPE_INT );
  171. config_setting_set_int( pSetting, tVal );
  172. basicSettingsProvider::getInstance()->compromise();
  173. LOG("Autoreset not defined - Set Disabled!");
  174. } else {
  175. m_nAutoResetTime = tVal;
  176. m_bEnableAutoReset = ( m_nAutoResetTime > 0 );
  177. sprintf( tBuffer, "App Autoreset Read From Settings: %d", m_nAutoResetTime );
  178. LOG(tBuffer);
  179. };
  180. #endif
  181. } else {
  182. LOG_ERROR("Failed Read Config Settings!");
  183. return false;
  184. };
  185. basicSettingsProvider::getInstance()->validate();
  186. LOG("Register Server ... ");
  187. m_pDBC->registerServer( m_nInstanceID, m_sIdentity, getpid(), &m_nServerID, &m_nServerNodeID );
  188. char tBuffer[128];
  189. sprintf( tBuffer,"Server Registered as [%d] nodeID[%d]",m_nServerID, m_nServerNodeID );
  190. LOG( tBuffer );
  191. DBC_SITE_DATA *tSiteData = m_pDBC->getSiteData( 1 );
  192. if( tSiteData ) {
  193. C3D_WGS84_LOC t;
  194. t.lat = tSiteData->lat;
  195. t.lon = tSiteData->lon;
  196. t.alt = tSiteData->alt;
  197. m_pSystemAnchor = systemAnchor::getInstance( tSiteData->SH, &t, tSiteData->local_binding );
  198. memcpy( &m_sSiteData, tSiteData, sizeof( DBC_SITE_DATA ));
  199. free( tSiteData );
  200. } else {
  201. LOG_ERROR("Error Loading Site DATA!");
  202. C3D_WGS84_LOC t;
  203. t.lat = 0;
  204. t.lon = 0;
  205. t.alt = 0;
  206. memset( &m_sSiteData, 0, sizeof( DBC_SITE_DATA ));
  207. m_pSystemAnchor = systemAnchor::getInstance( false, &t, true );
  208. };
  209. m_bKeepLocalReport = !m_sSiteData.skip_local_report;
  210. m_bDebugMode = m_pDBC->getAppDebugMode();
  211. if ( m_bDebugMode ) m_bKeepLocalReport = true; //Forced in case of debug mode
  212. m_bWorkWithRemoteServer = m_sSiteData.use_remote_server;
  213. //Check date/Time if correct!
  214. // Just in case that we have local reports
  215. if ( m_bKeepLocalReport ) {
  216. #ifdef DEBUG
  217. LOG("Working With Local Reports");
  218. #endif
  219. // On some systems the System Date may be in past cause last session was interrupted by power loose
  220. uint32_t tTS = m_pDBC->getLastEventTS();
  221. if ( tTS ) {
  222. // Have last TS
  223. struct timespec tS = { 0 };
  224. if( clock_gettime( CLOCK_REALTIME, &tS ) == 0 ){
  225. //time_t cTS = time(NULL);
  226. if ( tTS > tS.tv_sec ) {
  227. // System Time is in the Past
  228. LOG("Correcting System Time!");
  229. tS.tv_sec = tTS;
  230. tS.tv_nsec = tTS * 1000000;
  231. if (clock_settime(CLOCK_REALTIME, &tS) != 0) LOG_ERROR("Time Correction Failed!");
  232. };
  233. };
  234. #ifdef DEBUG
  235. } else {
  236. LOG("No Last TS fetched from DB!");
  237. #endif
  238. };
  239. m_bSessionStarted = m_pDBC->getEventSessionStatus( m_nServerNodeID );
  240. stopSession( false );
  241. };
  242. m_pHardwareManager = hardwareManager::getInstance();
  243. loadFilters();
  244. m_pModuleFactory = kernelModuleFactory::getInstance( m_nInstanceID );
  245. if( !m_pModuleFactory ) {
  246. LOG_ERROR("Module Factory Failure!");
  247. return false;
  248. };
  249. m_pModuleFactory->loadModules();
  250. // Read Media Mode ( Video/Pictures )
  251. kernelModuleEmbedVideo *tMO = (kernelModuleEmbedVideo*) m_pModuleFactory->getModule( KERNEL_MODULE_EMBED_VIDEO );
  252. if( tMO ) {
  253. m_bMediaMode = tMO->getMediaMode();
  254. } else {
  255. LOG_ERROR("No Embed Video Processing Module!");
  256. };
  257. m_pNodeFactory = deviceNodeFactory::getInstance();
  258. if( !m_pNodeFactory ){
  259. LOG_ERROR("Node Factory Failure!");
  260. return false;
  261. };
  262. if ( m_bWorkWithRemoteServer ) {
  263. #ifdef DEBUG
  264. LOG("Working With Remote Server");
  265. #endif
  266. if ( m_pRPC = new mojoRemoteClient( this ) ) {
  267. if( m_bWorkWithRemoteServer = m_pRPC->create() ){
  268. } else LOG_ERROR("Cannot Connect to Remote Server!");
  269. } else m_bWorkWithRemoteServer = false;
  270. };
  271. return init();
  272. };
  273. void mojoKernel::startSession(){
  274. m_mSessionProtector.lock();
  275. m_nNoAlarmCounter = 0;
  276. if( !m_bSessionStarted ) {
  277. if( !m_bFirstSession ) {
  278. if (!m_bSessionMustPreserve) m_pDBC->dropSession();
  279. m_bSessionMustPreserve = false;
  280. } else m_bFirstSession = false;
  281. uint64_t TS_64 = getMilliseconds() - 20; // -20 msec before
  282. uint32_t ts_s = TS_64 / 1000;
  283. uint16_t ts_ms = TS_64 % 1000;
  284. EVENT_CONTAINER *bEVT = createBasicEvent( m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_LRF_START_SESSION, NULL, NULL, NULL, ts_s, ts_ms );
  285. if ( bEVT ) {
  286. m_pEventHeap->broadcast( bEVT );
  287. };
  288. m_bSessionStarted = true;
  289. #ifdef EMBED_AUTORESET
  290. m_nSessionCounter = 0;
  291. #endif
  292. };
  293. m_mSessionProtector.unlock();
  294. };
  295. void mojoKernel::stopSession( bool forced ){
  296. m_mSessionProtector.lock();
  297. if( m_bSessionStarted ){
  298. uint64_t TS_64 = getMilliseconds() + 20; // +20 msec after
  299. uint32_t ts_s = TS_64 / 1000;
  300. uint16_t ts_ms = TS_64 % 1000;
  301. 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 );
  302. if ( bEVT ) {
  303. m_pEventHeap->broadcast( bEVT );
  304. };
  305. m_bSessionStarted = false;
  306. #ifdef EMBED_AUTORESET
  307. m_nSessionCounter = 0;
  308. #endif
  309. };
  310. m_mSessionProtector.unlock();
  311. };
  312. void mojoKernel::validateSession(){
  313. m_mSessionProtector.lock();
  314. if( m_bSessionStarted ){
  315. if( m_nNoAlarmCounter < NOTARGET_SESSION_CLOSE ){
  316. m_nNoAlarmCounter++;
  317. } else {
  318. m_mSessionProtector.unlock();
  319. stopSession( false );
  320. m_mSessionProtector.lock();
  321. };
  322. #ifdef EMBED_AUTORESET
  323. if ( m_bEnableAutoReset ) {
  324. if( m_nSessionCounter < m_nAutoResetTime ) {
  325. m_nSessionCounter ++;
  326. } else {
  327. m_nSessionCounter = 0;
  328. //Reset All Sensors
  329. DBC_INT_LIST *tL = m_pDBC->listNodes(false);
  330. if ( tL ) {
  331. for ( DBC_INT_LIST::iterator idx = tL->begin(); idx < tL->end(); idx++ ) {
  332. m_pDBC->pushMemCMD( *idx, 1, "'A'" );
  333. };
  334. delete( tL );
  335. };
  336. m_nNoAlarmCounter = NOTARGET_SESSION_CLOSE;//Session will be closed on next validation
  337. };
  338. };
  339. #endif
  340. };
  341. m_mSessionProtector.unlock();
  342. };
  343. #ifdef __MOJO_REMOTE_CLIENT_HEADER__
  344. bool mojoKernel::fillMojoInfo( ARGUS_DATA_MOJO_INFO* ptr ){
  345. if ( ptr == NULL ) return false;
  346. memset( ptr, 0, sizeof( ARGUS_DATA_MOJO_INFO ) );
  347. ptr->MOJO_WORK_MODE = ( m_bKeepLocalReport?4:0) | (m_bMediaMode?2:0) | (m_bDebugMode?1:0);
  348. if ( m_vTargetFilters.size() ) {
  349. for( vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++ ){
  350. LOG("check Filter");
  351. if((*idx)->getType() == TF_LA_METRO_TRAIN_FILTER ){
  352. LOG("T Filter Found!");
  353. kernelEventFilterTrain* tFT = (kernelEventFilterTrain*)(*idx);
  354. ptr->UPPER_ID = tFT->getUpperId();
  355. ptr->BOTTOM_ID = tFT->getBottomId();
  356. ptr->MOJO_WORK_MODE |= ( tFT->getMode() << 3 );
  357. break;
  358. };
  359. LOG("Wrong Filter");
  360. };
  361. };
  362. deviceNode **tA=NULL;
  363. uint32_t tCNT = m_pNodeFactory->fetchNodes( ARGUS_ANALOG_STATIC_CAMERA, tA );
  364. if( tCNT ){
  365. //deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[0];
  366. //ptr->CAM_ID = tCAM->getNodeID();
  367. //ptr->CAM_STREAM_ID = tCAM->getCamStream();
  368. //ptr->CAM_ID = tA[0]->getDeviceID();
  369. ptr->CAM_ID = tA[0]->getNodeID();
  370. ptr->CAM_STREAM_ID = tA[0]->getDeviceParam();
  371. } else{
  372. LOG("FIMO Cannot Get ARGUS_ANALOG_STATIC_CAMERA Camera!");
  373. tCNT = m_pNodeFactory->fetchNodes( ARGUS_ANALOG_PTZ_CAMERA, tA );
  374. if( tCNT ){
  375. //deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[0];
  376. //ptr->CAM_ID = tCAM->getNodeID();
  377. //ptr->CAM_STREAM_ID = tCAM->getCamStream();
  378. //ptr->CAM_ID = tA[0]->getDeviceID();
  379. ptr->CAM_ID = tA[0]->getNodeID();
  380. ptr->CAM_STREAM_ID = tA[0]->getDeviceParam();
  381. } else {
  382. LOG("FIMO Cannot Get ARGUS_ANALOG_PTZ_CAMERA Camera!");
  383. tCNT = m_pNodeFactory->fetchNodes( ARGUS_IP_STATIC_CAMERA, tA );
  384. if( tCNT ){
  385. //deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[0];
  386. //ptr->CAM_ID = tCAM->getNodeID();
  387. //ptr->CAM_STREAM_ID = tCAM->getCamStream();
  388. //ptr->CAM_ID = tA[0]->getDeviceID();
  389. ptr->CAM_ID = tA[0]->getNodeID();
  390. ptr->CAM_STREAM_ID = tA[0]->getDeviceParam();
  391. } else {
  392. LOG("FIMO Cannot Get ARGUS_IP_STATIC_CAMERA Camera!");
  393. tCNT = m_pNodeFactory->fetchNodes( ARGUS_IP_PTZ_CAMERA, tA );
  394. if( tCNT ){
  395. //deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[0];
  396. //ptr->CAM_ID = tCAM->getNodeID();
  397. //ptr->CAM_STREAM_ID = tCAM->getCamStream();
  398. //ptr->CAM_ID = tA[0]->getDeviceID();
  399. ptr->CAM_ID = tA[0]->getNodeID();
  400. ptr->CAM_STREAM_ID = tA[0]->getDeviceParam();
  401. } else {
  402. LOG("FIMO Cannot Get ARGUS_IP_PTZ_CAMERA Camera!");
  403. tCNT = m_pNodeFactory->fetchNodes( ARGUS_IP_PRESET_CAMERA, tA );
  404. if( tCNT ){
  405. //deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[0];
  406. //ptr->CAM_ID = tCAM->getNodeID();
  407. //ptr->CAM_STREAM_ID = tCAM->getCamStream();
  408. //ptr->CAM_ID = tA[0]->getDeviceID();
  409. ptr->CAM_ID = tA[0]->getNodeID();
  410. ptr->CAM_STREAM_ID = tA[0]->getDeviceParam();
  411. } else {
  412. LOG("FIMO Cannot Get ARGUS_IP_PRESET_CAMERA Camera!");
  413. tCNT = m_pNodeFactory->fetchNodes( ARGUS_ANALOG_PRESET_CAMERA, tA );
  414. if( tCNT ){
  415. //deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[0];
  416. //ptr->CAM_ID = tCAM->getNodeID();
  417. //ptr->CAM_STREAM_ID = tCAM->getCamStream();
  418. //ptr->CAM_ID = tA[0]->getDeviceID();
  419. ptr->CAM_ID = tA[0]->getNodeID();
  420. ptr->CAM_STREAM_ID = tA[0]->getDeviceParam();
  421. } else {
  422. LOG("FIMO Cannot Get ARGUS_ANALOG_PRESET_CAMERA Camera!");
  423. LOG("No Camera Defined In the system!");
  424. };
  425. };
  426. };
  427. };
  428. };
  429. };
  430. if( tCNT ){
  431. free(tA);
  432. tA = NULL;
  433. };
  434. ptr->CPU_COUNT = m_sSysInfo.CPU_CORES;
  435. ptr->TOT_DISK = m_sSysInfo.T_DISK;
  436. ptr->TOT_MEMO = m_sSysInfo.T_MEM;
  437. strncpy( ptr->TLS_VERSION, _BUILD_STRING_, 64 );
  438. ptr->TLS_VERSION[63]=(char)0;
  439. strncpy( ptr->LLS_VERSION, m_pHardwareManager->getSysInfo()->OS_INFO, 64 );
  440. ptr->LLS_VERSION[63]=(char)0;
  441. LOG("FIMO Overall info");
  442. return true;
  443. };
  444. #endif
  445. void mojoKernel::getSystemUtilization( DBC_SYS_LOAD_INFO *ptr ){
  446. if ( ptr == NULL ) return;
  447. m_mSysInfoProtector.lock();
  448. memcpy( ptr, &m_sSysInfo, sizeof(DBC_SYS_LOAD_INFO) );
  449. m_mSysInfoProtector.unlock();
  450. };
  451. void mojoKernel::m_fThreadProc( void ){
  452. time_t tTimeTSeconds = 0;
  453. m_bThreadStatus = TH_WORKING;
  454. LOG("Mojo Started!");
  455. while ( m_bThreadStatus < TH_ENDED ) {
  456. m_tTimeTSeconds = time( NULL );
  457. //Each Second
  458. if( m_tTimeTSeconds != tTimeTSeconds ){
  459. tTimeTSeconds = m_tTimeTSeconds;
  460. #ifdef LINUX_KERNEL_WATCHDOG
  461. if( m_iWatchdogFd > -1 ) {
  462. char tB[64];
  463. sprintf( tB, "4000\n" );
  464. //ToDo: Make Error Handling here!
  465. write( m_iWatchdogFd, &tB, strlen( tB ) );
  466. };
  467. #endif
  468. m_bDebugMode = m_pDBC->getAppDebugMode();
  469. validateFilters();
  470. validateSession();
  471. validateReports();
  472. //??
  473. if( !m_pHardwareManager->validate() ){
  474. LOG("Reload Mojo Node Due to Memory Consumption!");
  475. m_bThreadStatus = TH_ENDED;
  476. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  477. continue;
  478. };
  479. m_mSysInfoProtector.lock();
  480. m_pHardwareManager->updateSysInfo( &m_sSysInfo );
  481. m_mSysInfoProtector.unlock();
  482. if( m_sSiteData.use_remote_server ) {
  483. if( m_pRPC ){
  484. m_pRPC->validate();
  485. m_bWorkWithRemoteServer = ( m_pRPC->getConnectionStatus() == CS_CONNECTED );
  486. };
  487. };
  488. DBC_PUSHED_CMD * tCMD = m_pDBC->getNodePushedCMD( m_nServerNodeID ); //Kernel
  489. if( tCMD != NULL ) {
  490. m_pDBC->updateNodePushedCMD( tCMD->id, DBC_CMD_STARTED );
  491. switch( tCMD->CMD ){
  492. case CMD_KRNL_RELOAD:{
  493. LOG("Reload Mojo Node!");
  494. m_bThreadStatus = TH_ENDED;
  495. setExitCause( EXIT_CODE_RELOAD_SERVICES );
  496. break;
  497. };
  498. case CMD_KRNL_RESTART:{
  499. LOG("Restart Mojo Node!");
  500. m_bThreadStatus = TH_ENDED;
  501. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  502. break;
  503. };
  504. case CMD_KRNL_FACTORY_RESET:{
  505. LOG("Restart Mojo -> Factory Reset!");
  506. m_bThreadStatus = TH_ENDED;
  507. setExitCause( EXIT_CODE_FACTORY_RESET );
  508. break;
  509. };
  510. case CMD_NODES_RELOAD:{
  511. if( m_pNodeFactory != NULL ) {
  512. m_pNodeFactory->reloadNodes();
  513. };
  514. break;
  515. };
  516. case CMD_NODE_RELOAD:{
  517. uint32_t tNodeID = atoi( tCMD->PARAM );
  518. if( m_pNodeFactory != NULL ) {
  519. m_pNodeFactory->reloadNode( tNodeID );
  520. };
  521. break;
  522. };
  523. case CMD_KRNL_PNP:{
  524. if( m_pHardwareManager!= NULL ) {
  525. LOG_ERROR( "Restart and Detect HardWare!" );
  526. // Must be shutdowned all service that uses hardware prior to detection!
  527. m_bThreadStatus = TH_ENDED;
  528. setExitCause( EXIT_CODE_DETECT_HARDWARE ); // Go -> Detect Hardware in BACKEND
  529. };
  530. break;
  531. };
  532. case CMD_KRNL_UPDATE_IDENT:{
  533. LOG("Restart Mojo -> Update Ident!");
  534. sprintf( m_sIdentity, tCMD->PARAM );
  535. config_setting_remove ( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY );
  536. config_setting_t *pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  537. config_setting_set_string( pSetting, tCMD->PARAM );
  538. basicSettingsProvider::getInstance()->compromise();
  539. basicSettingsProvider::getInstance()->validate();
  540. m_pDBC->updateServerIdentity( m_nServerID, m_sIdentity );
  541. m_bThreadStatus = TH_ENDED;
  542. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  543. break;
  544. };
  545. case CMD_KRNL_REPLACE_DATABASE:{
  546. LOG("Restart Mojo -> Replace Database!");
  547. CSTR tNAME = m_pDBC->getDataParam( 0 );
  548. CSTR tUSER = m_pDBC->getDataParam( 1 );
  549. CSTR tGROUP = m_pDBC->getDataParam( 2 );
  550. if ( tNAME ) {
  551. char tB[256];
  552. sprintf( tB,"./data/%s.db", tCMD->PARAM );
  553. if ( fileExist( tB )) {
  554. LOG("Restart Mojo -> Close Database!");
  555. //Closing Database
  556. delete( m_pDBC );
  557. m_pDBC = NULL;
  558. LOG("Restart Mojo -> Delete Database!");
  559. //unlink( tNAME );
  560. LOG("Restart Mojo -> Copy Database!");
  561. fileCopy( tB, tNAME );
  562. LOG("Restart Mojo -> Chown Database!");
  563. ch_chown( tNAME, tUSER, tGROUP );
  564. LOG("Restart Mojo -> Delete Old Database!");
  565. unlink( tB );
  566. m_bThreadStatus = TH_ENDED;
  567. LOG("Restart Mojo -> Exit!");
  568. setExitCause( EXIT_CODE_NO_DATABASE );//EXIT_CODE_RELOAD_SERVICES );
  569. m_bThreadStatus = TH_ENDED;
  570. } else {
  571. LOG_ERROR("New Database Does not Exists!");
  572. };
  573. //free( tNAME );
  574. //free( tUSER );
  575. //free( tGROUP );
  576. };
  577. break;
  578. };
  579. case CMD_KRNL_SYNC_TIME:{
  580. uint32_t uts = atoi( tCMD->PARAM );
  581. if( uts ){
  582. #if ( __GLIBC__ < 3 ) && ( __GLIBC_MINOR__ < 31 )
  583. time_t ts = (time_t)uts;
  584. if( stime( &ts ) == 0 ){
  585. // Restert Server;
  586. m_bThreadStatus = TH_ENDED;
  587. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  588. };
  589. #else
  590. timespec ts;
  591. ts.tv_sec = (time_t)uts;
  592. ts.tv_nsec = 0;
  593. if( clock_settime( CLOCK_REALTIME, &ts ) == 0 ){
  594. // Restert Server;
  595. m_bThreadStatus = TH_ENDED;
  596. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  597. };
  598. #endif
  599. };
  600. break;
  601. };
  602. case CMD_KRNL_SYSTEM_TASK:{
  603. break;
  604. };
  605. case NODE_FORCE_FINISH_ALARM:{
  606. stopSession();
  607. break;
  608. };
  609. };
  610. free( tCMD );
  611. };
  612. if ( m_pDBC ) m_pDBC->cleanNodePushedCMD();
  613. };
  614. SLEEP(SLEEP_KERNEL_INTERVAL);
  615. };
  616. };
  617. void mojoKernel::loadFilters(){
  618. if( m_pDBC == NULL ) return;
  619. DBC_INT_LIST *tList = m_pDBC->listFilters( m_nServerID );
  620. if( tList != NULL ) {
  621. if( tList->size() > 0 ) {
  622. LOG("Have Filters!");
  623. for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) {
  624. LOG("Load Filter!");
  625. DBC_TARGET_FLITER_DATA *tData = NULL;
  626. tData = m_pDBC->getTargetFilterData( (*idx) );
  627. if( tData != NULL ) {
  628. LOG("Filter Data OK!");
  629. switch( tData->type ) {
  630. case TF_LA_METRO_TRAIN_FILTER:{
  631. LOG("Create LA Train Filter");
  632. kernelEventFilterTrain *tFilter = new kernelEventFilterTrain( (*idx) );
  633. if( tFilter != NULL ) {
  634. if( tFilter->initFilter() ){
  635. m_vTargetFilters.push_back( (kernelEventFilter*) tFilter );
  636. } else {
  637. delete( tFilter );
  638. };
  639. };
  640. break;
  641. };
  642. case TF_AUTHORISED_PEOPLE_FILTER: {
  643. LOG("Create APF ");
  644. kernelEventFilterAPF *tFilter = new kernelEventFilterAPF( (*idx) );
  645. if( tFilter != NULL ) {
  646. if( tFilter->initFilter() ){
  647. m_vTargetFilters.push_back( (kernelEventFilter*) tFilter );
  648. } else {
  649. delete( tFilter );
  650. };
  651. };
  652. break;
  653. };
  654. case TF_BUBBLE_SEGMENTATION_FILTER:{
  655. LOG("Create Bubble Filter ");
  656. kernelEventFilterBubble *tFilter = new kernelEventFilterBubble( (*idx) );
  657. if( tFilter != NULL ) {
  658. if( tFilter->initFilter() ){
  659. m_vTargetFilters.push_back( (kernelEventFilter*) tFilter );
  660. } else {
  661. delete( tFilter );
  662. };
  663. };
  664. break;
  665. };
  666. case TF_YOLO_FILTER:{
  667. break;
  668. };
  669. };
  670. free( tData );
  671. };
  672. };
  673. tList->clear();
  674. };
  675. delete( tList );
  676. #ifdef DEBUG
  677. } else {
  678. LOG("No Target Filters Assigned!");
  679. #endif
  680. };
  681. };
  682. void mojoKernel::unloadFilters(){
  683. if( m_vTargetFilters.size() > 0 ) {
  684. #ifdef DEBUG
  685. LOG("Have Filters to Unload");
  686. #endif
  687. for( vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++ ){
  688. switch( (*idx)->getType() ) {
  689. case TF_LA_METRO_TRAIN_FILTER:{
  690. delete( (kernelEventFilterTrain*)(*idx) );
  691. break;
  692. };
  693. case TF_AUTHORISED_PEOPLE_FILTER:{
  694. delete( (kernelEventFilterAPF*)(*idx) );
  695. break;
  696. };
  697. default:{
  698. char tBuffer[64];
  699. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  700. LOG( tBuffer );
  701. };
  702. };
  703. };
  704. m_vTargetFilters.clear();
  705. LOG("Unload Filters Done!");
  706. #ifdef DEBUG
  707. } else {
  708. LOG("No Filters to Unload!");
  709. #endif
  710. };
  711. };
  712. void mojoKernel::validateFilters(){
  713. if( m_vTargetFilters.size() > 0 ) {
  714. #ifdef DEBUG_EXTRA
  715. LOG("Filters Validation");
  716. #endif
  717. for( vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++ ){
  718. if( !(*idx)->validate() ) {
  719. LOG_ERROR("Filter is Invalid!");
  720. /*switch ((*idx)->getType()) {
  721. case TF_LA_METRO_TRAIN_FILTER:{
  722. delete( (kernelEventFilterTrain*)(*idx) );
  723. break;
  724. };
  725. case TF_AUTHORISED_PEOPLE_FILTER:{
  726. delete( (kernelEventFilterAPF*)(*idx) );
  727. break;
  728. };
  729. default:{
  730. char tBuffer[64];
  731. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  732. LOG( tBuffer );
  733. };
  734. };
  735. */
  736. };
  737. };
  738. };
  739. };
  740. bool mojoKernel::dispatchEvent( EVENT_CONTAINER *Evt ){
  741. if( Evt == NULL ) return false;
  742. if( m_bWorkWithRemoteServer && ( m_pRPC!=NULL) ) m_pRPC->addEvent( Evt );
  743. if( !m_bKeepLocalReport ) return true;
  744. if( m_pDBC == NULL) return false;
  745. switch (Evt->m_eClass) {
  746. case EVT_CLASS_SYSTEM:
  747. switch (Evt->m_eType) {
  748. case EVT_LRF_START_SESSION:
  749. case EVT_LRF_END_SESSION_TO:
  750. case EVT_LRF_END_SESSION_MA:{
  751. DBC_EVT_RECORD tRecord;
  752. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  753. tRecord.ts_s = Evt->m_UMSTS / 1000;
  754. tRecord.ts_ms = Evt->m_UMSTS % 1000;
  755. tRecord.node_id = m_nServerNodeID;
  756. tRecord.type = Evt->m_eType;
  757. strncpy(tRecord.uuid, Evt->m_UUID, 64 );
  758. m_pDBC->addEvent( &tRecord );
  759. break;
  760. };
  761. };
  762. break;
  763. /*
  764. case EVT_CLASS_EXTERNAL_CMD:
  765. switch( Evt->m_eType ){
  766. case EVT_FH_JAMER_ON:{
  767. uint32_t TS_A, TS_B;
  768. TS_A = Evt->m_UMSTS / 1000;
  769. TS_B = Evt->m_UMSTS % 1000;
  770. #ifdef SIMULATE_KERNEL_TARGETS
  771. m_bJamingTO = SIMULATE_JAMER_TO;
  772. #endif
  773. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  774. if( tData ) {
  775. if( m_pDBC->addEvent(
  776. 0,
  777. Evt->m_UUID,
  778. TS_A,
  779. TS_B,
  780. EVT_FH_JAMER_ON,
  781. 0,
  782. 0,
  783. 0,
  784. 0,
  785. 0,
  786. 0,
  787. 0,
  788. 0,
  789. 0,
  790. 0,
  791. tData->evtUUID
  792. ) ){
  793. //Simulate FH Module Event;
  794. EVENT_CONTAINER *ttt;
  795. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  796. if( nnn ){
  797. nnn->location.lat = 0;
  798. nnn->location.lon = 0;
  799. nnn->location.alt = 0;
  800. sprintf( nnn->missionID, tData->evtUUID );
  801. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_JAMER_ON, NULL, NULL, nnn );
  802. if( ttt ) {
  803. m_pEventHeap->broadcast( ttt );
  804. } else {
  805. free( nnn );
  806. };
  807. };
  808. };
  809. free(tData);
  810. };
  811. break;
  812. };
  813. case EVT_FH_MISSION_END:{
  814. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  815. if( tData ) {
  816. //DBC_EVT_DATA * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  817. if( 1 / *tEVT* / ) {
  818. uint32_t TS_A, TS_B;
  819. TS_A = Evt->m_UMSTS / 1000;
  820. TS_B = Evt->m_UMSTS % 1000;
  821. if( m_pDBC->addEvent(
  822. 0,
  823. Evt->m_UUID,
  824. TS_A,
  825. TS_B,
  826. EVT_FH_LANDED ) ){
  827. //Simulate FH Module Event;
  828. EVENT_CONTAINER *ttt;
  829. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  830. if( nnn ){
  831. nnn->location.lat = 0;
  832. nnn->location.lon = 0;
  833. nnn->location.alt = 0;
  834. sprintf( nnn->missionID, tData->evtUUID );
  835. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_LANDED, NULL, NULL, nnn );
  836. if( ttt ) {
  837. m_pEventHeap->broadcast( ttt );
  838. } else {
  839. free( nnn );
  840. };
  841. };
  842. };
  843. //free( tEVT );
  844. } else {
  845. LOG_ERROR("Linked Event Was Not Found!");
  846. };
  847. };
  848. break;
  849. }
  850. case EVT_FH_MISSION_BEGIN:{
  851. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  852. #ifdef SIMULATE_KERNEL_TARGETS
  853. m_bLandingTO = SIMULATE_LANDING_TO;
  854. #endif
  855. if( tData ) {
  856. #ifdef SIMULATE_KERNEL_TARGETS
  857. sprintf( m_cCurrentMission, tData->evtUUID );
  858. #endif
  859. DBC_EVT_RECORD * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  860. if( tEVT ) {
  861. uint32_t TS_A, TS_B;
  862. TS_A = Evt->m_UMSTS / 1000;
  863. TS_B = Evt->m_UMSTS % 1000;
  864. if( Evt->m_db_id = m_pDBC->addEvent(
  865. 0,
  866. Evt->m_UUID,
  867. TS_A,
  868. TS_B,
  869. EVT_FH_FLY_TO,
  870. 0,
  871. 0,
  872. 0,
  873. 0,
  874. 0,
  875. 0,
  876. tEVT->lat,
  877. tEVT->lon,
  878. tEVT->alt,
  879. tEVT->id,
  880. tEVT->uuid
  881. ) ){
  882. //Simulate FH Module Event;
  883. EVENT_CONTAINER * ttt;
  884. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  885. if( nnn ){
  886. nnn->location.lat = tEVT->lat;
  887. nnn->location.lon = tEVT->lon;
  888. nnn->location.alt = tEVT->alt;
  889. sprintf( nnn->missionID, tData->evtUUID );
  890. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_MISSION_BEGIN, NULL, NULL, nnn );
  891. if( ttt ) {
  892. m_pEventHeap->broadcast( ttt );
  893. } else {
  894. free( nnn );
  895. };
  896. };
  897. };
  898. free( tEVT );
  899. } else {
  900. LOG_ERROR("Linked Event Was Not Found!");
  901. };
  902. };
  903. break;
  904. };
  905. };
  906. break;
  907. */
  908. case EVT_CLASS_VIDEO:{
  909. switch( Evt->m_eType ) {
  910. case EVT_CAPTURE_DONE:
  911. case EVT_CAPTURE_END:{
  912. if ( Evt->m_pEvtData ) {
  913. handleVideo( (VIDEO_SEGMENT_INFO *) Evt->m_pEvtData );
  914. //Dont free Data - it points to static struct of Encoder!!
  915. memset(Evt->m_pEvtData, 0, sizeof(VIDEO_SEGMENT_INFO));
  916. Evt->m_pEvtData = NULL;
  917. #ifdef DEBUG_EXTRA
  918. LOG("VIDEO_SEGMENT_INFO Cleared!");
  919. #endif
  920. };
  921. break;
  922. };
  923. case EVT_VIDEO_FPS_DEGRADED:
  924. case EVT_VIDEO_FPS_RAISED:{
  925. if( !m_bDebugMode ) break;
  926. DBC_EVT_RECORD tRecord;
  927. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  928. uint32_t TS_A, TS_B;
  929. TS_A = Evt->m_UMSTS / 1000;
  930. TS_B = Evt->m_UMSTS % 1000;
  931. tRecord.node_id = Evt->m_nSourceNodeID;
  932. tRecord.type = Evt->m_eType;
  933. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  934. tRecord.ts_s = TS_A;
  935. tRecord.ts_ms = TS_B;
  936. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  937. break;
  938. };
  939. };
  940. break;
  941. };
  942. case EVT_CLASS_SENSOR_CLD:
  943. case EVT_CLASS_SENSOR_LGS:
  944. case EVT_CLASS_SENSOR_LFS:
  945. case EVT_CLASS_SENSOR_MND:
  946. case EVT_CLASS_SENSOR_PLS_210:
  947. case EVT_CLASS_SENSOR_PLS_360:
  948. case EVT_CLASS_SENSOR_ALS:
  949. case EVT_CLASS_SENSOR_3D:
  950. case EVT_CLASS_SENSOR_ACC:
  951. #ifdef DEBUG_EXTRA
  952. char tB[128];
  953. sprintf( tB,"Evt Class: %d Evt Type: %d ", Evt->m_eClass ,Evt->m_eType );
  954. LOG( tB );
  955. #endif
  956. switch( Evt->m_eType ) {
  957. case EVT_TARGET_1D:{
  958. //startSession();
  959. preserveSession();
  960. TARGET_DATA *tDATA =(TARGET_DATA *) Evt->m_pEvtData;
  961. if( tDATA != NULL ) {
  962. DBC_EVT_RECORD tRecord;
  963. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  964. uint32_t TS_A, TS_B;
  965. TS_A = Evt->m_UMSTS / 1000;
  966. TS_B = Evt->m_UMSTS % 1000;
  967. tRecord.node_id = Evt->m_nSourceNodeID;
  968. tRecord.type = Evt->m_eType;
  969. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  970. tRecord.ts_s = TS_A;
  971. tRecord.ts_ms = TS_B;
  972. tRecord.range_digital = tDATA->digital;
  973. tRecord.ra = tDATA->local_target.r;
  974. tRecord.area = tDATA->area;
  975. tRecord.lat = tDATA->global_target.lat;
  976. tRecord.lon = tDATA->global_target.lon;
  977. tRecord.alt = tDATA->global_target.alt;
  978. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  979. };
  980. break;
  981. };
  982. // While Camera Always Records Video on each Absolute Move
  983. // an Event Must be generated to see video in report
  984. case EVT_ROI_ACTIVE: //if( !m_bDebugMode ) break;
  985. case EVT_TARGET_2D_BLACK:
  986. case EVT_TARGET_2D_WHITE:{
  987. //startSession();
  988. preserveSession();
  989. TARGET_DATA *tDATA = (TARGET_DATA *)Evt->m_pEvtData;
  990. if( tDATA != NULL ) {
  991. DBC_EVT_RECORD tRecord;
  992. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  993. uint32_t TS_A, TS_B;
  994. TS_A = Evt->m_UMSTS / 1000;
  995. TS_B = Evt->m_UMSTS % 1000;
  996. tRecord.node_id = Evt->m_nSourceNodeID;
  997. tRecord.type = Evt->m_eType;
  998. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  999. tRecord.ts_s = TS_A;
  1000. tRecord.ts_ms = TS_B;
  1001. tRecord.range_digital = tDATA->digital;
  1002. tRecord.az = tDATA->local_target.fi;
  1003. tRecord.ra = tDATA->local_target.r;
  1004. tRecord.area = tDATA->area;
  1005. tRecord.lat = tDATA->global_target.lat;
  1006. tRecord.lon = tDATA->global_target.lon;
  1007. tRecord.alt = tDATA->global_target.alt;
  1008. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1009. };
  1010. break;
  1011. };
  1012. case EVT_TARGET_3D_BLACK:
  1013. case EVT_TARGET_3D_WHITE:{
  1014. //startSession();
  1015. preserveSession();
  1016. TARGET_DATA *tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1017. if( tDATA != NULL ) {
  1018. DBC_EVT_RECORD tRecord;
  1019. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1020. uint32_t TS_A, TS_B;
  1021. TS_A = Evt->m_UMSTS / 1000;
  1022. TS_B = Evt->m_UMSTS % 1000;
  1023. tRecord.node_id = Evt->m_nSourceNodeID;
  1024. tRecord.type = Evt->m_eType;
  1025. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1026. tRecord.ts_s = TS_A;
  1027. tRecord.ts_ms = TS_B;
  1028. tRecord.lrf_id = tDATA->lrf_id;
  1029. tRecord.range_digital = tDATA->digital;
  1030. tRecord.az = tDATA->local_target.fi;
  1031. tRecord.el = tDATA->local_target.tetta;
  1032. tRecord.ra = tDATA->local_target.r;
  1033. tRecord.area = tDATA->area;
  1034. tRecord.lat = tDATA->global_target.lat;
  1035. tRecord.lon = tDATA->global_target.lon;
  1036. tRecord.alt = tDATA->global_target.alt;
  1037. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1038. };
  1039. break;
  1040. };
  1041. case EVT_ERR_LRF_NO_IRQ:
  1042. case EVT_ERR_LRF_NO_ZERO:
  1043. case EVT_ERR_LRF_ANGLE:
  1044. case EVT_ERR_LRF_VOLTAGE:
  1045. case EVT_ERR_LRF_BROKEN:
  1046. case EVT_LRF_VIS_CHANGED:
  1047. case EVT_ERR_OVERFLOW:
  1048. case EVT_SST_OVERFLOW:
  1049. case EVT_COMMUNICATION_JAMED: if( !m_bDebugMode ) break;
  1050. case EVT_OP_ACK_MASK:
  1051. case EVT_OP_NACK_MASK:
  1052. case EVT_OP_ON_MASK:
  1053. case EVT_OP_OFF_MASK:
  1054. case EVT_CONNECTED:
  1055. case EVT_DISCONNECTED:{
  1056. DBC_EVT_RECORD tRecord;
  1057. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1058. uint32_t TS_A, TS_B;
  1059. TS_A = Evt->m_UMSTS / 1000;
  1060. TS_B = Evt->m_UMSTS % 1000;
  1061. tRecord.node_id = Evt->m_nSourceNodeID;
  1062. tRecord.type = Evt->m_eType;
  1063. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1064. tRecord.ts_s = TS_A;
  1065. tRecord.ts_ms = TS_B;
  1066. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1067. break;
  1068. };
  1069. case EVT_FLIP:{
  1070. if( !m_bDebugMode ) break;
  1071. DBC_EVT_RECORD tRecord;
  1072. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1073. uint32_t TS_A, TS_B;
  1074. TS_A = Evt->m_UMSTS / 1000;
  1075. TS_B = Evt->m_UMSTS % 1000;
  1076. tRecord.node_id = Evt->m_nSourceNodeID;
  1077. tRecord.type = Evt->m_eType;
  1078. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1079. tRecord.ts_s = TS_A;
  1080. tRecord.ts_ms = TS_B;
  1081. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1082. break;
  1083. };
  1084. };
  1085. break;
  1086. case EVT_CLASS_KRNL_FILTER_LATF:{
  1087. TARGET_DATA *tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1088. if ( ( Evt->m_eType > EVT_FLT_LATF_DIFF_DET ) || ( tDATA == NULL ) ) {
  1089. //AUX messages (no additional target data)
  1090. DBC_EVT_RECORD tRecord;
  1091. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1092. uint32_t TS_A, TS_B;
  1093. TS_A = Evt->m_UMSTS / 1000;
  1094. TS_B = Evt->m_UMSTS % 1000;
  1095. tRecord.node_id = m_nServerNodeID;
  1096. tRecord.type = Evt->m_eType;
  1097. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1098. tRecord.ts_s = TS_A;
  1099. tRecord.ts_ms = TS_B;
  1100. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1101. } else {
  1102. //If First Suspiciouse -> Start session to collect Fliter Messages
  1103. //if( Evt->m_eType == EVT_FLT_LATF_SUSP_START ) startSession();
  1104. // Regular LATF with target Info
  1105. DBC_EVT_RECORD tRecord;
  1106. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1107. uint32_t TS_A, TS_B;
  1108. TS_A = Evt->m_UMSTS / 1000;
  1109. TS_B = Evt->m_UMSTS % 1000;
  1110. tRecord.node_id = Evt->m_nSourceNodeID;// m_nServerNodeID;//??
  1111. tRecord.type = Evt->m_eType;
  1112. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1113. tRecord.ts_s = TS_A;
  1114. tRecord.ts_ms = TS_B;
  1115. tRecord.lrf_id = tDATA->lrf_id;
  1116. tRecord.range_digital = tDATA->digital;
  1117. tRecord.az = tDATA->local_target.fi;
  1118. tRecord.el = tDATA->local_target.tetta;
  1119. tRecord.ra = tDATA->local_target.r;
  1120. tRecord.area = tDATA->area;
  1121. tRecord.lat = 0;
  1122. tRecord.lon = 0;
  1123. tRecord.alt = 0;
  1124. #ifdef DEBUG_EXTRA
  1125. char tB[243];
  1126. 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 );
  1127. LOG( tB );
  1128. #endif
  1129. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1130. };
  1131. break;
  1132. };
  1133. case EVT_CLASS_KRNL_FILTER_BUBBLE:{
  1134. if( Evt->m_eType == EVT_FLT_BUBBLE_POPED ){
  1135. TARGET_DATA *tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1136. DBC_EVT_RECORD tRecord;
  1137. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1138. uint32_t TS_A, TS_B;
  1139. TS_A = Evt->m_UMSTS / 1000;
  1140. TS_B = Evt->m_UMSTS % 1000;
  1141. tRecord.node_id = Evt->m_nSourceNodeID;// m_nServerNodeID;//??
  1142. tRecord.type = Evt->m_eType;
  1143. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1144. tRecord.ts_s = TS_A;
  1145. tRecord.ts_ms = TS_B;
  1146. tRecord.lrf_id = tDATA->lrf_id;
  1147. tRecord.range_digital = tDATA->digital;
  1148. tRecord.az = tDATA->local_target.fi;
  1149. tRecord.el = tDATA->local_target.tetta;
  1150. tRecord.ra = tDATA->local_target.r;
  1151. tRecord.area = tDATA->area;
  1152. tRecord.lat = 0;
  1153. tRecord.lon = 0;
  1154. tRecord.alt = 0;
  1155. #ifdef DEBUG_EXTRA
  1156. char tB[243];
  1157. 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 );
  1158. LOG( tB );
  1159. #endif
  1160. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1161. };
  1162. break;
  1163. };
  1164. case EVT_CLASS_REMOTE_SERVER_CMD:{
  1165. switch ( Evt->m_eType ) {
  1166. case EVT_RSC_FRIZE_ACTIVE_B:{
  1167. //getFilterId
  1168. break;
  1169. };
  1170. };
  1171. break;
  1172. };
  1173. };
  1174. return true;
  1175. };
  1176. /*
  1177. typedef struct S_VIDEO_SEGMENT_INFO{
  1178. uint32_t stream_id;
  1179. uint32_t streamTSS;
  1180. uint32_t streamTMS;
  1181. DBC_INT_LIST eventIDS;
  1182. DBC_INT_LIST eventFrames;
  1183. DBC_INT_LIST eventTSS;
  1184. DBC_INT_LIST eventTMS;
  1185. char recordMD5[33];
  1186. char recordEXT[6];
  1187. uint8_t streamFPS;
  1188. char scratchPath[PATH_MAX];
  1189. uint32_t dataSize;
  1190. uint32_t db_id;
  1191. }VIDEO_SEGMENT_INFO;
  1192. */
  1193. void mojoKernel::handleVideo( VIDEO_SEGMENT_INFO *ptr ){
  1194. if ( fileExist( ptr->scratchPath ) ) {
  1195. bool bKeepVideoFile = false;
  1196. if( ptr->recordMD5[0] ) {
  1197. if( m_bKeepLocalReport ) {
  1198. //if wm is debug -> add all to database, else send to server
  1199. ptr->db_id = m_pDBC->addVideoRecord( ptr->stream_id, ptr->recordMD5, ptr->recordEXT, ptr->streamTSS, ptr->streamTMS, ptr->streamFPS );
  1200. if ( ptr->db_id ) {
  1201. #ifdef DEBUG_EXTRA
  1202. LOG("Video File Registered");
  1203. #endif
  1204. for (int idx = 0; idx < ptr->eventIDS.size(); idx++) {
  1205. #ifdef DEBUG_EXTRA
  1206. LOG("Binding Event to a Video File!");
  1207. #endif
  1208. if( m_pDBC->addVideoEvent( ptr->eventIDS[idx], ptr->db_id, ptr->eventFrames[idx], ptr->eventTSS[idx], ptr->eventTMS[idx] )) {
  1209. #ifdef DEBUG_EXTRA
  1210. LOG("OK");
  1211. #endif
  1212. };
  1213. };
  1214. CSTR tP = m_pDBC->getCurrentReportDir();
  1215. if ( tP ) {
  1216. char tB[MAX_PATH];
  1217. char dB[MAX_PATH];
  1218. //sprintf( tB, "%s/data/videos/%s.%s", getcwd(dB,MAX_PATH), ptr->recordMD5, ptr->recordEXT );
  1219. sprintf(tB, "%s/data/reports/%s/%s.%s", getcwd(dB, MAX_PATH), tP, ptr->recordMD5, ptr->recordEXT );
  1220. fileCopy( ptr->scratchPath, tB );
  1221. ch_chown( tB, "nobody", "nogroup" );
  1222. chmod( tB, 0664 );
  1223. #ifdef DEBUG_EXTRA
  1224. LOG("Video Saved");
  1225. #endif
  1226. free( tP );
  1227. } else {
  1228. LOG_ERROR("Cannot Get Report Path from DB!");
  1229. };
  1230. } else {
  1231. LOG_ERROR("Failure in registration video!");
  1232. };
  1233. };
  1234. if( m_bWorkWithRemoteServer && ( m_pRPC!=NULL) ) bKeepVideoFile = m_pRPC->addVideo( ptr );
  1235. };
  1236. if( !bKeepVideoFile ){
  1237. unlink( ptr->scratchPath );
  1238. #ifdef DEBUG_EXTRA
  1239. LOG("Video Scratch Deleted!");
  1240. #endif
  1241. };
  1242. } else LOG_ERROR("Video File is Missing!");
  1243. };
  1244. bool mojoKernel::validateReports( void ){
  1245. if ( m_bKeepLocalReport ) {
  1246. DBC_SITE_DATA *tSiteData = m_pDBC->getSiteData(1);
  1247. if( tSiteData ) {
  1248. // Check if settings has been changed in DB
  1249. if ( tSiteData->report_cleanup != m_sSiteData.report_cleanup || tSiteData->preserve_empty != m_sSiteData.preserve_empty ) return false;
  1250. } else {
  1251. LOG_ERROR("Error Loading Site DATA!");
  1252. return false;
  1253. };
  1254. if ( m_sSiteData.report_cleanup ) {
  1255. cleanUpReportDir( m_sSiteData.report_cleanup );
  1256. };
  1257. if ( m_sSiteData.preserve_empty ) {
  1258. float tT = 100.0f * ( 1.0f - ( 1.0f / ((float)m_sSiteData.preserve_empty)));
  1259. #ifdef DEBUG_EXTRA
  1260. char tB[128];
  1261. sprintf( tB, "Check Disk Size: %0.3f %0.3f %d [%d]", m_sSysInfo.disk, tT, m_sSiteData.preserve_empty );
  1262. LOG( tB );
  1263. #endif
  1264. if( m_sSysInfo.disk > tT ){
  1265. #ifdef DEBUG_EXTRA
  1266. LOG("Delete Some Reports!");
  1267. #endif
  1268. // delete some oldest report date
  1269. m_pDBC->deleteReportDate( m_pDBC->getFirstEventTS() );
  1270. };
  1271. };
  1272. }
  1273. return true;
  1274. };
  1275. void mojoKernel::backupReports( uint32_t days ){
  1276. if ( !m_bKeepLocalReport ) return;
  1277. REPORT_DIRINFO tRDI = { 0 };
  1278. uint32_t cTS = (uint32_t)time(NULL);
  1279. cTS -= 86400 * days;
  1280. fillRDF( cTS, tRDI );
  1281. DBC_INT_LIST *TL = m_pDBC->listOlderReportStat( tRDI.YEAR, tRDI.MONTH, tRDI.DAY );
  1282. if ( TL ) {
  1283. for ( DBC_INT_LIST::iterator idx = TL->begin(); idx < TL->end(); idx++ ) {
  1284. REPORT_DIRINFO *tRDI = m_pDBC->getReportDirInfo( *idx );
  1285. if ( tRDI ) {
  1286. char tB[MAX_PATH];
  1287. char dB[MAX_PATH];
  1288. sprintf( tB, "%s/data/reports/%s/%s/%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY );
  1289. char tPATH[MAX_PATH];
  1290. sprintf( tPATH, "%s/data/archive/%s_%s_%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY );
  1291. if ( fileExist( tB ) ) {
  1292. if ( !fileExist( tPATH ) ) {
  1293. // Try Create Job if sucess then create dir to skip next validation
  1294. if( createCronJob( 0, tPATH, cTS ) ){
  1295. // Create Backup Dir
  1296. uid_t uid = getUserUid( m_pDBC->getDataParam( 1 ) );
  1297. gid_t gid = getUserGid( m_pDBC->getDataParam( 2 ) );
  1298. if( createAccessibleDir( tPATH, STORAGE_DIR_MODE, uid, gid ) ){
  1299. // Create Just One Backup Per Time!
  1300. free( tRDI );
  1301. break;
  1302. };
  1303. };
  1304. };// else proceed next
  1305. };
  1306. free( tRDI );
  1307. };
  1308. };
  1309. delete( TL );
  1310. };
  1311. };
  1312. void mojoKernel::cleanUpReportDir( uint32_t days ){
  1313. // Cleanup Current reports no Backup
  1314. uint32_t oTS = m_pDBC->getFirstEventTS();
  1315. uint32_t cTS = (uint32_t)time(NULL);
  1316. cTS -= 86400 * days;
  1317. #ifdef DEBUG_EXTRA
  1318. char tB[128];
  1319. sprintf( tB, "Oldest %d border %d", oTS, cTS );
  1320. LOG( tB );
  1321. #endif
  1322. if ( oTS <= cTS ) {
  1323. #ifdef DEBUG_EXTRA
  1324. LOG("Deleting Old Reports");
  1325. #endif
  1326. m_pDBC->deleteReportDate( oTS );
  1327. };
  1328. };
  1329. bool mojoKernel::createCronJob( uint8_t job, CSTR param_str, int param_int ){
  1330. if ( !fileExist("/srv/etc/cron.lock") ) {
  1331. Json::Value root;
  1332. //Json::Value data;
  1333. switch ( job ) {
  1334. case CRON_JOB_CREATE_BACKUP:{
  1335. root["operation"] = CRON_JOB_CREATE_BACKUP;
  1336. //data["number"] = 1;
  1337. root["ts"] = param_int;
  1338. root["backup_dst"] = std::string( param_str );
  1339. break;
  1340. };
  1341. // No default JOB!
  1342. default: return false;
  1343. };
  1344. FILE *fd = fopen("/srv/etc/cron_.json","w+");
  1345. if ( fd ) {
  1346. if( HUMAN_FRIENDLY_JSON ) {
  1347. Json::FastWriter writer;
  1348. const std::string json_file = writer.write(root);
  1349. fprintf( fd, json_file.c_str() );
  1350. } else {
  1351. Json::StreamWriterBuilder builder;
  1352. const std::string json_file = Json::writeString( builder, root );
  1353. fprintf( fd, json_file.c_str() );
  1354. };
  1355. fclose( fd );
  1356. return true;
  1357. };
  1358. };
  1359. return false;
  1360. };
  1361. #undef LOG
  1362. #undef LOG_ERROR