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.

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