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.

1249 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. EVENT_CONTAINER *bEVT = createBasicEvent( m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_LRF_START_SESSION, NULL, NULL, NULL);
  272. if ( bEVT ) {
  273. m_pEventHeap->broadcast( bEVT );
  274. };
  275. m_bSessionStarted = true;
  276. };
  277. m_mSessionProtector.unlock();
  278. };
  279. void mojoKernel::stopSession( bool forced ){
  280. m_mSessionProtector.lock();
  281. if( m_bSessionStarted ){
  282. /*
  283. if ( m_bKeepLocalReport ) {
  284. DBC_EVT_RECORD tRecord;
  285. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  286. uint64_t TS_64 = getMilliseconds() + 20; // +20 msec before
  287. tRecord.ts_s = TS_64/ 1000;
  288. tRecord.ts_ms = TS_64 % 1000;
  289. tRecord.node_id = m_nServerNodeID;
  290. tRecord.type = forced ? EVT_LRF_END_SESSION_MA:EVT_LRF_END_SESSION_TO;
  291. CSTR tUUID = getUniqID_UUID( tRecord.ts_s );
  292. if( tUUID ) {
  293. strncpy(tRecord.uuid, tUUID, 64 );
  294. free( tUUID );
  295. } else {
  296. memset( tRecord.uuid, 0, 65 );
  297. };
  298. m_pDBC->addEvent( &tRecord );
  299. };
  300. */
  301. //Propogate Event
  302. EVENT_CONTAINER *bEVT = createBasicEvent( m_nServerNodeID, EVT_CLASS_SYSTEM, (forced?EVT_LRF_END_SESSION_MA:EVT_LRF_END_SESSION_TO), NULL, NULL, NULL);
  303. if ( bEVT ) {
  304. m_pEventHeap->broadcast( bEVT );
  305. };
  306. m_bSessionStarted = false;
  307. };
  308. m_mSessionProtector.unlock();
  309. };
  310. void mojoKernel::validateSession(){
  311. m_mSessionProtector.lock();
  312. if( m_bSessionStarted ){
  313. if( m_nNoAlarmCounter < NOTARGET_SESSION_CLOSE ){
  314. m_nNoAlarmCounter++;
  315. } else {
  316. DBC_EVT_RECORD tRecord;
  317. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  318. uint64_t TS_64 = getMilliseconds() + 20;
  319. tRecord.ts_s = TS_64/ 1000;
  320. tRecord.ts_ms = TS_64 % 1000;
  321. tRecord.node_id = m_nServerNodeID;
  322. tRecord.type = EVT_LRF_END_SESSION_TO;
  323. CSTR tUUID = getUniqID_UUID( tRecord.ts_s );
  324. if( tUUID ) {
  325. strncpy(tRecord.uuid, tUUID, 64 );
  326. free( tUUID );
  327. } else {
  328. memset( tRecord.uuid, 0, 65 );
  329. };
  330. m_pDBC->addEvent( &tRecord );
  331. m_bSessionStarted = false;
  332. };
  333. };
  334. m_mSessionProtector.unlock();
  335. };
  336. void mojoKernel::getSystemUtilization( DBC_SYS_LOAD_INFO *ptr ){
  337. if ( ptr == NULL ) return;
  338. m_mSysInfoProtector.lock();
  339. memcpy( ptr, &m_sSysInfo, sizeof(DBC_SYS_LOAD_INFO) );
  340. m_mSysInfoProtector.unlock();
  341. };
  342. void mojoKernel::m_fThreadProc( void ){
  343. time_t tTimeTSeconds = 0;
  344. m_bThreadStatus = TH_WORKING;
  345. LOG("Mojo Started!");
  346. while ( m_bThreadStatus < TH_ENDED ) {
  347. m_tTimeTSeconds = time( NULL );
  348. //Each Second
  349. if( m_tTimeTSeconds != tTimeTSeconds ){
  350. tTimeTSeconds = m_tTimeTSeconds;
  351. #ifdef LINUX_KERNEL_WATCHDOG
  352. if( m_iWatchdogFd > -1 ) {
  353. char tB[64];
  354. sprintf( tB, "2000\n" );
  355. //ToDo: Make Error Handling here!
  356. write( m_iWatchdogFd, &tB, strlen( tB ) );
  357. };
  358. #endif
  359. m_bDebugMode = m_pDBC->getAppDebugMode();
  360. validateFilters();
  361. validateSession();
  362. validateReports();
  363. //??
  364. if( !m_pHardwareManager->validate() ){
  365. LOG("Reload Mojo Node Due to Memory Consumption!");
  366. m_bThreadStatus = TH_ENDED;
  367. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  368. continue;
  369. };
  370. m_mSysInfoProtector.lock();
  371. m_pHardwareManager->updateSysInfo( &m_sSysInfo );
  372. m_mSysInfoProtector.unlock();
  373. if( m_sSiteData.use_remote_server ) {
  374. if( m_pRPC ){
  375. m_pRPC->validate();
  376. m_bWorkWithRemoteServer = ( m_pRPC->getConnectionStatus() == CS_CONNECTED );
  377. };
  378. };
  379. DBC_PUSHED_CMD * tCMD = m_pDBC->getNodePushedCMD( m_nServerNodeID ); //Kernel
  380. if( tCMD != NULL ) {
  381. m_pDBC->updateNodePushedCMD( tCMD->id, DBC_CMD_STARTED );
  382. switch( tCMD->CMD ){
  383. case CMD_KRNL_RELOAD:{
  384. LOG("Reload Mojo Node!");
  385. m_bThreadStatus = TH_ENDED;
  386. setExitCause( EXIT_CODE_RELOAD_SERVICES );
  387. break;
  388. };
  389. case CMD_KRNL_RESTART:{
  390. LOG("Restart Mojo Node!");
  391. m_bThreadStatus = TH_ENDED;
  392. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  393. break;
  394. };
  395. case CMD_KRNL_FACTORY_RESET:{
  396. LOG("Restart Mojo -> Factory Reset!");
  397. m_bThreadStatus = TH_ENDED;
  398. setExitCause( EXIT_CODE_FACTORY_RESET );
  399. break;
  400. };
  401. case CMD_NODES_RELOAD:{
  402. if( m_pNodeFactory != NULL ) {
  403. m_pNodeFactory->reloadNodes();
  404. };
  405. break;
  406. };
  407. case CMD_NODE_RELOAD:{
  408. uint32_t tNodeID = atoi( tCMD->PARAM );
  409. if( m_pNodeFactory != NULL ) {
  410. m_pNodeFactory->reloadNode( tNodeID );
  411. };
  412. break;
  413. };
  414. case CMD_KRNL_PNP:{
  415. if( m_pHardwareManager!= NULL ) {
  416. LOG_ERROR( "Restart and Detect HardWare!" );
  417. // Must be shutdowned all service that uses hardware prior to detection!
  418. m_bThreadStatus = TH_ENDED;
  419. setExitCause( EXIT_CODE_DETECT_HARDWARE ); // Go -> Detect Hardware in BACKEND
  420. };
  421. break;
  422. };
  423. case CMD_KRNL_UPDATE_IDENT:{
  424. LOG("Restart Mojo -> Update Ident!");
  425. sprintf( m_sIdentity, tCMD->PARAM );
  426. config_setting_remove ( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY );
  427. config_setting_t *pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  428. config_setting_set_string( pSetting, tCMD->PARAM );
  429. basicSettingsProvider::getInstance()->compromise();
  430. basicSettingsProvider::getInstance()->validate();
  431. m_pDBC->updateServerIdentity( m_nServerID, m_sIdentity );
  432. m_bThreadStatus = TH_ENDED;
  433. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  434. break;
  435. };
  436. case CMD_KRNL_REPLACE_DATABASE:{
  437. LOG("Restart Mojo -> Replace Database!");
  438. CSTR tNAME = m_pDBC->getDataParam( 0 );
  439. CSTR tUSER = m_pDBC->getDataParam( 1 );
  440. CSTR tGROUP = m_pDBC->getDataParam( 2 );
  441. if ( tNAME ) {
  442. char tB[256];
  443. sprintf( tB,"./data/%s.db", tCMD->PARAM );
  444. if ( fileExist( tB )) {
  445. LOG("Restart Mojo -> Close Database!");
  446. //Closing Database
  447. delete( m_pDBC );
  448. m_pDBC = NULL;
  449. LOG("Restart Mojo -> Delete Database!");
  450. //unlink( tNAME );
  451. LOG("Restart Mojo -> Copy Database!");
  452. fileCopy( tB, tNAME );
  453. LOG("Restart Mojo -> Chown Database!");
  454. ch_chown( tNAME, tUSER, tGROUP );
  455. LOG("Restart Mojo -> Delete Old Database!");
  456. unlink( tB );
  457. m_bThreadStatus = TH_ENDED;
  458. LOG("Restart Mojo -> Exit!");
  459. setExitCause( EXIT_CODE_NO_DATABASE );//EXIT_CODE_RELOAD_SERVICES );
  460. m_bThreadStatus = TH_ENDED;
  461. } else {
  462. LOG_ERROR("New Database Does not Exists!");
  463. };
  464. //free( tNAME );
  465. //free( tUSER );
  466. //free( tGROUP );
  467. };
  468. break;
  469. };
  470. case CMD_KRNL_SYNC_TIME:{
  471. uint32_t uts = atoi( tCMD->PARAM );
  472. if( uts ){
  473. #if ( __GLIBC__ < 3 ) && ( __GLIBC_MINOR__ < 31 )
  474. time_t ts = (time_t)uts;
  475. if( stime( &ts ) == 0 ){
  476. // Restert Server;
  477. m_bThreadStatus = TH_ENDED;
  478. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  479. };
  480. #else
  481. timespec ts;
  482. ts.tv_sec = (time_t)uts;
  483. ts.tv_nsec = 0;
  484. if( clock_settime( CLOCK_REALTIME, &ts ) == 0 ){
  485. // Restert Server;
  486. m_bThreadStatus = TH_ENDED;
  487. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  488. };
  489. #endif
  490. };
  491. break;
  492. };
  493. case CMD_KRNL_SYSTEM_TASK:{
  494. break;
  495. };
  496. case NODE_FORCE_FINISH_ALARM:{
  497. stopSession();
  498. break;
  499. };
  500. };
  501. free( tCMD );
  502. };
  503. if ( m_pDBC ) m_pDBC->cleanNodePushedCMD();
  504. };
  505. SLEEP(SLEEP_KERNEL_INTERVAL);
  506. };
  507. };
  508. void mojoKernel::loadFilters(){
  509. if( m_pDBC == NULL ) return;
  510. DBC_INT_LIST *tList = m_pDBC->listFilters( m_nServerID );
  511. if( tList != NULL ) {
  512. if( tList->size() > 0 ) {
  513. LOG("Have Filters!");
  514. for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) {
  515. LOG("Load Filter!");
  516. DBC_TARGET_FLITER_DATA *tData = NULL;
  517. tData = m_pDBC->getTargetFilterData( (*idx) );
  518. if( tData != NULL ) {
  519. LOG("Filter Data OK!");
  520. switch( tData->type ) {
  521. case TF_LA_METRO_TRAIN_FILTER:{
  522. LOG("Create LA Train Filter");
  523. kernelEventFilterTrain *tFilter = new kernelEventFilterTrain( (*idx) );
  524. if( tFilter != NULL ) {
  525. if( tFilter->initFilter() ){
  526. m_vTargetFilters.push_back( (kernelEventFilter*) tFilter );
  527. } else {
  528. delete( tFilter );
  529. };
  530. };
  531. break;
  532. };
  533. case TF_AUTHORISED_PEOPLE_FILTER: {
  534. LOG("Create APF ");
  535. kernelEventFilterAPF *tFilter = new kernelEventFilterAPF( (*idx) );
  536. if( tFilter != NULL ) {
  537. if( tFilter->initFilter() ){
  538. m_vTargetFilters.push_back( (kernelEventFilter*) tFilter );
  539. } else {
  540. delete( tFilter );
  541. };
  542. };
  543. break;
  544. };
  545. };
  546. free( tData );
  547. };
  548. };
  549. tList->clear();
  550. };
  551. delete( tList );
  552. #ifdef DEBUG
  553. } else {
  554. LOG("No Target Filters Assigned!");
  555. #endif
  556. };
  557. };
  558. void mojoKernel::unloadFilters(){
  559. if( m_vTargetFilters.size() > 0 ) {
  560. #ifdef DEBUG
  561. LOG("Have Filters to Unload");
  562. #endif
  563. for( vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++ ){
  564. switch( (*idx)->getType() ) {
  565. case TF_LA_METRO_TRAIN_FILTER:{
  566. delete( (kernelEventFilterTrain*)(*idx) );
  567. break;
  568. };
  569. case TF_AUTHORISED_PEOPLE_FILTER:{
  570. delete( (kernelEventFilterAPF*)(*idx) );
  571. break;
  572. };
  573. default:{
  574. char tBuffer[64];
  575. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  576. LOG( tBuffer );
  577. };
  578. };
  579. };
  580. m_vTargetFilters.clear();
  581. LOG("Unload Filters Done!");
  582. #ifdef DEBUG
  583. } else {
  584. LOG("No Filters to Unload!");
  585. #endif
  586. };
  587. };
  588. void mojoKernel::validateFilters(){
  589. if( m_vTargetFilters.size() > 0 ) {
  590. for( vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++ ){
  591. if( !(*idx)->validate() ) {
  592. /*switch ((*idx)->getType()) {
  593. case TF_LA_METRO_TRAIN_FILTER:{
  594. delete( (kernelEventFilterTrain*)(*idx) );
  595. break;
  596. };
  597. case TF_AUTHORISED_PEOPLE_FILTER:{
  598. delete( (kernelEventFilterAPF*)(*idx) );
  599. break;
  600. };
  601. default:{
  602. char tBuffer[64];
  603. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  604. LOG( tBuffer );
  605. };
  606. };
  607. */
  608. };
  609. };
  610. };
  611. };
  612. bool mojoKernel::dispatchEvent( EVENT_CONTAINER *Evt ){
  613. if( Evt == NULL ) return false;
  614. if( m_bWorkWithRemoteServer && ( m_pRPC!=NULL) ) m_pRPC->addEvent( Evt );
  615. if( !m_bKeepLocalReport ) return true;
  616. if( m_pDBC == NULL) return false;
  617. switch (Evt->m_eClass) {
  618. case EVT_CLASS_SYSTEM:
  619. switch (Evt->m_eType) {
  620. case EVT_LRF_START_SESSION:
  621. case EVT_LRF_END_SESSION_TO:
  622. case EVT_LRF_END_SESSION_MA:{
  623. DBC_EVT_RECORD tRecord;
  624. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  625. uint64_t TS_64 = getMilliseconds();// - 20; // 20 msec before
  626. tRecord.ts_s = TS_64/ 1000;
  627. tRecord.ts_ms = TS_64 % 1000;
  628. tRecord.node_id = m_nServerNodeID;
  629. tRecord.type = Evt->m_eType;
  630. CSTR tUUID = getUniqID_UUID( tRecord.ts_s );
  631. if( tUUID ) {
  632. strncpy(tRecord.uuid, tUUID, 64 );
  633. free( tUUID );
  634. } else {
  635. memset( tRecord.uuid, 0, 65 );
  636. };
  637. m_pDBC->addEvent( &tRecord );
  638. break;
  639. };
  640. };
  641. break;
  642. /*
  643. case EVT_CLASS_EXTERNAL_CMD:
  644. switch( Evt->m_eType ){
  645. case EVT_FH_JAMER_ON:{
  646. uint32_t TS_A, TS_B;
  647. TS_A = Evt->m_UMSTS / 1000;
  648. TS_B = Evt->m_UMSTS % 1000;
  649. #ifdef SIMULATE_KERNEL_TARGETS
  650. m_bJamingTO = SIMULATE_JAMER_TO;
  651. #endif
  652. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  653. if( tData ) {
  654. if( m_pDBC->addEvent(
  655. 0,
  656. Evt->m_UUID,
  657. TS_A,
  658. TS_B,
  659. EVT_FH_JAMER_ON,
  660. 0,
  661. 0,
  662. 0,
  663. 0,
  664. 0,
  665. 0,
  666. 0,
  667. 0,
  668. 0,
  669. 0,
  670. tData->evtUUID
  671. ) ){
  672. //Simulate FH Module Event;
  673. EVENT_CONTAINER *ttt;
  674. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  675. if( nnn ){
  676. nnn->location.lat = 0;
  677. nnn->location.lon = 0;
  678. nnn->location.alt = 0;
  679. sprintf( nnn->missionID, tData->evtUUID );
  680. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_JAMER_ON, NULL, NULL, nnn );
  681. if( ttt ) {
  682. m_pEventHeap->broadcast( ttt );
  683. } else {
  684. free( nnn );
  685. };
  686. };
  687. };
  688. free(tData);
  689. };
  690. break;
  691. };
  692. case EVT_FH_MISSION_END:{
  693. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  694. if( tData ) {
  695. //DBC_EVT_DATA * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  696. if( 1 / *tEVT* / ) {
  697. uint32_t TS_A, TS_B;
  698. TS_A = Evt->m_UMSTS / 1000;
  699. TS_B = Evt->m_UMSTS % 1000;
  700. if( m_pDBC->addEvent(
  701. 0,
  702. Evt->m_UUID,
  703. TS_A,
  704. TS_B,
  705. EVT_FH_LANDED ) ){
  706. //Simulate FH Module Event;
  707. EVENT_CONTAINER *ttt;
  708. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  709. if( nnn ){
  710. nnn->location.lat = 0;
  711. nnn->location.lon = 0;
  712. nnn->location.alt = 0;
  713. sprintf( nnn->missionID, tData->evtUUID );
  714. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_LANDED, NULL, NULL, nnn );
  715. if( ttt ) {
  716. m_pEventHeap->broadcast( ttt );
  717. } else {
  718. free( nnn );
  719. };
  720. };
  721. };
  722. //free( tEVT );
  723. } else {
  724. LOG_ERROR("Linked Event Was Not Found!");
  725. };
  726. };
  727. break;
  728. }
  729. case EVT_FH_MISSION_BEGIN:{
  730. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  731. #ifdef SIMULATE_KERNEL_TARGETS
  732. m_bLandingTO = SIMULATE_LANDING_TO;
  733. #endif
  734. if( tData ) {
  735. #ifdef SIMULATE_KERNEL_TARGETS
  736. sprintf( m_cCurrentMission, tData->evtUUID );
  737. #endif
  738. DBC_EVT_RECORD * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  739. if( tEVT ) {
  740. uint32_t TS_A, TS_B;
  741. TS_A = Evt->m_UMSTS / 1000;
  742. TS_B = Evt->m_UMSTS % 1000;
  743. if( Evt->m_db_id = m_pDBC->addEvent(
  744. 0,
  745. Evt->m_UUID,
  746. TS_A,
  747. TS_B,
  748. EVT_FH_FLY_TO,
  749. 0,
  750. 0,
  751. 0,
  752. 0,
  753. 0,
  754. 0,
  755. tEVT->lat,
  756. tEVT->lon,
  757. tEVT->alt,
  758. tEVT->id,
  759. tEVT->uuid
  760. ) ){
  761. //Simulate FH Module Event;
  762. EVENT_CONTAINER * ttt;
  763. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  764. if( nnn ){
  765. nnn->location.lat = tEVT->lat;
  766. nnn->location.lon = tEVT->lon;
  767. nnn->location.alt = tEVT->alt;
  768. sprintf( nnn->missionID, tData->evtUUID );
  769. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_MISSION_BEGIN, NULL, NULL, nnn );
  770. if( ttt ) {
  771. m_pEventHeap->broadcast( ttt );
  772. } else {
  773. free( nnn );
  774. };
  775. };
  776. };
  777. free( tEVT );
  778. } else {
  779. LOG_ERROR("Linked Event Was Not Found!");
  780. };
  781. };
  782. break;
  783. };
  784. };
  785. break;
  786. */
  787. case EVT_CLASS_VIDEO:{
  788. switch( Evt->m_eType ) {
  789. case EVT_CAPTURE_DONE:
  790. case EVT_CAPTURE_END:{
  791. if ( Evt->m_pEvtData ) {
  792. handleVideo( (VIDEO_SEGMENT_INFO *) Evt->m_pEvtData );
  793. memset(Evt->m_pEvtData, 0, sizeof(VIDEO_SEGMENT_INFO));
  794. Evt->m_pEvtData = NULL;
  795. #ifdef DEBUG
  796. LOG("VIDEO_SEGMENT_INFO Cleared!");
  797. #endif
  798. };
  799. break;
  800. };
  801. case EVT_VIDEO_FPS_DEGRADED:
  802. case EVT_VIDEO_FPS_RAISED:{
  803. if( !m_bDebugMode ) break;
  804. DBC_EVT_RECORD tRecord;
  805. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  806. uint32_t TS_A, TS_B;
  807. TS_A = Evt->m_UMSTS / 1000;
  808. TS_B = Evt->m_UMSTS % 1000;
  809. tRecord.node_id = Evt->m_nSourceNodeID;
  810. tRecord.type = Evt->m_eType;
  811. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  812. tRecord.ts_s = TS_A;
  813. tRecord.ts_ms = TS_B;
  814. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  815. break;
  816. };
  817. };
  818. break;
  819. };
  820. case EVT_CLASS_SENSOR_CLD:
  821. case EVT_CLASS_SENSOR_LGS:
  822. case EVT_CLASS_SENSOR_LFS:
  823. case EVT_CLASS_SENSOR_MND:
  824. case EVT_CLASS_SENSOR_PLS_210:
  825. case EVT_CLASS_SENSOR_PLS_360:
  826. case EVT_CLASS_SENSOR_ALS:
  827. case EVT_CLASS_SENSOR_3D:
  828. case EVT_CLASS_SENSOR_ACC:
  829. #ifdef DEBUG_EXTRA
  830. char tB[128];
  831. sprintf( tB,"Evt Class: %d Evt Type: %d ", Evt->m_eClass ,Evt->m_eType );
  832. LOG( tB );
  833. #endif
  834. switch( Evt->m_eType ) {
  835. case EVT_TARGET_1D:{
  836. //startSession();
  837. TARGET_DATA *tDATA =(TARGET_DATA *) Evt->m_pEvtData;
  838. if( tDATA != NULL ) {
  839. DBC_EVT_RECORD tRecord;
  840. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  841. uint32_t TS_A, TS_B;
  842. TS_A = Evt->m_UMSTS / 1000;
  843. TS_B = Evt->m_UMSTS % 1000;
  844. tRecord.node_id = Evt->m_nSourceNodeID;
  845. tRecord.type = Evt->m_eType;
  846. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  847. tRecord.ts_s = TS_A;
  848. tRecord.ts_ms = TS_B;
  849. tRecord.range_digital = tDATA->digital;
  850. tRecord.ra = tDATA->local_target.r;
  851. tRecord.area = tDATA->area;
  852. tRecord.lat = tDATA->global_target.lat;
  853. tRecord.lon = tDATA->global_target.lon;
  854. tRecord.alt = tDATA->global_target.alt;
  855. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  856. };
  857. break;
  858. };
  859. // While Camera Always Records Video on each Absolute Move
  860. // an Event Must be generated to see video in report
  861. case EVT_ROI_ACTIVE: //if( !m_bDebugMode ) break;
  862. case EVT_TARGET_2D_BLACK:
  863. case EVT_TARGET_2D_WHITE:{
  864. //startSession();
  865. TARGET_DATA *tDATA = (TARGET_DATA *)Evt->m_pEvtData;
  866. if( tDATA != NULL ) {
  867. DBC_EVT_RECORD tRecord;
  868. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  869. uint32_t TS_A, TS_B;
  870. TS_A = Evt->m_UMSTS / 1000;
  871. TS_B = Evt->m_UMSTS % 1000;
  872. tRecord.node_id = Evt->m_nSourceNodeID;
  873. tRecord.type = Evt->m_eType;
  874. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  875. tRecord.ts_s = TS_A;
  876. tRecord.ts_ms = TS_B;
  877. tRecord.range_digital = tDATA->digital;
  878. tRecord.az = tDATA->local_target.fi;
  879. tRecord.ra = tDATA->local_target.r;
  880. tRecord.area = tDATA->area;
  881. tRecord.lat = tDATA->global_target.lat;
  882. tRecord.lon = tDATA->global_target.lon;
  883. tRecord.alt = tDATA->global_target.alt;
  884. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  885. };
  886. break;
  887. };
  888. case EVT_TARGET_3D_BLACK:
  889. case EVT_TARGET_3D_WHITE:{
  890. //startSession();
  891. TARGET_DATA *tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  892. if( tDATA != NULL ) {
  893. DBC_EVT_RECORD tRecord;
  894. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  895. uint32_t TS_A, TS_B;
  896. TS_A = Evt->m_UMSTS / 1000;
  897. TS_B = Evt->m_UMSTS % 1000;
  898. tRecord.node_id = Evt->m_nSourceNodeID;
  899. tRecord.type = Evt->m_eType;
  900. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  901. tRecord.ts_s = TS_A;
  902. tRecord.ts_ms = TS_B;
  903. tRecord.lrf_id = tDATA->lrf_id;
  904. tRecord.range_digital = tDATA->digital;
  905. tRecord.az = tDATA->local_target.fi;
  906. tRecord.el = tDATA->local_target.tetta;
  907. tRecord.ra = tDATA->local_target.r;
  908. tRecord.area = tDATA->area;
  909. tRecord.lat = tDATA->global_target.lat;
  910. tRecord.lon = tDATA->global_target.lon;
  911. tRecord.alt = tDATA->global_target.alt;
  912. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  913. };
  914. break;
  915. };
  916. case EVT_ERR_LRF_NO_IRQ:
  917. case EVT_ERR_LRF_NO_ZERO:
  918. case EVT_ERR_LRF_ANGLE:
  919. case EVT_ERR_LRF_VOLTAGE:
  920. case EVT_ERR_LRF_BROKEN:
  921. case EVT_LRF_VIS_CHANGED:
  922. case EVT_ERR_OVERFLOW:
  923. case EVT_SST_OVERFLOW:
  924. case EVT_COMMUNICATION_JAMED: if( !m_bDebugMode ) break;
  925. case EVT_OP_ACK_MASK:
  926. case EVT_OP_NACK_MASK:
  927. case EVT_OP_ON_MASK:
  928. case EVT_OP_OFF_MASK:
  929. case EVT_CONNECTED:
  930. case EVT_DISCONNECTED:{
  931. DBC_EVT_RECORD tRecord;
  932. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  933. uint32_t TS_A, TS_B;
  934. TS_A = Evt->m_UMSTS / 1000;
  935. TS_B = Evt->m_UMSTS % 1000;
  936. tRecord.node_id = Evt->m_nSourceNodeID;
  937. tRecord.type = Evt->m_eType;
  938. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  939. tRecord.ts_s = TS_A;
  940. tRecord.ts_ms = TS_B;
  941. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  942. break;
  943. };
  944. case EVT_FLIP:{
  945. if( !m_bDebugMode ) break;
  946. DBC_EVT_RECORD tRecord;
  947. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  948. uint32_t TS_A, TS_B;
  949. TS_A = Evt->m_UMSTS / 1000;
  950. TS_B = Evt->m_UMSTS % 1000;
  951. tRecord.node_id = Evt->m_nSourceNodeID;
  952. tRecord.type = Evt->m_eType;
  953. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  954. tRecord.ts_s = TS_A;
  955. tRecord.ts_ms = TS_B;
  956. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  957. break;
  958. };
  959. };
  960. break;
  961. case EVT_CLASS_KRNL_FILTER_LATF:{
  962. /*switch( Evt->m_eType ) {
  963. case EVT_FLT_LATF_CHK:
  964. case EVT_FLT_LATF_UP_DETEC:
  965. case EVT_FLT_LATF_BT_DETEC:
  966. case EVT_FLT_LATF_TRP_DET:
  967. case EVT_FLT_LATF_TRP_UPDT:
  968. case EVT_FLT_LATF_SUSP_START:
  969. case EVT_FLT_LATF_SUSP_NAK:
  970. case EVT_FLT_LATF_REGULAR:
  971. case EVT_FLT_LATF_DISABLED:
  972. case EVT_FLT_LATF_SUSP_DET:
  973. case EVT_FLT_LATF_DIFF_DET:
  974. };*/
  975. DBC_EVT_RECORD tRecord;
  976. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  977. uint32_t TS_A, TS_B;
  978. TS_A = Evt->m_UMSTS / 1000;
  979. TS_B = Evt->m_UMSTS % 1000;
  980. tRecord.node_id = m_nServerNodeID;
  981. tRecord.type = Evt->m_eType;
  982. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  983. tRecord.ts_s = TS_A;
  984. tRecord.ts_ms = TS_B;
  985. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  986. break;
  987. };
  988. };
  989. return true;
  990. };
  991. /*
  992. typedef struct S_VIDEO_SEGMENT_INFO{
  993. uint32_t stream_id;
  994. uint32_t streamTSS;
  995. uint32_t streamTMS;
  996. DBC_INT_LIST eventIDS;
  997. DBC_INT_LIST eventFrames;
  998. DBC_INT_LIST eventTSS;
  999. DBC_INT_LIST eventTMS;
  1000. char recordMD5[33];
  1001. char recordEXT[6];
  1002. uint8_t streamFPS;
  1003. char scratchPath[PATH_MAX];
  1004. uint32_t dataSize;
  1005. uint32_t db_id;
  1006. }VIDEO_SEGMENT_INFO;
  1007. */
  1008. void mojoKernel::handleVideo( VIDEO_SEGMENT_INFO *ptr ){
  1009. if ( fileExist( ptr->scratchPath ) ) {
  1010. bool bKeepVideoFile = false;
  1011. if( ptr->recordMD5[0] ) {
  1012. if( m_bKeepLocalReport ) {
  1013. //if wm is debug -> add all to database, else send to server
  1014. ptr->db_id = m_pDBC->addVideoRecord( ptr->stream_id, ptr->recordMD5, ptr->recordEXT, ptr->streamTSS, ptr->streamTMS, ptr->streamFPS );
  1015. if ( ptr->db_id ) {
  1016. #ifdef DEBUG
  1017. LOG("Video File Registered");
  1018. #endif
  1019. for (int idx = 0; idx < ptr->eventIDS.size(); idx++) {
  1020. #ifdef DEBUG
  1021. LOG("Binding Event to a Video File!");
  1022. #endif
  1023. if( m_pDBC->addVideoEvent( ptr->eventIDS[idx], ptr->db_id, ptr->eventFrames[idx], ptr->eventTSS[idx], ptr->eventTMS[idx] )) {
  1024. #ifdef DEBUG
  1025. LOG("OK");
  1026. #endif
  1027. };
  1028. };
  1029. CSTR tP = m_pDBC->getCurrentReportDir();
  1030. if ( tP ) {
  1031. char tB[MAX_PATH];
  1032. char dB[MAX_PATH];
  1033. //sprintf( tB, "%s/data/videos/%s.%s", getcwd(dB,MAX_PATH), ptr->recordMD5, ptr->recordEXT );
  1034. sprintf(tB, "%s/data/reports/%s/%s.%s", getcwd(dB, MAX_PATH), tP, ptr->recordMD5, ptr->recordEXT );
  1035. fileCopy( ptr->scratchPath, tB );
  1036. ch_chown( tB, "nobody", "nogroup" );
  1037. chmod( tB, 0664 );
  1038. #ifdef DEBUG
  1039. LOG("Video Saved");
  1040. #endif
  1041. free( tP );
  1042. } else {
  1043. LOG_ERROR("Cannot Get Report Path from DB!");
  1044. };
  1045. } else {
  1046. LOG_ERROR("Failure in registration video!");
  1047. };
  1048. };
  1049. if( m_bWorkWithRemoteServer && ( m_pRPC!=NULL) ){
  1050. bKeepVideoFile = m_pRPC->addVideo( ptr );
  1051. };
  1052. };
  1053. if( !bKeepVideoFile ){
  1054. unlink( ptr->scratchPath );
  1055. #ifdef DEBUG
  1056. LOG("Video Scratch Deleted!");
  1057. #endif
  1058. };
  1059. } else LOG_ERROR("Video File is Missing!");
  1060. };
  1061. bool mojoKernel::validateReports( void ){
  1062. if ( m_bKeepLocalReport ) {
  1063. DBC_SITE_DATA *tSiteData = m_pDBC->getSiteData(1);
  1064. if( tSiteData ) {
  1065. // Check if settings has been changed in DB
  1066. if ( tSiteData->report_cleanup != m_sSiteData.report_cleanup || tSiteData->preserve_empty != m_sSiteData.preserve_empty ) return false;
  1067. } else {
  1068. LOG_ERROR("Error Loading Site DATA!");
  1069. return false;
  1070. };
  1071. if ( m_sSiteData.report_cleanup ) {
  1072. cleanUpReportDir( m_sSiteData.report_cleanup );
  1073. };
  1074. if ( m_sSiteData.preserve_empty ) {
  1075. float tT = 100.0f * ( 1.0f - ( 1.0f / ((float)m_sSiteData.preserve_empty)));
  1076. #ifdef DEBUG_EXTRA
  1077. char tB[128];
  1078. sprintf( tB, "Check Disk Size: %0.3f %0.3f %d [%d]", m_sSysInfo.disk, tT, m_sSiteData.preserve_empty );
  1079. LOG( tB );
  1080. #endif
  1081. if( m_sSysInfo.disk > tT ){
  1082. // delete some oldest report date
  1083. m_pDBC->deleteReportDate( m_pDBC->getFirstEventTS() );
  1084. };
  1085. };
  1086. }
  1087. return true;
  1088. };
  1089. void mojoKernel::backupReports( uint32_t days ){
  1090. if ( !m_bKeepLocalReport ) return;
  1091. REPORT_DIRINFO tRDI = { 0 };
  1092. uint32_t cTS = (uint32_t)time(NULL);
  1093. cTS -= 86400 * days;
  1094. fillRDF( cTS, tRDI );
  1095. DBC_INT_LIST *TL = m_pDBC->listOlderReportStat( tRDI.YEAR, tRDI.MONTH, tRDI.DAY );
  1096. if ( TL ) {
  1097. for ( DBC_INT_LIST::iterator idx = TL->begin(); idx < TL->end(); idx++ ) {
  1098. REPORT_DIRINFO *tRDI = m_pDBC->getReportDirInfo( *idx );
  1099. if ( tRDI ) {
  1100. char tB[MAX_PATH];
  1101. char dB[MAX_PATH];
  1102. sprintf( tB, "%s/data/reports/%s/%s/%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY );
  1103. char tPATH[MAX_PATH];
  1104. sprintf( tPATH, "%s/data/archive/%s_%s_%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY );
  1105. if ( fileExist( tB ) ) {
  1106. if ( !fileExist( tPATH ) ) {
  1107. // Try Create Job if sucess then create dir to skip next validation
  1108. if( createCronJob( 0, tPATH, cTS ) ){
  1109. // Create Backup Dir
  1110. uid_t uid = getUserUid( m_pDBC->getDataParam( 1 ) );
  1111. gid_t gid = getUserGid( m_pDBC->getDataParam( 2 ) );
  1112. if( createAccessibleDir( tPATH, STORAGE_DIR_MODE, uid, gid ) ){
  1113. // Create Just One Backup Per Time!
  1114. free( tRDI );
  1115. break;
  1116. };
  1117. };
  1118. };// else proceed next
  1119. };
  1120. free( tRDI );
  1121. };
  1122. };
  1123. delete( TL );
  1124. };
  1125. };
  1126. void mojoKernel::cleanUpReportDir( uint32_t days ){
  1127. // Cleanup Current reports no Backup
  1128. uint32_t oTS = m_pDBC->getFirstEventTS();
  1129. uint32_t cTS = (uint32_t)time(NULL);
  1130. cTS -= 86400 * days;
  1131. #ifdef DEBUG_EXTRA
  1132. char tB[128];
  1133. sprintf( tB, "Oldest %d border %d", oTS, cTS );
  1134. LOG( tB );
  1135. #endif
  1136. if ( oTS <= cTS ) {
  1137. m_pDBC->deleteReportDate( oTS );
  1138. };
  1139. };
  1140. bool mojoKernel::createCronJob( uint8_t job, CSTR param_str, int param_int ){
  1141. if ( !fileExist("/srv/etc/cron.lock") ) {
  1142. Json::Value root;
  1143. //Json::Value data;
  1144. switch ( job ) {
  1145. case CRON_JOB_CREATE_BACKUP:{
  1146. root["operation"] = CRON_JOB_CREATE_BACKUP;
  1147. //data["number"] = 1;
  1148. root["ts"] = param_int;
  1149. root["backup_dst"] = std::string( param_str );
  1150. break;
  1151. };
  1152. // No default JOB!
  1153. default: return false;
  1154. };
  1155. FILE *fd = fopen("/srv/etc/cron_.json","w+");
  1156. if ( fd ) {
  1157. if( HUMAN_FRIENDLY_JSON ) {
  1158. Json::FastWriter writer;
  1159. const std::string json_file = writer.write(root);
  1160. fprintf( fd, json_file.c_str() );
  1161. } else {
  1162. Json::StreamWriterBuilder builder;
  1163. const std::string json_file = Json::writeString( builder, root );
  1164. fprintf( fd, json_file.c_str() );
  1165. };
  1166. fclose( fd );
  1167. return true;
  1168. };
  1169. };
  1170. return false;
  1171. };
  1172. #undef LOG
  1173. #undef LOG_ERROR