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.

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