#include "rgooseNodeFactory.h" #include "rgooseIPCamera.h" #include "rgooseNode3D.h" #include "rgooseNode2D.h" #include "rgooseNodePTZCamControl.h" #include "rgooseNodeStaticCamControl.h" #include "rgooseNodeEthernet2Serial.h" #ifdef MEM_DEBUG #include "../alg/generic/heapDebug.h" #endif #define LOG( X ) m_pLog->Log( "NFAC", this, X ) #define LOG_ERROR( X ) m_pLog->Log( "NFAC", this, _LOG_ERROR, X ) argusNodeFactory * argusNodeFactory::_instance = NULL; /** * Returns the single instance of the object. */ argusNodeFactory* argusNodeFactory::getInstance( uint32_t SiteID, uint32_t ServerID ) { if( NULL == _instance ) { if ( SiteID ) { if (ServerID) { _instance = new argusNodeFactory( SiteID, ServerID ); }; }; }; return _instance; }; argusNodeFactory::argusNodeFactory( uint32_t SiteID, uint32_t ServerID ): basicThread(){ m_nSiteID = SiteID; m_nServerID = ServerID; m_pLog = basicLogCollection::getInstance(); m_pDBC = argusDBC::getInstance(); init(); }; argusNodeFactory::~argusNodeFactory(){ waitThreadEnd( true ); unloadNodes(); _instance = NULL; }; bool argusNodeFactory::unloadNodes( void ){ if( m_pDBC != NULL ){ if( m_vNetNodes.size() > 0 ){ for( vectorNetNodes::iterator index = m_vNetNodes.begin(); index < m_vNetNodes.end(); index++ ){ LOG(" Unload Node... BEGIN"); switch( (*index)->getNodeType() ){ case ARGUS_ANALOG_STATIC_CAMERA: LOG(" Static Analog Cam"); delete( (ARGUS_CAM*) (*index) ); break; case ARGUS_ANALOG_PTZ_CAMERA: LOG(" PTZ Analog Cam"); delete( (ARGUS_PTZCAM*) (*index) ); break; case ARGUS_IP_PTZ_CAMERA:{ LOG(" PTZ IP Cam"); delete( (ARGUS_PTZCAM*) (*index) ); break; }; case ARGUS_IP_STATIC_CAMERA:{ LOG(" Static IP Cam"); delete( (ARGUS_CAM*) (*index) ); break; }; case ARGUS_IP_UART_GATEWAY:{ LOG(" UART Gateway"); delete( (ARGUS_ETH2UART*) (*index) ); break; }; case ARGUS_ALS_SENSOR: case ARGUS_PLS_200_SENSOR: case ARGUS_PLS_360_SENSOR: case ARGUS_LFS_SENSOR:{ LOG(" 2D LRF"); delete( (ARGUS_2DSensor*) (*index) ); LOG(" 2D LRF done"); break; }; case ARGUS_3D_SENSOR:{ LOG(" 3D LRF"); delete( (argus3DLIDAR*) (*index) ); break; }; }; LOG(" Unload Node... DONE!"); }; m_vNetNodes.clear(); m_mNetNodesIDS.clear(); }; }; return true; }; /* bool argusNodeFactory::loadNodes( uint32_t SiteID ){ LOG("Node Loader"); if(m_pDBC == NULL) return false; m_nSiteID = SiteID; DBC_INT_LIST *tList = m_pDBC->listNodes( SiteID ); if( tList != NULL ) { for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ){ uint32_t id = *idx; char msg[64]; sprintf( msg, "Creating Node[%d]", id ); createNode( id ); }; tList->clear(); delete( tList ); return true; }; return false; }; */ bool argusNodeFactory::loadNodes( uint32_t SiteID, uint32_t ServerID ){ if( m_pDBC == NULL ) return false; if( SiteID ) m_nSiteID = SiteID; if( ServerID ) m_nServerID = ServerID; DBC_INT_LIST *tList = m_pDBC->listNodes( m_nSiteID, m_nServerID ); if( tList != NULL ) { for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) createNode((*idx)); tList->clear(); delete( tList ); } #ifdef DEBUG else { LOG("No Nodes Defined"); } #endif return true; }; bool argusNodeFactory::reloadNodes( uint32_t SiteID, uint32_t ServerID ){ if(!unloadNodes()) return false; return loadNodes( SiteID, ServerID ); }; bool argusNodeFactory::reloadNode( uint32_t NodeID ){ deleteNode( NodeID ); argusNetNode *tNode = createNode( NodeID ); if( tNode ) { return true; }; return false; }; argusNetNode *argusNodeFactory::createNode( uint32_t id ){ argusNetNode *tVal = NULL; if( m_pDBC != NULL ){ DBC_NODE_DATA *tData = NULL; tData = m_pDBC->getNodeData( id ); if( tData!= NULL ) { #ifdef DEBUG char tB[256]; sprintf( tB, "Node id: %d AUX id: %d", id, tData->aux_id ); LOG( tB ); sprintf( tB, "Node type: %d", tData->type ); LOG( tB ); sprintf( tB, "Node Name: %s", tData->name ); LOG( tB ); #endif switch( tData->type ) { case ARGUS_ANALOG_STATIC_CAMERA:{ #ifdef DEBUG LOG("Creating Analog Static Camera"); #endif ARGUS_CAM *tCamera; tCamera = new ARGUS_CAM( id ); if( tCamera ) { LOG("Starting Service"); tCamera->startService(); tVal = ( argusNetNode *) tCamera; LOG("Success"); }; break; }; case ARGUS_ANALOG_PTZ_CAMERA:{ #ifdef DEBUG LOG("Creating Analog PTZ Camera"); #endif ARGUS_PTZCAM *tCamera; tCamera = new ARGUS_PTZCAM( id ); if( tCamera ) { LOG("Starting Service"); tCamera->startService(); tVal = ( argusNetNode *) tCamera; LOG("Success"); }; break; }; case ARGUS_IP_STATIC_CAMERA:{ #ifdef DEBUG LOG("Creating IP Static Camera"); #endif ARGUS_CAM *tCamera; tCamera = new ARGUS_CAM( id ); if( tCamera ) { #ifdef DEBUG LOG("Starting Service"); #endif if( tCamera->startService() ){ tVal = ( argusNetNode *) tCamera; #ifdef DEBUG LOG("Success"); #endif } else { #ifdef DEBUG LOG_ERROR("Error Sterting Service"); #endif delete( tCamera ); }; }; break; }; case ARGUS_IP_PTZ_CAMERA:{ #ifdef DEBUG LOG("Creating IP PTZ Camera"); #endif ARGUS_PTZCAM *tCamera; tCamera = new ARGUS_PTZCAM( id ); if( tCamera ) { #ifdef DEBUG LOG("Starting Service"); #endif if( tCamera->startService() ){ tVal = ( argusNetNode *) tCamera; #ifdef DEBUG LOG("Success"); #endif } else { #ifdef DEBUG LOG_ERROR("Error Starting Service!"); #endif delete( tCamera ); }; }; break; }; case ARGUS_IP_UART_GATEWAY:{ #ifdef DEBUG LOG("Creating Ethernet to UART Gateway"); #endif ARGUS_ETH2UART *tGW; tGW = new ARGUS_ETH2UART( id ); if ( tGW ) { if ( tGW->create() ) { tVal = ( argusNetNode *) tGW; } else { delete( tGW ); }; }; break; }; case ARGUS_IP_VIDEO_SERVER: #ifdef DEBUG LOG("Creating Network Video Encoder"); #endif break; case ARGUS_LFS_SENSOR:{ #ifdef DEBUG LOG("Creating LFS Sensor"); #endif ARGUS_2DSensor *tSensor; tSensor = new ARGUS_2DSensor( id ); if( tSensor ) { LOG("Success"); tSensor->startService(); tVal = ( argusNetNode *) tSensor; }; break; }; case ARGUS_ALS_SENSOR:{ #ifdef DEBUG LOG("Creating ALS Sensor"); #endif ARGUS_2DSensor *tSensor; tSensor = new ARGUS_2DSensor( id ); if( tSensor ) { LOG("Success"); tSensor->startService(); tVal = ( argusNetNode *) tSensor; }; break; }; case ARGUS_PLS_200_SENSOR:{ #ifdef DEBUG LOG("Creating PLS200 Sensor"); #endif ARGUS_2DSensor *tSensor; tSensor = new ARGUS_2DSensor( id ); if( tSensor ) { LOG("Success"); tSensor->startService(); tVal = ( argusNetNode *) tSensor; }; break; }; case ARGUS_PLS_360_SENSOR:{ #ifdef DEBUG LOG("Creating PLS360 Sensor"); #endif ARGUS_2DSensor *tSensor; tSensor = new ARGUS_2DSensor( id ); if( tSensor ) { LOG("Success"); tSensor->startService(); tVal = ( argusNetNode *) tSensor; }; break; }; case ARGUS_3D_SENSOR:{ #ifdef DEBUG LOG("Creating 3D Sensor"); #endif argus3DLIDAR *t3DRadar; t3DRadar = new argus3DLIDAR( id ); if( t3DRadar ) { LOG("Success"); t3DRadar->startService(); tVal = ( argusNetNode *) t3DRadar; }; break; }; case ARGUS_JAMER: #ifdef DEBUG LOG("Creating RF Jamer"); #endif break; case ARGUS_NET_PLS_200: #ifdef DEBUG LOG("Creating Network-PLS200 Sensor"); #endif break; case ARGUS_DRONE_CONTROL: #ifdef DEBUG LOG("Creating Drone Controll"); #endif break; }; free(tData); }; }; if( tVal != NULL ){ #ifdef DEBUG char tB[64]; sprintf( tB, "Node PTR: %p", tVal ); LOG( tB ); #endif m_vNetNodes.push_back( tVal ); m_mNetNodesIDS[id] = tVal; }; return tVal; }; void argusNodeFactory::deleteNode( argusNetNode *ptr ){ if( ptr == NULL ) return; // if some troubles move this to an end of the function! for( vectorNetNodes::iterator index = m_vNetNodes.begin() ; index < m_vNetNodes.end(); index++ ){ if( (*index) == ptr ) { m_mNetNodesIDS.erase( ptr->getNodeID() ); m_vNetNodes.erase( index ); break; }; }; switch( ptr->getNodeType() ) { case ARGUS_ANALOG_STATIC_CAMERA: #ifdef DEBUG LOG("Destroying Analog Static Camera"); #endif break; case ARGUS_ANALOG_PTZ_CAMERA: #ifdef DEBUG LOG("Destroying Analog PTZ Camera"); #endif break; case ARGUS_IP_STATIC_CAMERA: #ifdef DEBUG LOG("Destroying Network Static Camera"); #endif //SLEEP(2000); LOG("Actual Destroying Network Static Camera"); delete((ARGUS_PTZCAM *) ptr ); break; case ARGUS_IP_PTZ_CAMERA: #ifdef DEBUG LOG("Destroying Network PTZ Camera"); #endif //SLEEP(2000); delete((ARGUS_PTZCAM *) ptr ); #ifdef DEBUG LOG("Done Successfuly"); #endif break; case ARGUS_IP_UART_GATEWAY: #ifdef DEBUG LOG("Destroying Ethernet to UART Gateway"); #endif break; case ARGUS_IP_VIDEO_SERVER: #ifdef DEBUG LOG("Destroying Network Video Encoder"); #endif break; case ARGUS_LFS_SENSOR: #ifdef DEBUG LOG("Destroying LFS Sensor"); #endif delete((ARGUS_2DSensor *) ptr ); break; case ARGUS_ALS_SENSOR: #ifdef DEBUG LOG("Destroying ALS Sensor"); #endif delete((ARGUS_2DSensor *) ptr ); break; case ARGUS_PLS_200_SENSOR: #ifdef DEBUG LOG("Destroying PLS200 Sensor"); #endif delete((ARGUS_2DSensor *) ptr ); break; case ARGUS_PLS_360_SENSOR: #ifdef DEBUG LOG("Destroying PLS360 Sensor"); #endif delete((ARGUS_2DSensor *) ptr ); break; case ARGUS_3D_SENSOR: #ifdef DEBUG LOG("Destroying 3D Sensor"); #endif delete((argus3DLIDAR *) ptr ); break; case ARGUS_JAMER: #ifdef DEBUG LOG("Destroying RF Jamer"); #endif break; case ARGUS_NET_PLS_200: #ifdef DEBUG LOG("Destroying Network-PLS200 Sensor"); #endif break; case ARGUS_DRONE_CONTROL: #ifdef DEBUG LOG("Destroying Drone Controll"); #endif break; }; }; /* @id Database ID of Node to be deleted */ argusNetNode *argusNodeFactory::getNode( uint32_t id ){ mapNode2Id::iterator it; it = m_mNetNodesIDS.find( id ); if( it != m_mNetNodesIDS.end() ) { return m_mNetNodesIDS[id]; }; return NULL; }; /* @id Database ID of Node to be deleted */ void argusNodeFactory::deleteNode( uint32_t id ){ argusNetNode *tNode = getNode( id ); deleteNode( tNode ); }; bool argusNodeFactory::validate( void ){ return ( m_bThreadStatus < TH_ENDED ); }; void argusNodeFactory::m_fThreadProc( void ){ if( m_pDBC == NULL ){ m_bThreadStatus = TH_ENDED; } else { m_bThreadStatus = TH_WORKING; while( m_bThreadStatus < TH_ENDED ) { DBC_INT_LIST *tList = NULL; #ifdef DEBUG #ifdef MEM_DEBUG size_t memStart, memEnd, memDiff; char MEM_MSG[256]; memStart = getCurrentRSS(); #endif #endif if( m_vNetNodes.size() ) { DBC_INT_LIST tL; for( vectorNetNodes::iterator idx = m_vNetNodes.begin(); idx < m_vNetNodes.end(); idx++ ) tL.push_back( (*idx)->getNodeID() ); tList = m_pDBC->listNewNodes( m_nSiteID, m_nServerID, &tL ); tL.clear(); } else { tList = m_pDBC->listNewNodes( m_nSiteID, m_nServerID, NULL ); }; if( tList != NULL ) { #ifdef DEBUG LOG("New Nodes Added!"); #endif for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) createNode( *idx ); tList->clear(); delete( tList ); } #ifdef DEBUG_EXTRA else { LOG( "No New Nodes Added"); } #endif #ifdef MEM_DEBUG memEnd = getCurrentRSS(); memDiff = memEnd - memStart; sprintf(MEM_MSG,"MEM START: %d MEM DIF1: %d", memStart, memDiff ); LOG( MEM_MSG ); #endif if( m_vNetNodes.size() ) { //Get Full List tList = m_pDBC->listNodes( m_nSiteID, m_nServerID ); if( tList != NULL ) { for( vectorNetNodes::iterator idx = m_vNetNodes.begin(); idx < m_vNetNodes.end(); idx++ ){ bool tRemoved = true; // Check if Removed By changes in DB for( DBC_INT_LIST::iterator idz = tList->begin(); idz < tList->end(); idz++ ) if( (*idz) == (*idx)->getNodeID() ) tRemoved = false; // If Not - check validation if( !tRemoved ){ #ifdef DEBUG_EXTRA LOG("Node Among Active Nodes.... make Validation"); #endif tRemoved = !(*idx)->validate(); }; // If Node is Invalid - Removing if( tRemoved ) { #ifdef DEBUG LOG("Node not passed Validation!"); #endif deleteNode((*idx)); }; }; tList->clear(); delete( tList ); } else { #ifdef DEUBG LOG("All Modules Must be Deleted!"); #endif for( vectorNetNodes::iterator idx = m_vNetNodes.begin(); idx < m_vNetNodes.end(); idx++ ) deleteNode((*idx)); m_vNetNodes.clear(); m_mNetNodesIDS.clear(); }; #ifdef DEBUG_EXTRA } else { LOG("No Nodes to Validate"); #endif }; #ifdef DEBUG #ifdef MEM_DEBUG memEnd = getCurrentRSS(); memDiff = memEnd - memStart; sprintf(MEM_MSG,"MEM START: %d MEM DIF2: %d", memStart, memDiff ); LOG( MEM_MSG ); #endif #endif // Sleep for 1 second for( int i = ( 1000 / DEFAULT_THREAD_SLEEP_TIME ); i && ( m_bThreadStatus < TH_ENDED ); i-- ) SLEEP( DEFAULT_THREAD_SLEEP_TIME ); }; }; LOG("EXIT FROM CTRL THREAD"); }; uint32_t argusNodeFactory::getNodesCount( ARGUS_NODE_TYPE type ){ uint32_t RetVal = 0; if( m_vNetNodes.size() ) for( vectorNetNodes::iterator index = m_vNetNodes.begin(); index < m_vNetNodes.end(); index++ ) if( (*index)->getNodeType() == type ) RetVal++; return RetVal; }; uint32_t argusNodeFactory::fetchNodes( ARGUS_NODE_TYPE type, argusNetNode **&RA ){ uint32_t RetVal = 0; if( RA == NULL ) { RetVal = getNodesCount( type ); if( RetVal > 0 ) { RA = (argusNetNode **) calloc( RetVal, sizeof(argusNetNode *)); if( RA == NULL ) return 0; uint32_t cnt = 0; for( vectorNetNodes::iterator index = m_vNetNodes.begin(); index < m_vNetNodes.end(); index++ ) { if( (*index)->getNodeType() == type ) { RA[cnt] = (*index); cnt++; }; }; }; } else { if( m_vNetNodes.size() ) { for( vectorNetNodes::iterator index = m_vNetNodes.begin(); index < m_vNetNodes.end(); index++ ) { if( (*index)->getNodeType() == type ) { RA[RetVal] = (*index); RetVal++; }; }; }; }; return RetVal; }; #undef LOG #undef LOG_ERROR