GrabbingDeviceManager.cpp
Go to the documentation of this file.
00001 #include <stdio.h>
00002 
00003 #include "GrabbingDeviceManager.h"
00004 #include "GrabbingDevice.h"
00005 #include "GrabberUtils.h"
00006 
00007 // #include "PumaLogger.h"
00008 #include "../../Workers/Puma2/PumaException.h"
00009 
00010 
00011 using namespace puma2;
00012 using namespace std;
00013 
00014 
00015 GrabbingDeviceManager* GrabbingDeviceManager::mTheManager =0;
00016 
00017 GrabbingDeviceManager* GrabbingDeviceManager::getGrabbingDeviceManager()
00018 {
00019   //check if an instance already exists
00020     if ( mTheManager == 0)
00021     {
00022       #ifdef DEBUG
00023       printf("%s Line[%d] : creating new instance \n", __FILE__, __LINE__);
00024       #endif
00025       mTheManager = new GrabbingDeviceManager();
00026     }
00027     return mTheManager;
00028 }
00029 
00030 
00031 
00032 bool GrabbingDeviceManager::connectGrabbingDevice(GrabbingDevice **_device, const uint _id)
00033 {
00034   unicap_device_t *udevice;
00035 
00036   // search handle for device
00037   bool found = findDeviceById( &udevice, _id );
00038   if ( !found ) return false;
00039 
00040   // try to connect to device
00041   bool connected = connectGrabbingDevice( _device, udevice );
00042   return connected;
00043 }
00044 
00045 
00046 
00047 bool GrabbingDeviceManager::connectGrabbingDevice(GrabbingDevice **_device, const string &_description)
00048 {
00049   unicap_device_t *udevice;
00050 
00051   // search handle for device
00052   bool found = findDeviceByDesc( &udevice, _description );
00053   if ( !found )
00054     {
00055       string message("GrabbingDeviceManager::connectGrabbingDevic nothing found");
00056       throw PumaException(PumaException::intolerable, message);
00057       return false;
00058     }
00059 
00060   // try to connect to device
00061   bool connected = connectGrabbingDevice( _device, udevice );
00062   return connected;
00063 }
00064 
00065 
00066 
00067 
00068 uint GrabbingDeviceManager::getNumDevices()
00069 {
00070   return mDevices.size();
00071 }
00072 
00073 
00074 
00075 
00076 string GrabbingDeviceManager::getDeviceInfo(const uint _id)
00077 {
00078   unicap_device_t *device;
00079   string result ="";
00080 
00081   // search handle for device
00082   bool found = findDeviceById( &device, _id );
00083 
00084   if ( !found ) return result ;
00085 
00086   result.append(device->identifier);
00087 
00088   return result;
00089 }
00090 
00091 
00092 
00093 string GrabbingDeviceManager::getDeviceInfo(const string &_description)
00094 {
00095   unicap_device_t *device;
00096   string result ="";
00097 
00098   // search handle for device
00099   bool found = findDeviceByDesc( &device, _description );
00100 
00101     if ( !found ) return result ;
00102 
00103   result.append(device->identifier);
00104 
00105   return result;
00106 }
00107 
00108 
00109 
00110 string GrabbingDeviceManager::getDeviceList()
00111 {
00112   char str[65535];
00113   ostringstream s;
00114 
00115   sprintf( str, "%5s|%50s|%20s|%20s|%20s|\n", "Index", "Identifier", "Model", "Vendor", "Serial");
00116   s << str;
00117 
00118   sprintf( str, "%5s|%50s|%20s|%20s|%20s|\n", "", "", "", "", "");
00119   s << str;
00120 
00121   for ( uint i = 0; i < mDevices.size(); i++ )
00122     {
00123         sprintf( str, "%5i|%50s|%20s|%20s|%20i|\n",
00124                i,
00125                mDevices.at(i)->identifier,
00126                mDevices.at(i)->model_name,
00127                mDevices.at(i)->vendor_name,
00128                (int) mDevices.at(i)->model_id
00129                );
00130         s << str;
00131     }
00132   return s.str();
00133 }
00134 
00135 
00136 
00137 GrabbingDeviceManager::GrabbingDeviceManager()
00138 {
00139 #ifdef HAVE_LOG4CPLUS
00140   mLogger = log4cplus::PumaLogger::getInstance();
00141 #endif
00142   scan();
00143 }
00144 
00145 
00146 
00147 GrabbingDeviceManager::GrabbingDeviceManager( const GrabbingDeviceManager&)
00148 {
00149   if ( mTheManager ) delete mTheManager;
00150 #ifdef HAVE_LOG4CPLUS
00151   if ( mLogger ) delete mLogger;
00152 #endif
00153 }
00154 
00155 
00156 
00157 GrabbingDeviceManager::~GrabbingDeviceManager()
00158 {
00159   // clean up device vector
00160   for ( uint i = 0; i < mDevices.size(); i++)
00161     {
00162       delete mDevices.at(i);
00163     }
00164   mDevices.clear();
00165 
00166 }
00167 
00168 
00169 
00170 bool GrabbingDeviceManager::scan()
00171 {
00172   bool result = true;
00173   int status = 1;
00174 
00175   //clean up
00176   for ( uint i = 0; i < mDevices.size(); i++)
00177     {
00178       delete mDevices.at(i);
00179     }
00180   mDevices.clear();
00181 
00182   for ( uint numDevices = 0; SUCCESS( status ); numDevices++ )
00183     {
00184       unicap_device_t *newDevice = new unicap_device_t();
00185       status = unicap_enumerate_devices( NULL, newDevice, numDevices);
00186 
00187       if ( SUCCESS( status ))
00188         {
00189         mDevices.push_back( newDevice );
00190         } else
00191         {
00192           delete newDevice;
00193           if ( numDevices == 0 ) result = false;
00194           break;
00195         }
00196     }
00197 
00198 #ifdef DEBUG
00199   printf("%s Line[%d] : found %i supported devices \n", __FILE__, __LINE__, getNumDevices());
00200 #endif
00201 
00202   return result;
00203 }
00204 
00205 
00206 
00207 bool GrabbingDeviceManager::findDeviceById(unicap_device_t** _device, const uint _id)
00208 {
00209   if ( _id > getNumDevices() )
00210     {
00211       return false;
00212     }
00213 
00214   *_device = mDevices.at(_id);
00215 
00216   return true;
00217 }
00218 
00219 
00220 
00221 bool GrabbingDeviceManager::findDeviceByDesc(unicap_device_t** _device, const string &_description)
00222 {
00223   vector<string> words = splitString(_description, " ");
00224 
00225 
00226   string description;
00227   int position =0, targetId = -1;
00228   bool stringFound;
00229 
00230   for ( uint deviceId = 0; deviceId < mDevices.size(); deviceId++ )
00231     {
00232       description = mDevices.at( deviceId )->identifier;
00233       description.append( mDevices.at( deviceId )->model_name);
00234       description.append( mDevices.at( deviceId )->vendor_name);
00235       description.append( toString(mDevices.at( deviceId )->model_id));
00236 
00237       stringFound = true;
00238 
00239       //printf(" search string is %s \n", description.c_str());
00240 
00241       for ( uint i = 0; i < words.size(); i++ )
00242         {
00243           position = description.find( words.at(i));
00244           stringFound &= ( position > -1 );
00245         }
00246       if ( stringFound )
00247         {
00248           targetId = deviceId;
00249           break;
00250         }
00251     }
00252 
00253   if ( targetId == -1 )
00254     {
00255       printf("no device matching description %s was found\n", _description.c_str());
00256       return false;
00257     }
00258   *_device = mDevices.at( targetId );
00259 
00260   return true;
00261 }
00262 
00263 
00264 
00265 bool GrabbingDeviceManager::connectGrabbingDevice(GrabbingDevice **_device, unicap_device_t *_udevice)
00266 {
00267   unicap_handle_t *handle = new unicap_handle_t();
00268 
00269   int status = unicap_open( handle, _udevice );
00270   if ( !SUCCESS( status ))
00271     {
00272       printf("could not connect to device\n");
00273       return false;
00274     }
00275 
00276   GrabbingDevice* newDevice = new GrabbingDevice( *handle );
00277 
00278   if ( newDevice == 0 ) return false;
00279 
00280   *_device = newDevice;
00281   return true;
00282 }


obj_rec_gui
Author(s): AGAS/agas@uni-koblenz.de
autogenerated on Mon Oct 6 2014 02:53:43