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.

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