Commit aac7502a authored by Micha Mueller's avatar Micha Mueller
Browse files

Make caching interval configurable by cmd line. Minor refactor

parent 6bf413cc
......@@ -126,7 +126,57 @@ bool Configuration::readGlobal() {
return true;
}
bool Configuration::read() {
bool Configuration::readAuthkeys(HttpsServer* server) {
//open file
std::string globalConfig = _cfgFilePath;
globalConfig.append("global.conf");
boost::property_tree::iptree cfg;
//parse to property_tree
try {
boost::property_tree::read_info(globalConfig, cfg);
} catch (boost::property_tree::info_parser_error& e) {
LOG(error) << "Error when reading authkeys from global.conf: " << e.what();
return false;
}
//read authkeys
BOOST_FOREACH(boost::property_tree::iptree::value_type &global, cfg.get_child("restAPI")) {
if (boost::iequals(global.first, "authkey")) {
#ifdef DEBUG
LOG(info) << "Authentication token \"" << global.second.data() << "\"";
#endif
std::bitset<NUM_PERMISSIONS> permissions;
BOOST_FOREACH(boost::property_tree::iptree::value_type &perm, global.second) {
if (boost::iequals(perm.first, "GETReq")) {
#ifdef DEBUG
LOG(info) << " Permission \"GETReq\"";
#endif
permissions[GETReq] = true;
} else if (boost::iequals(perm.first, "PUTReq")) {
#ifdef DEBUG
LOG(info) << " Permission \"PUTReq\"";
#endif
permissions[PUTReq] = true;
} else {
#ifdef DEBUG
LOG(warning) << "Permission \"" << perm.first << "\" not recognized. Omitting";
#endif
}
}
if (!server->addAuthkey(authkeyMap_t::value_type(global.second.data(), permissions))) {
#ifdef DEBUG
LOG(warning) << "Authkey already present!";
#endif
}
} else {
//
}
}
return true;
}
bool Configuration::readPlugins() {
std::string globalConfig = _cfgFilePath;
globalConfig.append("global.conf");
......@@ -242,56 +292,6 @@ bool Configuration::read() {
return true;
}
bool Configuration::readAuthkeys(HttpsServer* server) {
//open file
std::string globalConfig = _cfgFilePath;
globalConfig.append("global.conf");
boost::property_tree::iptree cfg;
//parse to property_tree
try {
boost::property_tree::read_info(globalConfig, cfg);
} catch (boost::property_tree::info_parser_error& e) {
LOG(error) << "Error when reading authkeys from global.conf: " << e.what();
return false;
}
//read authkeys
BOOST_FOREACH(boost::property_tree::iptree::value_type &global, cfg.get_child("restAPI")) {
if (boost::iequals(global.first, "authkey")) {
#ifdef DEBUG
LOG(info) << "Authentication token \"" << global.second.data() << "\"";
#endif
std::bitset<NUM_PERMISSIONS> permissions;
BOOST_FOREACH(boost::property_tree::iptree::value_type &perm, global.second) {
if (boost::iequals(perm.first, "GETReq")) {
#ifdef DEBUG
LOG(info) << " Permission \"GETReq\"";
#endif
permissions[GETReq] = true;
} else if (boost::iequals(perm.first, "PUTReq")) {
#ifdef DEBUG
LOG(info) << " Permission \"PUTReq\"";
#endif
permissions[PUTReq] = true;
} else {
#ifdef DEBUG
LOG(warning) << "Permission \"" << perm.first << "\" not recognized. Omitting";
#endif
}
}
if (!server->addAuthkey(authkeyMap_t::value_type(global.second.data(), permissions))) {
#ifdef DEBUG
LOG(warning) << "Authkey already present!";
#endif
}
} else {
//
}
}
return true;
}
bool Configuration::checkMqtt(const std::string& mqtt) {
//MQTT topic must have 128 bit = 16 bytes = 32 hex chars
//but can have more with some extra '/', therefore remove all '/'
......
......@@ -28,13 +28,13 @@
restAPISettings_t restAPISettings;
} global_t;
typedef std::set<std::string> mqttSet_t;
/**
* Class responsible of reading the global configuration as well as loading and invoking required dynamic libraries.
*/
class Configuration {
typedef std::set<std::string> mqttSet_t;
public:
/**
* Create new Configuration. Sets global config file to read from to cfgFile.
......@@ -53,21 +53,21 @@ public:
bool readGlobal();
/**
* Reads configuration (except global values) from global.conf (located at _cfgFilePath).
* Detects which sensor types are required and dynamically opens required libraries.
* Invokes dynamic libraries to read their configuration.
* Reads the authentication keys and forwards them to the HttpsServer
*
* @param server The Rest API server where to add the authkeys
* @return true on success, false otherwise
*/
bool read();
bool readAuthkeys(HttpsServer* server);
/**
* Reads the authentication keys and forwards them to the HttpsServer
* Reads the plugin configuration section from global.conf (located at _cfgFilePath).
* Detects which sensor types are required and dynamically opens required plugins.
* Invokes the plugin to read its configuration and generate specified sensors.
*
* @param server The Rest API server where to add the authkeys
* @return true on success, false otherwise
*/
bool readAuthkeys(HttpsServer* server);
bool readPlugins();
/**
* Read and set general sensor values (like interval, minvalues, ...).
......@@ -102,7 +102,10 @@ private:
global_t _global;
mqttSet_t _mqttTopics;
//vector which holds data of the opened plugins
/**
* Vector which holds data of the opened plugins.
* Only references to this member should be handed around, to keep the plugins everywhere in sync.
*/
pluginVector_t _plugins;
boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
......
......@@ -95,16 +95,17 @@ void printSyntax()
cout << endl;
cout << "Options:" << endl;
cout << " -b<host> MQTT broker" << endl;
cout << " -p<port> MQTT broker port" << endl;
cout << " -m<string> MQTT topic prefix" << endl;
cout << " -t<number> Thread count" << endl;
cout << " -w<path> Writable directory for temporal files" << endl;
cout << endl;
cout << " -d Daemonize" << endl;
cout << " -v Set verbosity of output." << endl
cout << " -b <host> MQTT broker" << endl;
cout << " -p <port> MQTT broker port" << endl;
cout << " -m <string> MQTT topic prefix" << endl;
cout << " -t <number> Thread count" << endl;
cout << " -w <path> Writable directory for temporal files" << endl;
cout << " -c <sec> Caching interval of the sensors in [s]" << endl;
cout << " -v <level> Set verbosity of output." << endl
<< " Can be a number between 5 (all output) and 0 (only fatal messages)." << endl
<< " Default level is 3 (info)" << endl;
cout << endl;
cout << " -d Daemonize" << endl;
cout << " -h This help page" << endl;
cout << endl;
}
......@@ -152,7 +153,7 @@ int main(int argc, char** argv) {
}
//define allowed command-line options once
const char opts[] = "b:p:m:t:v:w:dh";
const char opts[] = "b:p:m:t:v:w:c:dh";
//check if help flag specified
char c;
......@@ -230,6 +231,9 @@ int main(int argc, char** argv) {
pluginSettings.tempdir.append("/");
}
break;
case 'c':
pluginSettings.cacheInterval = stoul(optarg) * 1000;
break;
case 'h':
printSyntax();
return 1;
......@@ -269,7 +273,7 @@ int main(int argc, char** argv) {
LOG(info) << "Logging setup complete";
//Read in rest of configuration. Also creates all sensors
if(!_configuration->read()) {
if(!_configuration->readPlugins()) {
LOG(fatal) << "Failed to read configuration!";
return 1;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment