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.

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