Commit acd8311b authored by Micha Mueller's avatar Micha Mueller
Browse files

Allow for single_sensors in config files for more convenience

parent d125f685
......@@ -2,7 +2,7 @@ global {
mqttprefix /00112233445566778899AABBCC
}
template_group temp1 {
template_single_sensor temp1 {
interval 1000
minValues 3
}
......@@ -35,14 +35,11 @@ connection name1 {
interval 5000 ; Read sensor every 1000ms
}
}
group g2 {
default temp1
sensor SysORUpTime {
OID 9.1.4.1
mqttsuffix 0002
default temp1
}
single_sensor SysORUpTime {
default temp1
OID 9.1.4.1
mqttsuffix 0002
}
}
......
......@@ -18,6 +18,12 @@ template_group def2 {
interval 2000
}
template_single_sensor def3 {
interval 1000
minValues 5
mqttsuffix 0010
}
group temp {
default def1
path /sys/devices/virtual/thermal/thermal_zone1/temp
......@@ -27,15 +33,10 @@ group temp {
}
}
group freq {
interval 1000
minValues 10
single_sensor freq0 {
default def3
path /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
sensor freq0 {
mqttsuffix 0010
; filter s/Frequency=([[:digit:]]+)/\\1/
}
; filter s/Frequency=([[:digit:]]+)/\\1/
}
group freq {
......
......@@ -126,6 +126,30 @@ public:
delete group;
}
}
//template single sensor
} else if (boost::iequals(val.first, "template_single_" + _baseName)) {
LOG(debug) << "Template single " << _baseName << " \"" << val.second.data() << "\"";
if (!val.second.empty()) {
SGroup* group = new SGroup(val.second.data());
if (readSensorGroup(*group, val.second)) {
//group which consists of only one sensor
SBase* sensor = new SBase(val.second.data());
if (readSensorBase(*sensor, val.second)) {
group->pushBackSensor(sensor);
auto ret = _templateSensorGroups.insert(std::pair<std::string, SGroup*>(val.second.data(), group));
if(!ret.second) {
LOG(warning) << "Template single " << _baseName << " " << val.second.data() << " already exists! Omitting...";
delete group;
}
} else {
LOG(warning) << "Template single " << _baseName << " " << val.second.data() << " could not be read! Omitting";
delete group;
}
} else {
LOG(warning) << "Template single " << _baseName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
delete group;
}
}
//entity
} else if (boost::iequals(val.first, _entityName)) {
LOG(debug) << _entityName << " \"" << val.second.data() << "\"";
......@@ -150,12 +174,46 @@ public:
delete group;
}
}
//single sensor
} else if (boost::iequals(val.first, "single_" + _baseName)) {
LOG(debug) << "Single " << _baseName << " \"" << val.second.data() << "\"";
if (!val.second.empty()) {
SGroup* group = new SGroup(val.second.data());
if (readSensorGroup(*group, val.second)) {
//group which consists of only one sensor
SBase* sensor;
//perhaps one sensor is already present because it was copied from the template group
if (group->getSensors().size() != 0) {
sensor = dynamic_cast<SBase*>(group->getSensors()[0]);
sensor->setName(val.second.data());
if (readSensorBase(*sensor, val.second)) {
storeSensorGroup(group);
} else {
LOG(warning) << "Single " << _baseName << " " << val.second.data() << " could not be read! Omitting";
delete group;
}
} else {
sensor = new SBase(val.second.data());
if (readSensorBase(*sensor, val.second)) {
group->pushBackSensor(sensor);
storeSensorGroup(group);
} else {
LOG(warning) << "Single " << _baseName << " " << val.second.data() << " could not be read! Omitting";
delete group;
}
}
} else {
LOG(warning) << "Single " << _baseName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
delete group;
}
}
}
}
//read of config finished. Now we build the mqtt-topic for every sensor
for(auto g : _sensorGroups) {
for(auto s : g->getSensors()) {
s->setMqtt(_mqttPrefix + g->getMqttPart() + s->getMqtt());
LOG(debug) << g->getGroupName() << "::" << s->getName() << " using MQTT-topic \"" << s->getMqtt() << "\"";
}
}
return true;
......@@ -344,21 +402,71 @@ protected:
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
if (boost::iequals(val.first, _groupName)) {
LOG(debug) << " " << _groupName << " " << val.second.data();
SGroup* group = new SGroup(val.second.data());
if(readSensorGroup(*group, val.second)) {
setEntityForGroup(sEntity, *group);
if (isTemplate) {
auto ret = _templateSensorGroups.insert(std::pair<std::string, SGroup*>(val.second.data(), group));
if(!ret.second) {
LOG(warning) << "Template " << _groupName << " " << val.second.data() << " already exists! Omitting...";
delete group;
if (!val.second.empty()) {
SGroup* group = new SGroup(val.second.data());
if(readSensorGroup(*group, val.second)) {
setEntityForGroup(sEntity, *group);
if (isTemplate) {
auto ret = _templateSensorGroups.insert(std::pair<std::string, SGroup*>(val.second.data(), group));
if(!ret.second) {
LOG(warning) << "Template " << _groupName << " " << val.second.data() << " already exists! Omitting...";
delete group;
}
} else {
storeSensorGroup(group);
}
} else {
storeSensorGroup(group);
LOG(warning) << _groupName << " " << group->getGroupName() << " could not be read! Omitting";
delete group;
}
}
} else if (boost::iequals(val.first, "single_" + _baseName)) {
LOG(debug) << "Single " << _baseName << " \"" << val.second.data() << "\"";
if (!val.second.empty()) {
SGroup* group = new SGroup(val.second.data());
//group which consists of only one sensor
if (readSensorGroup(*group, val.second)) {
setEntityForGroup(sEntity, *group);
if (isTemplate) {
SBase* sensor = new SBase(val.second.data());
if (readSensorBase(*sensor, val.second)) {
group->pushBackSensor(sensor);
auto ret = _templateSensorGroups.insert(std::pair<std::string, SGroup*>(val.second.data(), group));
if(!ret.second) {
LOG(warning) << "Template single " << _baseName << " " << val.second.data() << " already exists! Omitting...";
delete group;
}
} else {
LOG(warning) << "Template single " << _baseName << " " << val.second.data() << " could not be read! Omitting";
delete group;
}
} else {
SBase* sensor;
//perhaps one sensor is already present because it was copied from the template group
if (group->getSensors().size() != 0) {
sensor = dynamic_cast<SBase*>(group->getSensors()[0]);
sensor->setName(val.second.data());
if (readSensorBase(*sensor, val.second)) {
storeSensorGroup(group);
} else {
LOG(warning) << "Single " << _baseName << " " << val.second.data() << " could not be read! Omitting";
delete group;
}
} else {
sensor = new SBase(val.second.data());
if (readSensorBase(*sensor, val.second)) {
group->pushBackSensor(sensor);
storeSensorGroup(group);
} else {
LOG(warning) << "Single " << _baseName << " " << val.second.data() << " could not be read! Omitting";
delete group;
}
}
}
} else {
LOG(warning) << "Single " << _baseName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
delete group;
}
} else {
LOG(warning) << _groupName << " " << group->getGroupName() << " could not be read! Omitting";
delete group;
}
}
}
......
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