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.

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