forked from MTJoker/HomeStatusDisplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeStatusDisplay.cpp
228 lines (184 loc) · 5.33 KB
/
HomeStatusDisplay.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "HomeStatusDisplay.hpp"
// function declarations
void handleMqttMessage(String topic, String msg);
#define WINDOW_STRING (F("/window/"))
#define DOOR_STRING (F("/door/"))
#define LIGHT_STRING (F("/light/"))
#define ALARM_STRING (F("/alarm/"))
#define ONE_MINUTE_MILLIS (60000)
int getFreeRamSize();
HomeStatusDisplay::HomeStatusDisplay()
:
m_webServer(m_config, m_leds, m_mqttHandler),
m_wifi(m_config),
m_mqttHandler(m_config, std::bind(&HomeStatusDisplay::mqttCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
m_leds(m_config),
m_lastWifiConnectionState(false),
m_lastMqttConnectionState(false),
m_oneMinuteTimerLast(0),
m_uptime(0)
{
}
void HomeStatusDisplay::begin(const char* version, const char* identifier)
{
// initialize serial
Serial.begin(115200);
Serial.println(F(""));
m_config.begin(version, identifier);
m_webServer.begin();
m_leds.begin();
m_wifi.begin();
m_mqttHandler.begin();
Serial.print(F("Free RAM: ")); Serial.println(ESP.getFreeHeap());
}
void HomeStatusDisplay::work()
{
unsigned long uptime = calcUptime();
checkConnections();
m_wifi.handleConnection();
m_webServer.handleClient(uptime);
if(m_wifi.connected())
{
m_mqttHandler.handle();
}
m_leds.update();
delay(100);
}
unsigned long HomeStatusDisplay::calcUptime()
{
unsigned long currentMillis = millis();
if(currentMillis - m_oneMinuteTimerLast >= ONE_MINUTE_MILLIS)
{
m_uptime++;
m_oneMinuteTimerLast = currentMillis;
Serial.println("Uptime: " + String(m_uptime) + "min");
}
return m_uptime;
}
void HomeStatusDisplay::mqttCallback(char* topic, byte* payload, unsigned int length)
{
int i = 0;
for(i = 0; (i < length) && (i < MQTT_MSG_MAX_LEN); i++)
{
mqttMsgBuffer[i] = payload[i];
}
mqttMsgBuffer[i] = '\0';
String mqttTopicString(topic);
String mqttMsgString = String(mqttMsgBuffer);
Serial.print(F("Received an MQTT message for topic ")); Serial.println(mqttTopicString + ": " + mqttMsgString);
if(mqttTopicString.equals(m_config.getMqttTestTopic()))
{
handleTest(mqttMsgString);
}
else if(isStatusTopic(mqttTopicString))
{
HSDConfig::deviceType type = getDeviceType(mqttTopicString);
String device = getDevice(mqttTopicString);
handleStatus(device, type, mqttMsgString);
}
}
bool HomeStatusDisplay::isStatusTopic(String& topic)
{
String mqttStatusTopic = String(m_config.getMqttStatusTopic());
int posOfLastSlashInStatusTopic = mqttStatusTopic.lastIndexOf("/");
return topic.startsWith(mqttStatusTopic.substring(0, posOfLastSlashInStatusTopic)) ? true : false;
}
HSDConfig::deviceType HomeStatusDisplay::getDeviceType(String& statusTopic)
{
HSDConfig::deviceType type = HSDConfig::TYPE_UNKNOWN;
if(statusTopic.indexOf(LIGHT_STRING) != -1)
{
type = HSDConfig::TYPE_LIGHT;
}
else if(statusTopic.indexOf(WINDOW_STRING) != -1)
{
type = HSDConfig::TYPE_WINDOW;
}
else if(statusTopic.indexOf(DOOR_STRING) != -1)
{
type = HSDConfig::TYPE_DOOR;
}
else if(statusTopic.indexOf(ALARM_STRING) != -1)
{
type = HSDConfig::TYPE_ALARM;
}
return type;
}
String HomeStatusDisplay::getDevice(String& statusTopic)
{
int posOfLastSlashInStatusTopic = statusTopic.lastIndexOf("/");
return statusTopic.substring(posOfLastSlashInStatusTopic + 1);
}
void HomeStatusDisplay::handleTest(String msg)
{
int type = msg.toInt();
if(type > 0)
{
Serial.print(F("Showing testpattern ")); Serial.println(type);
m_leds.test(type);
}
else if(type == 0)
{
m_leds.clear();
m_mqttHandler.reconnect(); // back to normal
}
}
void HomeStatusDisplay::handleStatus(String device, HSDConfig::deviceType type, String msg)
{
int ledNumber = m_config.getLedNumber(device, type);
if(ledNumber != -1)
{
int colorMapIndex = m_config.getColorMapIndex(type, msg);
if(colorMapIndex != -1)
{
HSDConfig::Behavior behavior = m_config.getLedBehavior(colorMapIndex);
HSDConfig::Color color = m_config.getLedColor(colorMapIndex);
Serial.println("Set led number " + String(ledNumber) + " to behavior " + String(behavior) + " with color " + String(color, HEX));
m_leds.set(ledNumber, behavior, color);
}
else
{
Serial.println("Unknown message " + msg + " for led number " + String(ledNumber) + ", set to OFF");
m_leds.set(ledNumber, HSDConfig::OFF, HSDConfig::NONE);
}
}
else
{
Serial.println("No LED defined for device " + device + " of type " + String(type) + ", ignoring it");
}
}
void HomeStatusDisplay::checkConnections()
{
if(!m_lastMqttConnectionState && m_mqttHandler.connected())
{
m_leds.clear();
m_lastMqttConnectionState = true;
}
else if(m_lastMqttConnectionState && !m_mqttHandler.connected())
{
m_leds.clear();
m_lastMqttConnectionState = false;
}
if(!m_mqttHandler.connected() && m_wifi.connected())
{
m_leds.setAll(HSDConfig::ON, HSDConfig::YELLOW);
}
if(!m_lastWifiConnectionState && m_wifi.connected())
{
m_leds.clear();
if(!m_mqttHandler.connected())
{
m_leds.setAll(HSDConfig::ON, HSDConfig::YELLOW);
}
m_lastWifiConnectionState = true;
}
else if(m_lastWifiConnectionState && !m_wifi.connected())
{
m_leds.clear();
m_lastWifiConnectionState = false;
}
if(!m_wifi.connected())
{
m_leds.setAll(HSDConfig::ON, HSDConfig::RED);
}
}