SysfsConfigurator.cpp 2.87 KB
Newer Older
1
/*
2
 * SysfsConfigurator.cpp
3
4
5
6
7
 *
 *  Created on: 18.11.2017
 *      Author: Micha Mueller
 */

8
#include "SysfsConfigurator.h"
9

10
11
12
13
#include <iostream>

using namespace std;

14
SysfsConfigurator::SysfsConfigurator() {}
15

16
SysfsConfigurator::~SysfsConfigurator() {}
17

18
bool SysfsConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
19
20
	//read one sensor at a time
	BOOST_FOREACH(boost::property_tree::iptree::value_type &sensor, cfg.get_child("sensors")) {
21
		if (STRCMP(sensor, "sensor")) {
22
			LOG(debug) << "Sensor \"" << sensor.second.data() << "\"";
23
			if (!sensor.second.empty()) {
24
				SysfsSingleSensor* sysfsSensor = new SysfsSingleSensor(sensor.second.data());
25
26
27
28

				//first check if default sensor is given
				boost::optional<boost::property_tree::iptree&> defaultS = sensor.second.get_child_optional("default");
				if(defaultS) {
29
					LOG(debug) << "  Using \"" << defaultS.get().data() << "\" as default.";
30
31
					sensorMap_t::iterator it = _templateSensors.find(defaultS.get().data());
					if(it != _templateSensors.end()) {
32
33
						*sysfsSensor = it->second;
						sysfsSensor->setName(sensor.second.data());
34
					} else {
35
						LOG(warning) << "Template sensor \"" << defaultS.get().data() << "\" not found! Using standard values.";
36
37
38
					}
				}
				//read remaining values
39
				if(readSensorBase(*sysfsSensor, sensor.second)) {
40
41
42
43
					_sensors.push_back(sysfsSensor);
				} else {
					LOG(warning) << "  Sensor \"" << sensor.second.data() << "\" has bad values! Ignoring...";
				}
44
45
46
			}
		}
	}
47
	return true;
48
49
}

50
bool SysfsConfigurator::derivedReadSensorBase(SysfsSensorBase& sensor, boost::property_tree::iptree& config) {
51
	BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
52
		if (STRCMP(val, "mqttsuffix")) {
53
			sensor.setMqtt(_mqttPrefix + val.second.data());
54
		} else if (STRCMP(val, ("path"))) {
55
			sensor.setPath(val.second.data());
56
		} else if (STRCMP(val, "filter")) {
57
			sensor.setFilter(true);
58
			string input = val.second.data();
59
60
61
62
63
64
65

			//check if input has sed format of "s/.../.../" for substitution
			regex checkSubstitute("s([^\\\\]{1})([\\S|\\s]*)\\1([\\S|\\s]*)\\1");
			smatch matchResults;

			if(regex_match(input, matchResults, checkSubstitute)) {
				//input has substitute format
66
67
				LOG(debug) << "  Init Regex with: " << matchResults[2].str();
				LOG(debug) << "  Substitution:	" << matchResults[3].str();
68
69
70
71
				sensor.setRegex(regex(matchResults[2].str(), regex_constants::extended));
				sensor.setSubstitution(matchResults[3].str());
			} else {
				//input is only a regex
72
				LOG(debug) << "  Init Regex with " << input;
73
74
75
76
77
				sensor.setRegex(regex(input, regex_constants::extended));
				sensor.setSubstitution("&");
			}
		}
	}
78

79
80
	LOG(debug) << "  MQTT     : " << sensor.getMqtt();
	LOG(debug) << "  Path     : " << sensor.getPath();
81
82
	if (sensor.hasFilter()) {
		//regex cannot be converted back to string
83
		LOG(debug) << "  Using regular expression to filter data";
84
	}
85
	return true;
86
87
}