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.

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