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.

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