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.

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