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.

326 lines
9.1 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*******************************************************************
  2. *
  3. * DESCRIPTION: ARGUS Kernel source
  4. *
  5. * AUTHOR: gagua
  6. *
  7. * HISTORY:
  8. *
  9. * DATE:5/29/2019
  10. *
  11. *******************************************************************/
  12. #include "RGoose_Kernel.h"
  13. #define LOG( X ) m_pLog->Log( "Kernel", this, X )
  14. #define LOG_ERROR( X ) m_pLog->Log( "Kernel", this, _LOG_ERROR, X )
  15. ARGUS_Kernel::ARGUS_Kernel( config_setting_t *Settings ):basicThread(){
  16. m_bExitCause = 0;
  17. m_pDBC = NULL;
  18. m_tTimeTSeconds = time( NULL );
  19. m_pLog = basicLogCollection::getInstance();
  20. m_pEventHeap = basicEventHeap::getInstance();
  21. m_pOnvifMediaObserver = onvifMediaObserver::getInstance();
  22. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "KERNEL" );
  23. if( basicSettingsProvider::getInstance()->isCompromised() ) {
  24. basicSettingsProvider::getInstance()->validate();
  25. };
  26. };
  27. ARGUS_Kernel::~ARGUS_Kernel(){
  28. #ifdef DEBUG
  29. LOG("Destructor");
  30. #endif
  31. WaitFinish(true);
  32. #ifdef DEBUG
  33. LOG("Deleting Components");
  34. #endif
  35. #ifdef DEBUG
  36. LOG("Destructor Finished");
  37. #endif
  38. };
  39. bool ARGUS_Kernel::create( void ){
  40. if( m_pSettings != NULL ) {
  41. config_setting_t * pSetting;
  42. int tVal;
  43. char tBuffer[256];
  44. //Connect( CSTR DBName, CSTR DBHost, CSTR DBUser, CSTR DBPassword, uint16_t Port = 3300 );
  45. LOG("Getting Settings ...");
  46. /*
  47. #ifdef __EU_PROJECT__
  48. if( !config_setting_lookup_bool( m_pSettings, KNL_SETTINGS_SAFE_SHORE, &tVal ) ){
  49. tVal = m_bSafeShoreSupport = true;
  50. pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_SAFE_SHORE, CONFIG_TYPE_BOOL );
  51. config_setting_set_bool( pSetting, tVal );
  52. m_pSettingsProvider->compromise();
  53. LOG("Safe-Shore support not defined");
  54. } else {
  55. m_bSafeShoreSupport = (bool)tVal;
  56. sprintf( tBuffer, "Safe-Shore support Read From Settings: %d", tVal );
  57. LOG( tBuffer );
  58. };
  59. #endif
  60. if( !config_setting_lookup_bool( m_pSettings, KNL_SETTINGS_DEFENDER, &tVal ) ){
  61. tVal = m_bDefenderSupport = true;
  62. pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DEFENDER, CONFIG_TYPE_BOOL );
  63. config_setting_set_bool( pSetting, tVal );
  64. m_pSettingsProvider->compromise();
  65. LOG("Defender support not defined");
  66. } else {
  67. m_bDefenderSupport = (bool)tVal;
  68. sprintf( tBuffer, "Defender support Read From Settings: %d", tVal );
  69. LOG( tBuffer );
  70. };
  71. if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_DETECTOR_WIDTH, &tVal )){
  72. tVal = m_nWidth = 1000;
  73. pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DETECTOR_WIDTH, CONFIG_TYPE_INT);
  74. config_setting_set_int(pSetting, tVal );
  75. m_pSettingsProvider->compromise();
  76. LOG("No Detector Width");
  77. } else {
  78. m_nWidth = tVal;
  79. sprintf( tBuffer, "Detector Width Read From Settings: %d", tVal );
  80. LOG( tBuffer );
  81. };
  82. if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_FILTER_KERNELS, &tVal )){
  83. tVal = m_nKernelFilter = 0x000000ff;
  84. pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_FILTER_KERNELS, CONFIG_TYPE_INT);
  85. config_setting_set_int(pSetting, tVal );
  86. m_pSettingsProvider->compromise();
  87. LOG("No Fltered Kernels Defined - All LRF Will Work");
  88. } else {
  89. m_nKernelFilter = tVal;
  90. sprintf( tBuffer, "Filter Kernels Read From Settings: %d", tVal );
  91. LOG( tBuffer );
  92. };
  93. if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_DETECTOR_SST, &tVal )){
  94. tVal = m_nSST = 0;
  95. pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DETECTOR_SST, CONFIG_TYPE_INT);
  96. config_setting_set_int(pSetting, tVal );
  97. m_pSettingsProvider->compromise();
  98. LOG("No Detector SST");
  99. } else {
  100. m_nSST = tVal;
  101. sprintf( tBuffer, "Detector SST Read From Settings: %d", tVal );
  102. LOG( tBuffer );
  103. };
  104. if( m_pSettingsProvider->isCompromised() ) {
  105. LOG("Save Settings");
  106. m_pSettingsProvider->validate();
  107. };
  108. */
  109. } else {
  110. LOG_ERROR("Failed Read Config Settings!");
  111. return false;
  112. };
  113. if( m_pDBC == NULL ) {
  114. LOG("Explicitly Create DBC Object");
  115. m_pDBC = new ARGUS_DBC(m_pSettings);
  116. };
  117. if( m_pDBC == NULL ){
  118. LOG_ERROR("DBC Object Creation Failure!");
  119. return false;
  120. };
  121. if(!m_pDBC->connect()){
  122. LOG_ERROR("DBC Connection Failure!");
  123. SLEEP(5000);
  124. //ToDO: Set Restart Kernel!
  125. return false;
  126. };
  127. #ifdef DEBUG
  128. LOG( "Settings Read Done" );
  129. #endif
  130. m_pDBC->getIPCamRecords( m_vCamRecords );
  131. #ifdef DEBUG
  132. LOG("Init IP Cameras");
  133. #endif
  134. if( m_vCamRecords.size() ) {
  135. ARGUS_Camera *tPtr = NULL;
  136. for( int i=0; i < m_vCamRecords.size(); i++ ) {
  137. tPtr = new ARGUS_Camera();
  138. if( tPtr != NULL ) {
  139. if( tPtr->create( m_vCamRecords.at(i)->ip, m_vCamRecords.at(i)->username, m_vCamRecords.at(i)->password ) ){
  140. #ifdef DEBUG
  141. LOG("Camera Created!");
  142. #endif
  143. m_vCameras.push_back( tPtr );
  144. tPtr = NULL;
  145. } else {
  146. LOG_ERROR("Camera Creation Failure!");
  147. delete( tPtr );
  148. tPtr = NULL;
  149. };
  150. };
  151. };
  152. };
  153. #ifdef DEBUG
  154. LOG("Start OnVIF Discovery");
  155. #endif
  156. if( m_pOnvifMediaObserver!= NULL ) {
  157. m_pOnvifMediaObserver->startDiscovery();
  158. m_bDiscoveryStarted = true;
  159. };
  160. #ifdef DEBUG
  161. LOG( "Kernel Trhead is about to be Created" );
  162. #endif
  163. if( init() == false ){
  164. LOG_ERROR("Error in starting Kernel Thread!");
  165. return false;
  166. };
  167. #ifdef DEBUG
  168. LOG( "Kernel Thread Created" );
  169. #endif
  170. return true;
  171. };
  172. void ARGUS_Kernel::WaitFinish( bool Force ){
  173. if( Force ){
  174. #ifdef DEBUG
  175. LOG( "Thread End Forced" );
  176. #endif
  177. } else {
  178. // 3 seconds sleep to settle down processes
  179. SLEEP(3000);
  180. };
  181. waitThreadEnd( Force );
  182. #ifdef DEBUG
  183. LOG( "Thread is Detached" );
  184. #endif
  185. };
  186. /* Main Kernel Thread Procedure */
  187. void ARGUS_Kernel::m_fThreadProc( void ){
  188. char Buffer[CTEXT];
  189. time_t tTimeTSeconds;
  190. int lastTSS = 0;
  191. int lastTMS = 0;
  192. EventRecordDBC * tEvtPtr;
  193. m_bThreadStatus = TH_WORKING;
  194. while( m_bThreadStatus < TH_ENDED ){
  195. m_tTimeTSeconds = time( NULL );
  196. // Network IP OnVif Discovery
  197. if( m_pOnvifMediaObserver!= NULL ){
  198. if( !m_bDiscoveryStarted ) {
  199. if((m_tTimeTSeconds%ONVIF_DISCOVERY_INTERVAL)==0){
  200. #ifdef DEBUG
  201. LOG("Start OnVIF Discovery");
  202. #endif
  203. m_pOnvifMediaObserver->startDiscovery();
  204. m_bDiscoveryStarted = true;
  205. };
  206. } else {
  207. if((m_tTimeTSeconds%ONVIF_DISCOVERY_INTERVAL)==ONVIF_DISCOVERY_RESULT_TIME){
  208. #ifdef DEBUG
  209. LOG("Validate IP Cameras");
  210. #endif
  211. validateCameras();
  212. m_bDiscoveryStarted = false;
  213. };
  214. };
  215. };
  216. // Event Processing Loop
  217. if( !m_pDBC->getLastEvents( m_vEvtRecords, lastTSS, lastTMS ) ){
  218. // Error In DB Access
  219. m_bThreadStatus = TH_ENDED;
  220. m_bExitCause = 1;
  221. continue;
  222. };
  223. if( m_vEvtRecords.size() > 0 ){
  224. validateEvents( lastTSS, lastTMS );
  225. };
  226. if( m_tTimeTSeconds != tTimeTSeconds ){ // Each Second Process
  227. tTimeTSeconds = m_tTimeTSeconds;
  228. };
  229. SLEEP( SLEEP_KERNEL_INTERVAL );
  230. };
  231. };
  232. int ARGUS_Kernel::isCameraListed( CSTR uid ){
  233. if( m_vCamRecords.size() )
  234. for( int index = 0; index < m_vCamRecords.size(); index++ )
  235. if(strstr(m_vCamRecords.at(index)->uuid,uid)!=NULL){
  236. m_vCamRecords.at(index)->online = true;
  237. return index;
  238. };
  239. return -1;
  240. };
  241. void ARGUS_Kernel::validateCameras( void ){
  242. vectorWoWoResults *DiscoverResults = m_pOnvifMediaObserver->getResults();
  243. if( DiscoverResults->size() > 0 ) {
  244. for (int index = 0; index < DiscoverResults->size(); index++) {
  245. char tBuffer[256];
  246. sprintf( tBuffer,"Found Camwera [%s] %s", DiscoverResults->at(index)->XAddrs, DiscoverResults->at(index)->RefID );
  247. LOG( tBuffer );
  248. int cid = isCameraListed(DiscoverResults->at(index)->RefID);
  249. if( cid != -1 ) {
  250. sprintf( tBuffer, "Camera is Listed as %d", cid );
  251. LOG( tBuffer );
  252. if( strstr(DiscoverResults->at(index)->XAddrs,m_vCamRecords.at(cid)->ip)==NULL ) {
  253. m_pDBC->updateIPCam( DiscoverResults->at(index)->RefID, DiscoverResults->at(index)->XAddrs );
  254. };
  255. } else {
  256. // Uncknown camera - no checking
  257. LOG("Camera is Not Listed");
  258. m_pDBC->updateIPCam( DiscoverResults->at(index)->RefID, DiscoverResults->at(index)->XAddrs );
  259. };
  260. };
  261. } else {
  262. LOG("No Online IP Cameras in Network");
  263. };
  264. };
  265. void ARGUS_Kernel::validateEvents( int &lastTSS, int &lastTMS ){
  266. EventRecordDBC *tPtr = NULL;
  267. basicEvent *tEvt = NULL;
  268. if(m_vEvtRecords.size()) {
  269. lastTSS = m_vEvtRecords.at(m_vEvtRecords.size()-1)->ts_s;
  270. lastTMS = m_vEvtRecords.at(m_vEvtRecords.size()-1)->ts_ms;
  271. #ifndef ONE_EVENT_PER_LOOP
  272. while(m_vEvtRecords.size()) {
  273. #endif
  274. tPtr = *m_vEvtRecords.begin();
  275. if( tPtr != NULL ) {
  276. if( tEvt != NULL ) {
  277. m_pEventHeap->broadcast( tEvt );
  278. //free( tEvt );
  279. };
  280. free( tPtr );
  281. };
  282. m_vEvtRecords.erase(m_vEvtRecords.begin());
  283. #ifndef ONE_EVENT_PER_LOOP
  284. };
  285. #endif
  286. };
  287. };
  288. bool ARGUS_Kernel::setStatus( int A ){
  289. m_bThreadStatus = (thredStatus)A;
  290. };
  291. bool ARGUS_Kernel::signalEvent( void ){
  292. if( m_pDBC->getLastEvents( m_vEvtRecords, 0,0 ) ){
  293. };
  294. return true;
  295. };
  296. #undef LOG
  297. #undef LOG_ERROR