Configuration.cpp 4.07 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*
 * Configuration.cpp
 *
 *  Created on: 13.12.2017
 *      Author: Micha Mueller
 */

#include "Configuration.h"
#include <iostream>
#include <string>
#include <unistd.h>
12
#include <dlfcn.h>
13
14
15
16
17
18
19

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

using namespace std;

20
21
22
23
24
25
Configuration::Configuration(const std::string& cfgFilePath) :
		_cfgFilePath(cfgFilePath) {

	if (_cfgFilePath[_cfgFilePath.length()-1] != '/') {
		_cfgFilePath.append("/");
	}
26
27
28
29
30
31

	//set default values for global variables
	_global.brokerHost = "";
	_global.brokerPort = 1883;
	_global.mqttPrefix = "";
	_global.threads	   = 1;
32
33
34
}

Configuration::~Configuration() {
35
	//TODO destroy dl's
36
37
38
39
40
}

bool Configuration::read() {
	std::string globalConfig = _cfgFilePath;
	globalConfig.append("global.conf");
41
42

	boost::property_tree::iptree cfg;
43
44
45
46
47
48
	try {
		boost::property_tree::read_info(globalConfig, cfg);
	} catch (boost::property_tree::info_parser_error& e) {
		cout << "global.conf not found! Please make sure the config-path is correct." << endl;
		return false;
	}
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

	//read global variables
	BOOST_FOREACH(boost::property_tree::iptree::value_type &global, cfg.get_child("global")) {
		cout << global.first << " " << global.second.data() << endl;
		if(boost::iequals(global.first, "mqttBroker")) {
			_global.brokerHost = global.second.data();
			size_t pos = _global.brokerHost.find(":");
			if (pos != string::npos) {
				_global.brokerPort = stoi(_global.brokerHost.substr(pos+1));
				_global.brokerHost.erase(pos);
			}
		} else if(boost::iequals(global.first, "mqttprefix")) {
			_global.mqttPrefix = global.second.data();
			if (_global.mqttPrefix[_global.mqttPrefix.length()-1] != '/') {
				_global.mqttPrefix.append("/");
			}
		} else if (boost::iequals(global.first, "threads")) {
			_global.threads = stoi(global.second.data());
		} else {
			cout << "  Value \"" << global.first << "\" not recognized. Omitting..." << endl;
		}
	}

72
73
	//dl-code based on http://tldp.org/HOWTO/C++-dlopen/thesolution.html

74
75
76
77
78
	std::string sysfsConfig = _cfgFilePath;
	sysfsConfig.append("sysfs.conf");
	ifstream sysfs(sysfsConfig.c_str());
	if (sysfs.good()) {
		cout << "sysfs.conf found" << endl;
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
		void* sysfs = dlopen("./sysfs.so", RTLD_NOW);
		if(!sysfs) {
			cerr << "Cannot load sysfs-library: " << dlerror() << endl;
			return false;
		}
		//reset errors
		dlerror();

		create_t* sysfsCreator = (create_t*) dlsym(sysfs, "create");
		const char* dlsym_error = dlerror();
		if (dlsym_error) {
			cerr << "Cannot load symbol create for sysfs: " << dlsym_error << endl;
			return false;
		}

		destroy_t* sysfsDestructor = (destroy_t*) dlsym(sysfs, "destroy");
		dlsym_error = dlerror();
		if (dlsym_error) {
			cerr << "Cannot load symbol destroy for sysfs: " << dlsym_error << endl;
			return false;
		}

		Configurator* sysfsConfigurator = sysfsCreator();
		std::vector<Sensor>& sysfsSensors = sysfsConfigurator->readConfig(sysfsConfig);

		for(auto& s : sysfsSensors) {
			if(checkMqtt(s.getMqtt())) {
				_sensors.push_back(&s);
			} else {
				cerr << "MQTT-Suffix used twice! Aborting" << endl;
			}
		}
111
112
	}

113
114
115
116
117
118
	std::string perfeventConfig = _cfgFilePath;
	perfeventConfig.append("perfevent.conf");
	ifstream perfevent(perfeventConfig.c_str());
	if (perfevent.good()) {
		cout << "perfevent.conf found" << endl;
		//perfevent.conf exists. Open perfevent.so
119
		//TODO check if mqtt used twice!
120
121
	}

122
123
124
	//TODO check for more config-files

	return true;
125
}
126
/*
127
bool Configuration::readSensorVals(Sensor& sensor, boost::property_tree::iptree& config) {
128
129
	BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
		if (boost::iequals(val.first, "interval")) {
130
			sensor.setInterval(stoull(val.second.data()));
131
		} else if (boost::iequals(val.first, "minValues")) {
132
			sensor.setMinValues(stoull(val.second.data()));
133
		} else {
134
			//
135
136
137
		}
	}

138
139
140
	cout << "  Interval : " << sensor.getInterval() << endl;
	cout << "  minValues: " << sensor.getMinValues() << endl;
	return true;
141
}*/
142

143
144
145
146
147
148
bool Configuration::checkMqtt(const std::string& mqtt) {
	auto returnIt = _mqttSuffixes.insert(mqtt);
	if (!returnIt.second) {
		return false;
	}
	return true;
149
}