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.

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