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.

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