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.

1503 lines
59 KiB

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