ConfiguratorTemplate.h 6.58 KB
Newer Older
1
2
3
4
5
6
7
/*
 * Configurator.h
 *
 *  Created on: 13.01.2018
 *      Author: Micha Mueller
 */

8
9
#ifndef SRC_CONFIGURATORTEMPLATE_H_
#define SRC_CONFIGURATORTEMPLATE_H_
10

11
#include "ConfiguratorInterface.h"
12

13
14
15
16
17
18
#include <map>

#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/info_parser.hpp>
19

20
21
#define STRCMP(node,str) boost::iequals(node.first,str)

Micha Mueller's avatar
Micha Mueller committed
22
/**
23
 * Non-virtual interface template for the configurators.
Micha Mueller's avatar
Micha Mueller committed
24
 */
25
template <typename S>
26
class ConfiguratorTemplate : public ConfiguratorInterface {
27
	//the template shall only be instantiated for classes which implement the sensor interface
28
29
	static_assert(std::is_base_of<Sensor, S>::value, "S must derive from Sensor!");

30
protected:
31
32
	typedef std::map<std::string, S> sensorMap_t;

33
public:
34
	ConfiguratorTemplate() : _cacheInterval(900000) {}
35

36
	virtual ~ConfiguratorTemplate() {
37
38
39
40
		for (auto s : _sensors) {
			delete s;
		}
	}
41

Micha Mueller's avatar
Micha Mueller committed
42
43
	/**
	 * Read in the given configuration
44
	 *
Micha Mueller's avatar
Micha Mueller committed
45
	 * @param	cfgPath Path to the config-file
46
47
	 *
	 * @return	True on success, false otherwise
Micha Mueller's avatar
Micha Mueller committed
48
	 */
49
	bool readConfig(std::string cfgPath) final {
50
		_cfgPath = cfgPath;
51
52
53
54
55

		boost::property_tree::iptree cfg;
		boost::property_tree::read_info(cfgPath, cfg);

		//read global variables (if present overwrite those from global.conf)
56
57
58
		boost::optional<boost::property_tree::iptree&> globalVals = cfg.get_child_optional("global");
		if (globalVals) {
			BOOST_FOREACH(boost::property_tree::iptree::value_type &global, cfg.get_child("global")) {
59
				if (STRCMP(global, "mqttprefix")) {
60
61
62
63
64
					_mqttPrefix = global.second.data();
					if (_mqttPrefix[_mqttPrefix.length()-1] != '/') {
						_mqttPrefix.append("/");
					}
					LOG(debug) << "  Using own MQTT-Prefix " << _mqttPrefix;
65
				} else if (STRCMP(global, "cacheInterval")) {
66
67
68
					_cacheInterval = stoul(global.second.data());
					LOG(debug) << "  Using own caching interval " << _cacheInterval << " [s]";
					_cacheInterval *= 1000;
69
70
71
72
73
				}
			}
		}

		//read template sensors
74
75
76
		boost::optional<boost::property_tree::iptree&> tempSens = cfg.get_child_optional("sensorTemplates");
		if (tempSens) {
			BOOST_FOREACH(boost::property_tree::iptree::value_type &sensorVal, cfg.get_child("sensorTemplates")) {
77
				if (STRCMP(sensorVal, "sensor")) {
78
79
80
81
82
83
84
85
					LOG(debug) << "Template Sensor \"" << sensorVal.second.data() << "\"";
					if (!sensorVal.second.empty()) {
						S sensor(sensorVal.second.data());
						if(readSensor(sensor, sensorVal.second)) {
							_templateSensors.insert(std::pair<std::string, S>(sensor.getName(), sensor));
						} else {
							LOG(warning) << "Template sensor \"" << sensorVal.second.data() << "\" has bad values! Ignoring...";
						}
86
87
88
89
90
91
92
					}
				}
			}
		}

		//read in plugin specific stuff (including actual sensors)
		return derivedReadConfig(cfg);
93
94
95
96
	}

	/**
	 * Clear internal storage and read in the configuration again.
97
98
	 *
	 * @return	True on success, false otherwise
99
	 */
100
	bool reReadConfig() final {
Micha Mueller's avatar
Micha Mueller committed
101
		//bring everything to a halt
102
103
104
		for (auto s : _sensors) {
			s->stopPolling();
		}
Micha Mueller's avatar
Micha Mueller committed
105
106
107
108
109
110
111
112
113
114
115
		for(auto g : _sensorGroups) {
			g->stopPolling();
		}

		//wait until everything is halted
		for (auto s : _sensors) {
			s->wait();
		}
		for(auto g : _sensorGroups) {
			g->wait();
		}
116

Micha Mueller's avatar
Micha Mueller committed
117
		//clean up plugin specific stuff
118
119
		derivedReReadConfig();

Micha Mueller's avatar
Micha Mueller committed
120
		//clean up sensors and groups
121
122
123
		for (auto s : _sensors) {
			delete s;
		}
Micha Mueller's avatar
Micha Mueller committed
124
125
126
127
		for(auto g : _sensorGroups) {
			delete g;
		}

128
		_sensors.clear();
Micha Mueller's avatar
Micha Mueller committed
129
		_sensorGroups.clear();
130
131
		_templateSensors.clear();

Micha Mueller's avatar
Micha Mueller committed
132
		//back to the very beginning
133
		return readConfig(_cfgPath);
134
	}
135

136
137
138
139
140
141
142
	/**
	 * Sets internal variables with the ones provided by pluginSettings.
	 * This method should be called once after constructing a configurator
	 * to provide him with the global default values.
	 *
	 * @param pluginSettings	Struct with global default settings for the plugins.
	 */
143
	void setGlobalSettings(const pluginSettings_t& pluginSettings) final {
144
145
		_mqttPrefix = pluginSettings.mqttPrefix;
		_cacheInterval = pluginSettings.cacheInterval;
146
147

		derivedSetGlobalSettings(pluginSettings);
148
149
	}

150
151
152
153
154
	/**
	 * Get all sensors
	 *
	 * @return	Vector containing pointers to all sensors of this plugin
	 */
155
	std::vector<Sensor*>& getSensors() final {
156
157
158
		return _sensors;
	}

Micha Mueller's avatar
Micha Mueller committed
159
160
161
162
163
164
165
166
167
	/**
	 * Get all sensor groups
	 *
	 * @return	Vector containing pointers to all sensor groups of this plugin
	 */
	std::vector<SensorGroup*>& getSensorGroups() {
		return _sensorGroups;
	}

168
protected:
169
170
171
172
173
174
175
176
177
178
179
180
181
	/**
	 * Non-virtual interface method for class-internal use only.
	 * Reads and sets the common base values of a sensor, then calls
	 * the corresponding derived function.
	 *
	 * @param sensor	The sensor for which to set the values
	 * @param config	A boost property (sub-)tree containing the sensor values
	 *
	 * @return	True on success, false otherwise
	 */
	bool readSensor(S& sensor, boost::property_tree::iptree& config) {
		//read in base values
		BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
182
			if (STRCMP(val, "interval")) {
183
				sensor.setInterval(stoull(val.second.data()));
184
			} else if (STRCMP(val, "minValues")) {
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
				sensor.setMinValues(stoull(val.second.data()));
			}
		}
		sensor.setCacheInterval(_cacheInterval);

		LOG(debug) << "  Interval : " << sensor.getInterval();
		LOG(debug) << "  minValues: " << sensor.getMinValues();

		return derivedReadSensor(sensor, config);
	}

	/**
	 * Pure virtual interface method, responsible for reading the plugin-specific
	 * configuration part.
	 *
	 * @param cfg	The (root) boost property tree from the plugins configuration file
	 *
	 * @return	True on success, false otherwise
	 */
	virtual bool derivedReadConfig(boost::property_tree::iptree& cfg) = 0;

	/**
	 * Pure virtual interface method, responsible for the plugin specific part if
	 * re-reading the config.
	 */
	virtual void derivedReReadConfig() = 0;

	/**
	 * Pure virtual interface method, responsible for setting global values specifically
	 * for its plugin.
	 *
	 * @param pluginSettings	The struct with global default plugin settings
	 */
	virtual void derivedSetGlobalSettings(const pluginSettings_t& pluginSettings) = 0;

	/**
	 * Pure virtual interface method, responsible for reading plugin-specific sensor
	 * values.
	 *
	 * @param sensor	The sensor for which to set the values
	 * @param config	A boost property (sub-)tree containing the sensor values
	 *
	 * @return	True on success, false otherwise
	 */
	virtual bool derivedReadSensor(S& sensor, boost::property_tree::iptree& config) = 0;

	std::string 	_cfgPath;
	std::string		_mqttPrefix;
	unsigned int	_cacheInterval;
234
	std::vector<Sensor*> _sensors;
Micha Mueller's avatar
Micha Mueller committed
235
	std::vector<SensorGroup*> _sensorGroups;
236
	sensorMap_t		_templateSensors;
237
238
};

239
#endif /* SRC_CONFIGURATORTEMPLATE_H_ */