-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.hpp
executable file
·109 lines (90 loc) · 3.27 KB
/
config.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
78
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
#pragma once
#include <iostream>
#include <list>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/program_options.hpp>
namespace pt = boost::property_tree;
namespace po = boost::program_options;
namespace dashbutton {
class config {
public:
static constexpr const char* ARG_HELP = "help";
static constexpr const char* ARG_PORT = "port";
static constexpr const char* ARG_DEV = "dev";
static constexpr const char* ARG_CONFIG = "config";
typedef struct {
std::string ip;
std::string topic;
std::string message;
} dashbutton_t;
typedef struct {
std::string host;
uint16_t port;
} mqtt_server_t;
typedef struct {
uint16_t port;
std::string deviceName;
} server_config_t;
public:
config(const std::string &file)
: fileName(file) {
try {
pt::ptree configTree;
pt::xml_parser::read_xml(fileName, configTree);
serverConfig.port = configTree.get<uint16_t>("dashbutton.server.port");
serverConfig.deviceName = configTree.get<std::string>("dashbutton.server.device");
mqttServer.host = configTree.get<std::string>("dashbutton.mqtt.host");
mqttServer.port = configTree.get<uint16_t>("dashbutton.mqtt.port");
for (const auto &entry : configTree.get_child("dashbutton.clients")) {
dashbutton_t btn;
const pt::ptree &client = entry.second;
btn.ip = client.get<std::string>("ip");
btn.topic = client.get<std::string>("topic");
btn.message = client.get<std::string>("message");
dashbuttonList.push_back(btn);
}
}
catch (pt::xml_parser::xml_parser_error& ex) {
std::cerr << ex.what() << std::endl;
}
}
~config() = default;
/**
* @brief buttonList
* @return
*/
const std::list<dashbutton_t>& buttonList() const {
return dashbuttonList;
}
/**
* @brief mqtt
* @return
*/
const mqtt_server_t& mqtt() const {
return mqttServer;
}
/**
* @brief server
* @return
*/
const server_config_t& server() const {
return serverConfig;
}
/**
* @brief override setting from XML file, with parameters given on command line
* @param vm command line arguments
*/
void overrideConfigByCommandLine(const po::variables_map &vm) {
if (vm.count(ARG_PORT))
serverConfig.port = vm[ARG_PORT].as<uint16_t>();
if (vm.count(ARG_DEV))
serverConfig.deviceName = vm[ARG_DEV].as<std::string>();
}
private:
std::string fileName;
mqtt_server_t mqttServer;
server_config_t serverConfig;
std::list<dashbutton_t> dashbuttonList;
};
} // namespace dashbutton