SOIL C++
C++ Unified Device Interface
Configuration.cpp
Go to the documentation of this file.
1#include "Configuration.h"
2#include <nlohmann/json.hpp>
3#include <fstream>
4
5
7
9{
10 std::string host = "127.0.0.1";
11 port = 1883;
12 username = "guest";
13 password = "guest";
14 clean_session = true;
15 root = "";
16 keep_alive = 30;
17 min_delay_ms = 0;
18 int connection_timeout_s = 30;
19 bool ssl = false;
20 bool verify = true;
21 bool websocket = false;
22 std::string path = "";
23 std::string certificate_authority = "";
25}
26
28{
29 std::ifstream infile(filename);
30 json j;
31 infile >> j;
32 host = j.value("host", "127.0.0.1");
33 port = j.value("port", 1883);
34 username = j.value("username", "guest");
35 password = j.value("password", "guest");
36 clean_session = j.value("clean", true);
37 root = j.value("root", "");
38 keep_alive = j.value("keep_alive", 30);
39 min_delay_ms = j.value("min_delay", 0);
40 connection_timeout_s = j.value("connection_timeout", 30);
41 ssl = j.value("ssl", false);
42 verify = j.value("verify", true);
43 websocket = j.value("websocket", false);
44 path = j.value("path", "");
45 certificate_authority = j.value("certificate_authority", "");
46
47}
48
50{
51}
52
54{
55 std::string prefix;
56 if (websocket)
57 {
58 if (ssl)
59 {
60 prefix = "wss";
61 }
62 else
63 {
64 prefix = "ws";
65 }
66 if (path.size() > 0)
67 {
68 if (path.at(0) != '/')
69 {
70 path = "/" + path;
71 }
72 }
73 }
74 else
75 {
76 if (ssl)
77 {
78 prefix = "ssl";
79 }
80 else
81 {
82 prefix = "tcp";
83 }
84 path = "";
85 }
86
87 return prefix + "://" + host + ":" + std::to_string(port) + path;
88
89}
90
nlohmann::json json
Configuration()
Default constructor.
std::string root
MQTT root topic.
Definition: Configuration.h:64
std::string host
Hostname of the MQTT broker.
Definition: Configuration.h:22
int min_delay_ms
Minimum delay between to messages in milliseconds.
Definition: Configuration.h:81
int keep_alive
Keep alive interval in seconds.
Definition: Configuration.h:72
bool ssl
Use secured connection.
Definition: Configuration.h:99
bool clean_session
Clean session flag.
Definition: Configuration.h:55
int port
Port of the MQTT broker.
Definition: Configuration.h:30
std::string password
Password for connecting to the MQTT broker.
Definition: Configuration.h:47
std::string certificate_authority
Path to CA PEM-file.
bool verify
Skip SSL verification.
bool websocket
Use websocket protocol.
std::string uri()
URI Builder.
int connection_timeout_s
Connection timeout in seconds.
Definition: Configuration.h:90
std::string path
Websocket path.
std::string username
Username for connecting to the MQTT broker.
Definition: Configuration.h:39
~Configuration()
Destructor.