This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmqtt.cpp
162 lines (131 loc) · 4.23 KB
/
mqtt.cpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// SPDX-FileCopyrightText: 2020 Kevin P. Fleming <[email protected]>
// SPDX-License-Identifier: Apache-2.0
// Copyright 2020 Kevin P. Fleming
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "mqtt.hpp"
#include "config.hpp"
#include <vector>
#include <algorithm>
#include <cstring>
namespace {
struct subscription {
subscription(const char* topic, Garaduino::MQTT::subscriptionHandler&& handler) : topic(topic), handler(std::move(handler)) {};
const char* topic;
Garaduino::MQTT::subscriptionHandler handler;
};
std::vector<subscription> subscriptions{};
void callback(char* topic, byte* payload, unsigned int length) {
String safeTopic{topic};
payload[length] = '\0';
String safePayload{(char *)payload};
DEBUG_PRINT(F("MQTT: topic: "));
DEBUG_PRINT(safeTopic);
DEBUG_PRINT(F(" payload: "));
DEBUG_PRINTLN(safePayload);
for (auto& subscription: subscriptions) {
if (safeTopic.equals(subscription.topic)) {
subscription.handler(safePayload);
}
}
}
};
namespace Garaduino {
void MQTT::start(TimerSet& timers, Web& web) {
DEBUG_PRINT(F("MQTT: initializing..."));
subscriptions.reserve(8);
mqtt.setServer(MQTT_BROKER_NAME, MQTT_BROKER_PORT);
mqtt.setCallback(callback);
if (connect()) {
DEBUG_PRINTLN(F(" done"));
} else {
DEBUG_PRINTLN(F(" failed"));
}
web.addStatusItems("mqtt", statusItems);
timers.every(MQTT_POLL_SECS * 1000, [this]{ return maintain(); });
}
bool MQTT::connect() {
if (mqtt.connect(MQTT_CLIENT_NAME, MQTT_STATUS_TOPIC, 0, true, "offline")) {
mqtt.publish(MQTT_STATUS_TOPIC, "online", true);
for (auto& message: queue) {
if (message.topic != nullptr) {
mqtt.publish(message.topic, message.payload, message.retain);
message.topic = nullptr;
}
}
for (auto& subscription: subscriptions) {
if (subscription.topic != nullptr) {
mqtt.subscribe(subscription.topic);
}
}
lastStateString = "connected";
return true;
} else {
lastStateString = "not connected";
return false;
}
}
Timers::HandlerResult MQTT::maintain() {
mqtt.loop();
if (!mqtt.connected()) {
DEBUG_PRINT(F("MQTT: connecting..."));
if (connect()) {
DEBUG_PRINTLN(F(" done"));
} else {
DEBUG_PRINTLN(F(" failed"));
// try again on the next cycle
}
}
return Timers::TimerStatus::repeat;
}
MQTT::queuedMessageArray::iterator MQTT::findSlotForTopic(const char* topic) {
if (auto result = std::find_if(queue.begin(), queue.end(), [topic](queuedMessage& m) { return m.topic == topic; }); result != queue.end()) {
return result;
}
return std::find_if(queue.begin(), queue.end(), [](queuedMessage& m) { return m.topic == nullptr; });
}
bool MQTT::queueMessage(const char* topic, const char* payload, bool retain) {
if (auto slot = findSlotForTopic(topic); slot != queue.end()) {
slot->topic = topic;
slot->payload = payload;
slot->retain = retain;
return true;
} else {
return false;
}
}
bool MQTT::publish(const char* topic, const char* payload) {
if (mqtt.connected() && mqtt.publish(topic, payload, false)) {
return true;
} else {
return queueMessage(topic, payload, false);
}
}
bool MQTT::publish(const char* topic, const String& payload) {
return publish(topic, payload.c_str());
}
bool MQTT::publishAndRetain(const char* topic, const char* payload) {
if (mqtt.connected() && mqtt.publish(topic, payload, true)) {
return true;
} else {
return queueMessage(topic, payload, true);
}
}
bool MQTT::publishAndRetain(const char* topic, const String& payload) {
return publishAndRetain(topic, payload.c_str());
}
void MQTT::subscribe(const char* topic, subscriptionHandler&& handler) {
subscriptions.emplace_back(topic, std::move(handler));
mqtt.subscribe(topic);
}
};