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.

1181 lines
45 KiB

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