-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoutes.cpp
342 lines (319 loc) · 9.45 KB
/
Routes.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "Routes.h"
#include <cstdint>
#include <Arduino.h>
#include <ESP.h>
#include <ESP8266WiFi.h>
#include "Files.h"
#include "Connectivity.h"
#include "Logging.h"
#include "Config.h"
Routes::Routes(ESP8266WebServer* webServer) {
server = webServer;
}
bool Routes::shouldRestart = false;
void Routes::handleRoot() {
server->keepAlive(false);
server->send(
200,
MIME_HTML,
F(
"<!doctype html><html>" HTML_HEAD "<body>"
"<h1>Settings</h1>"
"<p>Welcome to your ESP8266! What do you want to do?</p>"
"<nav><ul>"
"<li><a href='/wifi'>Configure WiFi</a></li>"
"<li><a href='/room-name'>Change Room Name</a></li>"
"<li><a href='/weather'>Toggle weather display</a></li>"
"<li><a href='/status'>View the device's status</a></li>"
"</ul></nav>"
"</body></html>"
)
);
}
void Routes::handleWiFi() {
char* ssid = readFromFile("ssid");
String page;
page += F(
"<!doctype html><html>" HTML_HEAD "<body>"
"<h1>WiFi Configuration</h1>"
"<h2>Available Networks</h2>"
"<ul id='list'><li>Loading</li></ul>"
"<h2>Connect to a Network</h2>"
"<form method='POST' action='wifi-save'>"
"<input type='text' placeholder='SSID' name='ssid' value='"
);
page += ssid;
page += F(
"' required />"
"<input type='password' placeholder='Password' name='password' required />"
"<input type='submit' value='Connect' />"
"</form>"
"<p>You may want to <a href='/'>return to the home page</a>.</p>"
"<script src='/wifi-script' defer></script>"
"</body></html>"
);
server->sendHeader(F("Cache-Control"), F("no-cache, no-store, must-revalidate"));
server->sendHeader(F("Expires"), F("0"));
server->keepAlive(false);
server->send(200, MIME_HTML, page);
free(ssid);
}
void Routes::handleWiFiScript() {
server->keepAlive(false);
server->send(
200,
F("text/javascript"),
F(
"const list = document.getElementById('list');"
"loadNetworks();"
"function loadNetworks() {"
"fetch('/wifi-result').then(response => {"
"if (!response.ok) console.error(response.status);"
"else return response.json();"
"}).then(json => {"
"list.innerHTML = '';"
"json.forEach(element => appendItem(element));"
"}).catch(error => {"
"console.error(error);"
"list.innerHTML = '';"
"appendItem('An error occurred');"
"});"
"}"
"function appendItem(text) {"
"const li = document.createElement('li');"
"li.appendChild(document.createTextNode(text));"
"list.appendChild(li);"
"}"
)
);
}
void Routes::handleWiFiResult() {
String page;
page += F("[");
uint8_t n = WiFi.scanNetworks();
if (n > 0) {
for (uint8_t i = 0; i < n; i++) {
page += '"';
page += WiFi.SSID(i);
page += '"';
if (i != n - 1) page += ',';
}
} else {
page += F("\"No networks found\"");
}
page += F("]");
server->keepAlive(false);
server->send(200, F("application/json"), page);
}
void Routes::handleWiFiSave() {
Routes::shouldRestart = true;
char ssid[32] = "";
char password[32] = "";
server->arg("ssid").toCharArray(ssid, sizeof(ssid) - 1);
server->arg("password").toCharArray(password, sizeof(password) - 1);
server->keepAlive(false);
server->send(
200,
MIME_HTML,
F(
"<!doctype html><html>" HTML_HEAD "<body>"
"<h1>Success</h1>"
"<p>Updated WiFi settings successfully! Your device will restart now!</p>"
"<script src='/request-restart' defer></script>"
"</body></html>"
)
);
writeToFile("ssid", ssid);
writeToFile("password", password);
log("Changed wifi config");
}
void Routes::handleRoomName() {
char* roomName = readFromFile("room_name");
String page;
page += F(
"<!doctype html><html>" HTML_HEAD "<body>"
"<h1>Room Name</h1>"
"<p>The current room name is “"
);
page += SAVED_OR_DEFAULT_ROOM_NAME(roomName);
page += F(
"”.</p>"
"<h2>Change Room Name</h2>"
"<form method='POST' action='room-name-save'>"
"<input type='text' placeholder='Room name' name='name' value='"
);
page += roomName;
page += F(
"' />"
"<input type='submit' value='Change' />"
"</form>"
"<p>You may want to <a href='/'>return to the home page</a>.</p>"
"</body></html>"
);
server->sendHeader(F("Cache-Control"), F( "no-cache, no-store, must-revalidate"));
server->sendHeader(F("Expires"), F("0"));
server->keepAlive(false);
server->send(200, MIME_HTML, page);
free(roomName);
}
void Routes::handleRoomNameSave() {
char roomName[32] = "";
server->arg("name").toCharArray(roomName, sizeof(roomName) - 1);
server->keepAlive(false);
server->send(
200,
MIME_HTML,
F(
"<!doctype html><html>" HTML_HEAD "<body>"
"<h1>Success</h1>"
"<p>Updated the room name successfully!</p>"
"<p>You may want to <a href='/'>return to the home page</a>.</p>"
"</body></html>"
)
);
writeToFile("room_name", roomName);
log("Changed room name");
}
void Routes::handleWeather() {
char* weatherDisplay = readFromFile("weather");
String page;
page += F(
"<!doctype html><html>" HTML_HEAD "<body>"
"<h1>Weather Display</h1>"
"<p>Currently the weather display is "
);
page += strcmp(weatherDisplay, "1") == 0 ? "enabled" : "disabled";
page += F(
". Please note that weather data is fetched from the Internet. "
"Data that can be regarded personal will get transmitted to the weather provider.</p>"
"<h2>Toggle Weather Display</h2>"
"<form method='POST' action='weather-save'>"
"<input type='hidden' name='bool' value='"
);
page += strcmp(weatherDisplay, "1") == 0 ? "0" : "1";
page += F(
"' />"
"<input type='submit' value='Toggle' />"
"</form>"
"<p>You may want to <a href='/'>return to the home page</a>.</p>"
"</body></html>"
);
server->sendHeader(F("Cache-Control"), F( "no-cache, no-store, must-revalidate"));
server->sendHeader(F("Expires"), F("0"));
server->keepAlive(false);
server->send(200, MIME_HTML, page);
free(weatherDisplay);
}
void Routes::handleWeatherSave() {
char weatherDisplay[32] = "";
server->arg("bool").toCharArray(weatherDisplay, sizeof(weatherDisplay) - 1);
server->keepAlive(false);
server->send(
200,
MIME_HTML,
F(
"<!doctype html><html>" HTML_HEAD "<body>"
"<h1>Success</h1>"
"<p>Updated the weather display successfully!</p>"
"<p>You may want to <a href='/'>return to the home page</a>.</p>"
"</body></html>"
)
);
writeToFile("weather", weatherDisplay);
log("Changed weather display");
}
void Routes::handleRequestRestart() {
server->keepAlive(false);
server->send(200, F("text/javascript"), F("console.log('Restarting');"));
if (Routes::shouldRestart) {
delay(2000);
ESP.restart();
}
}
void Routes::handleStatus() {
char* uptime = (char*) malloc(sizeof(char) * 16);
uint32_t seconds = millis() / 1000;
uint32_t minutes = seconds / 60;
uint16_t hours = minutes / 60;
sprintf_P(uptime, PSTR("%02u:%02u:%02u"), hours, minutes % 60, seconds % 60);
String page;
page += F(
"<!doctype html><html>" HTML_HEAD "<body>"
"<h1>Status</h1>"
"<ul>"
"<li>WiFi: "
);
page += WiFi.status() == WL_CONNECTED ? WiFi.SSID() : "Disconnected";
page += F(
"</li>"
"<li>Signal Strength: "
);
page += RSSIToPercent(WiFi.RSSI());
page += F(
" %</li>"
"<li>RAM Usage: "
);
page += (ESP.getFreeHeap() * 100) / 64000 * (-1) + 100;
page += F(
" %</li>"
"<li>RAM Fragmentation: "
);
page += ESP.getHeapFragmentation();
page += F(
" %</li>"
"<li>Uptime: "
);
page += uptime;
page += F(
"</li>"
#ifdef LOGGING
"<li>LOGGING IS ENABLED</li>"
#endif
"</ul>"
"<p>You may want to <a href='/'>return to the home page</a>.</p>"
"</body></html>"
);
server->sendHeader(F("Cache-Control"), F("no-cache, no-store, must-revalidate"));
server->sendHeader(F("Expires"), F("0"));
server->keepAlive(false);
server->send(200, MIME_HTML, page);
free(uptime);
}
void Routes::handleCommand() {
server->keepAlive(false);
server->send(
200,
F("application/json"),
F(
"{\"toast\":\"Online!\"}"
)
);
}
void Routes::handleCss() {
server->keepAlive(false);
server->send(
200,
F("text/css"),
F(
":root { font-family: sans-serif; line-height: 1.5; }"
"* { box-sizing: border-box; font-weight: normal; }"
"body { max-width: 480px; margin: auto; padding: 16px; }"
"p, li { color: rgba(0, 0, 0, .6); }"
"input { display: block; width: 100%; margin: 8px 0; }"
)
);
}
void Routes::handleNotFound() {
server->keepAlive(false);
server->send(
404,
MIME_HTML,
F(
"<!doctype html><html>" HTML_HEAD "<body>"
"<h1>404</h1>"
"<p>Not found!</p>"
"<p>You may want to <a href='/'>return to the home page</a>.</p>"
"</body></html>"
)
);
}