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.

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