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.

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