-
Notifications
You must be signed in to change notification settings - Fork 0
/
MqttClient.cc
120 lines (104 loc) · 3.08 KB
/
MqttClient.cc
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
#include "MqttClient.hh"
#include "Log.hh"
#include <cstring>
using namespace dooragent;
using namespace std;
using namespace std::chrono;
MqttClient::MqttClient()
{
}
MqttClient::~MqttClient()
{
disconnect();
Poll();
}
bool MqttClient::Connect(std::string host, int port)
{
Log::Message("MQTT: connecting to " + host);
int result = connect(host.c_str(), port, 10);
if (result == MOSQ_ERR_SUCCESS)
{
for (auto& sub: subscriptions)
{
auto& topic = sub.first;
Log::Trace("MQTT: (re)subscribing to " + topic);
subscribe(nullptr, topic.c_str(), 1);
}
}
else if (result == MOSQ_ERR_INVAL)
Log::Error("MQTT: invalid connection parameters");
else if (result == MOSQ_ERR_ERRNO)
Log::Error("MQTT: system error: " + string{strerror(errno)});
return (result == MOSQ_ERR_SUCCESS);
}
bool MqttClient::Poll()
{
Log::Trace("MQTT: polling");
int ret = loop_read();
if (ret == MOSQ_ERR_CONN_LOST || ret == MOSQ_ERR_NO_CONN)
{
Log::Error("MQTT: connection lost!");
return false;
}
if (want_write())
loop_write();
loop_misc();
return true;
}
int MqttClient::GetSocket()
{
return socket();
}
void MqttClient::SubscribeTopic(std::string topic)
{
subscribe(nullptr, topic.c_str(), 1);
subscriptions[topic].topic = topic;
Log::Message("MQTT: subscribed to " + topic);
}
void MqttClient::SubscribeTopic(std::string topic, topic_handler handler)
{
subscribe(nullptr, topic.c_str(), 1);
subscriptions[topic].topic = topic;
subscriptions[topic].handler = handler;
Log::Message("MQTT: subscribed to " + topic + " with handler function");
}
void MqttClient::PublishTopic(std::string topic, std::string payload, bool retain)
{
publish(nullptr, topic.c_str(), payload.size(), payload.c_str(), 1, retain);
Log::Trace("MQTT: published " + topic + "=" + payload + (retain ? "[r]" : ""));
}
std::optional<std::string> MqttClient::GetTopicValue(std::string topic) const
{
auto sub_iter = subscriptions.find(topic);
if (sub_iter != subscriptions.end())
{
auto& sub = sub_iter->second;
auto age = steady_clock::now() - sub.received;
if (age < 180s)
return sub.value;
else
Log::Warning("MQTT: not returning stale data for " + topic);
}
return nullopt;
}
void MqttClient::on_message(const struct mosquitto_message *message)
{
if (message->topic != nullptr && message->payload != nullptr)
{
std::string topic{message->topic};
std::string payload{(const char*)message->payload, (size_t)message->payloadlen};
Log::Trace("MQTT: message: " + topic + "=" + payload);
auto sub_iter = subscriptions.find(topic);
if (sub_iter != subscriptions.end())
{
auto& sub = sub_iter->second;
sub.value = payload;
sub.received = steady_clock::now();
if (sub.handler)
{
Log::Trace("MQTT: Calling handler");
sub.handler(topic, payload);
}
}
}
}