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.

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