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.

595 lines
19 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
5 years ago
​
​
​
​
​
​
5 years ago
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
6 years ago
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
6 years ago
6 years ago
​
6 years ago
6 years ago
​
6 years ago
​
​
6 years ago
​
6 years ago
​
​
​
​
6 years ago
​
6 years ago
6 years ago
​
6 years ago
6 years ago
​
6 years ago
6 years ago
​
6 years ago
6 years ago
​
6 years ago
6 years ago
​
6 years ago
6 years ago
​
6 years ago
6 years ago
​
6 years ago
6 years ago
​
6 years ago
6 years ago
​
6 years ago
6 years ago
​
​
6 years ago
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "rgooseNodeFactory.h"
  2. #include "rgooseIPCamera.h"
  3. #include "rgooseNode3D.h"
  4. #include "rgooseNode2D.h"
  5. #include "rgooseNodePTZCamControl.h"
  6. #include "rgooseNodeStaticCamControl.h"
  7. #ifdef MEM_DEBUG
  8. #include "../alg/generic/heapDebug.h"
  9. #endif
  10. #define LOG( X ) m_pLog->Log( "NFAC", this, X )
  11. #define LOG_ERROR( X ) m_pLog->Log( "NFAC", this, _LOG_ERROR, X )
  12. argusNodeFactory * argusNodeFactory::_instance = NULL;
  13. /**
  14. * Returns the single instance of the object.
  15. */
  16. argusNodeFactory* argusNodeFactory::getInstance( uint32_t ServerID ) {
  17. if( NULL == _instance) {
  18. if( ServerID ) {
  19. _instance = new argusNodeFactory( ServerID );
  20. };
  21. };
  22. return _instance;
  23. };
  24. argusNodeFactory::argusNodeFactory( uint32_t ServerID ): basicThread(){
  25. m_nSiteID = ServerID;
  26. m_pLog = basicLogCollection::getInstance();
  27. m_pDBC = argusDBC::getInstance();
  28. init();
  29. };
  30. argusNodeFactory::~argusNodeFactory(){
  31. waitThreadEnd( true );
  32. unloadNodes();
  33. _instance = NULL;
  34. };
  35. bool argusNodeFactory::unloadNodes( void ){
  36. if( m_pDBC != NULL ){
  37. if( m_vNetNodes.size() > 0 ){
  38. for( vectorNetNodes::iterator index = m_vNetNodes.begin(); index < m_vNetNodes.end(); index++ ){
  39. LOG(" Unload Node... BEGIN");
  40. switch( (*index)->getType() ){
  41. case ARGUS_ANALOG_STATIC_CAMERA:
  42. LOG(" Static Analog Cam");
  43. delete( (ARGUS_CAM*) (*index) );
  44. break;
  45. case ARGUS_ANALOG_PTZ_CAMERA:
  46. LOG(" PTZ Analog Cam");
  47. delete( (ARGUS_PTZCAM*) (*index) );
  48. break;
  49. case ARGUS_IP_PTZ_CAMERA:{
  50. LOG(" PTZ IP Cam");
  51. delete( (ARGUS_PTZCAM*) (*index) );
  52. break;
  53. };
  54. case ARGUS_IP_STATIC_CAMERA:{
  55. LOG(" Static IP Cam");
  56. delete( (ARGUS_CAM*) (*index) );
  57. break;
  58. };
  59. case ARGUS_IP_UART_GATEWAY:{
  60. LOG(" UART Gateway");
  61. delete( (argusNetNode*) (*index) );
  62. break;
  63. };
  64. case ARGUS_ALS_SENSOR:
  65. case ARGUS_PLS_200_SENSOR:
  66. case ARGUS_PLS_360_SENSOR:
  67. case ARGUS_LFS_SENSOR:{
  68. LOG(" 2D LRF");
  69. delete( (ARGUS_2DSensor*) (*index) );
  70. LOG(" 2D LRF done");
  71. break;
  72. };
  73. case ARGUS_3D_SENSOR:{
  74. LOG(" 3D LRF");
  75. delete( (argus3DLIDAR*) (*index) );
  76. break;
  77. };
  78. };
  79. LOG(" Unload Node... DONE!");
  80. };
  81. m_vNetNodes.clear();
  82. m_mNetNodesIDS.clear();
  83. };
  84. };
  85. return true;
  86. };
  87. /*
  88. bool argusNodeFactory::loadNodes( uint32_t SiteID ){
  89. LOG("Node Loader");
  90. if(m_pDBC == NULL) return false;
  91. m_nSiteID = SiteID;
  92. DBC_INT_LIST *tList = m_pDBC->listNodes( SiteID );
  93. if( tList != NULL ) {
  94. for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ){
  95. uint32_t id = *idx;
  96. char msg[64];
  97. sprintf( msg, "Creating Node[%d]", id );
  98. createNode( id );
  99. };
  100. tList->clear();
  101. delete( tList );
  102. return true;
  103. };
  104. return false;
  105. };
  106. */
  107. bool argusNodeFactory::loadNodes( uint32_t SiteID ){
  108. if( m_pDBC == NULL ) return false;
  109. m_nSiteID = SiteID;
  110. DBC_INT_LIST *tList = m_pDBC->listNodes( SiteID );
  111. if( tList != NULL ) {
  112. for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) createNode((*idx));
  113. tList->clear();
  114. delete( tList );
  115. }
  116. #ifdef DEBUG
  117. else {
  118. LOG("No Nodes Defined");
  119. }
  120. #endif
  121. return true;
  122. };
  123. bool argusNodeFactory::reloadNodes( uint32_t SiteID ){
  124. if(!unloadNodes()) return false;
  125. return loadNodes( SiteID );
  126. };
  127. bool argusNodeFactory::reloadNode( uint32_t NodeID ){
  128. deleteNode( NodeID );
  129. argusNetNode *tNode = createNode( NodeID );
  130. if( tNode ) {
  131. return true;
  132. };
  133. return false;
  134. };
  135. argusNetNode *argusNodeFactory::createNode( uint32_t id ){
  136. argusNetNode *tVal = NULL;
  137. if( m_pDBC != NULL ){
  138. DBC_NODE_DATA *tData = NULL;
  139. tData = m_pDBC->getNodeData( id );
  140. if( tData!= NULL ) {
  141. #ifdef DEBUG
  142. char tB[256];
  143. sprintf( tB, "Node id: %d AUX id: %d", id, tData->aux_id );
  144. LOG( tB );
  145. sprintf( tB, "Node type: %d", tData->type );
  146. LOG( tB );
  147. sprintf( tB, "Node Name: %s", tData->name );
  148. LOG( tB );
  149. #endif
  150. switch( tData->type ) {
  151. case ARGUS_ANALOG_STATIC_CAMERA:{
  152. #ifdef DEBUG
  153. LOG("Creating Analog Static Camera");
  154. #endif
  155. ARGUS_CAM *tCamera;
  156. tCamera = new ARGUS_CAM( id );
  157. if( tCamera ) {
  158. LOG("Starting Service");
  159. tCamera->startService();
  160. tVal = ( argusNetNode *) tCamera;
  161. LOG("Success");
  162. };
  163. break;
  164. };
  165. case ARGUS_ANALOG_PTZ_CAMERA:{
  166. #ifdef DEBUG
  167. LOG("Creating Analog PTZ Camera");
  168. #endif
  169. ARGUS_PTZCAM *tCamera;
  170. tCamera = new ARGUS_PTZCAM( id );
  171. if( tCamera ) {
  172. LOG("Starting Service");
  173. tCamera->startService();
  174. tVal = ( argusNetNode *) tCamera;
  175. LOG("Success");
  176. };
  177. break;
  178. };
  179. case ARGUS_IP_STATIC_CAMERA:{
  180. #ifdef DEBUG
  181. LOG("Creating IP Static Camera");
  182. #endif
  183. ARGUS_CAM *tCamera;
  184. tCamera = new ARGUS_CAM( id );
  185. if( tCamera ) {
  186. #ifdef DEBUG
  187. LOG("Starting Service");
  188. #endif
  189. if( tCamera->startService() ){
  190. tVal = ( argusNetNode *) tCamera;
  191. #ifdef DEBUG
  192. LOG("Success");
  193. #endif
  194. } else {
  195. #ifdef DEBUG
  196. LOG_ERROR("Error Sterting Service");
  197. #endif
  198. delete( tCamera );
  199. };
  200. };
  201. break;
  202. };
  203. case ARGUS_IP_PTZ_CAMERA:{
  204. #ifdef DEBUG
  205. LOG("Creating IP PTZ Camera");
  206. #endif
  207. ARGUS_PTZCAM *tCamera;
  208. tCamera = new ARGUS_PTZCAM( id );
  209. if( tCamera ) {
  210. #ifdef DEBUG
  211. LOG("Starting Service");
  212. #endif
  213. if( tCamera->startService() ){
  214. tVal = ( argusNetNode *) tCamera;
  215. #ifdef DEBUG
  216. LOG("Success");
  217. #endif
  218. } else {
  219. #ifdef DEBUG
  220. LOG_ERROR("Error Starting Service!");
  221. #endif
  222. delete( tCamera );
  223. };
  224. };
  225. break;
  226. };
  227. case ARGUS_IP_UART_GATEWAY:{
  228. #ifdef DEBUG
  229. LOG("Creating Ethernet to UART Gateway");
  230. #endif
  231. tVal = new argusNetNode( id );
  232. break;
  233. };
  234. case ARGUS_IP_VIDEO_SERVER:
  235. #ifdef DEBUG
  236. LOG("Creating Network Video Encoder");
  237. #endif
  238. break;
  239. case ARGUS_LFS_SENSOR:{
  240. #ifdef DEBUG
  241. LOG("Creating LFS Sensor");
  242. #endif
  243. ARGUS_2DSensor *tSensor;
  244. tSensor = new ARGUS_2DSensor( id );
  245. if( tSensor ) {
  246. LOG("Success");
  247. tSensor->startService();
  248. tVal = ( argusNetNode *) tSensor;
  249. };
  250. break;
  251. };
  252. case ARGUS_ALS_SENSOR:{
  253. #ifdef DEBUG
  254. LOG("Creating ALS Sensor");
  255. #endif
  256. ARGUS_2DSensor *tSensor;
  257. tSensor = new ARGUS_2DSensor( id );
  258. if( tSensor ) {
  259. LOG("Success");
  260. tSensor->startService();
  261. tVal = ( argusNetNode *) tSensor;
  262. };
  263. break;
  264. };
  265. case ARGUS_PLS_200_SENSOR:{
  266. #ifdef DEBUG
  267. LOG("Creating PLS200 Sensor");
  268. #endif
  269. ARGUS_2DSensor *tSensor;
  270. tSensor = new ARGUS_2DSensor( id );
  271. if( tSensor ) {
  272. LOG("Success");
  273. tSensor->startService();
  274. tVal = ( argusNetNode *) tSensor;
  275. };
  276. break;
  277. };
  278. case ARGUS_PLS_360_SENSOR:{
  279. #ifdef DEBUG
  280. LOG("Creating PLS360 Sensor");
  281. #endif
  282. ARGUS_2DSensor *tSensor;
  283. tSensor = new ARGUS_2DSensor( id );
  284. if( tSensor ) {
  285. LOG("Success");
  286. tSensor->startService();
  287. tVal = ( argusNetNode *) tSensor;
  288. };
  289. break;
  290. };
  291. case ARGUS_3D_SENSOR:{
  292. #ifdef DEBUG
  293. LOG("Creating 3D Sensor");
  294. #endif
  295. argus3DLIDAR *t3DRadar;
  296. t3DRadar = new argus3DLIDAR( id );
  297. if( t3DRadar ) {
  298. LOG("Success");
  299. t3DRadar->startService();
  300. tVal = ( argusNetNode *) t3DRadar;
  301. };
  302. break;
  303. };
  304. case ARGUS_JAMER:
  305. #ifdef DEBUG
  306. LOG("Creating RF Jamer");
  307. #endif
  308. break;
  309. case ARGUS_NET_PLS_200:
  310. #ifdef DEBUG
  311. LOG("Creating Network-PLS200 Sensor");
  312. #endif
  313. break;
  314. case ARGUS_DRONE_CONTROL:
  315. #ifdef DEBUG
  316. LOG("Creating Drone Controll");
  317. #endif
  318. break;
  319. };
  320. free(tData);
  321. };
  322. };
  323. if( tVal != NULL ){
  324. #ifdef DEBUG
  325. char tB[64];
  326. sprintf( tB, "Node PTR: %p", tVal );
  327. LOG( tB );
  328. #endif
  329. m_vNetNodes.push_back( tVal );
  330. m_mNetNodesIDS[id] = tVal;
  331. };
  332. return tVal;
  333. };
  334. void argusNodeFactory::deleteNode( argusNetNode *ptr ){
  335. if( ptr == NULL ) return;
  336. // if some troubles move this to an end of the function!
  337. for( vectorNetNodes::iterator index = m_vNetNodes.begin() ; index < m_vNetNodes.end(); index++ ){
  338. if( (*index) == ptr ) {
  339. m_mNetNodesIDS.erase( ptr->getDbId() );
  340. m_vNetNodes.erase( index );
  341. break;
  342. };
  343. };
  344. switch( ptr->getType() ) {
  345. case ARGUS_ANALOG_STATIC_CAMERA:
  346. #ifdef DEBUG
  347. LOG("Destroying Analog Static Camera");
  348. #endif
  349. break;
  350. case ARGUS_ANALOG_PTZ_CAMERA:
  351. #ifdef DEBUG
  352. LOG("Destroying Analog PTZ Camera");
  353. #endif
  354. break;
  355. case ARGUS_IP_STATIC_CAMERA:
  356. #ifdef DEBUG
  357. LOG("Destroying Network Static Camera");
  358. #endif
  359. SLEEP(2000);
  360. LOG("Actual Destroying Network Static Camera");
  361. delete((ARGUS_PTZCAM *) ptr );
  362. break;
  363. case ARGUS_IP_PTZ_CAMERA:
  364. #ifdef DEBUG
  365. LOG("Destroying Network PTZ Camera");
  366. #endif
  367. SLEEP(2000);
  368. delete((ARGUS_PTZCAM *) ptr );
  369. #ifdef DEBUG
  370. LOG("Done Successfuly");
  371. #endif
  372. break;
  373. case ARGUS_IP_UART_GATEWAY:
  374. #ifdef DEBUG
  375. LOG("Destroying Ethernet to UART Gateway");
  376. #endif
  377. break;
  378. case ARGUS_IP_VIDEO_SERVER:
  379. #ifdef DEBUG
  380. LOG("Destroying Network Video Encoder");
  381. #endif
  382. break;
  383. case ARGUS_LFS_SENSOR:
  384. #ifdef DEBUG
  385. LOG("Destroying LFS Sensor");
  386. #endif
  387. break;
  388. case ARGUS_ALS_SENSOR:
  389. #ifdef DEBUG
  390. LOG("Destroying ALS Sensor");
  391. #endif
  392. break;
  393. case ARGUS_PLS_200_SENSOR:
  394. #ifdef DEBUG
  395. LOG("Destroying PLS200 Sensor");
  396. #endif
  397. break;
  398. case ARGUS_PLS_360_SENSOR:
  399. #ifdef DEBUG
  400. LOG("Destroying PLS360 Sensor");
  401. #endif
  402. break;
  403. case ARGUS_3D_SENSOR:
  404. #ifdef DEBUG
  405. LOG("Destroying 3D Sensor");
  406. #endif
  407. break;
  408. case ARGUS_JAMER:
  409. #ifdef DEBUG
  410. LOG("Destroying RF Jamer");
  411. #endif
  412. break;
  413. case ARGUS_NET_PLS_200:
  414. #ifdef DEBUG
  415. LOG("Destroying Network-PLS200 Sensor");
  416. #endif
  417. break;
  418. case ARGUS_DRONE_CONTROL:
  419. #ifdef DEBUG
  420. LOG("Destroying Drone Controll");
  421. #endif
  422. break;
  423. };
  424. };
  425. /*
  426. @id Database ID of Node to be deleted
  427. */
  428. argusNetNode *argusNodeFactory::getNode( uint32_t id ){
  429. mapNode2Id::iterator it;
  430. it = m_mNetNodesIDS.find( id );
  431. if( it != m_mNetNodesIDS.end() ) {
  432. return m_mNetNodesIDS[id];
  433. };
  434. return NULL;
  435. };
  436. /*
  437. @id Database ID of Node to be deleted
  438. */
  439. void argusNodeFactory::deleteNode( uint32_t id ){
  440. argusNetNode *tNode = getNode( id );
  441. deleteNode( tNode );
  442. };
  443. bool argusNodeFactory::validate( void ){
  444. return ( m_bThreadStatus < TH_ENDED );
  445. };
  446. void argusNodeFactory::m_fThreadProc( void ){
  447. if( m_pDBC == NULL ){
  448. m_bThreadStatus = TH_ENDED;
  449. } else {
  450. m_bThreadStatus = TH_WORKING;
  451. while( m_bThreadStatus < TH_ENDED ) {
  452. DBC_INT_LIST *tList = NULL;
  453. #ifdef DEBUG
  454. #ifdef MEM_DEBUG
  455. size_t memStart, memEnd, memDiff;
  456. char MEM_MSG[256];
  457. memStart = getCurrentRSS();
  458. #endif
  459. #endif
  460. if( m_vNetNodes.size() ) {
  461. DBC_INT_LIST tL;
  462. for( vectorNetNodes::iterator idx = m_vNetNodes.begin(); idx < m_vNetNodes.end(); idx++ ) tL.push_back( (*idx)->getDbId() );
  463. tList = m_pDBC->listNewNodes( m_nSiteID, &tL );
  464. tL.clear();
  465. } else {
  466. tList = m_pDBC->listNewNodes( m_nSiteID, NULL );
  467. };
  468. if( tList != NULL ) {
  469. #ifdef DEBUG
  470. LOG("New Nodes Added!");
  471. #endif
  472. for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) createNode( *idx );
  473. tList->clear();
  474. delete( tList );
  475. }
  476. #ifdef DEBUG_EXTRA
  477. else {
  478. LOG( "No New Nodes Added");
  479. }
  480. #endif
  481. #ifdef MEM_DEBUG
  482. memEnd = getCurrentRSS();
  483. memDiff = memEnd - memStart;
  484. sprintf(MEM_MSG,"MEM START: %d MEM DIF1: %d", memStart, memDiff );
  485. LOG( MEM_MSG );
  486. #endif
  487. if( m_vNetNodes.size() ) {
  488. //Get Full List
  489. tList = m_pDBC->listNodes( m_nSiteID );
  490. if( tList != NULL ) {
  491. for( vectorNetNodes::iterator idx = m_vNetNodes.begin(); idx < m_vNetNodes.end(); idx++ ){
  492. bool tRemoved = true;
  493. // Check if Removed By changes in DB
  494. for( DBC_INT_LIST::iterator idz = tList->begin(); idz < tList->end(); idz++ ) if( (*idz) == (*idx)->getDbId() ) tRemoved = false;
  495. // If Not - check validation
  496. if( !tRemoved ){
  497. #ifdef DEBUG_EXTRA
  498. LOG("Node Among Active Nodes.... make Validation");
  499. #endif
  500. tRemoved = !(*idx)->validate();
  501. };
  502. // If Node is Invalid - Removing
  503. if( tRemoved ) {
  504. #ifdef DEBUG
  505. LOG("Node not passed Validation!");
  506. #endif
  507. deleteNode((*idx));
  508. };
  509. };
  510. tList->clear();
  511. delete( tList );
  512. } else {
  513. #ifdef DEUBG
  514. LOG("All Modules Must be Deleted!");
  515. #endif
  516. for( vectorNetNodes::iterator idx = m_vNetNodes.begin(); idx < m_vNetNodes.end(); idx++ ) deleteNode((*idx));
  517. m_vNetNodes.clear();
  518. m_mNetNodesIDS.clear();
  519. };
  520. }
  521. #ifdef DEBUG
  522. #ifdef DEBUG_EXTRA
  523. else {
  524. LOG("No Nodes to Validate");
  525. }
  526. #endif
  527. ;
  528. #ifdef MEM_DEBUG
  529. memEnd = getCurrentRSS();
  530. memDiff = memEnd - memStart;
  531. sprintf(MEM_MSG,"MEM START: %d MEM DIF2: %d", memStart, memDiff );
  532. LOG( MEM_MSG );
  533. #endif
  534. #else
  535. ;
  536. #endif
  537. for( int i = ( 15000 / DEFAULT_THREAD_SLEEP_TIME ); i && ( m_bThreadStatus < TH_ENDED ); i-- ) SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  538. };
  539. };
  540. LOG("EXIT FROM CTRL THREAD");
  541. };
  542. uint32_t argusNodeFactory::getNodesCount( ARGUS_NODE_TYPE type ){
  543. uint32_t RetVal = 0;
  544. if( m_vNetNodes.size() ) for( vectorNetNodes::iterator index = m_vNetNodes.begin(); index < m_vNetNodes.end(); index++ ) if( (*index)->getType() == type ) RetVal++;
  545. return RetVal;
  546. };
  547. uint32_t argusNodeFactory::fetchNodes( ARGUS_NODE_TYPE type, argusNetNode **&RA ){
  548. uint32_t RetVal = 0;
  549. if( RA == NULL ) {
  550. RetVal = getNodesCount( type );
  551. if( RetVal > 0 ) {
  552. RA = (argusNetNode **) calloc( RetVal, sizeof(argusNetNode *));
  553. if( RA == NULL ) return 0;
  554. uint32_t cnt = 0;
  555. for( vectorNetNodes::iterator index = m_vNetNodes.begin(); index < m_vNetNodes.end(); index++ ) {
  556. if( (*index)->getType() == type ) {
  557. RA[cnt] = (*index);
  558. cnt++;
  559. };
  560. };
  561. };
  562. } else {
  563. if( m_vNetNodes.size() ) {
  564. for( vectorNetNodes::iterator index = m_vNetNodes.begin(); index < m_vNetNodes.end(); index++ ) {
  565. if( (*index)->getType() == type ) {
  566. RA[RetVal] = (*index);
  567. RetVal++;
  568. };
  569. };
  570. };
  571. };
  572. return RetVal;
  573. };
  574. #undef LOG
  575. #undef LOG_ERROR