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.

1100 lines
42 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
3 years 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
​
​
​
​
​
​
​
​
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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
3 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
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
​
  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/kernelModuleEmbedVideo.h"
  26. #ifdef USE_MYSQL
  27. #include "../common/MySQL_dataBinder.h"
  28. #else
  29. #include "../common/SQLite_dataBinder.h"
  30. #endif
  31. #define LOG( X ) kernelPrototype::m_pLog->Log( "KRNL", this, X )
  32. #ifdef DEBUG
  33. #define LOG_ERROR( X ) {\
  34. char _tBfrErrMSG234[256];\
  35. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  36. kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, _tBfrErrMSG234 );\
  37. }
  38. #else
  39. #define LOG_ERROR( X ) kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, X );
  40. #endif
  41. mojoKernel *mojoKernel::_instance = NULL;
  42. mojoKernel *mojoKernel::getInstance( config_setting_t *ptr ){
  43. if ( _instance == NULL ) {
  44. _instance = new mojoKernel( ptr );
  45. };
  46. return _instance;
  47. };
  48. mojoKernel::mojoKernel( config_setting_t *ptr ):kernelPrototype( ptr ){
  49. m_pEventHeap = basicEventHeap::getInstance();
  50. memset( &m_sSysInfo, 0, sizeof( DBC_SYS_LOAD_INFO ));
  51. m_pShadow = NULL;
  52. m_pSystemAnchor = NULL;
  53. m_bSessionStarted = false;
  54. m_bWorkWithRemoteServer = false;
  55. m_bMediaMode = 0;
  56. //setWM( WM_NONE );
  57. };
  58. mojoKernel::~mojoKernel(){
  59. WaitFinish( true );
  60. stopSession( false );
  61. #ifdef DEBUG
  62. LOG(" Deleting Node Factory...");
  63. #endif
  64. if( m_pNodeFactory != NULL ) delete( m_pNodeFactory );
  65. #ifdef DEBUG
  66. LOG(" Deleting Module Factory...");
  67. #endif
  68. if( m_pModuleFactory != NULL ) delete( m_pModuleFactory );
  69. #ifdef DEBUG
  70. LOG(" Deleting Target Filters...");
  71. #endif
  72. unloadFilters();
  73. if( EXIT_CODE_KERNEL_CLEAR == getExitCause() ) {
  74. #ifdef DEBUG
  75. LOG("Unregister Server");
  76. #endif
  77. m_pDBC->unregisterServer();
  78. #ifdef DEBUG
  79. //LOG("Close DataBase Connection...");
  80. #endif
  81. //if( m_pDBC != NULL ) delete( m_pDBC );//Deleting in Backend
  82. //m_pDBC = NULL;
  83. } else {
  84. if( EXIT_CODE_DETECT_HARDWARE == getExitCause() ) {
  85. #ifdef DEBUG
  86. LOG("HW PNP Detection Requested...");
  87. #endif
  88. //m_pDBC->updateNodePushedCMD( tCMD->id, 30 );
  89. m_pHardwareManager->enumerateHardware();
  90. //m_pDBC->updateNodePushedCMD( tCMD->id, 60 );
  91. };
  92. #ifdef DEBUG
  93. //LOG("Close DataBase Connection...");
  94. #endif
  95. //if( m_pDBC != NULL ) delete( m_pDBC );
  96. //m_pDBC = NULL;
  97. };
  98. delete( m_pHardwareManager );
  99. _instance = NULL;
  100. };
  101. bool mojoKernel::create(){
  102. if( m_pSettings != NULL ) {
  103. config_setting_t * pSetting;
  104. int tVal;
  105. const char * tBuf;
  106. char tBuffer[128];
  107. LOG("Getting Settings ...");
  108. if( !config_setting_lookup_string( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, &tBuf ) ){
  109. char * tVar;
  110. char * tTS;
  111. tTS = getTStr(NULL);
  112. tVar = MD5String(tTS);
  113. free(tTS);
  114. sprintf( m_sIdentity, tVar );
  115. pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  116. config_setting_set_string( pSetting, tVar );
  117. free( tVar );
  118. basicSettingsProvider::getInstance()->compromise();
  119. LOG("Server Identity not defined - generating new!");
  120. } else {
  121. sprintf( m_sIdentity, tBuf );
  122. sprintf( tBuffer, "Server Identity Read From Settings: %s", tBuf );
  123. LOG( tBuffer );
  124. };
  125. if( !config_setting_lookup_int( m_pSettings, MOJO_KERNEL_TAG_SITE_ID, &tVal ) ){
  126. tVal = m_nInstanceID = 1;
  127. pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SITE_ID, CONFIG_TYPE_INT );
  128. config_setting_set_int( pSetting, tVal );
  129. basicSettingsProvider::getInstance()->compromise();
  130. LOG("Site ID not defined - using [1] as number!");
  131. } else {
  132. m_nInstanceID = tVal;
  133. sprintf( tBuffer, "App Instance ID Read From Settings: %d", m_nInstanceID );
  134. LOG( tBuffer );
  135. };
  136. } else {
  137. LOG_ERROR("Failed Read Config Settings!");
  138. return false;
  139. };
  140. basicSettingsProvider::getInstance()->validate();
  141. LOG("Register Server ... ");
  142. m_pDBC->registerServer( m_nInstanceID, m_sIdentity, getpid(), &m_nServerID, &m_nServerNodeID );
  143. char tBuffer[128];
  144. sprintf( tBuffer,"Server Registered as [%d] nodeID[%d]",m_nServerID, m_nServerNodeID );
  145. LOG( tBuffer );
  146. DBC_SITE_DATA *tSiteData = m_pDBC->getSiteData( 1 );
  147. if( tSiteData ) {
  148. C3D_WGS84_LOC t;
  149. t.lat = tSiteData->lat;
  150. t.lon = tSiteData->lon;
  151. t.alt = tSiteData->alt;
  152. m_pSystemAnchor = systemAnchor::getInstance( tSiteData->SH, &t, tSiteData->local_binding );
  153. memcpy( &m_sSiteData, tSiteData, sizeof( DBC_SITE_DATA ));
  154. free( tSiteData );
  155. } else {
  156. LOG_ERROR("Error Loading Site DATA!");
  157. C3D_WGS84_LOC t;
  158. t.lat = 0;
  159. t.lon = 0;
  160. t.alt = 0;
  161. memset( &m_sSiteData, 0, sizeof( DBC_SITE_DATA ));
  162. m_pSystemAnchor = systemAnchor::getInstance( false, &t, true );
  163. };
  164. //Check date/Time if correct!
  165. // On some systems the System Date may be in past cause last session was interrupted by power loose
  166. uint32_t tTS = m_pDBC->getLastEventTS();
  167. if ( tTS ) {
  168. // Have last TS
  169. struct timespec tS = { 0 };
  170. if( clock_gettime( CLOCK_REALTIME, &tS ) == 0 ){
  171. //time_t cTS = time(NULL);
  172. if ( tTS > tS.tv_sec ) {
  173. // System Time is in the Past
  174. LOG("Correcting System Time!");
  175. tS.tv_sec = tTS;
  176. tS.tv_nsec = tTS * 1000000;
  177. if (clock_settime(CLOCK_REALTIME, &tS) != 0) LOG_ERROR("Time Correction Failed!");
  178. };
  179. };
  180. }
  181. #ifdef DEBUG
  182. else LOG("No Last TS fetched from DB!")
  183. #endif
  184. ;
  185. m_bDebugMode = m_pDBC->getAppDebugMode();
  186. m_bSessionStarted = m_pDBC->getEventSessionStatus( m_nServerNodeID );
  187. stopSession( false );
  188. m_pHardwareManager = hardwareManager::getInstance();
  189. loadFilters();
  190. m_pModuleFactory = kernelModuleFactory::getInstance( m_nInstanceID );
  191. if( !m_pModuleFactory ) {
  192. LOG_ERROR("Module Factory Failure!");
  193. return false;
  194. };
  195. // Read Media Mode ( Video/Pictures )
  196. kernelModuleEmbedVideo *tMO = (kernelModuleEmbedVideo*) m_pModuleFactory->getModule( KERNEL_MODULE_EMBED_VIDEO );
  197. if( tMO ) {
  198. m_bMediaMode = tMO->getMediaMode();
  199. } else {
  200. LOG_ERROR("No Embed Video Processing Module!");
  201. };
  202. m_pNodeFactory = deviceNodeFactory::getInstance();
  203. if( !m_pNodeFactory ){
  204. LOG_ERROR("Node Factory Failure!");
  205. return false;
  206. };
  207. return init();
  208. };
  209. void mojoKernel::startSession(){
  210. m_mSessionProtector.lock();
  211. m_nNoAlarmCounter = 0;
  212. if( !m_bSessionStarted ) {
  213. DBC_EVT_RECORD tRecord;
  214. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  215. uint64_t TS_64 = getMilliseconds() - 20; // -20 msec before
  216. tRecord.ts_s = TS_64/ 1000;
  217. tRecord.ts_ms = TS_64 % 1000;
  218. tRecord.node_id = m_nServerNodeID;
  219. tRecord.type = EVT_LRF_START_SESSION;
  220. CSTR tUUID = getUniqID_UUID( tRecord.ts_s );
  221. if( tUUID ) {
  222. strncpy(tRecord.uuid, tUUID, 64 );
  223. free( tUUID );
  224. } else {
  225. memset( tRecord.uuid, 0, 65 );
  226. };
  227. m_pDBC->addEvent( &tRecord );
  228. m_bSessionStarted = true;
  229. }
  230. m_mSessionProtector.unlock();
  231. };
  232. void mojoKernel::stopSession( bool forced ){
  233. m_mSessionProtector.lock();
  234. if( m_bSessionStarted ){
  235. DBC_EVT_RECORD tRecord;
  236. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  237. uint64_t TS_64 = getMilliseconds() + 20; // +20 msec before
  238. tRecord.ts_s = TS_64/ 1000;
  239. tRecord.ts_ms = TS_64 % 1000;
  240. tRecord.node_id = m_nServerNodeID;
  241. tRecord.type = forced ? EVT_LRF_END_SESSION_MA:EVT_LRF_END_SESSION_TO;
  242. CSTR tUUID = getUniqID_UUID( tRecord.ts_s );
  243. if( tUUID ) {
  244. strncpy(tRecord.uuid, tUUID, 64 );
  245. free( tUUID );
  246. } else {
  247. memset( tRecord.uuid, 0, 65 );
  248. };
  249. m_pDBC->addEvent( &tRecord );
  250. m_bSessionStarted = false;
  251. };
  252. m_mSessionProtector.unlock();
  253. };
  254. void mojoKernel::validateSession(){
  255. m_mSessionProtector.lock();
  256. if( m_bSessionStarted ){
  257. if( m_nNoAlarmCounter < NOTARGET_SESSION_CLOSE ){
  258. m_nNoAlarmCounter++;
  259. } else {
  260. DBC_EVT_RECORD tRecord;
  261. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  262. uint64_t TS_64 = getMilliseconds() + 20;
  263. tRecord.ts_s = TS_64/ 1000;
  264. tRecord.ts_ms = TS_64 % 1000;
  265. tRecord.node_id = m_nServerNodeID;
  266. tRecord.type = EVT_LRF_END_SESSION_TO;
  267. CSTR tUUID = getUniqID_UUID( tRecord.ts_s );
  268. if( tUUID ) {
  269. strncpy(tRecord.uuid, tUUID, 64 );
  270. free( tUUID );
  271. } else {
  272. memset( tRecord.uuid, 0, 65 );
  273. };
  274. m_pDBC->addEvent( &tRecord );
  275. m_bSessionStarted = false;
  276. };
  277. };
  278. m_mSessionProtector.unlock();
  279. };
  280. void mojoKernel::m_fThreadProc( void ){
  281. time_t tTimeTSeconds = 0;
  282. m_bThreadStatus = TH_WORKING;
  283. LOG("Mojo Started!");
  284. while ( m_bThreadStatus < TH_ENDED ) {
  285. m_tTimeTSeconds = time( NULL );
  286. //Each Second
  287. if( m_tTimeTSeconds != tTimeTSeconds ){
  288. tTimeTSeconds = m_tTimeTSeconds;
  289. m_bDebugMode = m_pDBC->getAppDebugMode();
  290. validateFilters();
  291. validateSession();
  292. validateReports();
  293. m_pHardwareManager->updateSysInfo( &m_sSysInfo );
  294. DBC_PUSHED_CMD * tCMD = m_pDBC->getNodePushedCMD( m_nServerNodeID ); //Kernel
  295. if( tCMD != NULL ) {
  296. m_pDBC->updateNodePushedCMD( tCMD->id, DBC_CMD_STARTED );
  297. switch( tCMD->CMD ){
  298. case CMD_KRNL_RELOAD:{
  299. LOG("Reload Mojo Node!");
  300. m_bThreadStatus = TH_ENDED;
  301. setExitCause( EXIT_CODE_RELOAD_SERVICES );
  302. break;
  303. };
  304. case CMD_KRNL_RESTART:{
  305. LOG("Restart Mojo Node!");
  306. m_bThreadStatus = TH_ENDED;
  307. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  308. break;
  309. };
  310. case CMD_KRNL_FACTORY_RESET:{
  311. LOG("Restart Mojo -> Factory Reset!");
  312. m_bThreadStatus = TH_ENDED;
  313. setExitCause( EXIT_CODE_FACTORY_RESET );
  314. break;
  315. };
  316. case CMD_NODES_RELOAD:{
  317. if( m_pNodeFactory != NULL ) {
  318. m_pNodeFactory->reloadNodes();
  319. };
  320. break;
  321. };
  322. case CMD_NODE_RELOAD:{
  323. uint32_t tNodeID = atoi( tCMD->PARAM );
  324. if( m_pNodeFactory != NULL ) {
  325. m_pNodeFactory->reloadNode( tNodeID );
  326. };
  327. break;
  328. };
  329. case CMD_KRNL_PNP:{
  330. if( m_pHardwareManager!= NULL ) {
  331. LOG_ERROR( "Restart and Detect HardWare!" );
  332. // Must be shutdowned all service that uses hardware prior to detection!
  333. m_bThreadStatus = TH_ENDED;
  334. setExitCause( EXIT_CODE_DETECT_HARDWARE ); // Go -> Detect Hardware in BACKEND
  335. };
  336. break;
  337. };
  338. case CMD_KRNL_UPDATE_IDENT:{
  339. LOG("Restart Mojo -> Update Ident!");
  340. sprintf( m_sIdentity, tCMD->PARAM );
  341. config_setting_remove ( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY );
  342. config_setting_t *pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  343. config_setting_set_string( pSetting, tCMD->PARAM );
  344. basicSettingsProvider::getInstance()->compromise();
  345. basicSettingsProvider::getInstance()->validate();
  346. m_pDBC->updateServerIdentity( m_nServerID, m_sIdentity );
  347. m_bThreadStatus = TH_ENDED;
  348. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  349. break;
  350. };
  351. case CMD_KRNL_REPLACE_DATABASE:{
  352. LOG("Restart Mojo -> Replace Database!");
  353. CSTR tNAME = m_pDBC->getDataParam( 0 );
  354. CSTR tUSER = m_pDBC->getDataParam( 1 );
  355. CSTR tGROUP = m_pDBC->getDataParam( 2 );
  356. if ( tNAME ) {
  357. char tB[256];
  358. sprintf( tB,"./data/%s.db", tCMD->PARAM );
  359. if ( fileExist( tB )) {
  360. LOG("Restart Mojo -> Close Database!");
  361. //Closing Database
  362. delete( m_pDBC );
  363. m_pDBC = NULL;
  364. LOG("Restart Mojo -> Delete Database!");
  365. //unlink( tNAME );
  366. LOG("Restart Mojo -> Copy Database!");
  367. fileCopy( tB, tNAME );
  368. LOG("Restart Mojo -> Chown Database!");
  369. chown( tNAME, tUSER, tGROUP );
  370. LOG("Restart Mojo -> Delete Old Database!");
  371. unlink( tB );
  372. m_bThreadStatus = TH_ENDED;
  373. LOG("Restart Mojo -> Exit!");
  374. setExitCause( EXIT_CODE_NO_DATABASE );//EXIT_CODE_RELOAD_SERVICES );
  375. m_bThreadStatus = TH_ENDED;
  376. } else {
  377. LOG_ERROR("New Database Does not Exists!");
  378. };
  379. //free( tNAME );
  380. //free( tUSER );
  381. //free( tGROUP );
  382. };
  383. break;
  384. };
  385. case CMD_KRNL_SYNC_TIME:{
  386. uint32_t uts = atoi( tCMD->PARAM );
  387. if( uts ){
  388. #if ( __GLIBC__ < 3 ) && ( __GLIBC_MINOR__ < 31 )
  389. time_t ts = (time_t)uts;
  390. if( stime( &ts ) == 0 ){
  391. // Restert Server;
  392. m_bThreadStatus = TH_ENDED;
  393. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  394. };
  395. #else
  396. timespec ts;
  397. ts.tv_sec = (time_t)uts;
  398. ts.tv_nsec = 0;
  399. if( clock_settime( CLOCK_REALTIME, &ts ) == 0 ){
  400. // Restert Server;
  401. m_bThreadStatus = TH_ENDED;
  402. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  403. };
  404. #endif
  405. };
  406. break;
  407. };
  408. case CMD_KRNL_SYSTEM_TASK:{
  409. break;
  410. };
  411. case NODE_FORCE_FINISH_ALARM:{
  412. stopSession();
  413. break;
  414. };
  415. };
  416. free( tCMD );
  417. };
  418. if ( m_pDBC ) m_pDBC->cleanNodePushedCMD();
  419. };
  420. SLEEP(SLEEP_KERNEL_INTERVAL);
  421. };
  422. };
  423. void mojoKernel::loadFilters(){
  424. if( m_pDBC == NULL ) return;
  425. DBC_INT_LIST *tList = m_pDBC->listFilters( m_nServerID );
  426. if( tList != NULL ) {
  427. if( tList->size() > 0 ) {
  428. LOG("Have Filters!");
  429. for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) {
  430. LOG("Load Filter!");
  431. DBC_TARGET_FLITER_DATA *tData = NULL;
  432. tData = m_pDBC->getTargetFilterData( (*idx) );
  433. if( tData != NULL ) {
  434. LOG("Filter Data OK!");
  435. switch( tData->type ) {
  436. case TF_LA_METRO_TRAIN_FILTER:{
  437. LOG("Create LA Train Filter");
  438. kernelEventFilterTrain *tFilter = new kernelEventFilterTrain( (*idx) );
  439. if( tFilter != NULL ) {
  440. if( tFilter->initFilter() ){
  441. m_vTargetFilters.push_back( (kernelEventFilter*) tFilter );
  442. } else {
  443. delete( tFilter );
  444. };
  445. };
  446. break;
  447. };
  448. case TF_AUTHORISED_PEOPLE_FILTER: {
  449. LOG("Create APF ");
  450. kernelEventFilterAPF *tFilter = new kernelEventFilterAPF( (*idx) );
  451. if( tFilter != NULL ) {
  452. if( tFilter->initFilter() ){
  453. m_vTargetFilters.push_back( (kernelEventFilter*) tFilter );
  454. } else {
  455. delete( tFilter );
  456. };
  457. };
  458. break;
  459. };
  460. };
  461. free( tData );
  462. };
  463. };
  464. tList->clear();
  465. };
  466. delete( tList );
  467. #ifdef DEBUG
  468. } else {
  469. LOG("No Target Filters Assigned!");
  470. #endif
  471. };
  472. };
  473. void mojoKernel::unloadFilters(){
  474. if( m_vTargetFilters.size() > 0 ) {
  475. #ifdef DEBUG
  476. LOG("Have Filters to Unload");
  477. #endif
  478. for( vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++ ){
  479. switch( (*idx)->getType() ) {
  480. case TF_LA_METRO_TRAIN_FILTER:{
  481. delete( (kernelEventFilterTrain*)(*idx) );
  482. break;
  483. };
  484. case TF_AUTHORISED_PEOPLE_FILTER:{
  485. delete( (kernelEventFilterAPF*)(*idx) );
  486. break;
  487. };
  488. default:{
  489. char tBuffer[64];
  490. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  491. LOG( tBuffer );
  492. };
  493. };
  494. };
  495. m_vTargetFilters.clear();
  496. LOG("Unload Filters Done!");
  497. #ifdef DEBUG
  498. } else {
  499. LOG("No Filters to Unload!");
  500. #endif
  501. };
  502. };
  503. void mojoKernel::validateFilters(){
  504. if( m_vTargetFilters.size() > 0 ) {
  505. for( vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++ ){
  506. if( !(*idx)->validate() ) {
  507. /*switch ((*idx)->getType()) {
  508. case TF_LA_METRO_TRAIN_FILTER:{
  509. delete( (kernelEventFilterTrain*)(*idx) );
  510. break;
  511. };
  512. case TF_AUTHORISED_PEOPLE_FILTER:{
  513. delete( (kernelEventFilterAPF*)(*idx) );
  514. break;
  515. };
  516. default:{
  517. char tBuffer[64];
  518. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  519. LOG( tBuffer );
  520. };
  521. };
  522. */
  523. };
  524. };
  525. };
  526. };
  527. void mojoKernel::dispatchEvent( EVENT_CONTAINER *Evt ){
  528. if ( m_bWorkWithRemoteServer ) return;
  529. if (m_pDBC == NULL) return;
  530. if( Evt == NULL ) return;
  531. switch( Evt->m_eClass ) {
  532. /*
  533. case EVT_CLASS_EXTERNAL_CMD:
  534. switch( Evt->m_eType ){
  535. case EVT_FH_JAMER_ON:{
  536. uint32_t TS_A, TS_B;
  537. TS_A = Evt->m_UMSTS / 1000;
  538. TS_B = Evt->m_UMSTS % 1000;
  539. #ifdef SIMULATE_KERNEL_TARGETS
  540. m_bJamingTO = SIMULATE_JAMER_TO;
  541. #endif
  542. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  543. if( tData ) {
  544. if( m_pDBC->addEvent(
  545. 0,
  546. Evt->m_UUID,
  547. TS_A,
  548. TS_B,
  549. EVT_FH_JAMER_ON,
  550. 0,
  551. 0,
  552. 0,
  553. 0,
  554. 0,
  555. 0,
  556. 0,
  557. 0,
  558. 0,
  559. 0,
  560. tData->evtUUID
  561. ) ){
  562. //Simulate FH Module Event;
  563. EVENT_CONTAINER *ttt;
  564. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  565. if( nnn ){
  566. nnn->location.lat = 0;
  567. nnn->location.lon = 0;
  568. nnn->location.alt = 0;
  569. sprintf( nnn->missionID, tData->evtUUID );
  570. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_JAMER_ON, NULL, NULL, nnn );
  571. if( ttt ) {
  572. m_pEventHeap->broadcast( ttt );
  573. } else {
  574. free( nnn );
  575. };
  576. };
  577. };
  578. free(tData);
  579. };
  580. break;
  581. };
  582. case EVT_FH_MISSION_END:{
  583. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  584. if( tData ) {
  585. //DBC_EVT_DATA * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  586. if( 1 / *tEVT* / ) {
  587. uint32_t TS_A, TS_B;
  588. TS_A = Evt->m_UMSTS / 1000;
  589. TS_B = Evt->m_UMSTS % 1000;
  590. if( m_pDBC->addEvent(
  591. 0,
  592. Evt->m_UUID,
  593. TS_A,
  594. TS_B,
  595. EVT_FH_LANDED ) ){
  596. //Simulate FH Module Event;
  597. EVENT_CONTAINER *ttt;
  598. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  599. if( nnn ){
  600. nnn->location.lat = 0;
  601. nnn->location.lon = 0;
  602. nnn->location.alt = 0;
  603. sprintf( nnn->missionID, tData->evtUUID );
  604. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_LANDED, NULL, NULL, nnn );
  605. if( ttt ) {
  606. m_pEventHeap->broadcast( ttt );
  607. } else {
  608. free( nnn );
  609. };
  610. };
  611. };
  612. //free( tEVT );
  613. } else {
  614. LOG_ERROR("Linked Event Was Not Found!");
  615. };
  616. };
  617. break;
  618. }
  619. case EVT_FH_MISSION_BEGIN:{
  620. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  621. #ifdef SIMULATE_KERNEL_TARGETS
  622. m_bLandingTO = SIMULATE_LANDING_TO;
  623. #endif
  624. if( tData ) {
  625. #ifdef SIMULATE_KERNEL_TARGETS
  626. sprintf( m_cCurrentMission, tData->evtUUID );
  627. #endif
  628. DBC_EVT_RECORD * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  629. if( tEVT ) {
  630. uint32_t TS_A, TS_B;
  631. TS_A = Evt->m_UMSTS / 1000;
  632. TS_B = Evt->m_UMSTS % 1000;
  633. if( Evt->m_db_id = m_pDBC->addEvent(
  634. 0,
  635. Evt->m_UUID,
  636. TS_A,
  637. TS_B,
  638. EVT_FH_FLY_TO,
  639. 0,
  640. 0,
  641. 0,
  642. 0,
  643. 0,
  644. 0,
  645. tEVT->lat,
  646. tEVT->lon,
  647. tEVT->alt,
  648. tEVT->id,
  649. tEVT->uuid
  650. ) ){
  651. //Simulate FH Module Event;
  652. EVENT_CONTAINER * ttt;
  653. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  654. if( nnn ){
  655. nnn->location.lat = tEVT->lat;
  656. nnn->location.lon = tEVT->lon;
  657. nnn->location.alt = tEVT->alt;
  658. sprintf( nnn->missionID, tData->evtUUID );
  659. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_MISSION_BEGIN, NULL, NULL, nnn );
  660. if( ttt ) {
  661. m_pEventHeap->broadcast( ttt );
  662. } else {
  663. free( nnn );
  664. };
  665. };
  666. };
  667. free( tEVT );
  668. } else {
  669. LOG_ERROR("Linked Event Was Not Found!");
  670. };
  671. };
  672. break;
  673. };
  674. };
  675. break;
  676. */
  677. case EVT_CLASS_VIDEO:{
  678. switch( Evt->m_eType ) {
  679. case EVT_CAPTURE_DONE:
  680. case EVT_CAPTURE_END:{
  681. if ( Evt->m_pEvtData ) {
  682. handleVideo( (VIDEO_SEGMENT_INFO *) Evt->m_pEvtData );
  683. memset(Evt->m_pEvtData, 0, sizeof(VIDEO_SEGMENT_INFO));
  684. Evt->m_pEvtData = NULL;
  685. #ifdef DEBUG
  686. LOG("VIDEO_SEGMENT_INFO Cleared!");
  687. #endif
  688. };
  689. break;
  690. };
  691. case EVT_VIDEO_FPS_DEGRADED:
  692. case EVT_VIDEO_FPS_RAISED:{
  693. if( !m_bDebugMode ) break;
  694. DBC_EVT_RECORD tRecord;
  695. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  696. uint32_t TS_A, TS_B;
  697. TS_A = Evt->m_UMSTS / 1000;
  698. TS_B = Evt->m_UMSTS % 1000;
  699. tRecord.node_id = Evt->m_nSourceNodeID;
  700. tRecord.type = Evt->m_eType;
  701. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  702. tRecord.ts_s = TS_A;
  703. tRecord.ts_ms = TS_B;
  704. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  705. break;
  706. };
  707. };
  708. break;
  709. };
  710. case EVT_CLASS_SENSOR_CLD:
  711. case EVT_CLASS_SENSOR_LGS:
  712. case EVT_CLASS_SENSOR_LFS:
  713. case EVT_CLASS_SENSOR_MND:
  714. case EVT_CLASS_SENSOR_PLS_210:
  715. case EVT_CLASS_SENSOR_PLS_360:
  716. case EVT_CLASS_SENSOR_ALS:
  717. case EVT_CLASS_SENSOR_3D:
  718. case EVT_CLASS_SENSOR_ACC:
  719. #ifdef DEBUG_EXTRA
  720. char tB[128];
  721. sprintf( tB,"Evt Class: %d Evt Type: %d ", Evt->m_eClass ,Evt->m_eType );
  722. LOG( tB );
  723. #endif
  724. switch( Evt->m_eType ) {
  725. case EVT_TARGET_1D:{
  726. startSession();
  727. TARGET_DATA *tDATA =(TARGET_DATA *) Evt->m_pEvtData;
  728. if( tDATA != NULL ) {
  729. DBC_EVT_RECORD tRecord;
  730. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  731. uint32_t TS_A, TS_B;
  732. TS_A = Evt->m_UMSTS / 1000;
  733. TS_B = Evt->m_UMSTS % 1000;
  734. tRecord.node_id = Evt->m_nSourceNodeID;
  735. tRecord.type = Evt->m_eType;
  736. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  737. tRecord.ts_s = TS_A;
  738. tRecord.ts_ms = TS_B;
  739. tRecord.range_digital = tDATA->digital;
  740. tRecord.ra = tDATA->local_target.r;
  741. tRecord.area = tDATA->area;
  742. tRecord.lat = tDATA->global_target.lat;
  743. tRecord.lon = tDATA->global_target.lon;
  744. tRecord.alt = tDATA->global_target.alt;
  745. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  746. };
  747. break;
  748. };
  749. // While Camera Always Records Video on each Absolute Move
  750. // an Event Must be generated to see video in report
  751. case EVT_ROI_ACTIVE: //if( !m_bDebugMode ) break;
  752. case EVT_TARGET_2D_BLACK:
  753. case EVT_TARGET_2D_WHITE:{
  754. startSession();
  755. TARGET_DATA *tDATA = (TARGET_DATA *)Evt->m_pEvtData;
  756. if( tDATA != NULL ) {
  757. DBC_EVT_RECORD tRecord;
  758. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  759. uint32_t TS_A, TS_B;
  760. TS_A = Evt->m_UMSTS / 1000;
  761. TS_B = Evt->m_UMSTS % 1000;
  762. tRecord.node_id = Evt->m_nSourceNodeID;
  763. tRecord.type = Evt->m_eType;
  764. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  765. tRecord.ts_s = TS_A;
  766. tRecord.ts_ms = TS_B;
  767. tRecord.range_digital = tDATA->digital;
  768. tRecord.az = tDATA->local_target.fi;
  769. tRecord.ra = tDATA->local_target.r;
  770. tRecord.area = tDATA->area;
  771. tRecord.lat = tDATA->global_target.lat;
  772. tRecord.lon = tDATA->global_target.lon;
  773. tRecord.alt = tDATA->global_target.alt;
  774. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  775. };
  776. break;
  777. };
  778. case EVT_TARGET_3D_BLACK:
  779. case EVT_TARGET_3D_WHITE:{
  780. startSession();
  781. TARGET_DATA *tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  782. if( tDATA != NULL ) {
  783. DBC_EVT_RECORD tRecord;
  784. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  785. uint32_t TS_A, TS_B;
  786. TS_A = Evt->m_UMSTS / 1000;
  787. TS_B = Evt->m_UMSTS % 1000;
  788. tRecord.node_id = Evt->m_nSourceNodeID;
  789. tRecord.type = Evt->m_eType;
  790. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  791. tRecord.ts_s = TS_A;
  792. tRecord.ts_ms = TS_B;
  793. tRecord.lrf_id = tDATA->lrf_id;
  794. tRecord.range_digital = tDATA->digital;
  795. tRecord.az = tDATA->local_target.fi;
  796. tRecord.el = tDATA->local_target.tetta;
  797. tRecord.ra = tDATA->local_target.r;
  798. tRecord.area = tDATA->area;
  799. tRecord.lat = tDATA->global_target.lat;
  800. tRecord.lon = tDATA->global_target.lon;
  801. tRecord.alt = tDATA->global_target.alt;
  802. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  803. };
  804. break;
  805. };
  806. case EVT_ERR_LRF_NO_IRQ:
  807. case EVT_ERR_LRF_NO_ZERO:
  808. case EVT_ERR_LRF_ANGLE:
  809. case EVT_ERR_LRF_VOLTAGE:
  810. case EVT_ERR_LRF_BROKEN:
  811. case EVT_LRF_VIS_CHANGED:
  812. case EVT_ERR_OVERFLOW:
  813. case EVT_SST_OVERFLOW:
  814. case EVT_COMMUNICATION_JAMED: if( !m_bDebugMode ) break;
  815. case EVT_OP_ACK_MASK:
  816. case EVT_OP_NACK_MASK:
  817. case EVT_OP_ON_MASK:
  818. case EVT_OP_OFF_MASK:
  819. case EVT_CONNECTED:
  820. case EVT_DISCONNECTED:{
  821. DBC_EVT_RECORD tRecord;
  822. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  823. uint32_t TS_A, TS_B;
  824. TS_A = Evt->m_UMSTS / 1000;
  825. TS_B = Evt->m_UMSTS % 1000;
  826. tRecord.node_id = Evt->m_nSourceNodeID;
  827. tRecord.type = Evt->m_eType;
  828. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  829. tRecord.ts_s = TS_A;
  830. tRecord.ts_ms = TS_B;
  831. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  832. break;
  833. };
  834. case EVT_FLIP:{
  835. if( !m_bDebugMode ) break;
  836. DBC_EVT_RECORD tRecord;
  837. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  838. uint32_t TS_A, TS_B;
  839. TS_A = Evt->m_UMSTS / 1000;
  840. TS_B = Evt->m_UMSTS % 1000;
  841. tRecord.node_id = Evt->m_nSourceNodeID;
  842. tRecord.type = Evt->m_eType;
  843. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  844. tRecord.ts_s = TS_A;
  845. tRecord.ts_ms = TS_B;
  846. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  847. break;
  848. };
  849. };
  850. break;
  851. case EVT_CLASS_KRNL_FILTER_LATF:{
  852. /*switch( Evt->m_eType ) {
  853. case EVT_FLT_LATF_CHK:
  854. case EVT_FLT_LATF_UP_DETEC:
  855. case EVT_FLT_LATF_BT_DETEC:
  856. case EVT_FLT_LATF_TRP_DET:
  857. case EVT_FLT_LATF_TRP_UPDT:
  858. case EVT_FLT_LATF_SUSP_START:
  859. case EVT_FLT_LATF_SUSP_NAK:
  860. case EVT_FLT_LATF_REGULAR:
  861. case EVT_FLT_LATF_DISABLED:
  862. case EVT_FLT_LATF_SUSP_DET:
  863. case EVT_FLT_LATF_DIFF_DET:
  864. };*/
  865. DBC_EVT_RECORD tRecord;
  866. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  867. uint32_t TS_A, TS_B;
  868. TS_A = Evt->m_UMSTS / 1000;
  869. TS_B = Evt->m_UMSTS % 1000;
  870. tRecord.node_id = m_nServerNodeID;
  871. tRecord.type = Evt->m_eType;
  872. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  873. tRecord.ts_s = TS_A;
  874. tRecord.ts_ms = TS_B;
  875. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  876. break;
  877. };
  878. };
  879. };
  880. void mojoKernel::handleVideo( VIDEO_SEGMENT_INFO *ptr ){
  881. if ( fileExist( ptr->scratchPath ) ) {
  882. if( ptr->recordMD5[0] ) {
  883. //if wm is debug -> add all to database, else send to server
  884. ptr->db_id = m_pDBC->addVideoRecord( ptr->stream_id, ptr->recordMD5, ptr->recordEXT, ptr->streamTSS, ptr->streamTMS, ptr->streamFPS );
  885. if ( ptr->db_id ) {
  886. #ifdef DEBUG
  887. LOG("Video File Registered");
  888. #endif
  889. for (int idx = 0; idx < ptr->eventIDS.size(); idx++) {
  890. #ifdef DEBUG
  891. LOG("Binding Event to a Video File!");
  892. #endif
  893. if( m_pDBC->addVideoEvent( ptr->eventIDS[idx], ptr->db_id, ptr->eventFrames[idx], ptr->eventTSS[idx], ptr->eventTMS[idx] )) {
  894. #ifdef DEBUG
  895. LOG("OK");
  896. #endif
  897. };
  898. };
  899. CSTR tP = m_pDBC->getCurrentReportDir();
  900. if ( tP ) {
  901. char tB[MAX_PATH];
  902. char dB[MAX_PATH];
  903. //sprintf( tB, "%s/data/videos/%s.%s", getcwd(dB,MAX_PATH), ptr->recordMD5, ptr->recordEXT );
  904. sprintf(tB, "%s/data/reports/%s/%s.%s", getcwd(dB, MAX_PATH), tP, ptr->recordMD5, ptr->recordEXT );
  905. fileCopy(ptr->scratchPath, tB);
  906. chown( tB, "nobody", "nogroup" );
  907. chmod( tB, 0664 );
  908. #ifdef DEBUG
  909. LOG("Video Saved");
  910. #endif
  911. free( tP );
  912. } else {
  913. LOG_ERROR("Cannot Get Report Path from DB!");
  914. };
  915. } else {
  916. LOG_ERROR("Failure in registration video!");
  917. };
  918. };
  919. unlink( ptr->scratchPath );
  920. #ifdef DEBUG
  921. LOG("Video Scratch Deleted!");
  922. #endif
  923. } else LOG_ERROR("Video File is Missing!");
  924. };
  925. bool mojoKernel::validateReports( void ){
  926. DBC_SITE_DATA *tSiteData = m_pDBC->getSiteData( 1 );
  927. if( tSiteData ) {
  928. // Check if settings has been changed in DB
  929. if ( tSiteData->report_cleanup != m_sSiteData.report_cleanup || tSiteData->preserve_empty != m_sSiteData.preserve_empty ) return false;
  930. } else {
  931. LOG_ERROR("Error Loading Site DATA!");
  932. return false;
  933. };
  934. if ( m_sSiteData.report_cleanup ) {
  935. cleanUpReportDir( m_sSiteData.report_cleanup );
  936. };
  937. if ( m_sSiteData.preserve_empty ) {
  938. float tT = 100.0f * ( 1.0f - ( 1.0f / ((float)m_sSiteData.preserve_empty)));
  939. #ifdef DEBUG_EXTRA
  940. char tB[128];
  941. sprintf( tB, "Check Disk Size: %0.3f %0.3f %d [%d]", m_sSysInfo.disk, tT, m_sSiteData.preserve_empty );
  942. LOG( tB );
  943. #endif
  944. if( m_sSysInfo.disk > tT ){
  945. // delete some oldest report date
  946. m_pDBC->deleteReportDate( m_pDBC->getFirstEventTS() );
  947. };
  948. };
  949. return true;
  950. };
  951. void mojoKernel::backupReports( uint32_t days ){
  952. REPORT_DIRINFO tRDI = {0};
  953. uint32_t cTS = (uint32_t)time(NULL);
  954. cTS -= 86400 * days;
  955. m_pDBC->fillRDF( cTS, tRDI );
  956. DBC_INT_LIST *TL = m_pDBC->listOlderReportStat( tRDI.YEAR, tRDI.MONTH, tRDI.DAY );
  957. if ( TL ) {
  958. for ( DBC_INT_LIST::iterator idx = TL->begin(); idx < TL->end(); idx++ ) {
  959. REPORT_DIRINFO *tRDI = m_pDBC->getReportDirInfo( *idx );
  960. if ( tRDI ) {
  961. char tB[MAX_PATH];
  962. char dB[MAX_PATH];
  963. sprintf( tB, "%s/data/reports/%s/%s/%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY );
  964. char tPATH[MAX_PATH];
  965. sprintf( tPATH, "%s/data/archive/%s_%s_%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY );
  966. if ( fileExist( tB ) ) {
  967. if ( !fileExist( tPATH ) ) {
  968. // Try Create Job if sucess then create dir to skip next validation
  969. if( createCronJob( 0, tPATH, cTS ) ){
  970. // Create Backup Dir
  971. if( m_pDBC->createAccessibleDir( tPATH ) ){
  972. // Create Just One Backup Per Time!
  973. free( tRDI );
  974. break;
  975. };
  976. };
  977. };// else proceed next
  978. };
  979. free( tRDI );
  980. };
  981. };
  982. delete( TL );
  983. };
  984. };
  985. void mojoKernel::cleanUpReportDir( uint32_t days ){
  986. // Cleanup Current reports no Backup
  987. uint32_t oTS = m_pDBC->getFirstEventTS();
  988. uint32_t cTS = (uint32_t)time(NULL);
  989. cTS -= 86400 * days;
  990. #ifdef DEBUG_EXTRA
  991. char tB[128];
  992. sprintf( tB, "Oldest %d border %d", oTS, cTS );
  993. LOG( tB );
  994. #endif
  995. if ( oTS <= cTS ) {
  996. m_pDBC->deleteReportDate( oTS );
  997. };
  998. };
  999. bool mojoKernel::createCronJob( uint8_t job, CSTR param_str, int param_int ){
  1000. if ( !fileExist("/srv/etc/cron.lock") ) {
  1001. Json::Value root;
  1002. //Json::Value data;
  1003. switch ( job ) {
  1004. case CRON_JOB_CREATE_BACKUP:{
  1005. root["operation"] = CRON_JOB_CREATE_BACKUP;
  1006. //data["number"] = 1;
  1007. root["ts"] = param_int;
  1008. root["backup_dst"] = std::string( param_str );
  1009. break;
  1010. };
  1011. // No default JOB!
  1012. default: return false;
  1013. };
  1014. FILE *fd = fopen("/srv/etc/cron_.json","w+");
  1015. if ( fd ) {
  1016. if( HUMAN_FRIENDLY_JSON ) {
  1017. Json::FastWriter writer;
  1018. const std::string json_file = writer.write(root);
  1019. fprintf( fd, json_file.c_str() );
  1020. } else {
  1021. Json::StreamWriterBuilder builder;
  1022. const std::string json_file = Json::writeString( builder, root );
  1023. fprintf( fd, json_file.c_str() );
  1024. };
  1025. fclose( fd );
  1026. return true;
  1027. };
  1028. };
  1029. return false;
  1030. };
  1031. #undef LOG
  1032. #undef LOG_ERROR