Commit 5272e779 authored by Micha Mueller's avatar Micha Mueller
Browse files

WIP: Simplify writing plugin configurators

parent d48f1354
......@@ -22,26 +22,40 @@
#define STRCMP(node,str) boost::iequals(node.first,str)
#define CONFIG boost::property_tree::iptree& config
#define ATTRIBUTES BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) { \
if (false) {}
#define ADD(name,setter) else if (boost::iequals(val.first, name)) { sBase.setter(val.second.data()); }
#define END } return true;
/**
* Non-virtual interface template for the configurators.
*/
template <typename SBase, typename SGroup>
template <class SBase, class SGroup, class SEntity = void>
class ConfiguratorTemplate : public ConfiguratorInterface {
//the template shall only be instantiated for classes which derive from SensorBase/SensorGroup
static_assert(std::is_base_of<SensorBase, SBase>::value, "SBase must derive from SensorBase!");
static_assert(std::is_base_of<SensorGroupTemplate, SGroup>::value, "SGroup must derive from SensorGroupTemplate!");
protected:
typedef std::map<std::string, SGroup> sensorGroupMap_t;
typedef std::map<std::string, SGroup> sGroupMap_t;
typedef std::map<std::string, SEntity> sEntityMap_t;
public:
ConfiguratorTemplate() : _cacheInterval(900000) {}
ConfiguratorTemplate() :
_entityName("entity"),
_groupName("group"),
_sensorName("sensor"),
_cfgPath(""),
_mqttPrefix(""),
_cacheInterval(900000) {}
virtual ~ConfiguratorTemplate() {
for (auto g : _sensorGroups) {
delete g;
}
_templateSensorGroups.clear();
_templateSensorEntitys.clear();
}
/**
......@@ -165,7 +179,7 @@ protected:
* @return True on success, false otherwise
*/
bool readSensorBase(SBase& sBase, boost::property_tree::iptree& config) {
return derivedReadSensorBase(sBase, config);
return sensorBase(sBase, config);
}
/**
......@@ -200,7 +214,11 @@ protected:
LOG(debug) << " Interval : " << sGroup.getInterval();
LOG(debug) << " minValues: " << sGroup.getMinValues();
return derivedReadSensorGroup(sGroup, config);
return sensorGroup(sGroup, config);
}
bool readSensorEntity(SEntity& sEntity, boost::property_tree::iptree& config) {
return sensorEntity(sEntity, config);
}
/**
......@@ -236,7 +254,7 @@ protected:
*
* @return True on success, false otherwise
*/
virtual bool derivedReadSensorBase(SBase& sBase, boost::property_tree::iptree& config) = 0;
virtual bool sensorBase(SBase& sBase, boost::property_tree::iptree& config) = 0;
/**
* Pure virtual interface method, responsible for reading plugin-specific sensor
......@@ -247,13 +265,20 @@ protected:
*
* @return True on success, false otherwise
*/
virtual bool derivedReadSensorGroup(SGroup& sGroup, boost::property_tree::iptree& config) = 0;
virtual bool sensorGroup(SGroup& sGroup, boost::property_tree::iptree& config) = 0;
virtual bool sensorEntity(SEntity& sEntity, boost::property_tree::iptree& config) = 0;
std::string _entityName;
std::string _groupName;
std::string _sensorName;
std::string _cfgPath;
std::string _mqttPrefix;
unsigned int _cacheInterval;
std::vector<SensorGroupInterface*> _sensorGroups;
sensorGroupMap_t _templateSensorGroups;
sGroupMap_t _templateSensorGroups;
sEntityMap_t _templateSensorEntitys;
};
#endif /* SRC_CONFIGURATORTEMPLATE_H_ */
......@@ -11,10 +11,20 @@
using namespace std;
SysfsConfigurator::SysfsConfigurator() {}
SysfsConfigurator::SysfsConfigurator() {
_entityName = "host";
_groupName = "group";
_sensorName = "sensor";
}
SysfsConfigurator::~SysfsConfigurator() {}
bool SysfsConfigurator::Entity(Entity& entity, config) {
Add_Attribute("att", setter);
Add_Attribute("att2", setter2);
Group()
}
bool SysfsConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
//read one sensor at a time
BOOST_FOREACH(boost::property_tree::iptree::value_type &sensor, cfg.get_child("sensors")) {
......@@ -47,7 +57,14 @@ bool SysfsConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
return true;
}
bool SysfsConfigurator::derivedReadSensorBase(SysfsSensorBase& sBase, boost::property_tree::iptree& config) {
bool SysfsConfigurator::sensorBase(SysfsSensorBase& sBase, CONFIG) {
ATTRIBUTES
ADD("mqttsuffix", setMqtt)
ADD("path", setPath)
ADD("filter", setFilter)
END
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
if (STRCMP(val, "mqttsuffix")) {
sBase.setMqtt(_mqttPrefix + val.second.data());
......@@ -85,3 +102,12 @@ bool SysfsConfigurator::derivedReadSensorBase(SysfsSensorBase& sBase, boost::pro
return true;
}
bool SysfsConfigurator::sensorGroup(SysfsSensorGroup& sGroup, boost::property_tree::iptree& config) {
return true;
}
bool SysfsConfiguratorsensorEntity(void& sEntity, boost::property_tree::iptree& config) {
return false;
}
......@@ -12,7 +12,7 @@
#include "SysfsSensorGroup.h"
class SysfsConfigurator : public ConfiguratorTemplate<SysfsSensorBase, SysfsSensorGroup> {
class SysfsConfigurator : public ConfiguratorTemplate<SysfsSensorBase, SysfsSensorGroup, void> {
public:
SysfsConfigurator();
......@@ -23,8 +23,9 @@ protected:
bool derivedReadConfig(boost::property_tree::iptree& cfg) override;
void derivedReReadConfig() override { /* nothing to overwrite */ }
void derivedSetGlobalSettings(const pluginSettings_t& pluginSettings) override { /* nothing to overwrite */ }
bool derivedReadSensorBase(SysfsSensorBase& sBase, boost::property_tree::iptree& config) override;
bool derivedReadSensorGroup(SysfsSensorGroup& sGroup, boost::property_tree::iptree& config) override;
bool sensorBase(SysfsSensorBase& sBase, boost::property_tree::iptree& config) override;
bool sensorGroup(SysfsSensorGroup& sGroup, boost::property_tree::iptree& config) override;
bool sensorEntity(void& sEntity, boost::property_tree::iptree& config) override;
};
extern "C" ConfiguratorInterface* create() {
......
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