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.

1383 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. if( tCNT ){
  430. free(tA);
  431. tA = NULL;
  432. };
  433. ptr->CPU_COUNT = m_sSysInfo.CPU_CORES;
  434. ptr->TOT_DISK = m_sSysInfo.T_DISK;
  435. ptr->TOT_MEMO = m_sSysInfo.T_MEM;
  436. strncpy( ptr->TLS_VERSION, _BUILD_STRING_, 64 );
  437. ptr->TLS_VERSION[63]=(char)0;
  438. strncpy( ptr->LLS_VERSION, m_pHardwareManager->getSysInfo()->OS_INFO, 64 );
  439. ptr->LLS_VERSION[63]=(char)0;
  440. LOG("FIMO Overall info");
  441. return true;
  442. };
  443. #endif
  444. void mojoKernel::getSystemUtilization( DBC_SYS_LOAD_INFO *ptr ){
  445. if ( ptr == NULL ) return;
  446. m_mSysInfoProtector.lock();
  447. memcpy( ptr, &m_sSysInfo, sizeof(DBC_SYS_LOAD_INFO) );
  448. m_mSysInfoProtector.unlock();
  449. };
  450. void mojoKernel::m_fThreadProc( void ){
  451. time_t tTimeTSeconds = 0;
  452. m_bThreadStatus = TH_WORKING;
  453. LOG("Mojo Started!");
  454. while ( m_bThreadStatus < TH_ENDED ) {
  455. m_tTimeTSeconds = time( NULL );
  456. //Each Second
  457. if( m_tTimeTSeconds != tTimeTSeconds ){
  458. tTimeTSeconds = m_tTimeTSeconds;
  459. #ifdef LINUX_KERNEL_WATCHDOG
  460. if( m_iWatchdogFd > -1 ) {
  461. char tB[64];
  462. sprintf( tB, "4000\n" );
  463. //ToDo: Make Error Handling here!
  464. write( m_iWatchdogFd, &tB, strlen( tB ) );
  465. };
  466. #endif
  467. m_bDebugMode = m_pDBC->getAppDebugMode();
  468. validateFilters();
  469. validateSession();
  470. validateReports();
  471. //??
  472. if( !m_pHardwareManager->validate() ){
  473. LOG("Reload Mojo Node Due to Memory Consumption!");
  474. m_bThreadStatus = TH_ENDED;
  475. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  476. continue;
  477. };
  478. m_mSysInfoProtector.lock();
  479. m_pHardwareManager->updateSysInfo( &m_sSysInfo );
  480. m_mSysInfoProtector.unlock();
  481. if( m_sSiteData.use_remote_server ) {
  482. if( m_pRPC ){
  483. m_pRPC->validate();
  484. m_bWorkWithRemoteServer = ( m_pRPC->getConnectionStatus() == CS_CONNECTED );
  485. };
  486. };
  487. DBC_PUSHED_CMD * tCMD = m_pDBC->getNodePushedCMD( m_nServerNodeID ); //Kernel
  488. if( tCMD != NULL ) {
  489. m_pDBC->updateNodePushedCMD( tCMD->id, DBC_CMD_STARTED );
  490. switch( tCMD->CMD ){
  491. case CMD_KRNL_RELOAD:{
  492. LOG("Reload Mojo Node!");
  493. m_bThreadStatus = TH_ENDED;
  494. setExitCause( EXIT_CODE_RELOAD_SERVICES );
  495. break;
  496. };
  497. case CMD_KRNL_RESTART:{
  498. LOG("Restart Mojo Node!");
  499. m_bThreadStatus = TH_ENDED;
  500. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  501. break;
  502. };
  503. case CMD_KRNL_FACTORY_RESET:{
  504. LOG("Restart Mojo -> Factory Reset!");
  505. m_bThreadStatus = TH_ENDED;
  506. setExitCause( EXIT_CODE_FACTORY_RESET );
  507. break;
  508. };
  509. case CMD_NODES_RELOAD:{
  510. if( m_pNodeFactory != NULL ) {
  511. m_pNodeFactory->reloadNodes();
  512. };
  513. break;
  514. };
  515. case CMD_NODE_RELOAD:{
  516. uint32_t tNodeID = atoi( tCMD->PARAM );
  517. if( m_pNodeFactory != NULL ) {
  518. m_pNodeFactory->reloadNode( tNodeID );
  519. };
  520. break;
  521. };
  522. case CMD_KRNL_PNP:{
  523. if( m_pHardwareManager!= NULL ) {
  524. LOG_ERROR( "Restart and Detect HardWare!" );
  525. // Must be shutdowned all service that uses hardware prior to detection!
  526. m_bThreadStatus = TH_ENDED;
  527. setExitCause( EXIT_CODE_DETECT_HARDWARE ); // Go -> Detect Hardware in BACKEND
  528. };
  529. break;
  530. };
  531. case CMD_KRNL_UPDATE_IDENT:{
  532. LOG("Restart Mojo -> Update Ident!");
  533. sprintf( m_sIdentity, tCMD->PARAM );
  534. config_setting_remove ( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY );
  535. config_setting_t *pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  536. config_setting_set_string( pSetting, tCMD->PARAM );
  537. basicSettingsProvider::getInstance()->compromise();
  538. basicSettingsProvider::getInstance()->validate();
  539. m_pDBC->updateServerIdentity( m_nServerID, m_sIdentity );
  540. m_bThreadStatus = TH_ENDED;
  541. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  542. break;
  543. };
  544. case CMD_KRNL_REPLACE_DATABASE:{
  545. LOG("Restart Mojo -> Replace Database!");
  546. CSTR tNAME = m_pDBC->getDataParam( 0 );
  547. CSTR tUSER = m_pDBC->getDataParam( 1 );
  548. CSTR tGROUP = m_pDBC->getDataParam( 2 );
  549. if ( tNAME ) {
  550. char tB[256];
  551. sprintf( tB,"./data/%s.db", tCMD->PARAM );
  552. if ( fileExist( tB )) {
  553. LOG("Restart Mojo -> Close Database!");
  554. //Closing Database
  555. delete( m_pDBC );
  556. m_pDBC = NULL;
  557. LOG("Restart Mojo -> Delete Database!");
  558. //unlink( tNAME );
  559. LOG("Restart Mojo -> Copy Database!");
  560. fileCopy( tB, tNAME );
  561. LOG("Restart Mojo -> Chown Database!");
  562. ch_chown( tNAME, tUSER, tGROUP );
  563. LOG("Restart Mojo -> Delete Old Database!");
  564. unlink( tB );
  565. m_bThreadStatus = TH_ENDED;
  566. LOG("Restart Mojo -> Exit!");
  567. setExitCause( EXIT_CODE_NO_DATABASE );//EXIT_CODE_RELOAD_SERVICES );
  568. m_bThreadStatus = TH_ENDED;
  569. } else {
  570. LOG_ERROR("New Database Does not Exists!");
  571. };
  572. //free( tNAME );
  573. //free( tUSER );
  574. //free( tGROUP );
  575. };
  576. break;
  577. };
  578. case CMD_KRNL_SYNC_TIME:{
  579. uint32_t uts = atoi( tCMD->PARAM );
  580. if( uts ){
  581. #if ( __GLIBC__ < 3 ) && ( __GLIBC_MINOR__ < 31 )
  582. time_t ts = (time_t)uts;
  583. if( stime( &ts ) == 0 ){
  584. // Restert Server;
  585. m_bThreadStatus = TH_ENDED;
  586. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  587. };
  588. #else
  589. timespec ts;
  590. ts.tv_sec = (time_t)uts;
  591. ts.tv_nsec = 0;
  592. if( clock_settime( CLOCK_REALTIME, &ts ) == 0 ){
  593. // Restert Server;
  594. m_bThreadStatus = TH_ENDED;
  595. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  596. };
  597. #endif
  598. };
  599. break;
  600. };
  601. case CMD_KRNL_SYSTEM_TASK:{
  602. break;
  603. };
  604. case NODE_FORCE_FINISH_ALARM:{
  605. stopSession();
  606. break;
  607. };
  608. };
  609. free( tCMD );
  610. };
  611. if ( m_pDBC ) m_pDBC->cleanNodePushedCMD();
  612. };
  613. SLEEP(SLEEP_KERNEL_INTERVAL);
  614. };
  615. };
  616. void mojoKernel::loadFilters(){
  617. if( m_pDBC == NULL ) return;
  618. DBC_INT_LIST *tList = m_pDBC->listFilters( m_nServerID );
  619. if( tList != NULL ) {
  620. if( tList->size() > 0 ) {
  621. LOG("Have Filters!");
  622. for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) {
  623. LOG("Load Filter!");
  624. DBC_TARGET_FLITER_DATA *tData = NULL;
  625. tData = m_pDBC->getTargetFilterData( (*idx) );
  626. if( tData != NULL ) {
  627. LOG("Filter Data OK!");
  628. switch( tData->type ) {
  629. case TF_LA_METRO_TRAIN_FILTER:{
  630. LOG("Create LA Train Filter");
  631. kernelEventFilterTrain *tFilter = new kernelEventFilterTrain( (*idx) );
  632. if( tFilter != NULL ) {
  633. if( tFilter->initFilter() ){
  634. m_vTargetFilters.push_back( (kernelEventFilter*) tFilter );
  635. } else {
  636. delete( tFilter );
  637. };
  638. };
  639. break;
  640. };
  641. case TF_AUTHORISED_PEOPLE_FILTER: {
  642. LOG("Create APF ");
  643. kernelEventFilterAPF *tFilter = new kernelEventFilterAPF( (*idx) );
  644. if( tFilter != NULL ) {
  645. if( tFilter->initFilter() ){
  646. m_vTargetFilters.push_back( (kernelEventFilter*) tFilter );
  647. } else {
  648. delete( tFilter );
  649. };
  650. };
  651. break;
  652. };
  653. };
  654. free( tData );
  655. };
  656. };
  657. tList->clear();
  658. };
  659. delete( tList );
  660. #ifdef DEBUG
  661. } else {
  662. LOG("No Target Filters Assigned!");
  663. #endif
  664. };
  665. };
  666. void mojoKernel::unloadFilters(){
  667. if( m_vTargetFilters.size() > 0 ) {
  668. #ifdef DEBUG
  669. LOG("Have Filters to Unload");
  670. #endif
  671. for( vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++ ){
  672. switch( (*idx)->getType() ) {
  673. case TF_LA_METRO_TRAIN_FILTER:{
  674. delete( (kernelEventFilterTrain*)(*idx) );
  675. break;
  676. };
  677. case TF_AUTHORISED_PEOPLE_FILTER:{
  678. delete( (kernelEventFilterAPF*)(*idx) );
  679. break;
  680. };
  681. default:{
  682. char tBuffer[64];
  683. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  684. LOG( tBuffer );
  685. };
  686. };
  687. };
  688. m_vTargetFilters.clear();
  689. LOG("Unload Filters Done!");
  690. #ifdef DEBUG
  691. } else {
  692. LOG("No Filters to Unload!");
  693. #endif
  694. };
  695. };
  696. void mojoKernel::validateFilters(){
  697. if( m_vTargetFilters.size() > 0 ) {
  698. #ifdef DEBUG_EXTRA
  699. LOG("Filters Validation");
  700. #endif
  701. for( vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++ ){
  702. if( !(*idx)->validate() ) {
  703. LOG_ERROR("Filter is Invalid!");
  704. /*switch ((*idx)->getType()) {
  705. case TF_LA_METRO_TRAIN_FILTER:{
  706. delete( (kernelEventFilterTrain*)(*idx) );
  707. break;
  708. };
  709. case TF_AUTHORISED_PEOPLE_FILTER:{
  710. delete( (kernelEventFilterAPF*)(*idx) );
  711. break;
  712. };
  713. default:{
  714. char tBuffer[64];
  715. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  716. LOG( tBuffer );
  717. };
  718. };
  719. */
  720. };
  721. };
  722. };
  723. };
  724. bool mojoKernel::dispatchEvent( EVENT_CONTAINER *Evt ){
  725. if( Evt == NULL ) return false;
  726. if( m_bWorkWithRemoteServer && ( m_pRPC!=NULL) ) m_pRPC->addEvent( Evt );
  727. if( !m_bKeepLocalReport ) return true;
  728. if( m_pDBC == NULL) return false;
  729. switch (Evt->m_eClass) {
  730. case EVT_CLASS_SYSTEM:
  731. switch (Evt->m_eType) {
  732. case EVT_LRF_START_SESSION:
  733. case EVT_LRF_END_SESSION_TO:
  734. case EVT_LRF_END_SESSION_MA:{
  735. DBC_EVT_RECORD tRecord;
  736. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  737. tRecord.ts_s = Evt->m_UMSTS / 1000;
  738. tRecord.ts_ms = Evt->m_UMSTS % 1000;
  739. tRecord.node_id = m_nServerNodeID;
  740. tRecord.type = Evt->m_eType;
  741. strncpy(tRecord.uuid, Evt->m_UUID, 64 );
  742. m_pDBC->addEvent( &tRecord );
  743. break;
  744. };
  745. };
  746. break;
  747. /*
  748. case EVT_CLASS_EXTERNAL_CMD:
  749. switch( Evt->m_eType ){
  750. case EVT_FH_JAMER_ON:{
  751. uint32_t TS_A, TS_B;
  752. TS_A = Evt->m_UMSTS / 1000;
  753. TS_B = Evt->m_UMSTS % 1000;
  754. #ifdef SIMULATE_KERNEL_TARGETS
  755. m_bJamingTO = SIMULATE_JAMER_TO;
  756. #endif
  757. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  758. if( tData ) {
  759. if( m_pDBC->addEvent(
  760. 0,
  761. Evt->m_UUID,
  762. TS_A,
  763. TS_B,
  764. EVT_FH_JAMER_ON,
  765. 0,
  766. 0,
  767. 0,
  768. 0,
  769. 0,
  770. 0,
  771. 0,
  772. 0,
  773. 0,
  774. 0,
  775. tData->evtUUID
  776. ) ){
  777. //Simulate FH Module Event;
  778. EVENT_CONTAINER *ttt;
  779. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  780. if( nnn ){
  781. nnn->location.lat = 0;
  782. nnn->location.lon = 0;
  783. nnn->location.alt = 0;
  784. sprintf( nnn->missionID, tData->evtUUID );
  785. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_JAMER_ON, NULL, NULL, nnn );
  786. if( ttt ) {
  787. m_pEventHeap->broadcast( ttt );
  788. } else {
  789. free( nnn );
  790. };
  791. };
  792. };
  793. free(tData);
  794. };
  795. break;
  796. };
  797. case EVT_FH_MISSION_END:{
  798. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  799. if( tData ) {
  800. //DBC_EVT_DATA * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  801. if( 1 / *tEVT* / ) {
  802. uint32_t TS_A, TS_B;
  803. TS_A = Evt->m_UMSTS / 1000;
  804. TS_B = Evt->m_UMSTS % 1000;
  805. if( m_pDBC->addEvent(
  806. 0,
  807. Evt->m_UUID,
  808. TS_A,
  809. TS_B,
  810. EVT_FH_LANDED ) ){
  811. //Simulate FH Module Event;
  812. EVENT_CONTAINER *ttt;
  813. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  814. if( nnn ){
  815. nnn->location.lat = 0;
  816. nnn->location.lon = 0;
  817. nnn->location.alt = 0;
  818. sprintf( nnn->missionID, tData->evtUUID );
  819. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_LANDED, NULL, NULL, nnn );
  820. if( ttt ) {
  821. m_pEventHeap->broadcast( ttt );
  822. } else {
  823. free( nnn );
  824. };
  825. };
  826. };
  827. //free( tEVT );
  828. } else {
  829. LOG_ERROR("Linked Event Was Not Found!");
  830. };
  831. };
  832. break;
  833. }
  834. case EVT_FH_MISSION_BEGIN:{
  835. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  836. #ifdef SIMULATE_KERNEL_TARGETS
  837. m_bLandingTO = SIMULATE_LANDING_TO;
  838. #endif
  839. if( tData ) {
  840. #ifdef SIMULATE_KERNEL_TARGETS
  841. sprintf( m_cCurrentMission, tData->evtUUID );
  842. #endif
  843. DBC_EVT_RECORD * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  844. if( tEVT ) {
  845. uint32_t TS_A, TS_B;
  846. TS_A = Evt->m_UMSTS / 1000;
  847. TS_B = Evt->m_UMSTS % 1000;
  848. if( Evt->m_db_id = m_pDBC->addEvent(
  849. 0,
  850. Evt->m_UUID,
  851. TS_A,
  852. TS_B,
  853. EVT_FH_FLY_TO,
  854. 0,
  855. 0,
  856. 0,
  857. 0,
  858. 0,
  859. 0,
  860. tEVT->lat,
  861. tEVT->lon,
  862. tEVT->alt,
  863. tEVT->id,
  864. tEVT->uuid
  865. ) ){
  866. //Simulate FH Module Event;
  867. EVENT_CONTAINER * ttt;
  868. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  869. if( nnn ){
  870. nnn->location.lat = tEVT->lat;
  871. nnn->location.lon = tEVT->lon;
  872. nnn->location.alt = tEVT->alt;
  873. sprintf( nnn->missionID, tData->evtUUID );
  874. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_MISSION_BEGIN, NULL, NULL, nnn );
  875. if( ttt ) {
  876. m_pEventHeap->broadcast( ttt );
  877. } else {
  878. free( nnn );
  879. };
  880. };
  881. };
  882. free( tEVT );
  883. } else {
  884. LOG_ERROR("Linked Event Was Not Found!");
  885. };
  886. };
  887. break;
  888. };
  889. };
  890. break;
  891. */
  892. case EVT_CLASS_VIDEO:{
  893. switch( Evt->m_eType ) {
  894. case EVT_CAPTURE_DONE:
  895. case EVT_CAPTURE_END:{
  896. if ( Evt->m_pEvtData ) {
  897. handleVideo( (VIDEO_SEGMENT_INFO *) Evt->m_pEvtData );
  898. //Dont free Data - it points to static struct of Encoder!!
  899. memset(Evt->m_pEvtData, 0, sizeof(VIDEO_SEGMENT_INFO));
  900. Evt->m_pEvtData = NULL;
  901. #ifdef DEBUG_EXTRA
  902. LOG("VIDEO_SEGMENT_INFO Cleared!");
  903. #endif
  904. };
  905. break;
  906. };
  907. case EVT_VIDEO_FPS_DEGRADED:
  908. case EVT_VIDEO_FPS_RAISED:{
  909. if( !m_bDebugMode ) break;
  910. DBC_EVT_RECORD tRecord;
  911. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  912. uint32_t TS_A, TS_B;
  913. TS_A = Evt->m_UMSTS / 1000;
  914. TS_B = Evt->m_UMSTS % 1000;
  915. tRecord.node_id = Evt->m_nSourceNodeID;
  916. tRecord.type = Evt->m_eType;
  917. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  918. tRecord.ts_s = TS_A;
  919. tRecord.ts_ms = TS_B;
  920. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  921. break;
  922. };
  923. };
  924. break;
  925. };
  926. case EVT_CLASS_SENSOR_CLD:
  927. case EVT_CLASS_SENSOR_LGS:
  928. case EVT_CLASS_SENSOR_LFS:
  929. case EVT_CLASS_SENSOR_MND:
  930. case EVT_CLASS_SENSOR_PLS_210:
  931. case EVT_CLASS_SENSOR_PLS_360:
  932. case EVT_CLASS_SENSOR_ALS:
  933. case EVT_CLASS_SENSOR_3D:
  934. case EVT_CLASS_SENSOR_ACC:
  935. #ifdef DEBUG_EXTRA
  936. char tB[128];
  937. sprintf( tB,"Evt Class: %d Evt Type: %d ", Evt->m_eClass ,Evt->m_eType );
  938. LOG( tB );
  939. #endif
  940. switch( Evt->m_eType ) {
  941. case EVT_TARGET_1D:{
  942. //startSession();
  943. preserveSession();
  944. TARGET_DATA *tDATA =(TARGET_DATA *) Evt->m_pEvtData;
  945. if( tDATA != NULL ) {
  946. DBC_EVT_RECORD tRecord;
  947. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  948. uint32_t TS_A, TS_B;
  949. TS_A = Evt->m_UMSTS / 1000;
  950. TS_B = Evt->m_UMSTS % 1000;
  951. tRecord.node_id = Evt->m_nSourceNodeID;
  952. tRecord.type = Evt->m_eType;
  953. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  954. tRecord.ts_s = TS_A;
  955. tRecord.ts_ms = TS_B;
  956. tRecord.range_digital = tDATA->digital;
  957. tRecord.ra = tDATA->local_target.r;
  958. tRecord.area = tDATA->area;
  959. tRecord.lat = tDATA->global_target.lat;
  960. tRecord.lon = tDATA->global_target.lon;
  961. tRecord.alt = tDATA->global_target.alt;
  962. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  963. };
  964. break;
  965. };
  966. // While Camera Always Records Video on each Absolute Move
  967. // an Event Must be generated to see video in report
  968. case EVT_ROI_ACTIVE: //if( !m_bDebugMode ) break;
  969. case EVT_TARGET_2D_BLACK:
  970. case EVT_TARGET_2D_WHITE:{
  971. //startSession();
  972. preserveSession();
  973. TARGET_DATA *tDATA = (TARGET_DATA *)Evt->m_pEvtData;
  974. if( tDATA != NULL ) {
  975. DBC_EVT_RECORD tRecord;
  976. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  977. uint32_t TS_A, TS_B;
  978. TS_A = Evt->m_UMSTS / 1000;
  979. TS_B = Evt->m_UMSTS % 1000;
  980. tRecord.node_id = Evt->m_nSourceNodeID;
  981. tRecord.type = Evt->m_eType;
  982. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  983. tRecord.ts_s = TS_A;
  984. tRecord.ts_ms = TS_B;
  985. tRecord.range_digital = tDATA->digital;
  986. tRecord.az = tDATA->local_target.fi;
  987. tRecord.ra = tDATA->local_target.r;
  988. tRecord.area = tDATA->area;
  989. tRecord.lat = tDATA->global_target.lat;
  990. tRecord.lon = tDATA->global_target.lon;
  991. tRecord.alt = tDATA->global_target.alt;
  992. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  993. };
  994. break;
  995. };
  996. case EVT_TARGET_3D_BLACK:
  997. case EVT_TARGET_3D_WHITE:{
  998. //startSession();
  999. preserveSession();
  1000. TARGET_DATA *tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1001. if( tDATA != NULL ) {
  1002. DBC_EVT_RECORD tRecord;
  1003. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1004. uint32_t TS_A, TS_B;
  1005. TS_A = Evt->m_UMSTS / 1000;
  1006. TS_B = Evt->m_UMSTS % 1000;
  1007. tRecord.node_id = Evt->m_nSourceNodeID;
  1008. tRecord.type = Evt->m_eType;
  1009. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1010. tRecord.ts_s = TS_A;
  1011. tRecord.ts_ms = TS_B;
  1012. tRecord.lrf_id = tDATA->lrf_id;
  1013. tRecord.range_digital = tDATA->digital;
  1014. tRecord.az = tDATA->local_target.fi;
  1015. tRecord.el = tDATA->local_target.tetta;
  1016. tRecord.ra = tDATA->local_target.r;
  1017. tRecord.area = tDATA->area;
  1018. tRecord.lat = tDATA->global_target.lat;
  1019. tRecord.lon = tDATA->global_target.lon;
  1020. tRecord.alt = tDATA->global_target.alt;
  1021. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1022. };
  1023. break;
  1024. };
  1025. case EVT_ERR_LRF_NO_IRQ:
  1026. case EVT_ERR_LRF_NO_ZERO:
  1027. case EVT_ERR_LRF_ANGLE:
  1028. case EVT_ERR_LRF_VOLTAGE:
  1029. case EVT_ERR_LRF_BROKEN:
  1030. case EVT_LRF_VIS_CHANGED:
  1031. case EVT_ERR_OVERFLOW:
  1032. case EVT_SST_OVERFLOW:
  1033. case EVT_COMMUNICATION_JAMED: if( !m_bDebugMode ) break;
  1034. case EVT_OP_ACK_MASK:
  1035. case EVT_OP_NACK_MASK:
  1036. case EVT_OP_ON_MASK:
  1037. case EVT_OP_OFF_MASK:
  1038. case EVT_CONNECTED:
  1039. case EVT_DISCONNECTED:{
  1040. DBC_EVT_RECORD tRecord;
  1041. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1042. uint32_t TS_A, TS_B;
  1043. TS_A = Evt->m_UMSTS / 1000;
  1044. TS_B = Evt->m_UMSTS % 1000;
  1045. tRecord.node_id = Evt->m_nSourceNodeID;
  1046. tRecord.type = Evt->m_eType;
  1047. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1048. tRecord.ts_s = TS_A;
  1049. tRecord.ts_ms = TS_B;
  1050. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1051. break;
  1052. };
  1053. case EVT_FLIP:{
  1054. if( !m_bDebugMode ) break;
  1055. DBC_EVT_RECORD tRecord;
  1056. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1057. uint32_t TS_A, TS_B;
  1058. TS_A = Evt->m_UMSTS / 1000;
  1059. TS_B = Evt->m_UMSTS % 1000;
  1060. tRecord.node_id = Evt->m_nSourceNodeID;
  1061. tRecord.type = Evt->m_eType;
  1062. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1063. tRecord.ts_s = TS_A;
  1064. tRecord.ts_ms = TS_B;
  1065. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1066. break;
  1067. };
  1068. };
  1069. break;
  1070. case EVT_CLASS_KRNL_FILTER_LATF:{
  1071. TARGET_DATA *tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1072. if ( ( Evt->m_eType > EVT_FLT_LATF_DIFF_DET ) || ( tDATA == NULL ) ) {
  1073. //AUX messages (no additional target data)
  1074. DBC_EVT_RECORD tRecord;
  1075. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1076. uint32_t TS_A, TS_B;
  1077. TS_A = Evt->m_UMSTS / 1000;
  1078. TS_B = Evt->m_UMSTS % 1000;
  1079. tRecord.node_id = m_nServerNodeID;
  1080. tRecord.type = Evt->m_eType;
  1081. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1082. tRecord.ts_s = TS_A;
  1083. tRecord.ts_ms = TS_B;
  1084. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1085. } else {
  1086. //If First Suspiciouse -> Start session to collect Fliter Messages
  1087. //if( Evt->m_eType == EVT_FLT_LATF_SUSP_START ) startSession();
  1088. // Regular LATF with target Info
  1089. DBC_EVT_RECORD tRecord;
  1090. memset( &tRecord, 0, sizeof(DBC_EVT_RECORD));
  1091. uint32_t TS_A, TS_B;
  1092. TS_A = Evt->m_UMSTS / 1000;
  1093. TS_B = Evt->m_UMSTS % 1000;
  1094. tRecord.node_id = Evt->m_nSourceNodeID;// m_nServerNodeID;//??
  1095. tRecord.type = Evt->m_eType;
  1096. strncpy( tRecord.uuid, Evt->m_UUID, 64 );
  1097. tRecord.ts_s = TS_A;
  1098. tRecord.ts_ms = TS_B;
  1099. tRecord.lrf_id = tDATA->lrf_id;
  1100. tRecord.range_digital = tDATA->digital;
  1101. tRecord.az = tDATA->local_target.fi;
  1102. tRecord.el = tDATA->local_target.tetta;
  1103. tRecord.ra = tDATA->local_target.r;
  1104. tRecord.area = tDATA->area;
  1105. tRecord.lat = 0;
  1106. tRecord.lon = 0;
  1107. tRecord.alt = 0;
  1108. #ifdef DEBUG_EXTRA
  1109. char tB[243];
  1110. 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 );
  1111. LOG( tB );
  1112. #endif
  1113. Evt->m_db_id = m_pDBC->addEvent( &tRecord );
  1114. };
  1115. break;
  1116. };
  1117. };
  1118. return true;
  1119. };
  1120. /*
  1121. typedef struct S_VIDEO_SEGMENT_INFO{
  1122. uint32_t stream_id;
  1123. uint32_t streamTSS;
  1124. uint32_t streamTMS;
  1125. DBC_INT_LIST eventIDS;
  1126. DBC_INT_LIST eventFrames;
  1127. DBC_INT_LIST eventTSS;
  1128. DBC_INT_LIST eventTMS;
  1129. char recordMD5[33];
  1130. char recordEXT[6];
  1131. uint8_t streamFPS;
  1132. char scratchPath[PATH_MAX];
  1133. uint32_t dataSize;
  1134. uint32_t db_id;
  1135. }VIDEO_SEGMENT_INFO;
  1136. */
  1137. void mojoKernel::handleVideo( VIDEO_SEGMENT_INFO *ptr ){
  1138. if ( fileExist( ptr->scratchPath ) ) {
  1139. bool bKeepVideoFile = false;
  1140. if( ptr->recordMD5[0] ) {
  1141. if( m_bKeepLocalReport ) {
  1142. //if wm is debug -> add all to database, else send to server
  1143. ptr->db_id = m_pDBC->addVideoRecord( ptr->stream_id, ptr->recordMD5, ptr->recordEXT, ptr->streamTSS, ptr->streamTMS, ptr->streamFPS );
  1144. if ( ptr->db_id ) {
  1145. #ifdef DEBUG_EXTRA
  1146. LOG("Video File Registered");
  1147. #endif
  1148. for (int idx = 0; idx < ptr->eventIDS.size(); idx++) {
  1149. #ifdef DEBUG_EXTRA
  1150. LOG("Binding Event to a Video File!");
  1151. #endif
  1152. if( m_pDBC->addVideoEvent( ptr->eventIDS[idx], ptr->db_id, ptr->eventFrames[idx], ptr->eventTSS[idx], ptr->eventTMS[idx] )) {
  1153. #ifdef DEBUG_EXTRA
  1154. LOG("OK");
  1155. #endif
  1156. };
  1157. };
  1158. CSTR tP = m_pDBC->getCurrentReportDir();
  1159. if ( tP ) {
  1160. char tB[MAX_PATH];
  1161. char dB[MAX_PATH];
  1162. //sprintf( tB, "%s/data/videos/%s.%s", getcwd(dB,MAX_PATH), ptr->recordMD5, ptr->recordEXT );
  1163. sprintf(tB, "%s/data/reports/%s/%s.%s", getcwd(dB, MAX_PATH), tP, ptr->recordMD5, ptr->recordEXT );
  1164. fileCopy( ptr->scratchPath, tB );
  1165. ch_chown( tB, "nobody", "nogroup" );
  1166. chmod( tB, 0664 );
  1167. #ifdef DEBUG_EXTRA
  1168. LOG("Video Saved");
  1169. #endif
  1170. free( tP );
  1171. } else {
  1172. LOG_ERROR("Cannot Get Report Path from DB!");
  1173. };
  1174. } else {
  1175. LOG_ERROR("Failure in registration video!");
  1176. };
  1177. };
  1178. if( m_bWorkWithRemoteServer && ( m_pRPC!=NULL) ) bKeepVideoFile = m_pRPC->addVideo( ptr );
  1179. };
  1180. if( !bKeepVideoFile ){
  1181. unlink( ptr->scratchPath );
  1182. #ifdef DEBUG_EXTRA
  1183. LOG("Video Scratch Deleted!");
  1184. #endif
  1185. };
  1186. } else LOG_ERROR("Video File is Missing!");
  1187. };
  1188. bool mojoKernel::validateReports( void ){
  1189. if ( m_bKeepLocalReport ) {
  1190. DBC_SITE_DATA *tSiteData = m_pDBC->getSiteData(1);
  1191. if( tSiteData ) {
  1192. // Check if settings has been changed in DB
  1193. if ( tSiteData->report_cleanup != m_sSiteData.report_cleanup || tSiteData->preserve_empty != m_sSiteData.preserve_empty ) return false;
  1194. } else {
  1195. LOG_ERROR("Error Loading Site DATA!");
  1196. return false;
  1197. };
  1198. if ( m_sSiteData.report_cleanup ) {
  1199. cleanUpReportDir( m_sSiteData.report_cleanup );
  1200. };
  1201. if ( m_sSiteData.preserve_empty ) {
  1202. float tT = 100.0f * ( 1.0f - ( 1.0f / ((float)m_sSiteData.preserve_empty)));
  1203. #ifdef DEBUG_EXTRA
  1204. char tB[128];
  1205. sprintf( tB, "Check Disk Size: %0.3f %0.3f %d [%d]", m_sSysInfo.disk, tT, m_sSiteData.preserve_empty );
  1206. LOG( tB );
  1207. #endif
  1208. if( m_sSysInfo.disk > tT ){
  1209. #ifdef DEBUG_EXTRA
  1210. LOG("Delete Some Reports!");
  1211. #endif
  1212. // delete some oldest report date
  1213. m_pDBC->deleteReportDate( m_pDBC->getFirstEventTS() );
  1214. };
  1215. };
  1216. }
  1217. return true;
  1218. };
  1219. void mojoKernel::backupReports( uint32_t days ){
  1220. if ( !m_bKeepLocalReport ) return;
  1221. REPORT_DIRINFO tRDI = { 0 };
  1222. uint32_t cTS = (uint32_t)time(NULL);
  1223. cTS -= 86400 * days;
  1224. fillRDF( cTS, tRDI );
  1225. DBC_INT_LIST *TL = m_pDBC->listOlderReportStat( tRDI.YEAR, tRDI.MONTH, tRDI.DAY );
  1226. if ( TL ) {
  1227. for ( DBC_INT_LIST::iterator idx = TL->begin(); idx < TL->end(); idx++ ) {
  1228. REPORT_DIRINFO *tRDI = m_pDBC->getReportDirInfo( *idx );
  1229. if ( tRDI ) {
  1230. char tB[MAX_PATH];
  1231. char dB[MAX_PATH];
  1232. sprintf( tB, "%s/data/reports/%s/%s/%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY );
  1233. char tPATH[MAX_PATH];
  1234. sprintf( tPATH, "%s/data/archive/%s_%s_%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY );
  1235. if ( fileExist( tB ) ) {
  1236. if ( !fileExist( tPATH ) ) {
  1237. // Try Create Job if sucess then create dir to skip next validation
  1238. if( createCronJob( 0, tPATH, cTS ) ){
  1239. // Create Backup Dir
  1240. uid_t uid = getUserUid( m_pDBC->getDataParam( 1 ) );
  1241. gid_t gid = getUserGid( m_pDBC->getDataParam( 2 ) );
  1242. if( createAccessibleDir( tPATH, STORAGE_DIR_MODE, uid, gid ) ){
  1243. // Create Just One Backup Per Time!
  1244. free( tRDI );
  1245. break;
  1246. };
  1247. };
  1248. };// else proceed next
  1249. };
  1250. free( tRDI );
  1251. };
  1252. };
  1253. delete( TL );
  1254. };
  1255. };
  1256. void mojoKernel::cleanUpReportDir( uint32_t days ){
  1257. // Cleanup Current reports no Backup
  1258. uint32_t oTS = m_pDBC->getFirstEventTS();
  1259. uint32_t cTS = (uint32_t)time(NULL);
  1260. cTS -= 86400 * days;
  1261. #ifdef DEBUG_EXTRA
  1262. char tB[128];
  1263. sprintf( tB, "Oldest %d border %d", oTS, cTS );
  1264. LOG( tB );
  1265. #endif
  1266. if ( oTS <= cTS ) {
  1267. #ifdef DEBUG_EXTRA
  1268. LOG("Deleting Old Reports");
  1269. #endif
  1270. m_pDBC->deleteReportDate( oTS );
  1271. };
  1272. };
  1273. bool mojoKernel::createCronJob( uint8_t job, CSTR param_str, int param_int ){
  1274. if ( !fileExist("/srv/etc/cron.lock") ) {
  1275. Json::Value root;
  1276. //Json::Value data;
  1277. switch ( job ) {
  1278. case CRON_JOB_CREATE_BACKUP:{
  1279. root["operation"] = CRON_JOB_CREATE_BACKUP;
  1280. //data["number"] = 1;
  1281. root["ts"] = param_int;
  1282. root["backup_dst"] = std::string( param_str );
  1283. break;
  1284. };
  1285. // No default JOB!
  1286. default: return false;
  1287. };
  1288. FILE *fd = fopen("/srv/etc/cron_.json","w+");
  1289. if ( fd ) {
  1290. if( HUMAN_FRIENDLY_JSON ) {
  1291. Json::FastWriter writer;
  1292. const std::string json_file = writer.write(root);
  1293. fprintf( fd, json_file.c_str() );
  1294. } else {
  1295. Json::StreamWriterBuilder builder;
  1296. const std::string json_file = Json::writeString( builder, root );
  1297. fprintf( fd, json_file.c_str() );
  1298. };
  1299. fclose( fd );
  1300. return true;
  1301. };
  1302. };
  1303. return false;
  1304. };
  1305. #undef LOG
  1306. #undef LOG_ERROR