solarpowerlog trunk
/home/tobi/workspace/solarpowerlog/src/Inverters/interfaces/InverterBase.cpp
Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  solarpowerlog
00003  Copyright (C) 2009  Tobias Frost
00004 
00005  This file is part of solarpowerlog.
00006 
00007  Solarpowerlog is free software; However, it is dual-licenced
00008  as described in the file "COPYING".
00009 
00010  For this file (InverterBase.cpp), the license terms are:
00011 
00012  You can redistribute it and/or modify it under the terms of the GNU
00013  General Public License as published by the Free Software Foundation; either
00014  version 3 of the License, or (at your option) any later version.
00015 
00016  This program is distributed in the hope that it will be useful, but
00017  WITHOUT ANY WARRANTY; without even the implied warranty of
00018  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  Lesser General Public License for more details.
00020 
00021  You should have received a copy of the GNU Library General Public
00022  License along with this proramm; if not, see
00023  <http://www.gnu.org/licenses/>.
00024  ----------------------------------------------------------------------------
00025  */
00026 
00027 /*
00028  * InverterBase.cpp
00029  *
00030  *  Created on: May 9, 2009
00031  *      Author: tobi
00032  */
00033 
00034 #ifdef HAVE_CONFIG_H
00035 #include "config.h"
00036 #endif
00037 
00038 #include <map>
00039 #include <assert.h>
00040 
00041 #include "Inverters/interfaces/InverterBase.h"
00042 #include "Connections/factories/IConnectFactory.h"
00043 #include "interfaces/CCapability.h"
00044 
00045 #include "Inverters/Capabilites.h"
00046 #include "patterns/IValue.h"
00047 #include "patterns/CValue.h"
00048 
00049 #include "Inverters/interfaces/ICapaIterator.h"
00050 
00051 using namespace std;
00052 
00053 IInverterBase::IInverterBase( const string& name,
00054      const string & configurationpath, const string& role )
00055 {
00056 
00057      // Setup the logger
00058      logger.Setup(name,configurationpath, role);
00059 
00060      this->name = name;
00061      this->configurationpath = configurationpath;
00062      connection = IConnectFactory::Factory(configurationpath);
00063      connection->SetupLogger(logger.getLoggername());
00064 
00065      pair<map<string, CCapability*>::iterator, bool> b;
00066 
00067      string s;
00068      CCapability *c;
00069 
00070      // Add the "must have" capabilites.
00071      c = new CCapability(CAPA_CAPAS_UPDATED, CAPA_CAPAS_UPDATED_TYPE, this);
00072      b = CapabilityMap.insert(pair<string, CCapability*> (
00073           CAPA_CAPAS_UPDATED, c));
00074      assert( b.second );
00075 
00076      c = new CCapability(CAPA_CAPAS_REMOVEALL, CAPA_CAPAS_REMOVEALL_TYPE,
00077           this);
00078      b = CapabilityMap.insert(pair<string, CCapability*> (
00079           CAPA_CAPAS_REMOVEALL, c));
00080      assert( b.second );
00081 
00082      c = new CCapability(CAPA_INVERTER_DATASTATE,
00083           CAPA_INVERTER_DATASTATE_TYPE, this);
00084      b = CapabilityMap.insert(pair<string, CCapability*> (
00085           CAPA_INVERTER_DATASTATE, c));
00086      assert( b.second );
00087 
00088 }
00089 
00090 IInverterBase::~IInverterBase()
00091 {
00092      if (connection)
00093           delete connection;
00094      connection = NULL;
00095 
00096      // TODO auto destruct all elements in the capability map.
00097      map<string, CCapability*>::iterator it = GetCapabilityIterator();
00098      for (; it != GetCapabilityLastIterator(); it++)
00099           delete (*it).second;
00100      CapabilityMap.clear();
00101 
00102 }
00103 
00104 const std::string& IInverterBase::GetName( void ) const
00105 {
00106      return name;
00107 }
00108 
00109 ICapaIterator *IInverterBase::GetCapaNewIterator()
00110 {
00111      return new ICapaIterator(this);
00112 }
00113 
00115 map<string, CCapability*>::iterator IInverterBase::GetCapabilityIterator()
00116 {
00117      map<string, CCapability*>::iterator it = CapabilityMap.begin();
00118      return it;
00119 }
00120 
00123 map<string, CCapability*>::iterator IInverterBase::GetCapabilityLastIterator()
00124 {
00125      map<string, CCapability*>::iterator it = CapabilityMap.end();
00126      return it;
00127 }
00128 
00129 CCapability *IInverterBase::GetConcreteCapability( const string &identifier )
00130 {
00131      map<string, CCapability*>::iterator it = CapabilityMap.find(identifier);
00132      if (it == CapabilityMap.end())
00133           return 0;
00134      return it->second;
00135 }
00136 
00137 void IInverterBase::AddCapability( const string &id, CCapability* capa )
00138 {
00139      CapabilityMap.insert(pair<string, CCapability*> (id, capa));
00140      LOGDEBUG(logger, "Added new Capability to " << name << ": " << id);
00141 }