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.

1246 lines
48 KiB

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