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.

1103 lines
42 KiB

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