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.

1083 lines
42 KiB

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