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.

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