#include "deviceNodeFactory.h" #ifdef USE_MYSQL #include "MySQL_dataBinder.h" #else #include "SQLite_dataBinder.h" #endif #include "deviceNode3D.h" #include "deviceNode2D.h" #include "deviceNodePTZCamControl.h" #include "deviceNodeStaticCamControl.h" #include "deviceNodeEthernet2Serial.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 ) deviceNodeFactory *deviceNodeFactory::_instance = NULL; /** * Returns the single instance of the object. */ deviceNodeFactory* deviceNodeFactory::getInstance() { if( NULL == _instance ) { _instance = new deviceNodeFactory(); }; return _instance; }; deviceNodeFactory::deviceNodeFactory(): basicThread(){ m_pLog = basicLogCollection::getInstance(); #ifdef USE_MYSQL m_pDBC = mysqlDBC::getInstance(); #else m_pDBC = sqliteDBC::getInstance(); #endif m_nPendingInitCounter = 0; init(); }; deviceNodeFactory::~deviceNodeFactory(){ waitThreadEnd( true ); unloadNodes(); _instance = NULL; }; bool deviceNodeFactory::unloadNodes( void ){ if( m_pDBC != NULL ){ if( m_vDeviceNodes.size() > 0 ){ for( vectorDeviceNodes::iterator index = m_vDeviceNodes.begin(); index < m_vDeviceNodes.end(); index++ ){ LOG(" Unload Node... BEGIN"); switch( (*index)->getNodeType() ){ case ARGUS_ANALOG_STATIC_CAMERA: LOG(" Static Analog Cam"); delete( (deviceNodeStaticCamControl*) (*index) ); break; case ARGUS_ANALOG_PTZ_CAMERA: LOG(" PTZ Analog Cam"); delete( (deviceNodePTZCamControl*) (*index) ); break; case ARGUS_IP_PTZ_CAMERA:{ LOG(" PTZ IP Cam"); delete( (deviceNodePTZCamControl*) (*index) ); break; }; case ARGUS_IP_STATIC_CAMERA:{ LOG(" Static IP Cam"); delete( (deviceNodeStaticCamControl*) (*index) ); break; }; case ARGUS_IP_UART_GATEWAY:{ LOG(" UART Gateway"); delete( (deviceETH2UART*) (*index) ); break; }; case ARGUS_ALS_SENSOR: case ARGUS_PLS_200_SENSOR: case ARGUS_PLS_360_SENSOR: case ARGUS_LFS_SENSOR:{ LOG(" 2D LRF"); delete( (device2DSensor*) (*index) ); LOG(" 2D LRF done"); break; }; case ARGUS_3D_SENSOR:{ LOG(" 3D LRF"); delete( (device3DSensor*) (*index) ); break; }; }; LOG(" Unload Node... DONE!"); }; m_vDeviceNodes.clear(); m_mDeviceNodesIDS.clear(); }; }; return true; }; /* bool deviceNodeFactory::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 deviceNodeFactory::loadNodes(){ if( m_pDBC == NULL ) return false; DBC_INT_LIST *tList = m_pDBC->listNodes(); m_nPendingInitCounter = 0; 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 deviceNodeFactory::reloadNodes( ){ if(!unloadNodes()) return false; return loadNodes( ); }; bool deviceNodeFactory::reloadNode( uint32_t NodeID ){ deleteNode( NodeID ); deviceNode *tNode = createNode( NodeID ); if( tNode ) { return true; }; return false; }; deviceNode *deviceNodeFactory::createNode( uint32_t id ){ deviceNode *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 deviceNodeStaticCamControl *tCamera; tCamera = new deviceNodeStaticCamControl( id ); if( tCamera ) { LOG("Starting Service"); tCamera->startService(); tVal = ( deviceNode *) tCamera; LOG("Success"); }; break; }; case ARGUS_ANALOG_PTZ_CAMERA:{ #ifdef DEBUG LOG("Creating Analog PTZ Camera"); #endif deviceNodePTZCamControl *tCamera; tCamera = new deviceNodePTZCamControl( id ); if( tCamera ) { LOG("Starting Service"); tCamera->startService(); tVal = ( deviceNode *) tCamera; LOG("Success"); }; break; }; case ARGUS_IP_STATIC_CAMERA:{ #ifdef DEBUG LOG("Creating IP Static Camera"); #endif deviceNodeStaticCamControl *tCamera; tCamera = new deviceNodeStaticCamControl( id ); if( tCamera ) { #ifdef DEBUG LOG("Starting Service"); #endif if( tCamera->startService() ){ tVal = ( deviceNode *) 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 deviceNodePTZCamControl *tCamera; tCamera = new deviceNodePTZCamControl( id ); if( tCamera ) { #ifdef DEBUG LOG("Starting Service"); #endif if( tCamera->startService() ){ tVal = ( deviceNode *) 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 deviceETH2UART *tGW; tGW = new deviceETH2UART( id ); if ( tGW ) { if ( tGW->create() ) { tVal = ( deviceNode *) tGW; } else { delete( tGW ); }; }; break; }; case ARGUS_IP_VIDEO_SERVER: #ifdef DEBUG LOG("Creating Network Video Encoder"); #endif break; case ARGUS_LFS_SENSOR: case ARGUS_ALS_SENSOR: case ARGUS_PLS_200_SENSOR: case ARGUS_PLS_360_SENSOR:{ #ifdef DEBUG LOG("Creating 2D Sensor"); #endif device2DSensor *tSensor; tSensor = new device2DSensor( id ); if( tSensor ) { #ifdef DEBUG LOG("Starting Service"); #endif if( tSensor->startService() ) { #ifdef DEBUG LOG("Success"); #endif tVal = ( deviceNode *) tSensor; } else { delete( tSensor ); }; }; break; }; case ARGUS_3D_SENSOR:{ #ifdef DEBUG LOG("Creating 3D Sensor"); #endif device3DSensor *t3DRadar; t3DRadar = new device3DSensor( id ); if( t3DRadar ) { LOG("Success"); t3DRadar->startService(); tVal = ( deviceNode *) 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_vDeviceNodes.push_back( tVal ); m_mDeviceNodesIDS[id] = tVal; } else { #ifdef DEBUG_EXTRA LOG("Node Creation Failure -> Push to Pending Mode!"); #endif m_nPendingNodes.push_back( id ); m_nPendingInitCounter = FACTORY_NODE_INIT_PENDING_COUNT; }; return tVal; }; void deviceNodeFactory::deleteNode( deviceNode *ptr ){ if( ptr == NULL ) return; // if some troubles move this to an end of the function! for( vectorDeviceNodes::iterator index = m_vDeviceNodes.begin() ; index < m_vDeviceNodes.end(); index++ ){ if( (*index) == ptr ) { m_mDeviceNodesIDS.erase( ptr->getNodeID() ); m_vDeviceNodes.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((deviceNodePTZCamControl *) ptr ); break; case ARGUS_IP_PTZ_CAMERA: #ifdef DEBUG LOG("Destroying Network PTZ Camera"); #endif //SLEEP(2000); delete((deviceNodePTZCamControl *) 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((device2DSensor *) ptr ); break; case ARGUS_ALS_SENSOR: #ifdef DEBUG LOG("Destroying ALS Sensor"); #endif delete((device2DSensor *) ptr ); break; case ARGUS_PLS_200_SENSOR: #ifdef DEBUG LOG("Destroying PLS200 Sensor"); #endif delete((device2DSensor *) ptr ); break; case ARGUS_PLS_360_SENSOR: #ifdef DEBUG LOG("Destroying PLS360 Sensor"); #endif delete((device2DSensor *) ptr ); break; case ARGUS_3D_SENSOR: #ifdef DEBUG LOG("Destroying 3D Sensor"); #endif delete((device3DSensor *) 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 */ deviceNode *deviceNodeFactory::getNode( uint32_t id ){ mapDeviceNode2Id::iterator it; it = m_mDeviceNodesIDS.find( id ); if( it != m_mDeviceNodesIDS.end() ) { return m_mDeviceNodesIDS[id]; }; return NULL; }; /* @id Database ID of Node to be deleted */ void deviceNodeFactory::deleteNode( uint32_t id ){ deviceNode *tNode = getNode( id ); deleteNode( tNode ); }; bool deviceNodeFactory::validate( void ){ return ( m_bThreadStatus < TH_ENDED ); }; void deviceNodeFactory::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_nPendingInitCounter ) m_nPendingInitCounter--; if (m_vDeviceNodes.size()) { DBC_INT_LIST tL; for( vectorDeviceNodes::iterator idx = m_vDeviceNodes.begin(); idx < m_vDeviceNodes.end(); idx++ ) tL.push_back( (*idx)->getNodeID() ); tList = m_pDBC->listNewNodes( &tL ); tL.clear(); } else { tList = m_pDBC->listNewNodes( NULL ); }; if( tList != NULL ) { #ifdef DEBUG LOG("Nodes are Added!"); #endif for( DBC_INT_LIST::iterator idx = tList->begin(); idx != tList->end(); idx++ ) { if ( m_nPendingNodes.size() ) { bool mustAdd = true; for ( DBC_INT_LIST::iterator dx = m_nPendingNodes.begin(); dx != m_nPendingNodes.end(); dx++ ) { if ( ( *dx == *idx ) && ( m_nPendingInitCounter ) ) mustAdd = false; }; if ( mustAdd ){ createNode(*idx); #ifdef DEBUG_EXTRA } else { LOG( "Node is in PENDING mode!"); #endif }; } else { createNode(*idx); }; }; tList->clear(); delete( tList ); #ifdef DEBUG_EXTRA } else { LOG( "No New Nodes"); #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_vDeviceNodes.size() ) { //Get Full List tList = m_pDBC->listNodes(); if( tList != NULL ) { for( vectorDeviceNodes::iterator idx = m_vDeviceNodes.begin(); idx < m_vDeviceNodes.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_EXTRA LOG("Node not passed Validation!"); #endif deleteNode((*idx)); }; }; tList->clear(); delete( tList ); } else { #ifdef DEUBG_EXTRA LOG("All Modules Must be Deleted!"); #endif for( vectorDeviceNodes::iterator idx = m_vDeviceNodes.begin(); idx < m_vDeviceNodes.end(); idx++ ) deleteNode((*idx)); m_vDeviceNodes.clear(); m_mDeviceNodesIDS.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 deviceNodeFactory::getNodesCount( ARGUS_NODE_TYPE type ){ uint32_t RetVal = 0; if( m_vDeviceNodes.size() ) for( vectorDeviceNodes::iterator index = m_vDeviceNodes.begin(); index < m_vDeviceNodes.end(); index++ ) if( (*index)->getNodeType() == type ) RetVal++; return RetVal; }; uint32_t deviceNodeFactory::fetchNodes( ARGUS_NODE_TYPE type, deviceNode **&RA ){ uint32_t RetVal = 0; if( RA == NULL ) { RetVal = getNodesCount( type ); if( RetVal > 0 ) { RA = (deviceNode **) calloc( RetVal, sizeof(deviceNode *)); if( RA == NULL ) return 0; uint32_t cnt = 0; for( vectorDeviceNodes::iterator index = m_vDeviceNodes.begin(); index < m_vDeviceNodes.end(); index++ ) { if( (*index)->getNodeType() == type ) { RA[cnt] = (*index); cnt++; }; }; }; } else { if( m_vDeviceNodes.size() ) { for( vectorDeviceNodes::iterator index = m_vDeviceNodes.begin(); index < m_vDeviceNodes.end(); index++ ) { if( (*index)->getNodeType() == type ) { RA[RetVal] = (*index); RetVal++; }; }; }; }; return RetVal; }; #undef LOG #undef LOG_ERROR