This repository was archived by the owner on Jul 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMqttPublisher_Paho.cpp
More file actions
143 lines (119 loc) · 4.3 KB
/
Copy pathMqttPublisher_Paho.cpp
File metadata and controls
143 lines (119 loc) · 4.3 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2019-2023 (c) Christian von Arnim, ISW University of Stuttgart (for umati and VDW e.V.)
* Copyright 2020 (c) Dominik Basner, Sotec GmbH (for VDW e.V.)
* Copyright 2023 (c) Marc Fischer, ISW University of Stuttgart (for umati and VDW e.V.)
*/
#include "MqttPublisher_Paho.hpp"
#include <easylogging++.h>
#include <sstream>
namespace Umati {
namespace MqttPublisher_Paho {
MqttPublisher_Paho::MqttPublisher_Paho(
const std::string &protocol,
const std::string &host,
std::uint16_t port,
const std::string &CaCertPath,
const std::string &CaTrustStorePath,
const std::string &onlineTopic,
const std::string &versionTopic,
const std::string &gitClientVersion,
const std::string &username,
const std::string &password,
const std::string &httpProxy,
const std::string &httpsProxy)
: m_cli(getUri(protocol, host, port), getClientId(), 0, nullptr),
m_callbacks(this),
m_onlineTopic(onlineTopic),
m_versionTopic(versionTopic),
m_gitClientVersion(gitClientVersion) {
m_cli.set_callback(m_callbacks);
mqtt::connect_options opts_conn = getOptions(username, password);
if (httpsProxy.length() != 0) {
opts_conn.set_https_proxy(httpsProxy);
}
if (httpProxy.length() != 0) {
opts_conn.set_http_proxy(httpProxy);
}
if (protocol == "wss") {
mqtt::ssl_options ssl_opts;
ssl_opts.ca_path(CaCertPath);
ssl_opts.set_trust_store(CaTrustStorePath);
ssl_opts.set_verify(false);
opts_conn.set_ssl(ssl_opts);
}
mqtt::will_options opts_will = getLastWill();
opts_conn.set_will(opts_will);
try {
LOG(INFO) << "Connect to " << host;
m_cli.connect(opts_conn)->wait();
} catch (const mqtt::exception &ex) {
LOG(ERROR) << "Paho Exception:" << ex.what();
}
}
std::string MqttPublisher_Paho::getUri(std::string protocol, std::string host, std::uint16_t port) {
std::stringstream ss;
ss << protocol << "://" << host << ":" << port << "/ws";
return ss.str();
}
mqtt::connect_options MqttPublisher_Paho::getOptions(const std::string &username, const std::string &password) {
mqtt::connect_options opts_conn;
opts_conn.set_keep_alive_interval(std::chrono::seconds(10));
opts_conn.set_clean_session(true);
opts_conn.set_automatic_reconnect(2, 10);
if (!username.empty()) {
opts_conn.set_user_name(username);
}
if (!password.empty()) {
opts_conn.set_password(password);
}
return opts_conn;
}
mqtt::will_options MqttPublisher_Paho::getLastWill() const {
mqtt::will_options opts_will;
opts_will.set_topic(m_onlineTopic);
opts_will.set_payload(std::string("0"));
opts_will.set_retained(true);
return opts_will;
}
void MqttPublisher_Paho::Publish(std::string channel, std::string message) {
try {
m_cli.publish(channel, message, 0, true);
} catch (const mqtt::exception &ex) {
LOG(ERROR) << "Paho Exception:" << ex.what();
}
}
std::string MqttPublisher_Paho::getClientId() {
std::stringstream ss;
ss << "Dashboard Paho Client ";
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis('a', 'z');
for (int i = 0; i < 12; ++i) {
ss << static_cast<char>(dis(gen));
}
return ss.str();
}
MqttPublisher_Paho::~MqttPublisher_Paho() {
try {
auto tokenPtr = m_cli.publish(m_onlineTopic, std::string("0"), 0, true);
tokenPtr->wait_for(1000);
if (!tokenPtr->is_complete()) {
LOG(ERROR) << "Could not send client offline state";
}
m_cli.disconnect();
} catch (const mqtt::exception &ex) {
LOG(ERROR) << "Paho Exception:" << ex.what();
}
}
MqttPublisher_Paho::MqttCallbacks::MqttCallbacks(MqttPublisher_Paho *mqttPublisher_paho) : m_mqttPublisher_paho(mqttPublisher_paho) {}
void MqttPublisher_Paho::MqttCallbacks::connected(const std::string &cause) {
LOG(INFO) << "Mqtt Connected: " << cause;
m_mqttPublisher_paho->Publish(m_mqttPublisher_paho->m_onlineTopic, "1");
m_mqttPublisher_paho->Publish(m_mqttPublisher_paho->m_versionTopic, m_mqttPublisher_paho->m_gitClientVersion);
}
void MqttPublisher_Paho::MqttCallbacks::connection_lost(const std::string &cause) { LOG(ERROR) << "Connection lost: " << cause; }
} // namespace MqttPublisher_Paho
} // namespace Umati