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.

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