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.

1544 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. 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. #ifdef DEBUG
  659. LOG("Loadding Filters ...");
  660. #endif
  661. DBC_INT_LIST *tList = m_pDBC->listFilters( m_nServerID );
  662. if( tList != NULL ) {
  663. if( tList->size() > 0 ) {
  664. for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) {
  665. #ifdef DEBUG_EXTRA
  666. LOG("Load Filter!");
  667. #endif
  668. DBC_TARGET_FLITER_DATA *tData = NULL;
  669. tData = m_pDBC->getTargetFilterData( (*idx) );
  670. if( tData != NULL ) {
  671. #ifdef DEBUG_EXTRA
  672. LOG("Filter Data OK!");
  673. #endif
  674. switch( tData->type ) {
  675. case TF_LA_METRO_TRAIN_FILTER:{
  676. #ifdef DEBUG
  677. LOG("Create Train Filter");
  678. #endif
  679. kernelEventFilterTrain *tFilter = new kernelEventFilterTrain( (*idx) );
  680. if( tFilter != NULL ) {
  681. if( tFilter->initFilter() ){
  682. #ifdef DEBUG
  683. LOG("Add Train Filter");
  684. #endif
  685. m_vTargetFilters.push_back( (kernelEventFilter*) tFilter );
  686. } else {
  687. #ifdef DEBUG
  688. LOG_ERROR("Error in Filter Init!");
  689. #endif
  690. delete( tFilter );
  691. };
  692. };
  693. break;
  694. };
  695. case TF_AUTHORISED_PEOPLE_FILTER: {
  696. LOG("Create APF");
  697. kernelEventFilterAPF *tFilter = new kernelEventFilterAPF( (*idx) );
  698. if( tFilter != NULL ) {
  699. if( tFilter->initFilter() ){
  700. #ifdef DEBUG
  701. LOG("Add APF");
  702. #endif
  703. m_vTargetFilters.push_back( (kernelEventFilter*) tFilter );
  704. } else {
  705. #ifdef DEBUG
  706. LOG_ERROR("Error in Filter Init!");
  707. #endif
  708. delete( tFilter );
  709. };
  710. };
  711. break;
  712. };
  713. case TF_BUBBLE_SEGMENTATION_FILTER:{
  714. #ifdef DEBUG
  715. LOG("Create Bubble Filter");
  716. #endif
  717. kernelEventFilterBubble *tFilter = new kernelEventFilterBubble( (*idx) );
  718. if( tFilter != NULL ) {
  719. #ifdef DEBUG_EXTRA
  720. LOG("Init Bubble Filter");
  721. #endif
  722. if( tFilter->initFilter() ){
  723. #ifdef DEBUG
  724. LOG("Add Bubble Filter");
  725. #endif
  726. m_vTargetFilters.push_back( (kernelEventFilter*) tFilter );
  727. } else {
  728. #ifdef DEBUG
  729. LOG_ERROR("Error in Filter Init!");
  730. #endif
  731. delete( tFilter );
  732. };
  733. };
  734. break;
  735. };
  736. case TF_YOLO_FILTER:{
  737. break;
  738. };
  739. };
  740. #ifdef DEBUG_EXTRA
  741. LOG("Delete Filter Data");
  742. #endif
  743. free( tData );
  744. };
  745. };
  746. #ifdef DEBUG_EXTRA
  747. LOG("Clear Filter List");
  748. #endif
  749. tList->clear();
  750. };
  751. #ifdef DEBUG_EXTRA
  752. LOG("Delete Filter List");
  753. #endif
  754. delete( tList );
  755. #ifdef DEBUG
  756. } else {
  757. LOG("No Target Filters Assigned!");
  758. #endif
  759. };
  760. #ifdef DEBUG
  761. LOG("Done Load Filters!");
  762. #endif
  763. };
  764. void mojoKernel::unloadFilters(){
  765. if( m_vTargetFilters.size() > 0 ) {
  766. #ifdef DEBUG
  767. LOG("Have Filters to Unload");
  768. #endif
  769. for( vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++ ){
  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. case TF_BUBBLE_SEGMENTATION_FILTER:{
  780. delete( (kernelEventFilterBubble*)(*idx) );
  781. break;
  782. };
  783. case TF_YOLO_FILTER:{
  784. //ToDo: Must be changed according to Steph's code
  785. //delete( (kernelEventFilterYOLO*)(*idx) );
  786. break;
  787. };
  788. default:{
  789. char tBuffer[64];
  790. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  791. LOG( tBuffer );
  792. };
  793. };
  794. };
  795. m_vTargetFilters.clear();
  796. LOG("Unload Filters Done!");
  797. #ifdef DEBUG
  798. } else {
  799. LOG("No Filters to Unload!");
  800. #endif
  801. };
  802. };
  803. void mojoKernel::validateFilters(){
  804. if( m_vTargetFilters.size() > 0 ) {
  805. #ifdef DEBUG_EXTRA
  806. LOG("Filters Validation");
  807. #endif
  808. for( vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++ ){
  809. if( !(*idx)->validate() ) {
  810. LOG_ERROR("Filter is Invalid!");
  811. /*switch ((*idx)->getType()) {
  812. case TF_LA_METRO_TRAIN_FILTER:{
  813. delete( (kernelEventFilterTrain*)(*idx) );
  814. break;
  815. };
  816. case TF_AUTHORISED_PEOPLE_FILTER:{
  817. delete( (kernelEventFilterAPF*)(*idx) );
  818. break;
  819. };
  820. default:{
  821. char tBuffer[64];
  822. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  823. LOG( tBuffer );
  824. };
  825. };
  826. */
  827. };
  828. };
  829. };
  830. };
  831. bool mojoKernel::dispatchEvent( EVENT_CONTAINER *Evt ){
  832. if( Evt == NULL ) return false;
  833. if( m_bWorkWithRemoteServer && ( m_pRPC!=NULL) ) m_pRPC->addEvent( Evt );
  834. if( !m_bKeepLocalReport ) return true;
  835. if( m_pDBC == NULL) return false;
  836. switch (Evt->m_eClass) {
  837. case EVT_CLASS_SYSTEM:
  838. switch (Evt->m_eType) {
  839. case EVT_LRF_START_SESSION:
  840. case EVT_LRF_END_SESSION_TO:
  841. case EVT_LRF_END_SESSION_MA:{
  842. DBC_EVT_RECORD tRecord;
  843. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  844. tRecord.ts_s = Evt->m_UMSTS / 1000;
  845. tRecord.ts_ms = Evt->m_UMSTS % 1000;
  846. tRecord.node_id = m_nServerNodeID;
  847. tRecord.type = Evt->m_eType;
  848. strncpy(tRecord.uuid, Evt->m_UUID, 64 );
  849. m_pDBC->addEvent( &tRecord );
  850. break;
  851. };
  852. };
  853. break;
  854. /*
  855. case EVT_CLASS_EXTERNAL_CMD:
  856. switch( Evt->m_eType ){
  857. case EVT_FH_JAMER_ON:{
  858. uint32_t TS_A, TS_B;
  859. TS_A = Evt->m_UMSTS / 1000;
  860. TS_B = Evt->m_UMSTS % 1000;
  861. #ifdef SIMULATE_KERNEL_TARGETS
  862. m_bJamingTO = SIMULATE_JAMER_TO;
  863. #endif
  864. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  865. if( tData ) {
  866. if( m_pDBC->addEvent(
  867. 0,
  868. Evt->m_UUID,
  869. TS_A,
  870. TS_B,
  871. EVT_FH_JAMER_ON,
  872. 0,
  873. 0,
  874. 0,
  875. 0,
  876. 0,
  877. 0,
  878. 0,
  879. 0,
  880. 0,
  881. 0,
  882. tData->evtUUID
  883. ) ){
  884. //Simulate FH Module Event;
  885. EVENT_CONTAINER *ttt;
  886. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  887. if( nnn ){
  888. nnn->location.lat = 0;
  889. nnn->location.lon = 0;
  890. nnn->location.alt = 0;
  891. sprintf( nnn->missionID, tData->evtUUID );
  892. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_JAMER_ON, NULL, NULL, nnn );
  893. if( ttt ) {
  894. m_pEventHeap->broadcast( ttt );
  895. } else {
  896. free( nnn );
  897. };
  898. };
  899. };
  900. free(tData);
  901. };
  902. break;
  903. };
  904. case EVT_FH_MISSION_END:{
  905. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  906. if( tData ) {
  907. //DBC_EVT_DATA * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  908. if( 1 / *tEVT* / ) {
  909. uint32_t TS_A, TS_B;
  910. TS_A = Evt->m_UMSTS / 1000;
  911. TS_B = Evt->m_UMSTS % 1000;
  912. if( m_pDBC->addEvent(
  913. 0,
  914. Evt->m_UUID,
  915. TS_A,
  916. TS_B,
  917. EVT_FH_LANDED ) ){
  918. //Simulate FH Module Event;
  919. EVENT_CONTAINER *ttt;
  920. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  921. if( nnn ){
  922. nnn->location.lat = 0;
  923. nnn->location.lon = 0;
  924. nnn->location.alt = 0;
  925. sprintf( nnn->missionID, tData->evtUUID );
  926. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_LANDED, NULL, NULL, nnn );
  927. if( ttt ) {
  928. m_pEventHeap->broadcast( ttt );
  929. } else {
  930. free( nnn );
  931. };
  932. };
  933. };
  934. //free( tEVT );
  935. } else {
  936. LOG_ERROR("Linked Event Was Not Found!");
  937. };
  938. };
  939. break;
  940. }
  941. case EVT_FH_MISSION_BEGIN:{
  942. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  943. #ifdef SIMULATE_KERNEL_TARGETS
  944. m_bLandingTO = SIMULATE_LANDING_TO;
  945. #endif
  946. if( tData ) {
  947. #ifdef SIMULATE_KERNEL_TARGETS
  948. sprintf( m_cCurrentMission, tData->evtUUID );
  949. #endif
  950. DBC_EVT_RECORD * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  951. if( tEVT ) {
  952. uint32_t TS_A, TS_B;
  953. TS_A = Evt->m_UMSTS / 1000;
  954. TS_B = Evt->m_UMSTS % 1000;
  955. if( Evt->m_db_id = m_pDBC->addEvent(
  956. 0,
  957. Evt->m_UUID,
  958. TS_A,
  959. TS_B,
  960. EVT_FH_FLY_TO,
  961. 0,
  962. 0,
  963. 0,
  964. 0,
  965. 0,
  966. 0,
  967. tEVT->lat,
  968. tEVT->lon,
  969. tEVT->alt,
  970. tEVT->id,
  971. tEVT->uuid
  972. ) ){
  973. //Simulate FH Module Event;
  974. EVENT_CONTAINER * ttt;
  975. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  976. if( nnn ){
  977. nnn->location.lat = tEVT->lat;
  978. nnn->location.lon = tEVT->lon;
  979. nnn->location.alt = tEVT->alt;
  980. sprintf( nnn->missionID, tData->evtUUID );
  981. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_MISSION_BEGIN, NULL, NULL, nnn );
  982. if( ttt ) {
  983. m_pEventHeap->broadcast( ttt );
  984. } else {
  985. free( nnn );
  986. };
  987. };
  988. };
  989. free( tEVT );
  990. } else {
  991. LOG_ERROR("Linked Event Was Not Found!");
  992. };
  993. };
  994. break;
  995. };
  996. };
  997. break;
  998. */
  999. case EVT_CLASS_VIDEO:{
  1000. switch( Evt->m_eType ) {
  1001. case EVT_CAPTURE_DONE:
  1002. case EVT_CAPTURE_END:{
  1003. if ( Evt->m_pEvtData ) {
  1004. handleVideo( (VIDEO_SEGMENT_INFO *) Evt->m_pEvtData );
  1005. //Dont free Data - it points to static struct of Encoder!!
  1006. memset(Evt->m_pEvtData, 0, sizeof(VIDEO_SEGMENT_INFO));
  1007. Evt->m_pEvtData = NULL;
  1008. #ifdef DEBUG_EXTRA
  1009. LOG("VIDEO_SEGMENT_INFO Cleared!");
  1010. #endif
  1011. };
  1012. break;
  1013. };
  1014. case EVT_VIDEO_FPS_DEGRADED:
  1015. case EVT_VIDEO_FPS_RAISED:{
  1016. if( !m_bDebugMode ) break;
  1017. DBC_EVT_RECORD tRecord;
  1018. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1019. uint32_t TS_A, TS_B;
  1020. TS_A = Evt->m_UMSTS / 1000;
  1021. TS_B = Evt->m_UMSTS % 1000;
  1022. tRecord.node_id = Evt->m_nSourceNodeID;
  1023. tRecord.type = Evt->m_eType;
  1024. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1025. tRecord.ts_s = TS_A;
  1026. tRecord.ts_ms = TS_B;
  1027. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1028. break;
  1029. };
  1030. };
  1031. break;
  1032. };
  1033. case EVT_CLASS_SENSOR_CLD:
  1034. case EVT_CLASS_SENSOR_LGS:
  1035. case EVT_CLASS_SENSOR_LFS:
  1036. case EVT_CLASS_SENSOR_MND:
  1037. case EVT_CLASS_SENSOR_PLS_210:
  1038. case EVT_CLASS_SENSOR_PLS_360:
  1039. case EVT_CLASS_SENSOR_ALS:
  1040. case EVT_CLASS_SENSOR_3D:
  1041. case EVT_CLASS_SENSOR_ACC:
  1042. #ifdef DEBUG_EXTRA
  1043. char tB[128];
  1044. sprintf( tB,"Evt Class: %d Evt Type: %d ", Evt->m_eClass ,Evt->m_eType );
  1045. LOG( tB );
  1046. #endif
  1047. switch( Evt->m_eType ) {
  1048. case EVT_TARGET_1D:{
  1049. //startSession();
  1050. preserveSession();
  1051. TARGET_DATA *tDATA =(TARGET_DATA *) Evt->m_pEvtData;
  1052. if( tDATA != NULL ) {
  1053. DBC_EVT_RECORD tRecord;
  1054. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1055. uint32_t TS_A, TS_B;
  1056. TS_A = Evt->m_UMSTS / 1000;
  1057. TS_B = Evt->m_UMSTS % 1000;
  1058. tRecord.node_id = Evt->m_nSourceNodeID;
  1059. tRecord.type = Evt->m_eType;
  1060. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1061. tRecord.ts_s = TS_A;
  1062. tRecord.ts_ms = TS_B;
  1063. tRecord.range_digital = tDATA->digital;
  1064. tRecord.ra = tDATA->local_target.r;
  1065. tRecord.area = tDATA->area;
  1066. tRecord.lat = tDATA->global_target.lat;
  1067. tRecord.lon = tDATA->global_target.lon;
  1068. tRecord.alt = tDATA->global_target.alt;
  1069. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1070. };
  1071. break;
  1072. };
  1073. // While Camera Always Records Video on each Absolute Move
  1074. // an Event Must be generated to see video in report
  1075. case EVT_ROI_ACTIVE: //if( !m_bDebugMode ) break;
  1076. case EVT_TARGET_2D_BLACK:
  1077. case EVT_TARGET_2D_WHITE:{
  1078. //startSession();
  1079. preserveSession();
  1080. TARGET_DATA *tDATA = (TARGET_DATA *)Evt->m_pEvtData;
  1081. if( tDATA != NULL ) {
  1082. DBC_EVT_RECORD tRecord;
  1083. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1084. uint32_t TS_A, TS_B;
  1085. TS_A = Evt->m_UMSTS / 1000;
  1086. TS_B = Evt->m_UMSTS % 1000;
  1087. tRecord.node_id = Evt->m_nSourceNodeID;
  1088. tRecord.type = Evt->m_eType;
  1089. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1090. tRecord.ts_s = TS_A;
  1091. tRecord.ts_ms = TS_B;
  1092. tRecord.lrf_id = tDATA->lrf_id;
  1093. tRecord.range_digital = tDATA->digital;
  1094. tRecord.az = tDATA->local_target.fi;
  1095. tRecord.ra = tDATA->local_target.r;
  1096. tRecord.area = tDATA->area;
  1097. tRecord.lat = tDATA->global_target.lat;
  1098. tRecord.lon = tDATA->global_target.lon;
  1099. tRecord.alt = tDATA->global_target.alt;
  1100. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1101. };
  1102. break;
  1103. };
  1104. case EVT_TARGET_3D_BLACK:
  1105. case EVT_TARGET_3D_WHITE:{
  1106. //startSession();
  1107. preserveSession();
  1108. TARGET_DATA *tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1109. if( tDATA != NULL ) {
  1110. DBC_EVT_RECORD tRecord;
  1111. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1112. uint32_t TS_A, TS_B;
  1113. TS_A = Evt->m_UMSTS / 1000;
  1114. TS_B = Evt->m_UMSTS % 1000;
  1115. tRecord.node_id = Evt->m_nSourceNodeID;
  1116. tRecord.type = Evt->m_eType;
  1117. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1118. tRecord.ts_s = TS_A;
  1119. tRecord.ts_ms = TS_B;
  1120. tRecord.lrf_id = tDATA->lrf_id;
  1121. tRecord.range_digital = tDATA->digital;
  1122. tRecord.az = tDATA->local_target.fi;
  1123. tRecord.el = tDATA->local_target.tetta;
  1124. tRecord.ra = tDATA->local_target.r;
  1125. tRecord.area = tDATA->area;
  1126. tRecord.lat = tDATA->global_target.lat;
  1127. tRecord.lon = tDATA->global_target.lon;
  1128. tRecord.alt = tDATA->global_target.alt;
  1129. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1130. };
  1131. break;
  1132. };
  1133. case EVT_ERR_LRF_NO_IRQ:
  1134. case EVT_ERR_LRF_NO_ZERO:
  1135. case EVT_ERR_LRF_ANGLE:
  1136. case EVT_ERR_LRF_VOLTAGE:
  1137. case EVT_ERR_LRF_BROKEN:
  1138. case EVT_LRF_VIS_CHANGED:
  1139. case EVT_ERR_OVERFLOW:
  1140. case EVT_SST_OVERFLOW:
  1141. case EVT_COMMUNICATION_JAMED: if( !m_bDebugMode ) break;
  1142. case EVT_OP_ACK_MASK:
  1143. case EVT_OP_NACK_MASK:
  1144. case EVT_OP_ON_MASK:
  1145. case EVT_OP_OFF_MASK:
  1146. case EVT_CONNECTED:
  1147. case EVT_DISCONNECTED:{
  1148. DBC_EVT_RECORD tRecord;
  1149. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1150. uint32_t TS_A, TS_B;
  1151. TS_A = Evt->m_UMSTS / 1000;
  1152. TS_B = Evt->m_UMSTS % 1000;
  1153. tRecord.node_id = Evt->m_nSourceNodeID;
  1154. tRecord.type = Evt->m_eType;
  1155. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1156. tRecord.ts_s = TS_A;
  1157. tRecord.ts_ms = TS_B;
  1158. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1159. break;
  1160. };
  1161. case EVT_FLIP:{
  1162. if( !m_bDebugMode ) break;
  1163. DBC_EVT_RECORD tRecord;
  1164. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1165. uint32_t TS_A, TS_B;
  1166. TS_A = Evt->m_UMSTS / 1000;
  1167. TS_B = Evt->m_UMSTS % 1000;
  1168. tRecord.node_id = Evt->m_nSourceNodeID;
  1169. tRecord.type = Evt->m_eType;
  1170. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1171. tRecord.ts_s = TS_A;
  1172. tRecord.ts_ms = TS_B;
  1173. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1174. break;
  1175. };
  1176. };
  1177. break;
  1178. case EVT_CLASS_KRNL_FILTER_LATF:{
  1179. TARGET_DATA *tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1180. if ( ( Evt->m_eType > EVT_FLT_LATF_DIFF_DET ) || ( tDATA == NULL ) ) {
  1181. //AUX messages (no additional target data)
  1182. DBC_EVT_RECORD tRecord;
  1183. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1184. uint32_t TS_A, TS_B;
  1185. TS_A = Evt->m_UMSTS / 1000;
  1186. TS_B = Evt->m_UMSTS % 1000;
  1187. tRecord.node_id = m_nServerNodeID;
  1188. tRecord.type = Evt->m_eType;
  1189. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1190. tRecord.ts_s = TS_A;
  1191. tRecord.ts_ms = TS_B;
  1192. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1193. } else {
  1194. //If First Suspiciouse -> Start session to collect Fliter Messages
  1195. //if( Evt->m_eType == EVT_FLT_LATF_SUSP_START ) startSession();
  1196. // Regular LATF with target Info
  1197. DBC_EVT_RECORD tRecord;
  1198. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1199. uint32_t TS_A, TS_B;
  1200. TS_A = Evt->m_UMSTS / 1000;
  1201. TS_B = Evt->m_UMSTS % 1000;
  1202. tRecord.node_id = Evt->m_nSourceNodeID;// m_nServerNodeID;//??
  1203. tRecord.type = Evt->m_eType;
  1204. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1205. tRecord.ts_s = TS_A;
  1206. tRecord.ts_ms = TS_B;
  1207. tRecord.lrf_id = tDATA->lrf_id;
  1208. tRecord.range_digital = tDATA->digital;
  1209. tRecord.az = tDATA->local_target.fi;
  1210. tRecord.el = tDATA->local_target.tetta;
  1211. tRecord.ra = tDATA->local_target.r;
  1212. tRecord.area = tDATA->area;
  1213. tRecord.lat = 0;
  1214. tRecord.lon = 0;
  1215. tRecord.alt = 0;
  1216. #ifdef DEBUG_EXTRA
  1217. char tB[243];
  1218. 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 );
  1219. LOG( tB );
  1220. #endif
  1221. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1222. };
  1223. break;
  1224. };
  1225. case EVT_CLASS_KRNL_FILTER_BUBBLE:{
  1226. 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 ){
  1227. preserveSession();
  1228. TARGET_DATA *tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1229. DBC_EVT_RECORD tRecord;
  1230. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1231. uint32_t TS_A, TS_B;
  1232. TS_A = Evt->m_UMSTS / 1000;
  1233. TS_B = Evt->m_UMSTS % 1000;
  1234. tRecord.node_id = Evt->m_nSourceNodeID;// m_nServerNodeID;//??
  1235. tRecord.type = Evt->m_eType;
  1236. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1237. tRecord.ts_s = TS_A;
  1238. tRecord.ts_ms = TS_B;
  1239. tRecord.lrf_id = tDATA->lrf_id;
  1240. tRecord.range_digital = tDATA->digital;
  1241. tRecord.az = tDATA->local_target.fi;
  1242. tRecord.el = tDATA->local_target.tetta;
  1243. tRecord.ra = tDATA->local_target.r;
  1244. tRecord.area = tDATA->area;
  1245. tRecord.lat = 0;
  1246. tRecord.lon = 0;
  1247. tRecord.alt = 0;
  1248. #ifdef DEBUG_EXTRA
  1249. char tB[243];
  1250. 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 );
  1251. LOG( tB );
  1252. #endif
  1253. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1254. };
  1255. break;
  1256. };
  1257. case EVT_CLASS_REMOTE_SERVER_CMD:{
  1258. switch ( Evt->m_eType ) {
  1259. case EVT_RPC_FRIZE_ACTIVE_B:{
  1260. //getFilterId
  1261. break;
  1262. };
  1263. };
  1264. break;
  1265. };
  1266. };
  1267. return true;
  1268. };
  1269. /*
  1270. typedef struct S_VIDEO_SEGMENT_INFO{
  1271. uint32_t stream_id;
  1272. uint32_t streamTSS;
  1273. uint32_t streamTMS;
  1274. DBC_INT_LIST eventIDS;
  1275. DBC_INT_LIST eventFrames;
  1276. DBC_INT_LIST eventTSS;
  1277. DBC_INT_LIST eventTMS;
  1278. char recordMD5[33];
  1279. char recordEXT[6];
  1280. uint8_t streamFPS;
  1281. char scratchPath[PATH_MAX];
  1282. uint32_t dataSize;
  1283. uint32_t db_id;
  1284. }VIDEO_SEGMENT_INFO;
  1285. */
  1286. void mojoKernel::handleVideo( VIDEO_SEGMENT_INFO *ptr ){
  1287. if ( fileExist( ptr->scratchPath ) ) {
  1288. bool bKeepVideoFile = false;
  1289. if( ptr->recordMD5[0] ) {
  1290. if( m_bKeepLocalReport ) {
  1291. //if wm is debug -> add all to database, else send to server
  1292. ptr->db_id = m_pDBC->addVideoRecord( ptr->stream_id, ptr->recordMD5, ptr->recordEXT, ptr->streamTSS, ptr->streamTMS, ptr->streamFPS );
  1293. if ( ptr->db_id ) {
  1294. #ifdef DEBUG_EXTRA
  1295. LOG("Video File Registered");
  1296. #endif
  1297. for (int idx = 0; idx < ptr->eventIDS.size(); idx++) {
  1298. #ifdef DEBUG_EXTRA
  1299. LOG("Binding Event to a Video File!");
  1300. #endif
  1301. if( m_pDBC->addVideoEvent( ptr->eventIDS[idx], ptr->db_id, ptr->eventFrames[idx], ptr->eventTSS[idx], ptr->eventTMS[idx] )) {
  1302. #ifdef DEBUG_EXTRA
  1303. LOG("OK");
  1304. #endif
  1305. };
  1306. };
  1307. CSTR tP = m_pDBC->getCurrentReportDir();
  1308. if ( tP ) {
  1309. char tB[MAX_PATH];
  1310. char dB[MAX_PATH];
  1311. //sprintf( tB, "%s/data/videos/%s.%s", getcwd(dB,MAX_PATH), ptr->recordMD5, ptr->recordEXT );
  1312. sprintf(tB, "%s/data/reports/%s/%s.%s", getcwd(dB, MAX_PATH), tP, ptr->recordMD5, ptr->recordEXT );
  1313. fileCopy( ptr->scratchPath, tB );
  1314. ch_chown( tB, "nobody", "nogroup" );
  1315. chmod( tB, 0664 );
  1316. #ifdef DEBUG_EXTRA
  1317. LOG("Video Saved");
  1318. #endif
  1319. free( tP );
  1320. } else {
  1321. LOG_ERROR("Cannot Get Report Path from DB!");
  1322. };
  1323. } else {
  1324. LOG_ERROR("Failure in registration video!");
  1325. };
  1326. };
  1327. if( m_bWorkWithRemoteServer && ( m_pRPC!=NULL) ) bKeepVideoFile = m_pRPC->addVideo( ptr );
  1328. };
  1329. if( !bKeepVideoFile ){
  1330. unlink( ptr->scratchPath );
  1331. #ifdef DEBUG_EXTRA
  1332. LOG("Video Scratch Deleted!");
  1333. #endif
  1334. };
  1335. } else LOG_ERROR("Video File is Missing!");
  1336. };
  1337. bool mojoKernel::validateReports( void ){
  1338. /// @todo Stephane needs to follow this function...
  1339. if ( m_bKeepLocalReport ) {
  1340. DBC_SITE_DATA *tSiteData = m_pDBC->getSiteData(1);
  1341. if( tSiteData ) {
  1342. // Check if settings has been changed in DB
  1343. if ( tSiteData->report_cleanup != m_sSiteData.report_cleanup || tSiteData->preserve_empty != m_sSiteData.preserve_empty ) {
  1344. memcpy( &m_sSiteData, tSiteData, sizeof( DBC_SITE_DATA ));
  1345. };
  1346. free( tSiteData );
  1347. } else {
  1348. LOG_ERROR("Error Loading Site DATA!");
  1349. return false;
  1350. };
  1351. if ( m_sSiteData.report_cleanup ) {
  1352. cleanUpReportDir( m_sSiteData.report_cleanup );
  1353. };
  1354. if ( m_sSiteData.preserve_empty ) {
  1355. float tT = 100.0f * ( 1.0f - ( 1.0f / ((float)m_sSiteData.preserve_empty)));
  1356. #ifdef DEBUG_EXTRA
  1357. char tB[128];
  1358. sprintf( tB, "Check Disk Size: %0.3f %0.3f %d [%d]", m_sSysInfo.disk, tT, m_sSiteData.preserve_empty );
  1359. LOG( tB );
  1360. #endif
  1361. if( m_sSysInfo.disk > tT ){
  1362. #ifdef DEBUG_EXTRA
  1363. LOG("Delete Some Reports!");
  1364. #endif
  1365. // delete some oldest report date
  1366. m_pDBC->deleteReportDate( m_pDBC->getFirstEventTS() );
  1367. };
  1368. };
  1369. }
  1370. return true;
  1371. };
  1372. void mojoKernel::backupReports( uint32_t days ){
  1373. if ( !m_bKeepLocalReport ) return;
  1374. REPORT_DIRINFO tRDI = { 0 };
  1375. uint32_t cTS = (uint32_t)time(NULL);
  1376. cTS -= 86400 * days;
  1377. fillRDF( cTS, tRDI );
  1378. DBC_INT_LIST *TL = m_pDBC->listOlderReportStat( tRDI.YEAR, tRDI.MONTH, tRDI.DAY );
  1379. if ( TL ) {
  1380. for ( DBC_INT_LIST::iterator idx = TL->begin(); idx < TL->end(); idx++ ) {
  1381. REPORT_DIRINFO *tRDI = m_pDBC->getReportDirInfo( *idx );
  1382. if ( tRDI ) {
  1383. char tB[MAX_PATH];
  1384. char dB[MAX_PATH];
  1385. sprintf( tB, "%s/data/reports/%s/%s/%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY );
  1386. char tPATH[MAX_PATH];
  1387. sprintf( tPATH, "%s/data/archive/%s_%s_%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY );
  1388. if ( fileExist( tB ) ) {
  1389. if ( !fileExist( tPATH ) ) {
  1390. // Try Create Job if sucess then create dir to skip next validation
  1391. if( createCronJob( 0, tPATH, cTS ) ){
  1392. // Create Backup Dir
  1393. uid_t uid = getUserUid( m_pDBC->getDataParam( 1 ) );
  1394. gid_t gid = getUserGid( m_pDBC->getDataParam( 2 ) );
  1395. if( createAccessibleDir( tPATH, STORAGE_DIR_MODE, uid, gid ) ){
  1396. // Create Just One Backup Per Time!
  1397. free( tRDI );
  1398. break;
  1399. };
  1400. };
  1401. };// else proceed next
  1402. };
  1403. free( tRDI );
  1404. };
  1405. };
  1406. delete( TL );
  1407. };
  1408. };
  1409. void mojoKernel::cleanUpReportDir( uint32_t days ){
  1410. // Cleanup Current reports no Backup
  1411. uint32_t oTS = m_pDBC->getFirstEventTS();
  1412. uint32_t cTS = (uint32_t)time(NULL);
  1413. cTS -= 86400 * days;
  1414. #ifdef DEBUG_EXTRA
  1415. char tB[128];
  1416. sprintf( tB, "Oldest %d border %d", oTS, cTS );
  1417. LOG( tB );
  1418. #endif
  1419. if ( oTS <= cTS ) {
  1420. #ifdef DEBUG_EXTRA
  1421. LOG("Deleting Old Reports");
  1422. #endif
  1423. /// @todo Stephane needs to see when this gets called...
  1424. m_pDBC->deleteReportDate( oTS );
  1425. };
  1426. };
  1427. bool mojoKernel::createCronJob( uint8_t job, CSTR param_str, int param_int ){
  1428. if ( !fileExist("/srv/etc/cron.lock") ) {
  1429. Json::Value root;
  1430. //Json::Value data;
  1431. switch ( job ) {
  1432. case CRON_JOB_CREATE_BACKUP:{
  1433. root["operation"] = CRON_JOB_CREATE_BACKUP;
  1434. //data["number"] = 1;
  1435. root["ts"] = param_int;
  1436. root["backup_dst"] = std::string( param_str );
  1437. break;
  1438. };
  1439. // No default JOB!
  1440. default: return false;
  1441. };
  1442. FILE *fd = fopen("/srv/etc/cron_.json","w+");
  1443. if ( fd ) {
  1444. if( HUMAN_FRIENDLY_JSON ) {
  1445. Json::FastWriter writer;
  1446. const std::string json_file = writer.write(root);
  1447. fprintf( fd, json_file.c_str() );
  1448. } else {
  1449. Json::StreamWriterBuilder builder;
  1450. const std::string json_file = Json::writeString( builder, root );
  1451. fprintf( fd, json_file.c_str() );
  1452. };
  1453. fclose( fd );
  1454. return true;
  1455. };
  1456. };
  1457. return false;
  1458. };
  1459. #undef LOG
  1460. #undef LOG_ERROR