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.

1470 lines
58 KiB

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