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.

1220 lines
47 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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
​
  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. #ifdef LINUX_KERNEL_WATCHDOG
  108. LOG("Disable WD!");
  109. if( m_iWatchdogFd > -1 ) {
  110. char tB[64];
  111. sprintf( tB, "-1\n" );
  112. //ToDo: Make Error Handling here!
  113. write( m_iWatchdogFd, &tB, strlen( tB ) );
  114. };
  115. LOG("WatchDog turned Off!");
  116. #endif
  117. _instance = NULL;
  118. };
  119. bool mojoKernel::create(){
  120. if( m_pSettings != NULL ) {
  121. config_setting_t * pSetting;
  122. int tVal;
  123. const char * tBuf;
  124. char tBuffer[128];
  125. LOG("Getting Settings ...");
  126. if( !config_setting_lookup_string( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, &tBuf ) ){
  127. char * tVar;
  128. char * tTS;
  129. tTS = getTStr(NULL);
  130. tVar = MD5String(tTS);
  131. free(tTS);
  132. sprintf( m_sIdentity, tVar );
  133. pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  134. config_setting_set_string( pSetting, tVar );
  135. free( tVar );
  136. basicSettingsProvider::getInstance()->compromise();
  137. LOG("Server Identity not defined - generating new!");
  138. } else {
  139. sprintf( m_sIdentity, tBuf );
  140. sprintf( tBuffer, "Server Identity Read From Settings: %s", tBuf );
  141. LOG( tBuffer );
  142. };
  143. if( !config_setting_lookup_int( m_pSettings, MOJO_KERNEL_TAG_SITE_ID, &tVal ) ){
  144. tVal = m_nInstanceID = 1;
  145. pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SITE_ID, CONFIG_TYPE_INT );
  146. config_setting_set_int( pSetting, tVal );
  147. basicSettingsProvider::getInstance()->compromise();
  148. LOG("Site ID not defined - using [1] as number!");
  149. } else {
  150. m_nInstanceID = tVal;
  151. sprintf( tBuffer, "App Instance ID Read From Settings: %d", m_nInstanceID );
  152. LOG( tBuffer );
  153. };
  154. } else {
  155. LOG_ERROR("Failed Read Config Settings!");
  156. return false;
  157. };
  158. basicSettingsProvider::getInstance()->validate();
  159. LOG("Register Server ... ");
  160. m_pDBC->registerServer( m_nInstanceID, m_sIdentity, getpid(), &m_nServerID, &m_nServerNodeID );
  161. char tBuffer[128];
  162. sprintf( tBuffer,"Server Registered as [%d] nodeID[%d]",m_nServerID, m_nServerNodeID );
  163. LOG( tBuffer );
  164. DBC_SITE_DATA *tSiteData = m_pDBC->getSiteData( 1 );
  165. if( tSiteData ) {
  166. C3D_WGS84_LOC t;
  167. t.lat = tSiteData->lat;
  168. t.lon = tSiteData->lon;
  169. t.alt = tSiteData->alt;
  170. m_pSystemAnchor = systemAnchor::getInstance( tSiteData->SH, &t, tSiteData->local_binding );
  171. memcpy( &m_sSiteData, tSiteData, sizeof( DBC_SITE_DATA ));
  172. free( tSiteData );
  173. } else {
  174. LOG_ERROR("Error Loading Site DATA!");
  175. C3D_WGS84_LOC t;
  176. t.lat = 0;
  177. t.lon = 0;
  178. t.alt = 0;
  179. memset( &m_sSiteData, 0, sizeof( DBC_SITE_DATA ));
  180. m_pSystemAnchor = systemAnchor::getInstance( false, &t, true );
  181. };
  182. m_bKeepLocalReport = !m_sSiteData.skip_local_report;
  183. m_bDebugMode = m_pDBC->getAppDebugMode();
  184. if ( m_bDebugMode ) m_bKeepLocalReport = true; //Forced in case of debug mode
  185. m_bWorkWithRemoteServer = m_sSiteData.use_remote_server;
  186. if ( m_bWorkWithRemoteServer ) {
  187. #ifdef DEBUG
  188. LOG("Working With Remote Server");
  189. #endif
  190. if ( m_pRPC = new mojoRemoteClient( this ) ) {
  191. if( m_bWorkWithRemoteServer = m_pRPC->create() ){
  192. } else LOG_ERROR("Cannot Connect to Remote Server!");
  193. } else m_bWorkWithRemoteServer = false;
  194. };
  195. //Check date/Time if correct!
  196. // Just in case that we have local reports
  197. if ( m_bKeepLocalReport ) {
  198. #ifdef DEBUG
  199. LOG("Working With Local Reports");
  200. #endif
  201. // On some systems the System Date may be in past cause last session was interrupted by power loose
  202. uint32_t tTS = m_pDBC->getLastEventTS();
  203. if ( tTS ) {
  204. // Have last TS
  205. struct timespec tS = { 0 };
  206. if( clock_gettime( CLOCK_REALTIME, &tS ) == 0 ){
  207. //time_t cTS = time(NULL);
  208. if ( tTS > tS.tv_sec ) {
  209. // System Time is in the Past
  210. LOG("Correcting System Time!");
  211. tS.tv_sec = tTS;
  212. tS.tv_nsec = tTS * 1000000;
  213. if (clock_settime(CLOCK_REALTIME, &tS) != 0) LOG_ERROR("Time Correction Failed!");
  214. };
  215. };
  216. #ifdef DEBUG
  217. } else {
  218. LOG("No Last TS fetched from DB!");
  219. #endif
  220. };
  221. m_bSessionStarted = m_pDBC->getEventSessionStatus( m_nServerNodeID );
  222. stopSession( false );
  223. };
  224. m_pHardwareManager = hardwareManager::getInstance();
  225. loadFilters();
  226. m_pModuleFactory = kernelModuleFactory::getInstance( m_nInstanceID );
  227. if( !m_pModuleFactory ) {
  228. LOG_ERROR("Module Factory Failure!");
  229. return false;
  230. };
  231. m_pModuleFactory->loadModules();
  232. // Read Media Mode ( Video/Pictures )
  233. kernelModuleEmbedVideo *tMO = (kernelModuleEmbedVideo*) m_pModuleFactory->getModule( KERNEL_MODULE_EMBED_VIDEO );
  234. if( tMO ) {
  235. m_bMediaMode = tMO->getMediaMode();
  236. } else {
  237. LOG_ERROR("No Embed Video Processing Module!");
  238. };
  239. m_pNodeFactory = deviceNodeFactory::getInstance();
  240. if( !m_pNodeFactory ){
  241. LOG_ERROR("Node Factory Failure!");
  242. return false;
  243. };
  244. return init();
  245. };
  246. void mojoKernel::startSession(){
  247. m_mSessionProtector.lock();
  248. m_nNoAlarmCounter = 0;
  249. if( !m_bSessionStarted ) {
  250. /*
  251. if( m_bKeepLocalReport ){
  252. DBC_EVT_RECORD tRecord;
  253. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  254. uint64_t TS_64 = getMilliseconds() - 20; // -20 msec before
  255. tRecord.ts_s = TS_64/ 1000;
  256. tRecord.ts_ms = TS_64 % 1000;
  257. tRecord.node_id = m_nServerNodeID;
  258. tRecord.type = EVT_LRF_START_SESSION;
  259. CSTR tUUID = getUniqID_UUID( tRecord.ts_s );
  260. if( tUUID ) {
  261. strncpy(tRecord.uuid, tUUID, 64 );
  262. free( tUUID );
  263. } else {
  264. memset( tRecord.uuid, 0, 65 );
  265. };
  266. m_pDBC->addEvent( &tRecord );
  267. };
  268. */
  269. //Propogate Event
  270. EVENT_CONTAINER *bEVT = createBasicEvent( m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_LRF_START_SESSION, NULL, NULL, NULL);
  271. if ( bEVT ) {
  272. m_pEventHeap->broadcast( bEVT );
  273. };
  274. m_bSessionStarted = true;
  275. };
  276. m_mSessionProtector.unlock();
  277. };
  278. void mojoKernel::stopSession( bool forced ){
  279. m_mSessionProtector.lock();
  280. if( m_bSessionStarted ){
  281. /*
  282. if ( m_bKeepLocalReport ) {
  283. DBC_EVT_RECORD tRecord;
  284. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  285. uint64_t TS_64 = getMilliseconds() + 20; // +20 msec before
  286. tRecord.ts_s = TS_64/ 1000;
  287. tRecord.ts_ms = TS_64 % 1000;
  288. tRecord.node_id = m_nServerNodeID;
  289. tRecord.type = forced ? EVT_LRF_END_SESSION_MA:EVT_LRF_END_SESSION_TO;
  290. CSTR tUUID = getUniqID_UUID( tRecord.ts_s );
  291. if( tUUID ) {
  292. strncpy(tRecord.uuid, tUUID, 64 );
  293. free( tUUID );
  294. } else {
  295. memset( tRecord.uuid, 0, 65 );
  296. };
  297. m_pDBC->addEvent( &tRecord );
  298. };
  299. */
  300. //Propogate Event
  301. EVENT_CONTAINER *bEVT = createBasicEvent( m_nServerNodeID, EVT_CLASS_SYSTEM, (forced?EVT_LRF_END_SESSION_MA:EVT_LRF_END_SESSION_TO), NULL, NULL, NULL);
  302. if ( bEVT ) {
  303. m_pEventHeap->broadcast( bEVT );
  304. };
  305. m_bSessionStarted = false;
  306. };
  307. m_mSessionProtector.unlock();
  308. };
  309. void mojoKernel::validateSession(){
  310. m_mSessionProtector.lock();
  311. if( m_bSessionStarted ){
  312. if( m_nNoAlarmCounter < NOTARGET_SESSION_CLOSE ){
  313. m_nNoAlarmCounter++;
  314. } else {
  315. DBC_EVT_RECORD tRecord;
  316. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  317. uint64_t TS_64 = getMilliseconds() + 20;
  318. tRecord.ts_s = TS_64/ 1000;
  319. tRecord.ts_ms = TS_64 % 1000;
  320. tRecord.node_id = m_nServerNodeID;
  321. tRecord.type = EVT_LRF_END_SESSION_TO;
  322. CSTR tUUID = getUniqID_UUID( tRecord.ts_s );
  323. if( tUUID ) {
  324. strncpy(tRecord.uuid, tUUID, 64 );
  325. free( tUUID );
  326. } else {
  327. memset( tRecord.uuid, 0, 65 );
  328. };
  329. m_pDBC->addEvent( &tRecord );
  330. m_bSessionStarted = false;
  331. };
  332. };
  333. m_mSessionProtector.unlock();
  334. };
  335. void mojoKernel::getSystemUtilization( DBC_SYS_LOAD_INFO *ptr ){
  336. if ( ptr == NULL ) return;
  337. m_mSysInfoProtector.lock();
  338. memcpy( ptr, &m_sSysInfo, sizeof(DBC_SYS_LOAD_INFO) );
  339. m_mSysInfoProtector.unlock();
  340. };
  341. void mojoKernel::m_fThreadProc( void ){
  342. time_t tTimeTSeconds = 0;
  343. m_bThreadStatus = TH_WORKING;
  344. LOG("Mojo Started!");
  345. while ( m_bThreadStatus < TH_ENDED ) {
  346. m_tTimeTSeconds = time( NULL );
  347. //Each Second
  348. if( m_tTimeTSeconds != tTimeTSeconds ){
  349. tTimeTSeconds = m_tTimeTSeconds;
  350. #ifdef LINUX_KERNEL_WATCHDOG
  351. if( m_iWatchdogFd > -1 ) {
  352. char tB[64];
  353. sprintf( tB, "2000\n" );
  354. //ToDo: Make Error Handling here!
  355. write( m_iWatchdogFd, &tB, strlen( tB ) );
  356. };
  357. #endif
  358. m_bDebugMode = m_pDBC->getAppDebugMode();
  359. validateFilters();
  360. validateSession();
  361. validateReports();
  362. //??
  363. if( !m_pHardwareManager->validate() ){
  364. LOG("Reload Mojo Node Due to Memory Consumption!");
  365. m_bThreadStatus = TH_ENDED;
  366. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  367. continue;
  368. };
  369. m_mSysInfoProtector.lock();
  370. m_pHardwareManager->updateSysInfo( &m_sSysInfo );
  371. m_mSysInfoProtector.unlock();
  372. if( m_sSiteData.use_remote_server ) {
  373. if( m_pRPC ){
  374. m_pRPC->validate();
  375. m_bWorkWithRemoteServer = ( m_pRPC->getConnectionStatus() == CS_CONNECTED );
  376. };
  377. };
  378. DBC_PUSHED_CMD * tCMD = m_pDBC->getNodePushedCMD( m_nServerNodeID ); //Kernel
  379. if( tCMD != NULL ) {
  380. m_pDBC->updateNodePushedCMD( tCMD->id, DBC_CMD_STARTED );
  381. switch( tCMD->CMD ){
  382. case CMD_KRNL_RELOAD:{
  383. LOG("Reload Mojo Node!");
  384. m_bThreadStatus = TH_ENDED;
  385. setExitCause( EXIT_CODE_RELOAD_SERVICES );
  386. break;
  387. };
  388. case CMD_KRNL_RESTART:{
  389. LOG("Restart Mojo Node!");
  390. m_bThreadStatus = TH_ENDED;
  391. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  392. break;
  393. };
  394. case CMD_KRNL_FACTORY_RESET:{
  395. LOG("Restart Mojo -> Factory Reset!");
  396. m_bThreadStatus = TH_ENDED;
  397. setExitCause( EXIT_CODE_FACTORY_RESET );
  398. break;
  399. };
  400. case CMD_NODES_RELOAD:{
  401. if( m_pNodeFactory != NULL ) {
  402. m_pNodeFactory->reloadNodes();
  403. };
  404. break;
  405. };
  406. case CMD_NODE_RELOAD:{
  407. uint32_t tNodeID = atoi( tCMD->PARAM );
  408. if( m_pNodeFactory != NULL ) {
  409. m_pNodeFactory->reloadNode( tNodeID );
  410. };
  411. break;
  412. };
  413. case CMD_KRNL_PNP:{
  414. if( m_pHardwareManager!= NULL ) {
  415. LOG_ERROR( "Restart and Detect HardWare!" );
  416. // Must be shutdowned all service that uses hardware prior to detection!
  417. m_bThreadStatus = TH_ENDED;
  418. setExitCause( EXIT_CODE_DETECT_HARDWARE ); // Go -> Detect Hardware in BACKEND
  419. };
  420. break;
  421. };
  422. case CMD_KRNL_UPDATE_IDENT:{
  423. LOG("Restart Mojo -> Update Ident!");
  424. sprintf( m_sIdentity, tCMD->PARAM );
  425. config_setting_remove ( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY );
  426. config_setting_t *pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  427. config_setting_set_string( pSetting, tCMD->PARAM );
  428. basicSettingsProvider::getInstance()->compromise();
  429. basicSettingsProvider::getInstance()->validate();
  430. m_pDBC->updateServerIdentity( m_nServerID, m_sIdentity );
  431. m_bThreadStatus = TH_ENDED;
  432. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  433. break;
  434. };
  435. case CMD_KRNL_REPLACE_DATABASE:{
  436. LOG("Restart Mojo -> Replace Database!");
  437. CSTR tNAME = m_pDBC->getDataParam( 0 );
  438. CSTR tUSER = m_pDBC->getDataParam( 1 );
  439. CSTR tGROUP = m_pDBC->getDataParam( 2 );
  440. if ( tNAME ) {
  441. char tB[256];
  442. sprintf( tB,"./data/%s.db", tCMD->PARAM );
  443. if ( fileExist( tB )) {
  444. LOG("Restart Mojo -> Close Database!");
  445. //Closing Database
  446. delete( m_pDBC );
  447. m_pDBC = NULL;
  448. LOG("Restart Mojo -> Delete Database!");
  449. //unlink( tNAME );
  450. LOG("Restart Mojo -> Copy Database!");
  451. fileCopy( tB, tNAME );
  452. LOG("Restart Mojo -> Chown Database!");
  453. ch_chown( tNAME, tUSER, tGROUP );
  454. LOG("Restart Mojo -> Delete Old Database!");
  455. unlink( tB );
  456. m_bThreadStatus = TH_ENDED;
  457. LOG("Restart Mojo -> Exit!");
  458. setExitCause( EXIT_CODE_NO_DATABASE );//EXIT_CODE_RELOAD_SERVICES );
  459. m_bThreadStatus = TH_ENDED;
  460. } else {
  461. LOG_ERROR("New Database Does not Exists!");
  462. };
  463. //free( tNAME );
  464. //free( tUSER );
  465. //free( tGROUP );
  466. };
  467. break;
  468. };
  469. case CMD_KRNL_SYNC_TIME:{
  470. uint32_t uts = atoi( tCMD->PARAM );
  471. if( uts ){
  472. #if ( __GLIBC__ < 3 ) && ( __GLIBC_MINOR__ < 31 )
  473. time_t ts = (time_t)uts;
  474. if( stime( &ts ) == 0 ){
  475. // Restert Server;
  476. m_bThreadStatus = TH_ENDED;
  477. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  478. };
  479. #else
  480. timespec ts;
  481. ts.tv_sec = (time_t)uts;
  482. ts.tv_nsec = 0;
  483. if( clock_settime( CLOCK_REALTIME, &ts ) == 0 ){
  484. // Restert Server;
  485. m_bThreadStatus = TH_ENDED;
  486. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  487. };
  488. #endif
  489. };
  490. break;
  491. };
  492. case CMD_KRNL_SYSTEM_TASK:{
  493. break;
  494. };
  495. case NODE_FORCE_FINISH_ALARM:{
  496. stopSession();
  497. break;
  498. };
  499. };
  500. free( tCMD );
  501. };
  502. if ( m_pDBC ) m_pDBC->cleanNodePushedCMD();
  503. };
  504. SLEEP(SLEEP_KERNEL_INTERVAL);
  505. };
  506. };
  507. void mojoKernel::loadFilters(){
  508. if( m_pDBC == NULL ) return;
  509. DBC_INT_LIST *tList = m_pDBC->listFilters( m_nServerID );
  510. if( tList != NULL ) {
  511. if( tList->size() > 0 ) {
  512. LOG("Have Filters!");
  513. for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) {
  514. LOG("Load Filter!");
  515. DBC_TARGET_FLITER_DATA *tData = NULL;
  516. tData = m_pDBC->getTargetFilterData( (*idx) );
  517. if( tData != NULL ) {
  518. LOG("Filter Data OK!");
  519. switch( tData->type ) {
  520. case TF_LA_METRO_TRAIN_FILTER:{
  521. LOG("Create LA Train Filter");
  522. kernelEventFilterTrain *tFilter = new kernelEventFilterTrain( (*idx) );
  523. if( tFilter != NULL ) {
  524. if( tFilter->initFilter() ){
  525. m_vTargetFilters.push_back( (kernelEventFilter*) tFilter );
  526. } else {
  527. delete( tFilter );
  528. };
  529. };
  530. break;
  531. };
  532. case TF_AUTHORISED_PEOPLE_FILTER: {
  533. LOG("Create APF ");
  534. kernelEventFilterAPF *tFilter = new kernelEventFilterAPF( (*idx) );
  535. if( tFilter != NULL ) {
  536. if( tFilter->initFilter() ){
  537. m_vTargetFilters.push_back( (kernelEventFilter*) tFilter );
  538. } else {
  539. delete( tFilter );
  540. };
  541. };
  542. break;
  543. };
  544. };
  545. free( tData );
  546. };
  547. };
  548. tList->clear();
  549. };
  550. delete( tList );
  551. #ifdef DEBUG
  552. } else {
  553. LOG("No Target Filters Assigned!");
  554. #endif
  555. };
  556. };
  557. void mojoKernel::unloadFilters(){
  558. if( m_vTargetFilters.size() > 0 ) {
  559. #ifdef DEBUG
  560. LOG("Have Filters to Unload");
  561. #endif
  562. for( vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++ ){
  563. switch( (*idx)->getType() ) {
  564. case TF_LA_METRO_TRAIN_FILTER:{
  565. delete( (kernelEventFilterTrain*)(*idx) );
  566. break;
  567. };
  568. case TF_AUTHORISED_PEOPLE_FILTER:{
  569. delete( (kernelEventFilterAPF*)(*idx) );
  570. break;
  571. };
  572. default:{
  573. char tBuffer[64];
  574. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  575. LOG( tBuffer );
  576. };
  577. };
  578. };
  579. m_vTargetFilters.clear();
  580. LOG("Unload Filters Done!");
  581. #ifdef DEBUG
  582. } else {
  583. LOG("No Filters to Unload!");
  584. #endif
  585. };
  586. };
  587. void mojoKernel::validateFilters(){
  588. if( m_vTargetFilters.size() > 0 ) {
  589. for( vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++ ){
  590. if( !(*idx)->validate() ) {
  591. /*switch ((*idx)->getType()) {
  592. case TF_LA_METRO_TRAIN_FILTER:{
  593. delete( (kernelEventFilterTrain*)(*idx) );
  594. break;
  595. };
  596. case TF_AUTHORISED_PEOPLE_FILTER:{
  597. delete( (kernelEventFilterAPF*)(*idx) );
  598. break;
  599. };
  600. default:{
  601. char tBuffer[64];
  602. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  603. LOG( tBuffer );
  604. };
  605. };
  606. */
  607. };
  608. };
  609. };
  610. };
  611. bool mojoKernel::dispatchEvent( EVENT_CONTAINER *Evt ){
  612. if( Evt == NULL ) return false;
  613. if( m_bWorkWithRemoteServer && ( m_pRPC!=NULL) ) m_pRPC->addEvent( Evt );
  614. if( m_pDBC == NULL) return false;
  615. if( !m_bKeepLocalReport ) return true;
  616. switch (Evt->m_eClass) {
  617. case EVT_CLASS_SYSTEM:
  618. switch (Evt->m_eType) {
  619. case EVT_LRF_START_SESSION:
  620. case EVT_LRF_END_SESSION_TO:
  621. case EVT_LRF_END_SESSION_MA:{
  622. DBC_EVT_RECORD tRecord;
  623. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  624. uint64_t TS_64 = getMilliseconds();// - 20; // 20 msec before
  625. tRecord.ts_s = TS_64/ 1000;
  626. tRecord.ts_ms = TS_64 % 1000;
  627. tRecord.node_id = m_nServerNodeID;
  628. tRecord.type = Evt->m_eType;
  629. CSTR tUUID = getUniqID_UUID( tRecord.ts_s );
  630. if( tUUID ) {
  631. strncpy(tRecord.uuid, tUUID, 64 );
  632. free( tUUID );
  633. } else {
  634. memset( tRecord.uuid, 0, 65 );
  635. };
  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. void mojoKernel::handleVideo( VIDEO_SEGMENT_INFO *ptr ){
  991. if ( fileExist( ptr->scratchPath ) ) {
  992. if( ptr->recordMD5[0] ) {
  993. if( m_bKeepLocalReport ) {
  994. //if wm is debug -> add all to database, else send to server
  995. ptr->db_id = m_pDBC->addVideoRecord( ptr->stream_id, ptr->recordMD5, ptr->recordEXT, ptr->streamTSS, ptr->streamTMS, ptr->streamFPS );
  996. if ( ptr->db_id ) {
  997. #ifdef DEBUG
  998. LOG("Video File Registered");
  999. #endif
  1000. for (int idx = 0; idx < ptr->eventIDS.size(); idx++) {
  1001. #ifdef DEBUG
  1002. LOG("Binding Event to a Video File!");
  1003. #endif
  1004. if( m_pDBC->addVideoEvent( ptr->eventIDS[idx], ptr->db_id, ptr->eventFrames[idx], ptr->eventTSS[idx], ptr->eventTMS[idx] )) {
  1005. #ifdef DEBUG
  1006. LOG("OK");
  1007. #endif
  1008. };
  1009. };
  1010. CSTR tP = m_pDBC->getCurrentReportDir();
  1011. if ( tP ) {
  1012. char tB[MAX_PATH];
  1013. char dB[MAX_PATH];
  1014. //sprintf( tB, "%s/data/videos/%s.%s", getcwd(dB,MAX_PATH), ptr->recordMD5, ptr->recordEXT );
  1015. sprintf(tB, "%s/data/reports/%s/%s.%s", getcwd(dB, MAX_PATH), tP, ptr->recordMD5, ptr->recordEXT );
  1016. fileCopy(ptr->scratchPath, tB);
  1017. ch_chown( tB, "nobody", "nogroup" );
  1018. chmod( tB, 0664 );
  1019. #ifdef DEBUG
  1020. LOG("Video Saved");
  1021. #endif
  1022. free( tP );
  1023. } else {
  1024. LOG_ERROR("Cannot Get Report Path from DB!");
  1025. };
  1026. } else {
  1027. LOG_ERROR("Failure in registration video!");
  1028. };
  1029. };
  1030. };
  1031. unlink( ptr->scratchPath );
  1032. #ifdef DEBUG
  1033. LOG("Video Scratch Deleted!");
  1034. #endif
  1035. } else LOG_ERROR("Video File is Missing!");
  1036. };
  1037. bool mojoKernel::validateReports( void ){
  1038. if ( m_bKeepLocalReport ) {
  1039. DBC_SITE_DATA *tSiteData = m_pDBC->getSiteData(1);
  1040. if( tSiteData ) {
  1041. // Check if settings has been changed in DB
  1042. if ( tSiteData->report_cleanup != m_sSiteData.report_cleanup || tSiteData->preserve_empty != m_sSiteData.preserve_empty ) return false;
  1043. } else {
  1044. LOG_ERROR("Error Loading Site DATA!");
  1045. return false;
  1046. };
  1047. if ( m_sSiteData.report_cleanup ) {
  1048. cleanUpReportDir( m_sSiteData.report_cleanup );
  1049. };
  1050. if ( m_sSiteData.preserve_empty ) {
  1051. float tT = 100.0f * ( 1.0f - ( 1.0f / ((float)m_sSiteData.preserve_empty)));
  1052. #ifdef DEBUG_EXTRA
  1053. char tB[128];
  1054. sprintf( tB, "Check Disk Size: %0.3f %0.3f %d [%d]", m_sSysInfo.disk, tT, m_sSiteData.preserve_empty );
  1055. LOG( tB );
  1056. #endif
  1057. if( m_sSysInfo.disk > tT ){
  1058. // delete some oldest report date
  1059. m_pDBC->deleteReportDate( m_pDBC->getFirstEventTS() );
  1060. };
  1061. };
  1062. }
  1063. return true;
  1064. };
  1065. void mojoKernel::backupReports( uint32_t days ){
  1066. if ( !m_bKeepLocalReport ) return;
  1067. REPORT_DIRINFO tRDI = { 0 };
  1068. uint32_t cTS = (uint32_t)time(NULL);
  1069. cTS -= 86400 * days;
  1070. fillRDF( cTS, tRDI );
  1071. DBC_INT_LIST *TL = m_pDBC->listOlderReportStat( tRDI.YEAR, tRDI.MONTH, tRDI.DAY );
  1072. if ( TL ) {
  1073. for ( DBC_INT_LIST::iterator idx = TL->begin(); idx < TL->end(); idx++ ) {
  1074. REPORT_DIRINFO *tRDI = m_pDBC->getReportDirInfo( *idx );
  1075. if ( tRDI ) {
  1076. char tB[MAX_PATH];
  1077. char dB[MAX_PATH];
  1078. sprintf( tB, "%s/data/reports/%s/%s/%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY );
  1079. char tPATH[MAX_PATH];
  1080. sprintf( tPATH, "%s/data/archive/%s_%s_%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY );
  1081. if ( fileExist( tB ) ) {
  1082. if ( !fileExist( tPATH ) ) {
  1083. // Try Create Job if sucess then create dir to skip next validation
  1084. if( createCronJob( 0, tPATH, cTS ) ){
  1085. // Create Backup Dir
  1086. uid_t uid = getUserUid( m_pDBC->getDataParam( 1 ) );
  1087. gid_t gid = getUserGid( m_pDBC->getDataParam( 2 ) );
  1088. if( createAccessibleDir( tPATH, STORAGE_DIR_MODE, uid, gid ) ){
  1089. // Create Just One Backup Per Time!
  1090. free( tRDI );
  1091. break;
  1092. };
  1093. };
  1094. };// else proceed next
  1095. };
  1096. free( tRDI );
  1097. };
  1098. };
  1099. delete( TL );
  1100. };
  1101. };
  1102. void mojoKernel::cleanUpReportDir( uint32_t days ){
  1103. // Cleanup Current reports no Backup
  1104. uint32_t oTS = m_pDBC->getFirstEventTS();
  1105. uint32_t cTS = (uint32_t)time(NULL);
  1106. cTS -= 86400 * days;
  1107. #ifdef DEBUG_EXTRA
  1108. char tB[128];
  1109. sprintf( tB, "Oldest %d border %d", oTS, cTS );
  1110. LOG( tB );
  1111. #endif
  1112. if ( oTS <= cTS ) {
  1113. m_pDBC->deleteReportDate( oTS );
  1114. };
  1115. };
  1116. bool mojoKernel::createCronJob( uint8_t job, CSTR param_str, int param_int ){
  1117. if ( !fileExist("/srv/etc/cron.lock") ) {
  1118. Json::Value root;
  1119. //Json::Value data;
  1120. switch ( job ) {
  1121. case CRON_JOB_CREATE_BACKUP:{
  1122. root["operation"] = CRON_JOB_CREATE_BACKUP;
  1123. //data["number"] = 1;
  1124. root["ts"] = param_int;
  1125. root["backup_dst"] = std::string( param_str );
  1126. break;
  1127. };
  1128. // No default JOB!
  1129. default: return false;
  1130. };
  1131. FILE *fd = fopen("/srv/etc/cron_.json","w+");
  1132. if ( fd ) {
  1133. if( HUMAN_FRIENDLY_JSON ) {
  1134. Json::FastWriter writer;
  1135. const std::string json_file = writer.write(root);
  1136. fprintf( fd, json_file.c_str() );
  1137. } else {
  1138. Json::StreamWriterBuilder builder;
  1139. const std::string json_file = Json::writeString( builder, root );
  1140. fprintf( fd, json_file.c_str() );
  1141. };
  1142. fclose( fd );
  1143. return true;
  1144. };
  1145. };
  1146. return false;
  1147. };
  1148. #undef LOG
  1149. #undef LOG_ERROR