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

8
9
#ifndef SRC_CONFIGURATORTEMPLATE_H_
#define SRC_CONFIGURATORTEMPLATE_H_
10

11
12
13
14
15
16
#include <map>

#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/info_parser.hpp>
17
18
19
#include "ConfiguratorInterface.h"
#include "SensorBase.h"
#include "SensorGroupTemplate.h"
20

21
//#define STRCMP(node,str) boost::iequals(node.first,str) //DEPRECATED
Micha Mueller's avatar
Micha Mueller committed
22
#define CFG_VAL	boost::property_tree::iptree&
23

24
#define ADD						BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config)
25
#define ATTRIBUTE(name,setter)		do { if (boost::iequals(val.first, name)) { s.setter(val.second.data()); } } while(0)
Micha Mueller's avatar
Micha Mueller committed
26
#define SETTING(name)				if (boost::iequals(val.first, name))
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 <class SBase, class SGroup, class SEntity = nullptr_t>
32
class ConfiguratorTemplate : public ConfiguratorInterface {
33
	//the template shall only be instantiated for classes which derive from SensorBase/SensorGroup
34
	static_assert(std::is_base_of<SensorBase, SBase>::value, "SBase must derive from SensorBase!");
35
	static_assert(std::is_base_of<SensorGroupInterface, SGroup>::value, "SGroup must derive from SensorGroupInterface!");
36

37
protected:
Michael Ott's avatar
Michael Ott committed
38
	typedef std::map<std::string, SBase*> sBaseMap_t;
39
40
	typedef std::map<std::string, SGroup*> sGroupMap_t;
	typedef std::map<std::string, SEntity*> sEntityMap_t;
41

42
public:
43
	ConfiguratorTemplate() :
44
45
46
		_entityName("INVALID"),
		_groupName("INVALID"),
		_baseName("INVALID"),
47
48
49
		_cfgPath(""),
		_mqttPrefix(""),
		_cacheInterval(900000) {}
50

51
52
	ConfiguratorTemplate(const ConfiguratorTemplate&) = delete;

53
	virtual ~ConfiguratorTemplate() {
54
55
		for (auto g : _sensorGroups) {
			delete g;
56
		}
57
58
59
		for (auto e : _sensorEntitys) {
			delete e;
		}
Michael Ott's avatar
Michael Ott committed
60
61
62
		for (auto tb : _templateSensorBases) {
			delete tb.second;
		}
63
		for (auto tg : _templateSensorGroups) {
64
			delete tg.second;
65
66
		}
		for (auto te : _templateSensorEntitys) {
67
			delete te.second;
68
		}
69
		_sensorGroupInterfaces.clear();
70
71
		_sensorGroups.clear();
		_sensorEntitys.clear();
Michael Ott's avatar
Michael Ott committed
72
		_templateSensorBases.clear();
73
		_templateSensorGroups.clear();
74
		_templateSensorEntitys.clear();
75
	}
76

77
78
	ConfiguratorTemplate& operator=(const ConfiguratorTemplate&) = delete;

Micha Mueller's avatar
Micha Mueller committed
79
80
	/**
	 * Read in the given configuration
81
	 *
82
83
	 * Overwriting this method is only required if a custom logic is really necessary!
	 *
Micha Mueller's avatar
Micha Mueller committed
84
	 * @param	cfgPath Path to the config-file
85
86
	 *
	 * @return	True on success, false otherwise
Micha Mueller's avatar
Micha Mueller committed
87
	 */
88
	bool readConfig(std::string cfgPath) {
89
		_cfgPath = cfgPath;
90
91
92
93
94

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

		//read global variables (if present overwrite those from global.conf)
95
96
		readGlobal(cfg);

97
98
		//read groups and templates for groups. If present also entity/-template stuff
		BOOST_FOREACH(boost::property_tree::iptree::value_type &val, cfg) {
99
			//TODO allow single sensors for convenience?
100
101
102
103
			//template entity
			if (boost::iequals(val.first, "template_" + _entityName)) {
				LOG(debug) << "Template " << _entityName << " \"" << val.second.data() << "\"";
				if (!val.second.empty()) {
Micha Mueller's avatar
Micha Mueller committed
104
105
106
					//name of an entity is only used to identify templates and is not stored otherwise
					SEntity* entity = new SEntity();
					if (readSensorEntity(*entity, val.second, true)) {
107
						auto ret = _templateSensorEntitys.insert(std::pair<std::string, SEntity*>(val.second.data(), entity));
108
						if(!ret.second) {
109
							LOG(warning) << "Template " << _entityName << " " << val.second.data() << " already exists! Omitting...";
Micha Mueller's avatar
Micha Mueller committed
110
							delete entity;
111
112
113
						}
					} else {
						LOG(warning) << "Template " << _entityName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
Micha Mueller's avatar
Micha Mueller committed
114
						delete entity;
115
116
117
118
119
120
121
122
					}
				}
			//template group
			} else if (boost::iequals(val.first, "template_" + _groupName)) {
				LOG(debug) << "Template " << _groupName << " \"" << val.second.data() << "\"";
				if (!val.second.empty()) {
					SGroup* group = new SGroup(val.second.data());
					if (readSensorGroup(*group, val.second)) {
123
						auto ret = _templateSensorGroups.insert(std::pair<std::string, SGroup*>(val.second.data(), group));
124
						if(!ret.second) {
125
							LOG(warning) << "Template " << _groupName << " " << val.second.data() << " already exists! Omitting...";
Micha Mueller's avatar
Micha Mueller committed
126
							delete group;
127
						}
128
129
					} else {
						LOG(warning) << "Template " << _groupName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
Micha Mueller's avatar
Micha Mueller committed
130
						delete group;
131
132
					}
				}
Michael Ott's avatar
Michael Ott committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
			//template base
			} else if (boost::iequals(val.first, "template_" + _baseName)) {
				LOG(debug) << "Template " << _baseName << " \"" << val.second.data() << "\"";
				if (!val.second.empty()) {
					SBase* base = new SBase(val.second.data());
					if (readSensorBase(*base, val.second)) {
						auto ret = _templateSensorBases.insert(std::pair<std::string, SBase*>(val.second.data(), base));
						if(!ret.second) {
							LOG(warning) << "Template " << _baseName << " " << val.second.data() << " already exists! Omitting...";
							delete base;
						}
					} else {
						LOG(warning) << "Template " << _baseName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
						delete base;
					}
				}
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
			//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;
					}
				}
173
174
175
176
			//entity
			} else if (boost::iequals(val.first, _entityName)) {
				LOG(debug) << _entityName << " \"" << val.second.data() << "\"";
				if (!val.second.empty()) {
Micha Mueller's avatar
Micha Mueller committed
177
178
					SEntity* entity = new SEntity();
					if (readSensorEntity(*entity, val.second, false)) {
179
180
181
						_sensorEntitys.push_back(entity);
					} else {
						LOG(warning) << _entityName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
Micha Mueller's avatar
Micha Mueller committed
182
						delete entity;
183
					}
184
185
186
187
188
189
190
				}
			//group
			} else if (boost::iequals(val.first, _groupName)) {
				LOG(debug) << _groupName << " \"" << val.second.data() << "\"";
				if (!val.second.empty()) {
					SGroup* group = new SGroup(val.second.data());
					if (readSensorGroup(*group, val.second)) {
191
						storeSensorGroup(group);
192
193
					} else {
						LOG(warning) << _groupName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
Micha Mueller's avatar
Micha Mueller committed
194
						delete group;
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
			//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;
					}
				}
230
231
			}
		}
232
233
234
235
		//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());
236
				LOG(debug) << g->getGroupName() << "::" << s->getName() << " using MQTT-topic \"" << s->getMqtt() << "\"";
237
238
			}
		}
239
		return true;
240
241
242
243
	}

	/**
	 * Clear internal storage and read in the configuration again.
244
245
	 *
	 * @return	True on success, false otherwise
246
	 */
247
	bool reReadConfig() final {
Micha Mueller's avatar
Micha Mueller committed
248
249
		//bring everything to a halt
		for(auto g : _sensorGroups) {
250
			g->stop();
Micha Mueller's avatar
Micha Mueller committed
251
252
253
254
255
256
		}

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

258
		//clean up sensors/groups/entitys and templates
Micha Mueller's avatar
Micha Mueller committed
259
260
261
		for(auto g : _sensorGroups) {
			delete g;
		}
262
263
264
		for(auto e : _sensorEntitys) {
			delete e;
		}
Michael Ott's avatar
Michael Ott committed
265
266
267
		for (auto tb : _templateSensorBases) {
			delete tb.second;
		}
268
		for (auto tg : _templateSensorGroups) {
269
			delete tg.second;
270
271
		}
		for (auto te : _templateSensorEntitys) {
272
			delete te.second;
273
		}
274
		_sensorGroupInterfaces.clear();
Micha Mueller's avatar
Micha Mueller committed
275
		_sensorGroups.clear();
276
		_sensorEntitys.clear();
Michael Ott's avatar
Michael Ott committed
277
		_templateSensorBases.clear();
278
		_templateSensorGroups.clear();
279
		_templateSensorEntitys.clear();
280

Micha Mueller's avatar
Micha Mueller committed
281
		//back to the very beginning
282
		return readConfig(_cfgPath);
283
	}
284

285
286
287
288
289
290
291
	/**
	 * 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.
	 */
292
	void setGlobalSettings(const pluginSettings_t& pluginSettings) final {
293
294
		_mqttPrefix = pluginSettings.mqttPrefix;
		_cacheInterval = pluginSettings.cacheInterval;
295
296

		derivedSetGlobalSettings(pluginSettings);
297
298
	}

Micha Mueller's avatar
Micha Mueller committed
299
300
301
302
303
	/**
	 * Get all sensor groups
	 *
	 * @return	Vector containing pointers to all sensor groups of this plugin
	 */
Micha Mueller's avatar
Micha Mueller committed
304
	std::vector<SensorGroupInterface*>& getSensorGroups() final {
305
306
307
		return _sensorGroupInterfaces;
	}

308
protected:
309
310
311
	void storeSensorGroup(SGroup* sGroup) {
		_sensorGroups.push_back(sGroup);
		_sensorGroupInterfaces.push_back(sGroup);
Micha Mueller's avatar
Micha Mueller committed
312
313
	}

314
315
	/**
	 * Non-virtual interface method for class-internal use only.
316
	 * Reads and sets the common base values of a sensor base (currently none),
317
318
	 * then calls the corresponding derived function to read plugin specific
	 * values.
319
	 *
320
	 * @param sBase		The sensor base for which to set the values
321
322
323
324
	 * @param config	A boost property (sub-)tree containing the sensor values
	 *
	 * @return	True on success, false otherwise
	 */
Micha Mueller's avatar
Micha Mueller committed
325
	bool readSensorBase(SBase& sBase, CFG_VAL config) {
Michael Ott's avatar
Michael Ott committed
326
327
328
329
330
331
332
333
334
335
336
337
338
339
		boost::optional<boost::property_tree::iptree&> def = config.get_child_optional("default");
		if(def) {
			//we copy all values from default (including copy constructing its sensors)
			//if own sensors are specified they are appended
			LOG(debug) << "  Using \"" << def.get().data() << "\" as default.";
			auto it = _templateSensorBases.find(def.get().data());
			if(it != _templateSensorBases.end()) {
				sBase = *(it->second);
				sBase.setName(config.data());
			} else {
				LOG(warning) << "Template " << _groupName << "\"" << def.get().data() << "\" not found! Using standard values.";
			}
		}
		
340
341
342
343
344
345
346
		BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
			if (boost::iequals(val.first, "mqttsuffix")) {
				sBase.setMqtt(val.second.data());
			}
		}
		sensorBase(sBase, config);
		return true;
347
348
349
350
	}

	/**
	 * Non-virtual interface method for class-internal use only.
351
352
	 * Reads and sets the common base values of a sensor group, then calls
	 * the corresponding derived function to read in plugin specific values.
353
	 *
354
	 * @param sGroup	The sensor group for which to set the values
355
356
357
358
	 * @param config	A boost property (sub-)tree containing the sensor values
	 *
	 * @return	True on success, false otherwise
	 */
Micha Mueller's avatar
Micha Mueller committed
359
	bool readSensorGroup(SGroup& sGroup, CFG_VAL config) {
360
		sGroup.setCacheInterval(_cacheInterval);
361
362
363
364
365
366
367
368
		//first check if default group is given
		boost::optional<boost::property_tree::iptree&> def = config.get_child_optional("default");
		if(def) {
			//we copy all values from default (including copy constructing its sensors)
			//if own sensors are specified they are appended
			LOG(debug) << "  Using \"" << def.get().data() << "\" as default.";
			auto it = _templateSensorGroups.find(def.get().data());
			if(it != _templateSensorGroups.end()) {
369
370
				sGroup = *(it->second);
				sGroup.setGroupName(config.data());
371
372
373
374
375
			} else {
				LOG(warning) << "Template " << _groupName << "\"" << def.get().data() << "\" not found! Using standard values.";
			}
		}

376
		//read in values inherited from SensorGroupInterface
377
		BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
378
			if (boost::iequals(val.first, "interval")) {
379
				sGroup.setInterval(stoull(val.second.data()));
380
			} else if (boost::iequals(val.first, "minValues")) {
381
				sGroup.setMinValues(stoull(val.second.data()));
382
383
			} else if (boost::iequals(val.first, "mqttPart")) {
				sGroup.setMqttPart(val.second.data());
384
385
			} else if (boost::iequals(val.first, _baseName)) {
				LOG(debug) << "  " << _baseName << " " << val.second.data();
386
387
				SBase* sensor = new SBase(val.second.data());
				if (readSensorBase(*sensor, val.second)) {
388
					sGroup.pushBackSensor(sensor);
389
				} else {
390
					LOG(warning) << _baseName << " " << sGroup.getGroupName() << "::" << sensor->getName() << " could not be read! Omitting";
Micha Mueller's avatar
Micha Mueller committed
391
					delete sensor;
392
				}
393
394
395
			}
		}

396
397
398
		//TODO keep debug logging for config?
//		LOG(debug) << "  Interval : " << sGroup.getInterval();
//		LOG(debug) << "  minValues: " << sGroup.getMinValues();
399

400
401
		sensorGroup(sGroup, config);
		return true;
402
403
	}

404
405
406
407
408
409
410
	/**
	 * Non-virtual interface method for class-internal use only.
	 * Reads and sets the common base values of a sensor entity, then calls
	 * the corresponding derived function to read in plugin specific values.
	 *
	 * @param sEntity	The aggregating entity for which to set the values
	 * @param config	A boost property (sub-)tree containing the sensor values
Micha Mueller's avatar
Micha Mueller committed
411
412
	 * @param isTemplate	Indicate if sEntity is a template. If so, also store
	 * 						the corresponding sGroups in the template map
413
414
415
	 *
	 * @return	True on success, false otherwise
	 */
Micha Mueller's avatar
Micha Mueller committed
416
	bool readSensorEntity(SEntity& sEntity, CFG_VAL config, bool isTemplate) {
417
418
419
420
421
422
423
		//first check if default entity is given
		boost::optional<boost::property_tree::iptree&> def = config.get_child_optional("default");
		if(def) {
			//we copy all values from default
			LOG(debug) << "  Using \"" << def.get().data() << "\" as default.";
			auto it = _templateSensorEntitys.find(def.get().data());
			if(it != _templateSensorEntitys.end()) {
424
				sEntity = *(it->second);
Micha Mueller's avatar
Micha Mueller committed
425
426
427
428
				for(auto g : _templateSensorGroups) {
					if (isEntityOfGroup(*(it->second), *(g.second))) {
						SGroup* group = new SGroup(*(g.second));
						setEntityForGroup(sEntity, *group);
429
						storeSensorGroup(group);
Micha Mueller's avatar
Micha Mueller committed
430
431
					}
				}
432
433
434
435
			} else {
				LOG(warning) << "Template " << _entityName << "\"" << def.get().data() << "\" not found! Using standard values.";
			}
		}
Micha Mueller's avatar
Micha Mueller committed
436

437
		sensorEntity(sEntity, config);
Micha Mueller's avatar
Micha Mueller committed
438
439
440
441

		BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
			if (boost::iequals(val.first, _groupName)) {
				LOG(debug) << "  " << _groupName << " " << val.second.data();
442
443
444
445
446
447
448
449
450
451
452
453
				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);
Micha Mueller's avatar
Micha Mueller committed
454
455
						}
					} else {
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
						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;
Micha Mueller's avatar
Micha Mueller committed
506
507
508
509
510
					}
				}
			}
		}

511
512
513
514
515
		if(!isTemplate) {
			for(auto g : _sensorGroups) {
				if(isEntityOfGroup(sEntity, *g)) {
					finalizeGroup(*g);
				}
Micha Mueller's avatar
Micha Mueller committed
516
517
			}
		}
518
		return true;
519
520
	}

Micha Mueller's avatar
Micha Mueller committed
521
	bool readGlobal(CFG_VAL config) {
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
		boost::optional<boost::property_tree::iptree&> globalVals = config.get_child_optional("global");
		if (globalVals) {
			BOOST_FOREACH(boost::property_tree::iptree::value_type &global, config.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;
				}
			}
537
			global(config.get_child("global"));
538
		}
539
		return true;
540
541
	}

542
	/**
543
	 * Virtual interface method, responsible for setting global values specifically
544
545
546
547
	 * for its plugin.
	 *
	 * @param pluginSettings	The struct with global default plugin settings
	 */
548
549
550
	virtual void derivedSetGlobalSettings(const pluginSettings_t& pluginSettings) {
		//Overwrite if necessary
	}
551
552
553

	/**
	 * Pure virtual interface method, responsible for reading plugin-specific sensor
554
	 * base values.
555
	 *
Micha Mueller's avatar
Micha Mueller committed
556
	 * @param s			The sensor base for which to set the values
557
558
	 * @param config	A boost property (sub-)tree containing the sensor values
	 */
Micha Mueller's avatar
Micha Mueller committed
559
	virtual void sensorBase(SBase& s, CFG_VAL config) = 0;
560
561
562
563
564

	/**
	 * Pure virtual interface method, responsible for reading plugin-specific sensor
	 * group values.
	 *
Micha Mueller's avatar
Micha Mueller committed
565
	 * @param s			The sensor group for which to set the values
566
	 * @param config	A boost property (sub-)tree containing the group values
567
	 */
Micha Mueller's avatar
Micha Mueller committed
568
	virtual void sensorGroup(SGroup& s, CFG_VAL config) = 0;
569

570
571
572
573
	/**
	 * Virtual interface method, responsible for reading plugin-specific sensor
	 * entity values.
	 *
Micha Mueller's avatar
Micha Mueller committed
574
	 * @param s			The sensor entity for which to set the values
575
576
	 * @param config	A boost property (sub-)tree containing the entity values
	 */
Micha Mueller's avatar
Micha Mueller committed
577
	virtual void sensorEntity(SEntity& s, CFG_VAL config) {
578
579
580
		//Overwrite if necessary
		LOG(warning) << "Method sensorEntity called, but was not overwritten! Either you have unwanted entitys in your config file or forgot to overwrite this method";
	}
581

Micha Mueller's avatar
Micha Mueller committed
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
	/**
	 * Check if e is the corresponding entity of g
	 *
	 * @param e
	 * @param g
	 *
	 * @return	True if (g.entity == &e)
	 */
	//TODO not very convenient for writing plugins. Are there better solutions?
	virtual bool isEntityOfGroup(SEntity& e, SGroup& g) {
		//Overwrite if necessary
		LOG(warning) << "Method isEntityOfGroup called, but was not overwritten! Either you have unwanted entitys in your config file or forgot to overwrite this method";
		return false;
	}

	/**
	 * Sets e as entity for group g
	 *
	 * @param e
	 * @param g
	 */
	//TODO not very convenient for writing plugins. Are there better solutions?
	virtual void setEntityForGroup(SEntity& e, SGroup& g) {
		//Overwrite if necessary
		LOG(warning) << "Method setEntityForGroup called, but was not overwritten! Either you have unwanted entitys in your config file or forgot to overwrite this method";
	}

	/**
	 * Finalize the group g with everything it needs from its entity (set e.g. the mqttPart for entity)
	 *
	 * @param g
	 */
	//TODO not very convenient for writing plugins. Are there better solutions?
	virtual void finalizeGroup(SGroup& g) {
		//Overwrite if necessary
		LOG(warning) << "Method finalizeEntityGroup called, but was not overwritten! Either you have unwanted entitys in your config file or forgot to overwrite this method";
	}

620
621
622
623
624
	/**
	 * Virtual interface method, responsible for reading plugin-specific global values.
	 *
	 * @param config	A boost property (sub-)tree containing the global values
	 */
Micha Mueller's avatar
Micha Mueller committed
625
	virtual void global(CFG_VAL config) {}
626

627
628
	std::string		_entityName;
	std::string		_groupName;
629
	std::string		_baseName;
630
631
632
633

	std::string 	_cfgPath;
	std::string		_mqttPrefix;
	unsigned int	_cacheInterval;
634
635
636
	std::vector<SensorGroupInterface*> _sensorGroupInterfaces;
	std::vector<SGroup*>	_sensorGroups;
	std::vector<SEntity*>	_sensorEntitys;
Michael Ott's avatar
Michael Ott committed
637
	sBaseMap_t		_templateSensorBases;
638
639
	sGroupMap_t		_templateSensorGroups;
	sEntityMap_t	_templateSensorEntitys;
640
641
};

642
#endif /* SRC_CONFIGURATORTEMPLATE_H_ */