-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bricks.Events.cpp
95 lines (80 loc) · 2.66 KB
/
Bricks.Events.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
#include "Bricks.Events.h"
namespace Bricks {
void Events::init() {
mqtt.setServer(BRICKS_MQTT_HOST, 1883);
mqtt.setCallback(Events::onEvent);
connectWiFi();
connectMQTT();
}
void Events::loop() {
if (mqtt.connected()) {
mqtt.loop();
}
else {
Log.warning("MQTT: disconnected" CR);
connectMQTT();
}
}
void Events::publish(const uint8_t *macAddr, Message message) {
publish(macAddr, message.key, message.value);
}
void Events::publish(const uint8_t *macAddr, const char *key, const char *value) {
char macStr[MAC_STR_SIZE];
Bricks::Utils::macToStr(macAddr, macStr);
char topic[MAX_TOPIC_SIZE];
sprintf(topic, BRICKS_MESSAGES_IN "/%s/%s", macStr, key);
publish(topic, value);
}
void Events::publish(const char *topic, const char *value) {
if (mqtt.connected()) {
Log.trace("MQTT: -> %s: %s" CR, topic, value);
mqtt.publish(topic, value);
}
else {
Log.error("MQTT: Publishing failed [disconnected]" CR);
}
}
void Events::onEvent(char *topic, byte *bytes, unsigned int length) {
bytes[length] = '\0';
char *value = (char *) bytes;
Log.trace("MQTT: <- %s: %s" CR, topic, value);
uint8_t macAddr[MAC_ADDR_SIZE];
char key[KEY_SIZE];
parseTopic(topic, macAddr, key);
gOutbox.send(macAddr, key, value);
}
void Events::parseTopic(const char *topic, uint8_t *macAddr, char *key) {
sscanf(topic, BRICKS_MESSAGES_OUT "/" MAC_FORMAT "/%s",
&macAddr[0], &macAddr[1], &macAddr[2], &macAddr[3], &macAddr[4], &macAddr[5], key);
}
void Events::connectWiFi() {
Log.notice("WIFI: Connecting to [%s]" CR, BRICKS_WIFI_SSID);
WiFi.begin(BRICKS_WIFI_SSID, BRICKS_WIFI_PASSWORD);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Log.trace("WIFI: Still connecting..." CR);
}
Log.notice("WIFI: Connected [%s] [Channel %d]" CR, WiFi.localIP().toString().c_str(), WiFi.channel());
if(WiFi.channel() != BRICKS_WIFI_CHANNEL) {
Log.error("WIFI: Bricks currently requires WiFi on channel %d", BRICKS_WIFI_CHANNEL);
}
}
void Events::connectMQTT() {
Log.notice("MQTT: Connecting to [%s]" CR, BRICKS_MQTT_HOST);
while(!mqtt.connected()) {
if(mqtt.connect(BRICKS_MQTT_CLIENT, BRICKS_MQTT_USER, BRICKS_MQTT_PASSWORD)) {
Log.notice("MQTT: Connected" CR);
subscribe(BRICKS_MESSAGES_OUT "/#");
}
else {
Log.warning("MQTT: Failed [%s]. Retrying in 5 secs" CR, mqtt.state());
delay(5000);
}
}
}
void Events::subscribe(const char *topic) {
mqtt.subscribe(topic);
Log.trace("MQTT: Subscribed [%s]" CR, topic);
}
Events gEvents = Events();
}