|
solarpowerlog trunk
|
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 (Registry.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 * Registry.cpp 00029 * 00030 * Created on: May 9, 2009 00031 * Author: tobi 00032 * 00033 * 00034 */ 00035 00036 #ifdef HAVE_CONFIG_H 00037 #include "config.h" 00038 #endif 00039 00040 #include <iostream> 00041 00042 #include "configuration/Registry.h" 00043 #include "interfaces/CWorkScheduler.h" 00044 #include "Inverters/interfaces/InverterBase.h" 00045 00046 using namespace std; 00047 00049 Registry::Registry() 00050 { 00051 Config = NULL; 00052 00053 00054 } 00055 00056 bool Registry::LoadConfig( std::string name ) 00057 { 00058 if (Config) 00059 delete Config; 00060 Config = new libconfig::Config; 00061 try { 00062 Config->readFile(name.c_str()); 00063 } catch (libconfig::ParseException ex) { 00064 std::cerr << "Error parsing configuration file " << name 00065 << " at Line " << ex.getLine() << ". (" 00066 << ex.getError() << ")" << std::endl; 00067 delete Config; 00068 Config = 0; 00069 return false; 00070 } catch (libconfig::FileIOException ex) { 00071 std::cerr << "Error parsing configuration file " << name 00072 << ". IO Exception " << std::endl; 00073 delete Config; 00074 Config = 0; 00075 return false; 00076 } 00077 00078 // Be more sloppy on datatypes -> automatically convert if possible. 00079 Config->setAutoConvert(true); 00080 return true; 00081 } 00082 00083 libconfig::Setting & Registry::GetSettingsForObject( std::string section, 00084 std::string objname ) 00085 { 00086 00087 assert(Config); 00088 00089 libconfig::Setting &s = Config->lookup(section); 00090 00091 if (objname == "") 00092 return s; 00093 00094 for (int i = 0; i < s.getLength(); i++) { 00095 00096 std::string tmp = s[i]["name"]; 00097 if (tmp == objname) 00098 return s[i]; 00099 } 00100 00101 // note: we cannot deliver a object here ... we simply do not have one! 00102 // As libconfig::SettingNotFoundException is private only, we also cannot 00103 // throw here. So, as convention, we return the root of the configuration here.... 00104 // We "BUG" here, as it is the responsibility of the caller to ensure the 00105 // objects existence. 00106 // (Only Objects with a valid name should query their configuration) 00107 std::cerr << "BUG: " << __FILE__ << ":" << __LINE__ 00108 << " --> Queried for unknown Object " << objname 00109 << " in section " << section << std::endl; 00110 00111 assert(false); 00112 00113 return Config->getRoot(); 00114 } 00115 00116 IInverterBase *Registry::GetInverter( const string & name ) const 00117 { 00118 00119 list<IInverterBase*>::const_iterator iter; 00120 for (iter = inverters.begin(); iter != inverters.end(); iter++) { 00121 if ((*iter)->GetName() == name) 00122 return (*iter); 00123 } 00124 00125 return 0; 00126 } 00127 00128 void Registry::AddInverter( const IInverterBase *inverter ) 00129 { 00130 assert (inverter); 00131 inverters.push_back((IInverterBase*) inverter); 00132 } 00133 00135 Registry::~Registry() 00136 { 00137 if (Config) 00138 delete Config; 00139 Config = NULL; 00140 if (mainscheduler) 00141 delete mainscheduler; 00142 }