Commit 886a0016 authored by Michael Ott's avatar Michael Ott
Browse files

Add sensor templates

parent 2854a172
......@@ -35,6 +35,7 @@ class ConfiguratorTemplate : public ConfiguratorInterface {
static_assert(std::is_base_of<SensorGroupInterface, SGroup>::value, "SGroup must derive from SensorGroupInterface!");
protected:
typedef std::map<std::string, SBase*> sBaseMap_t;
typedef std::map<std::string, SGroup*> sGroupMap_t;
typedef std::map<std::string, SEntity*> sEntityMap_t;
......@@ -56,6 +57,9 @@ public:
for (auto e : _sensorEntitys) {
delete e;
}
for (auto tb : _templateSensorBases) {
delete tb.second;
}
for (auto tg : _templateSensorGroups) {
delete tg.second;
}
......@@ -65,6 +69,7 @@ public:
_sensorGroupInterfaces.clear();
_sensorGroups.clear();
_sensorEntitys.clear();
_templateSensorBases.clear();
_templateSensorGroups.clear();
_templateSensorEntitys.clear();
}
......@@ -91,7 +96,6 @@ public:
//read groups and templates for groups. If present also entity/-template stuff
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, cfg) {
//TODO allow for template sensors?
//TODO allow single sensors for convenience?
//template entity
if (boost::iequals(val.first, "template_" + _entityName)) {
......@@ -126,6 +130,22 @@ public:
delete group;
}
}
//template base
} else if (boost::iequals(val.first, "template_" + _baseName)) {
LOG(debug) << "Template " << _baseName << " \"" << val.second.data() << "\"";
if (!val.second.empty()) {
SBase* base = new SBase(val.second.data());
if (readSensorBase(*base, val.second)) {
auto ret = _templateSensorBases.insert(std::pair<std::string, SBase*>(val.second.data(), base));
if(!ret.second) {
LOG(warning) << "Template " << _baseName << " " << val.second.data() << " already exists! Omitting...";
delete base;
}
} else {
LOG(warning) << "Template " << _baseName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
delete base;
}
}
//template single sensor
} else if (boost::iequals(val.first, "template_single_" + _baseName)) {
LOG(debug) << "Template single " << _baseName << " \"" << val.second.data() << "\"";
......@@ -242,6 +262,9 @@ public:
for(auto e : _sensorEntitys) {
delete e;
}
for (auto tb : _templateSensorBases) {
delete tb.second;
}
for (auto tg : _templateSensorGroups) {
delete tg.second;
}
......@@ -251,6 +274,7 @@ public:
_sensorGroupInterfaces.clear();
_sensorGroups.clear();
_sensorEntitys.clear();
_templateSensorBases.clear();
_templateSensorGroups.clear();
_templateSensorEntitys.clear();
......@@ -299,7 +323,20 @@ protected:
* @return True on success, false otherwise
*/
bool readSensorBase(SBase& sBase, CFG_VAL config) {
//TODO default templates useful?
boost::optional<boost::property_tree::iptree&> def = config.get_child_optional("default");
if(def) {
//we copy all values from default (including copy constructing its sensors)
//if own sensors are specified they are appended
LOG(debug) << " Using \"" << def.get().data() << "\" as default.";
auto it = _templateSensorBases.find(def.get().data());
if(it != _templateSensorBases.end()) {
sBase = *(it->second);
sBase.setName(config.data());
} else {
LOG(warning) << "Template " << _groupName << "\"" << def.get().data() << "\" not found! Using standard values.";
}
}
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
if (boost::iequals(val.first, "mqttsuffix")) {
sBase.setMqtt(val.second.data());
......@@ -597,6 +634,7 @@ protected:
std::vector<SensorGroupInterface*> _sensorGroupInterfaces;
std::vector<SGroup*> _sensorGroups;
std::vector<SEntity*> _sensorEntitys;
sBaseMap_t _templateSensorBases;
sGroupMap_t _templateSensorGroups;
sEntityMap_t _templateSensorEntitys;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment