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.

430 lines
12 KiB

6 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 "rgooseKernel.h"
  13. #include <sys/types.h>
  14. #define LOG( X ) m_pLog->Log( "KRNL", this, X )
  15. #ifdef DEBUG
  16. #define LOG_ERROR( X ) {\
  17. char _tBfrErrMSG2w34[256];\
  18. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  19. m_pLog->Log( "KRNL", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  20. }
  21. #else
  22. #define LOG_ERROR( X ) m_pLog->Log( "KRNL", this, _LOG_ERROR, X );
  23. #endif
  24. argusKernel *argusKernel::_instance = NULL;
  25. /**
  26. * Returns the single instance of the object.
  27. */
  28. argusKernel* argusKernel::getInstance( config_setting_t *Settings ) {
  29. // check if the instance has been created yet
  30. if( 0 == _instance ){
  31. // if not, then create it
  32. _instance = new argusKernel( Settings );
  33. };
  34. // return the single instance
  35. return _instance;
  36. };
  37. argusKernel::argusKernel( config_setting_t *Settings ):basicThread(){
  38. m_bExitCause = 0;
  39. m_nServerID = 0;
  40. m_nSiteID = 0;
  41. m_pDBC = NULL;
  42. m_tTimeTSeconds = time( NULL );
  43. m_pLog = basicLogCollection::getInstance();
  44. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "KERNEL" );
  45. m_pServiceAnnouncer = NULL;
  46. m_pEventHeap = NULL;
  47. m_pOnvifMediaObserver = NULL;
  48. m_pNodeFactory = NULL;
  49. m_pModuleFactory = NULL;
  50. };
  51. argusKernel::~argusKernel(){
  52. #ifdef DEBUG
  53. LOG("Destructor");
  54. #endif
  55. WaitFinish(true);
  56. if( m_pDBC != NULL ) m_pDBC->unregisterServer( m_nSiteID, m_nServerNodeID );
  57. #ifdef DEBUG
  58. LOG("Deleting Components");
  59. #endif
  60. if( m_pStreamManager != NULL ) delete( m_pStreamManager );
  61. //Last DBC
  62. if( m_pDBC != NULL ) delete( m_pDBC );
  63. #ifdef DEBUG
  64. LOG("Destructor Finished");
  65. #endif
  66. };
  67. bool argusKernel::create( void ){
  68. char tBuffer[256];
  69. if( m_pSettings != NULL ) {
  70. config_setting_t * pSetting;
  71. int tVal;
  72. const char * tBuf;
  73. //Connect( CSTR DBName, CSTR DBHost, CSTR DBUser, CSTR DBPassword, uint16_t Port = 3300 );
  74. LOG("Getting Settings ...");
  75. if( !config_setting_lookup_string( m_pSettings, ARGUS_KERNEL_TAG_SERVER_IDENTITY, &tBuf ) ){
  76. char * tVar;
  77. char * tTS;
  78. tTS = getTStr(NULL);
  79. tVar = MD5String(tTS);
  80. free(tTS);
  81. sprintf( m_sIdentity, tVar );
  82. pSetting = config_setting_add( m_pSettings, ARGUS_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  83. config_setting_set_string( pSetting, tVar );
  84. free( tVar );
  85. basicSettingsProvider::getInstance()->compromise();
  86. LOG("Server Identity not defined - generating new!");
  87. } else {
  88. sprintf( m_sIdentity, tBuf );
  89. sprintf( tBuffer, "Server Identity Read From Settings: %s", tBuf );
  90. LOG( tBuffer );
  91. };
  92. if( !config_setting_lookup_int( m_pSettings, ARGUS_KERNEL_TAG_SITE_ID, &tVal ) ){
  93. tVal = m_nSiteID = 1;
  94. pSetting = config_setting_add( m_pSettings, ARGUS_KERNEL_TAG_SITE_ID, CONFIG_TYPE_INT );
  95. config_setting_set_int( pSetting, tVal );
  96. basicSettingsProvider::getInstance()->compromise();
  97. LOG("Site ID not defined - using [1] as number!");
  98. } else {
  99. m_nSiteID = tVal;
  100. sprintf( tBuffer, "Site ID Read From Settings: %d", m_nSiteID );
  101. LOG( tBuffer );
  102. };
  103. } else {
  104. LOG_ERROR("Failed Read Config Settings!");
  105. return false;
  106. };
  107. if( m_pDBC == NULL ) {
  108. LOG("Explicitly Create DBC Object");
  109. m_pDBC = argusDBC::getInstance( m_pSettings );
  110. };
  111. if( m_pDBC == NULL ){
  112. LOG_ERROR("DBC Object Creation Failure!");
  113. return false;
  114. };
  115. if(!m_pDBC->connect()){
  116. LOG_ERROR("DBC Connection Failure!");
  117. SLEEP(5000);
  118. //ToDO: Set Restart Kernel!
  119. return false;
  120. };
  121. #ifdef DEBUG
  122. LOG( "Settings Read Done" );
  123. #endif
  124. m_pDBC->registerServer( m_nSiteID, m_sIdentity );
  125. m_nServerID = m_pDBC->getServerID( m_sIdentity );
  126. m_nServerNodeID = m_pDBC->getServerNode( m_nServerID );
  127. //Init Subcomponents
  128. m_pHardwareManager = argusHardwareManager::getInstance();
  129. if( m_pHardwareManager!= NULL ) {
  130. m_pHardwareManager->validate();
  131. } else {
  132. LOG_ERROR("Failure to get HardwareManager Instance!");
  133. return false;
  134. };
  135. m_pServiceAnnouncer = new ssdpAnnouncer( m_pSettings );
  136. if( basicSettingsProvider::getInstance()->isCompromised() ) {
  137. basicSettingsProvider::getInstance()->validate();
  138. };
  139. m_pEventHeap = basicEventHeap::getInstance();
  140. m_pOnvifMediaObserver = onvifMediaObserver::getInstance();
  141. m_pNodeFactory = argusNodeFactory::getInstance();
  142. m_pModuleFactory = argusModuleFactory::getInstance();
  143. //enumerateHardware();
  144. //m_pNodeFactory->setDBC( m_pDBC );
  145. m_pNodeFactory->loadNodes( m_nSiteID );
  146. //m_pModuleFactory->setDBC( m_pDBC ); //m_pDBC->getIPCamRecords( m_vCamRecords );
  147. m_pModuleFactory->loadModules( m_nSiteID );
  148. #ifdef DEBUG
  149. LOG( "Start SSDP Beacon" );
  150. #endif
  151. if( m_pServiceAnnouncer != NULL ) {
  152. if( !m_pServiceAnnouncer->init() ){
  153. delete(m_pServiceAnnouncer);
  154. m_pServiceAnnouncer = NULL;
  155. };
  156. };
  157. m_pStreamManager = NULL;//mediaNginxManager::getInstance();
  158. //if( m_pStreamManager != NULL ) {
  159. // m_pStreamManager->attachStream("rtsp://gagua:4GetMyID@192.168.0.104:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1", true, 12, "12918" );
  160. //};
  161. /*
  162. #ifdef DEBUG
  163. LOG("Init IP Cameras");
  164. #endif
  165. if( m_vCamRecords.size() ) {
  166. ARGUS_Camera *tPtr = NULL;
  167. for( int i=0; i < m_vCamRecords.size(); i++ ) {
  168. tPtr = new ARGUS_Camera();
  169. if( tPtr != NULL ) {
  170. if( tPtr->create( m_vCamRecords.at(i)->ip, m_vCamRecords.at(i)->username, m_vCamRecords.at(i)->password ) ){
  171. #ifdef DEBUG
  172. LOG("Camera Created!");
  173. #endif
  174. m_vCameras.push_back( tPtr );
  175. tPtr = NULL;
  176. } else {
  177. LOG_ERROR("Camera Creation Failure!");
  178. delete( tPtr );
  179. tPtr = NULL;
  180. };
  181. };
  182. };
  183. };
  184. */
  185. #ifdef DEBUG
  186. LOG("Start OnVIF Discovery");
  187. #endif
  188. if( m_pOnvifMediaObserver!= NULL ) {
  189. m_pOnvifMediaObserver->startDiscovery();
  190. m_bDiscoveryStarted = true;
  191. };
  192. #ifdef DEBUG
  193. LOG( "Kernel Trhead is about to be Created" );
  194. #endif
  195. if( init() == false ){
  196. LOG_ERROR("Error in starting Kernel Thread!");
  197. return false;
  198. };
  199. #ifdef DEBUG
  200. LOG( "Kernel Thread Created" );
  201. #endif
  202. /*
  203. m_pServletWS = new servletWS();
  204. if( m_pServletWS != NULL ) {
  205. LOG("ServletWS Created");
  206. if(m_pServletWS->create(8080)){
  207. LOG("ServletWS Started");
  208. //if( ! m_pServletWS->attachChannel("rtsp://gagua:4GetMyID@192.168.0.109:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1", false ) ){
  209. // LOG("Error Adding Channel!");
  210. //} else {
  211. // LOG("Channel Attached");
  212. //};
  213. };
  214. };
  215. */
  216. #ifdef DEBUG
  217. sprintf( tBuffer, "Server is registered as id[%d]", m_nServerID );
  218. LOG( tBuffer );
  219. #endif
  220. return true;
  221. };
  222. void argusKernel::WaitFinish( bool Force ){
  223. if( Force ){
  224. #ifdef DEBUG
  225. LOG( "Thread End Forced" );
  226. #endif
  227. } else {
  228. // 3 seconds sleep to settle down processes
  229. SLEEP(3000);
  230. };
  231. waitThreadEnd( Force );
  232. #ifdef DEBUG
  233. LOG( "Thread is Detached" );
  234. #endif
  235. };
  236. /* Main Kernel Thread Procedure */
  237. void argusKernel::m_fThreadProc( void ){
  238. char Buffer[CTEXT];
  239. time_t tTimeTSeconds;
  240. int lastTSS = 0;
  241. int lastTMS = 0;
  242. EventRecordDBC * tEvtPtr;
  243. pid_t pid = gettid();
  244. char pidBuf[16];
  245. sprintf(pidBuf,"pid:%d", pid);
  246. LOG(pidBuf);
  247. m_bThreadStatus = TH_WORKING;
  248. while( m_bThreadStatus < TH_ENDED ){
  249. m_tTimeTSeconds = time( NULL );
  250. // Network IP OnVif Discovery
  251. //*
  252. if( m_pOnvifMediaObserver!= NULL ){
  253. if( !m_bDiscoveryStarted ) {
  254. if((m_tTimeTSeconds%ONVIF_DISCOVERY_INTERVAL)==0){
  255. #ifdef DEBUG
  256. LOG("Start OnVIF Discovery");
  257. #endif
  258. m_pOnvifMediaObserver->startDiscovery();
  259. m_bDiscoveryStarted = true;
  260. };
  261. } else {
  262. //*
  263. if((m_tTimeTSeconds%ONVIF_DISCOVERY_INTERVAL)==ONVIF_DISCOVERY_RESULT_TIME){
  264. #ifdef DEBUG
  265. LOG("Validate IP Cameras");
  266. #endif
  267. validateCameras();
  268. m_bDiscoveryStarted = false;
  269. };
  270. //*/
  271. };
  272. };
  273. //*/
  274. //*
  275. // Event Processing Loop
  276. if( !m_pDBC->getLastEvents( m_vEvtRecords, lastTSS, lastTMS ) ){
  277. // Error In DB Access
  278. m_bThreadStatus = TH_ENDED;
  279. m_bExitCause = 1;
  280. continue;
  281. };
  282. /* if( m_vEvtRecords.size() > 0 ){
  283. validateEvents( lastTSS, lastTMS );
  284. };
  285. */
  286. //*/
  287. if( m_tTimeTSeconds != tTimeTSeconds ){ // Each Second Process
  288. tTimeTSeconds = m_tTimeTSeconds;
  289. if( m_pModuleFactory != NULL ) {
  290. m_pModuleFactory->validate();
  291. };
  292. //if( m_pNodeFactory != NULL ) {
  293. //m_pNodeFactory->validate();
  294. //};
  295. };
  296. SLEEP( SLEEP_KERNEL_INTERVAL );
  297. };
  298. };
  299. int argusKernel::isCameraListed( CSTR uid ){
  300. if( m_vCamRecords.size() ){
  301. for( int index = 0; index < m_vCamRecords.size(); index++ ){
  302. if(strstr(m_vCamRecords.at(index)->uuid,uid)!=NULL){
  303. m_vCamRecords.at(index)->online = true;
  304. return index;
  305. };
  306. };
  307. } else {
  308. LOG("No Registered Cameras!");
  309. };
  310. return -1;
  311. };
  312. void argusKernel::validateCameras( void ){
  313. vectorWoWoResults *DiscoverResults = m_pOnvifMediaObserver->getResults();
  314. if( DiscoverResults->size() > 0 ) {
  315. for (int index = 0; index < DiscoverResults->size(); index++) {
  316. char tBuffer[256];
  317. sprintf( tBuffer,"Found Camwera [%s] %s", DiscoverResults->at(index)->XAddrs, DiscoverResults->at(index)->RefID );
  318. LOG( tBuffer );
  319. uint32_t cid = m_pDBC->getCamId( DiscoverResults->at(index)->RefID ); //isCameraListed( DiscoverResults->at(index)->RefID );
  320. if( cid ) {
  321. sprintf( tBuffer, "Camera is Listed as %d", cid );
  322. LOG( tBuffer );
  323. //if( strstr(DiscoverResults->at(index)->XAddrs,m_vCamRecords.at(cid)->ip)==NULL ) {
  324. // LOG( DiscoverResults->at(index)->XAddrs );
  325. m_pDBC->updateIPCam( m_nSiteID, cid, DiscoverResults->at(index)->XAddrs );// DiscoverResults->at(index));
  326. //};
  327. } else {
  328. // Uncknown camera - no checking
  329. LOG("Camera is Not Listed");
  330. m_pDBC->addIPCam( m_nSiteID, DiscoverResults->at(index) );
  331. };
  332. };
  333. } else {
  334. LOG("No Online IP Cameras in Network");
  335. };
  336. };
  337. /*
  338. void ARGUS_Kernel::validateEvents( int &lastTSS, int &lastTMS ){
  339. EventRecordDBC *tPtr = NULL;
  340. basicEvent *tEvt = NULL;
  341. if(m_vEvtRecords.size()) {
  342. lastTSS = m_vEvtRecords.at(m_vEvtRecords.size()-1)->ts_s;
  343. lastTMS = m_vEvtRecords.at(m_vEvtRecords.size()-1)->ts_ms;
  344. #ifndef ONE_EVENT_PER_LOOP
  345. while(m_vEvtRecords.size()) {
  346. #endif
  347. tPtr = *m_vEvtRecords.begin();
  348. if( tPtr != NULL ) {
  349. if( tEvt != NULL ) {
  350. m_pEventHeap->broadcast( tEvt );
  351. //free( tEvt );
  352. };
  353. free( tPtr );
  354. };
  355. m_vEvtRecords.erase(m_vEvtRecords.begin());
  356. #ifndef ONE_EVENT_PER_LOOP
  357. };
  358. #endif
  359. };
  360. };
  361. */
  362. bool argusKernel::signalEvent( void ){
  363. if( m_pDBC->getLastEvents( m_vEvtRecords, 0,0 ) ){
  364. };
  365. return true;
  366. };
  367. int argusKernel::getNetNodes( ARGUS_NODE_TYPE type, argusNetNode **&RA ){
  368. RA = NULL;
  369. int cnt = 0;
  370. if( m_pNodeFactory != NULL ){
  371. cnt = m_pNodeFactory->getNodeCount( type );
  372. if( cnt ) {
  373. RA = (argusNetNode **) calloc( cnt, sizeof( argusNetNode * ) );
  374. m_pNodeFactory->fetchNodes(type, RA );
  375. };
  376. };
  377. return cnt;
  378. };
  379. argusNetNode *argusKernel::getNetNode( int id ){
  380. return NULL;
  381. };
  382. int argusKernel::getModules( ARGUS_MODULE_TYPE type, argusKernelModule **&RA ){
  383. return 0;
  384. };
  385. argusKernelModule *argusKernel::getModule( int id ){
  386. return NULL;
  387. };
  388. #undef LOG
  389. #undef LOG_ERROR