Configurator.h 6.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/*
 * Configurator.h
 *
 *  Created on: 13.01.2018
 *      Author: Micha Mueller
 */

#ifndef SRC_CONFIGURATOR_H_
#define SRC_CONFIGURATOR_H_

11
12
#include "Sensor.h"

13
14
#include <vector>
#include <string>
15
16
17
18
19
20
#include <map>

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

22
23
24
typedef struct {
	std::string mqttPrefix;
	std::string	tempdir;
25
	unsigned int cacheInterval;
26
} pluginSettings_t;
27

Micha Mueller's avatar
Micha Mueller committed
28
/**
29
 * Non-virtual interface template for the configurators.
Micha Mueller's avatar
Micha Mueller committed
30
 */
31
template <typename S>
32
class Configurator {
33
34
35
36
37
	//the template shall only be instantiated for the sensors corresponding to the configurator
	static_assert(std::is_base_of<Sensor, S>::value, "S must derive from Sensor!");

	typedef std::map<std::string, S> sensorMap_t;

38
public:
39
	Configurator() : _cacheInterval(900000) {}
40
41
42
43
44
45

	virtual ~Configurator() {
		for (auto s : _sensors) {
			delete s;
		}
	}
46

Micha Mueller's avatar
Micha Mueller committed
47
48
	/**
	 * Read in the given configuration
49
	 *
Micha Mueller's avatar
Micha Mueller committed
50
	 * @param	cfgPath Path to the config-file
51
52
	 *
	 * @return	True on success, false otherwise
Micha Mueller's avatar
Micha Mueller committed
53
	 */
54
	bool readConfig(std::string cfgPath) {
55
		_cfgPath = cfgPath;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

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

		//read global variables (if present overwrite those from global.conf)
		BOOST_FOREACH(boost::property_tree::iptree::value_type &global, cfg.get_child("global")) {
			if (boost::iequals(global.first, "mqttprefix")) {
				_mqttPrefix = global.second.data();
				if (_mqttPrefix[_mqttPrefix.length()-1] != '/') {
					_mqttPrefix.append("/");
				}
				LOG(debug) << "  Using own MQTT-Prefix " << _mqttPrefix;
			} else if (boost::iequals(global.first, "cacheInterval")) {
				_cacheInterval = stoul(global.second.data());
				LOG(debug) << "  Using own caching interval " << _cacheInterval << " [s]";
				_cacheInterval *= 1000;
			} else {
				LOG(error) << "  Value \"" << global.first << "\" not recognized. Omitting...";
			}
		}

		//read template sensors
		BOOST_FOREACH(boost::property_tree::iptree::value_type &sens, cfg.get_child("templates")) {
			if (boost::iequals(sens.first, "sensor")) {
				LOG(debug) << "Template Sensor \"" << sens.second.data() << "\"";
				if (!sens.second.empty()) {
					S sensor(sens.second.data());
					if(readSensor(sensor, sens.second)) {
						_templateSensors.insert(sensorMap_t::value_type(sensor.getName(), sensor));
					} else {
						LOG(warning) << "Template sensor \"" << sens.second.data() << "\" has bad values! Ignoring...";
					}
				}
			}
		}

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

	/**
	 * Clear internal storage and read in the configuration again.
98
99
	 *
	 * @return	True on success, false otherwise
100
	 */
101
	bool reReadConfig() {
102
103
104
		for (auto s : _sensors) {
			s->stopPolling();
		}
105
106
107

		derivedReReadConfig();

108
109
110
111
		for (auto s : _sensors) {
			delete s;
		}
		_sensors.clear();
112
113
114
		_templateSensors.clear();

		return readConfig(_cfgPath);
115
	}
116

117
118
119
120
121
122
123
124
	/**
	 * 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.
	 */
	void setGlobalSettings(const pluginSettings_t& pluginSettings) {
125
126
		_mqttPrefix = pluginSettings.mqttPrefix;
		_cacheInterval = pluginSettings.cacheInterval;
127
128

		derivedSetGlobalSettings(pluginSettings);
129
130
	}

131
132
133
134
135
	/**
	 * Get all sensors
	 *
	 * @return	Vector containing pointers to all sensors of this plugin
	 */
136
137
138
139
	std::vector<Sensor*>& getSensors() {
		return _sensors;
	}

140
protected:
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
	/**
	 * 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) {
			if (boost::iequals(val.first, "interval")) {
				sensor.setInterval(stoull(val.second.data()));
			} else if (boost::iequals(val.first, "mqttsuffix")) {
				sensor.setMqtt(sensor.getMqtt() + val.second.data());
			} else if (boost::iequals(val.first, "minValues")) {
				sensor.setMinValues(stoull(val.second.data()));
			} else {
				//do nothing. Derived function should read in other values
			}
		}
		sensor.setCacheInterval(_cacheInterval);

		LOG(debug) << "  MQTT     : " << sensor.getMqtt();
		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;
211
	std::vector<Sensor*> _sensors;
212
	sensorMap_t		_templateSensors;
213
	boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
214
215
};

Micha Mueller's avatar
Micha Mueller committed
216
//typedef for more readable usage of create()- and destroy()-methods, required for dynamic libraries
217
218
typedef Configurator<Sensor>* create_t();
typedef void destroy_t(Configurator<Sensor>*);
219
220

#endif /* SRC_CONFIGURATOR_H_ */