10.12., 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

Commit 370f145c authored by Micha Müller's avatar Micha Müller

New take on overwriting template Sensors/Groups

Should not break CPU sensor instatiation anymore
Invalidates commit 1a5e05a7
parent 14cb641a
......@@ -311,7 +311,7 @@ All the different plugins share some same general principles in common regarding
* __subSampling__ (subsampling factor S. If S>=1, only one reading every S is sent over MQTT, and the others are kept locally. If S<1, readings are never sent out and only kept locally)
* __publish__ (if set to "true", the sensor will be published when the auto-publish feature is enabled. Otherwise it is omitted. Default is "true".)
5. Be aware that naming of sensor/group/entity is not fixed. A plugin developer can name them as he likes, e.g. counter/multicounter/host.
6. It is possible to define template sensors, groups, or entities in the config file. To specify a template sensor/group/entity simply prefix its definition with `template_` (see the example below). You can reference them later by using the `default` attribute. A template entity can consist of groups and these in turn can consist of sensors. When using a template, all of its attribute values are copied to the actual sensor. Copied attributes can be overwritten in the actual entity/group/sensor (some of them even should be overwritten, e.g. the mqttPart). Groups/sensors associated with a template are copied to the actual entity/group. One can specify further groups/sensors which are then added to those copied from the template. If a group's/sensor's name is identical to one of the groups/sensors introduced by the template, it will not be added but instead replaces the corresponding group/sensor of the template. This can be used to purposefully replace single groups/sensors introduced by a template. Template entitys/groups/sensors themself are never used in live operation of the plugin. They are purely cosmetic for convenient configuration.
6. It is possible to define template sensors, groups, or entities in the config file. To specify a template sensor/group/entity simply prefix its definition with `template_` (see the example below). You can reference them later by using the `default` attribute. A template entity can consist of groups and these in turn can consist of sensors. When using a template, all of its attribute values are copied to the actual sensor. Copied attributes can be overwritten in the actual entity/group/sensor (some of them even should be overwritten, e.g. the mqttPart). Groups/sensors associated with a template are copied to the actual entity/group. One can specify further groups/sensors which are then added to those copied from the template. If a group's/sensor's name is identical to one of the groups/sensors introduced by the template, it will not be added but instead overwrites the corresponding group/sensor of the template (overwrite means: specified attributes replace template attributes. Otherwise template values are kept). This can be used to purposefully overwrite single (attributes of) groups/sensors introduced by a template. Template entitys/groups/sensors themself are never used in live operation of the plugin. They are purely cosmetic for convenient configuration.
In the following two abstract config files are shown to visualize the structure, one with the optional entity level and one without. A real example configuration file for every plugin should be provided in the `/config` directory. One should use them as a starting point to write own configuration files.
```
......
......@@ -372,9 +372,24 @@ class ConfiguratorTemplate : public ConfiguratorInterface {
if (!isTemplate) {
LOG(debug) << " " << _baseName << " " << val.second.data();
}
SB_Ptr sensor = std::make_shared<SBase>(val.second.data());
bool overwriting = false;
//Check if sensor with equal name is already present.
//If yes, we do not want a new sensor but instead overwrite the
//existing one.
for (const auto &s : sGroup.getDerivedSensors()) {
if (s->getName() == sensor->getName()) {
sensor = s;
overwriting = true;
break;
}
}
if (readSensorBase(*sensor, val.second, false)) {
sGroup.pushBackSensor(sensor);
if (!overwriting) {
sGroup.pushBackSensor(sensor);
}
} else if (!isTemplate) {
LOG(warning) << _baseName << " " << sGroup.getGroupName() << "::"
<< sensor->getName() << " could not be read! Omitting";
......
......@@ -329,13 +329,26 @@ class ConfiguratorTemplateEntity : public ConfiguratorTemplate<SBase, SGroup> {
}
} else {
SG_Ptr group = std::make_shared<SGroup>(val.second.data());
if (readSensorGroup(*group, val.second)) {
group->setEntity(&sEntity);
if (group->getGroupName().size() > 0) {
group->setGroupName(sEntity.getName() + "::" + group->getGroupName());
} else {
group->setGroupName(sEntity.getName());
bool overwriting = false;
if (group->getGroupName().size() > 0) {
group->setGroupName(sEntity.getName() + "::" + group->getGroupName());
} else {
group->setGroupName(sEntity.getName());
}
//Check if group with equal name is already present.
//If yes, we do not want a new group but instead
//overwrite the existing one.
for (const auto &g : _sensorGroups) {
if ((g->getGroupName() == group->getGroupName()) && (&sEntity == g->getEntity())) {
group = g;
overwriting = true;
break;
}
}
if (readSensorGroup(*group, val.second) && !overwriting) {
storeSensorGroup(group);
} else {
LOG(warning) << _groupName << " " << group->getGroupName()
......@@ -372,6 +385,25 @@ class ConfiguratorTemplateEntity : public ConfiguratorTemplate<SBase, SGroup> {
}
} else {
SG_Ptr group = std::make_shared<SGroup>(val.second.data());
bool overwriting = false;
if (group->getGroupName().size() > 0) {
group->setGroupName(sEntity.getName() + "::" + group->getGroupName());
} else {
group->setGroupName(sEntity.getName());
}
//Check if group with equal name is already present.
//If yes, we do not want a new group but instead
//overwrite the existing one.
for (const auto &g : _sensorGroups) {
if ((g->getGroupName() == group->getGroupName()) && (&sEntity == g->getEntity())) {
group = g;
overwriting = true;
break;
}
}
//group which consists of only one sensor
if (readSensorGroup(*group, val.second)) {
group->setEntity(&sEntity);
......@@ -380,7 +412,7 @@ class ConfiguratorTemplateEntity : public ConfiguratorTemplate<SBase, SGroup> {
if (group->getDerivedSensors().size() != 0) {
sensor = group->getDerivedSensors()[0];
sensor->setName(val.second.data());
if (readSensorBase(*sensor, val.second)) {
if (readSensorBase(*sensor, val.second) && !overwriting) {
storeSensorGroup(group);
} else {
LOG(warning) << "Single " << _baseName << " "
......@@ -390,7 +422,9 @@ class ConfiguratorTemplateEntity : public ConfiguratorTemplate<SBase, SGroup> {
sensor = std::make_shared<SBase>(val.second.data());
if (readSensorBase(*sensor, val.second)) {
group->pushBackSensor(sensor);
storeSensorGroup(group);
if (!overwriting) {
storeSensorGroup(group);
}
} else {
LOG(warning) << "Single " << _baseName << " "
<< val.second.data() << " could not be read! Omitting";
......
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