PDUConfigurator.cpp 4.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*
 * PDUConfigurator.cpp
 *
 *  Created on: 24.02.2018
 *      Author: Micha Mueller
 */

#include "PDUConfigurator.h"

10
11
#include <iostream>

12
13
14
15
#include <boost/foreach.hpp>
#include <boost/property_tree/info_parser.hpp>
#include <boost/algorithm/string.hpp>

16
17
using namespace std;

18
19
20
21
22
23
24
25
26
27
28
29
PDUConfigurator::PDUConfigurator() {
	// TODO Auto-generated constructor stub

}

PDUConfigurator::~PDUConfigurator() {
	for (auto s : _sensors) {
		delete s;
	}
}

std::vector<Sensor*>& PDUConfigurator::readConfig(std::string cfgPath) {
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
	//read template counters

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

	//read global variables (if present overwrite those from global.conf)
	//currently only overwriting of mqttPrefix is supported
	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")) {
			if(boost::iequals(global.first, "mqttprefix")) {
				_mqttPrefix = global.second.data();
				if (_mqttPrefix[_mqttPrefix.length()-1] != '/') {
					_mqttPrefix.append("/");
				}
				cout << "  Using own MQTT-Prefix " << _mqttPrefix << endl;
			} else {
				cout << "  Value \"" << global.first << "\" not recognized. Omitting..." << endl;
			}
		}
	}

	//read template sensors
	BOOST_FOREACH(boost::property_tree::iptree::value_type &sensor, cfg.get_child("SensorTemplate")) {
		if (boost::iequals(sensor.first, "sensor")) {
			cout << "Template Sensor \"" << sensor.second.data() << "\"" << endl;
			if (!sensor.second.empty()) {
				PDUSensor pduSensor(sensor.second.data());
				if(readSensor(pduSensor, sensor.second)) {
					_templateSensors.insert(sensorMap_t::value_type(pduSensor.getName(), pduSensor));
				} else {
					cout << "Template sensor \"" << sensor.second.data() << "\" has bad values! Ignoring..." << endl;
				}
			}
		}
	}

	//read one pdu at a time
	BOOST_FOREACH(boost::property_tree::iptree::value_type &pdu, cfg.get_child("pdus")) {
		if (boost::iequals(pdu.first, "pdu")) {
			cout << "PDU \"" << pdu.second.data() << "\"" << endl;
			if (!pdu.second.empty()) {
				//create PDU at end of list
				_pdus.push_back(PDUUnit());
				PDUUnit& pduUnit = _pdus.back();

				//read PDU-values
				BOOST_FOREACH(boost::property_tree::iptree::value_type &val, pdu.second) {
78
79
					if (boost::iequals(val.first, "TTL")) {
						pduUnit.setTTL(stoull(val.second.data()));
80
81
					} else if (boost::iequals(val.first, "host")) {
						pduUnit.setHost(val.second.data() + ":443");
82
83
84
85
					} else if (boost::iequals(val.first, "sensors")) {
						BOOST_FOREACH(boost::property_tree::iptree::value_type &sensor, val.second) {
							cout << "sensor \"" << sensor.second.data() << "\"" << endl;
							if (!sensor.second.empty()) {
86
								PDUSensor* pduSensor = new PDUSensor(sensor.second.data());
87
88
89
90
91
92
93

								//first check if default sensor is given
								boost::optional<boost::property_tree::iptree&> defaultC = sensor.second.get_child_optional("default");
								if(defaultC) {
									cout << "  Using \"" << defaultC.get().data() << "\" as default." << endl;
									sensorMap_t::iterator it = _templateSensors.find(defaultC.get().data());
									if(it != _templateSensors.end()) {
94
95
										*pduSensor = it->second;
										pduSensor->setName(sensor.second.data());
96
97
98
99
100
101
									} else {
										cout << "  Template sensor \"" << defaultC.get().data() << "\" not found! Using standard values." << endl;
									}
								}

								//read remaining values
102
								if(readSensor(*pduSensor, sensor.second)) {
103
									//set pointer to corresponding pdu
104
105
									pduSensor->setPdu(&pduUnit);
									_sensors.push_back(pduSensor);
106
107
108
109
110
111
112
113
114
115
								} else {
									cout << "  Counter \"" << sensor.second.data() << "\" has bad values! Ignoring..." << endl;
								}
							}
						}
					}
				}
			}
		}
	}
116
117
118
119
	return _sensors;
}

bool PDUConfigurator::readSensor(PDUSensor& sensor, boost::property_tree::iptree& config) {
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
	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(_mqttPrefix + val.second.data());
		} else if (boost::iequals(val.first, "minValues")) {
			sensor.setMinValues(stoull(val.second.data()));
		} else if (boost::iequals(val.first, "clustsafeID")) {
			sensor.setClustsafeID(stoull(val.second.data()));
		} else if (boost::iequals(val.first, "outletID")) {
			sensor.setOutletID(stoull(val.second.data()));
		} else if (boost::iequals(val.first, "default")) {
			//avoid unnecessary "Value not recognized" message
		} else {
			cout << "  Value \"" << val.first << "\" not recognized. Omitting..." << endl;
		}
	}

	cout << "  MQTT     : " << sensor.getMqtt() << endl;
	cout << "  Interval : " << sensor.getInterval() << endl;
	cout << "  minValues: " << sensor.getMinValues() << endl;
	cout << "  ClustID  : " << sensor.getClustsafeID() << endl;
	cout << "  OutletID : " << sensor.getOutletID() << endl;
	return true;
144
145
}