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.

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