-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMQTTManager.cpp
157 lines (114 loc) · 4.79 KB
/
MQTTManager.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
#include "MQTTManager.h"
#include <WiFi.h>
#include "HABridgeConnectionState.h"
#include "HAPrincetonButton.h"
#include "HAInfactoryHumidity.h"
#include "HAInfactoryTemperature.h"
#include "PrincetonKeys.h"
constexpr auto LOG_TAG = LOG_TAG_MQTT;
MQTTManagerClass::MQTTManagerClass() {
this->mqttClient.setKeepAlive(MQTT_KEEP_ALIVE);
this->haDevices.push_back(habridgeConnectionState);
this->haDevices.push_back(haInfactoryTemperature);
this->haDevices.push_back(haInfactoryHumidity);
this->haDevices.push_back(haPrincetonButton);
}
void MQTTManagerClass::connect(const String &host, const uint16_t &port, const String &username, const String &password) {
this->clientId = Utils::getMQTTRoot();
this->port = port;
this->username = username;
this->password = password;
if (!this->host.fromString(host)) {
LOGD(LOG_TAG, "Invalid host! Stuck in a loop, use reset button to reboot.");
while (true);
}
this->connect();
}
void MQTTManagerClass::connect() {
this->connected = false;
if (!WiFi.isConnected()) {
return;
}
this->mqttClient.begin(this->host, this->port, this->wifiClient);
this->mqttClient.setWill(Utils::buildMQTTRootTopic("bridge/state").c_str(), R"({"state": "offline"})", true, 0);
this->mqttClient.onMessage([this](const String &topic, const String &payload) {
this->onMessage(topic, payload);
});
uint8_t retries = 0;
while (!this->mqttClient.connected()) {
++retries;
LOGD(LOG_TAG, "Connection attempt #%d to %s:%d as \"%s\"...", retries, this->host.toString().c_str(), this->port, this->clientId.c_str());
if (this->mqttClient.connect(this->clientId.c_str(), this->username.c_str(), this->password.c_str())) {
this->onConnected();
return;
}
LOGD(LOG_TAG, "Failed to connect! Connect return code: %d", this->mqttClient.lastError());
if (retries == MQTT_MAX_CONNECTION_ATTEMPTS_BEFORE_REBOOT) {
LOGD(LOG_TAG, "Reaching maximum connection attempts! Rebooting in second...");
delay(1000);
ESP.restart();
}
LOGD(LOG_TAG, "Retrying in %d milliseconds...", MQTT_CONNECTION_ATTEMPT_INTERVAL);
delay(MQTT_CONNECTION_ATTEMPT_INTERVAL);
}
}
void MQTTManagerClass::loop() {
if (!this->mqttClient.connected()) {
this->connect();
} else {
this->mqttClient.loop();
}
}
void MQTTManagerClass::onConnected() {
if (this->connected) {
return;
}
this->connected = true;
LOGD(LOG_TAG, "Successfully connected!");
for (const auto &haDevice: this->haDevices) {
const auto haTopic = haDevice.getTopic();
if (haTopic.length() <= 0) {
continue;
}
this->mqttClient.publish(haDevice.getTopic().c_str(), haDevice.getPayload().c_str(), true, 0);
LOGD(LOG_TAG, "Published HA device to \"%s\"", haDevice.getTopic().c_str());
}
this->mqttClient.publish(Utils::buildMQTTRootTopic("bridge/state"), R"({"state": "online"})", true, 0);
LOGD(LOG_TAG, "Published bridge online state");
this->mqttClient.subscribe(Utils::buildMQTTRootTopic("bridge/reboot"));
this->mqttClient.subscribe(Utils::buildMQTTRootTopic("princeton_button/+"));
}
void MQTTManagerClass::onMessage(const String &topic, const String &payload) {
LOGD(LOG_TAG, "Received message on \"%s\": %s", topic.c_str(), payload.c_str());
if (topic == Utils::buildMQTTRootTopic("princeton_button/add")) {
onPrincetonButtonAdd(payload);
} else if (topic == Utils::buildMQTTRootTopic("princeton_button/remove")) {
onPrincetonButtonRemove(payload);
} else if (topic == Utils::buildMQTTRootTopic("bridge/reboot")) {
LOGD(LOG_TAG, "Rebooting in second...");
delay(1000);
ESP.restart();
}
}
bool MQTTManagerClass::publish(const String &topic, const String &payload, const bool &retained, const uint8_t &qos) {
LOGD(LOG_TAG, "Publishing to \"%s\": %s", topic.c_str(), payload.c_str());
return this->mqttClient.publish(topic.c_str(), payload.c_str(), retained, qos);
}
bool MQTTManagerClass::publishRoot(const String &topic, const String &payload, const bool &retained, const uint8_t &qos) {
return this->publish(Utils::buildMQTTRootTopic(topic).c_str(), payload.c_str(), retained, qos);
}
void MQTTManagerClass::onPrincetonButtonAdd(const String &payload) {
const uint32_t code = strtoul(payload.c_str(), nullptr, 16);
if (code == 0 || code == ULONG_MAX) {
return;
}
PrincetonKeys.addKey(code);
}
void MQTTManagerClass::onPrincetonButtonRemove(const String &payload) {
const uint32_t code = strtoul(payload.c_str(), nullptr, 16);
if (code == 0 || code == ULONG_MAX) {
return;
}
PrincetonKeys.removeKey(code);
}
MQTTManagerClass MQTTManager;