forked from jhoff/Split-Flap-Display
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitFlapWebServer.cpp
More file actions
525 lines (439 loc) · 18.3 KB
/
SplitFlapWebServer.cpp
File metadata and controls
525 lines (439 loc) · 18.3 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#include "SplitFlapWebServer.h"
#include <ArduinoJson.h>
#include <AsyncJson.h>
#define AP_SSID "Split Flap Display"
#ifndef WIFI_SSID
#define WIFI_SSID ""
#endif
#ifndef WIFI_PASS
#define WIFI_PASS ""
#endif
SplitFlapWebServer::SplitFlapWebServer(JsonSettings &settings)
: settings(settings), server(80), multiWordDelay(1000), rebootRequired(false), attemptReconnect(false),
multiWordCurrentIndex(0), numMultiWords(0), wifiCheckInterval(1000), connectionMode(0), checkDateInterval(250),
centering(1) {
lastSwitchMultiTime = millis();
}
void SplitFlapWebServer::init() {
if (! LittleFS.begin()) {
Serial.println("An Error has occurred while mounting LittleFS");
return;
}
setTimezone();
}
void SplitFlapWebServer::setTimezone() {
const char *sntpServer = "pool.ntp.org";
const char *defaultTz = "UTC0";
String timezoneSetting = settings.getString("timezone");
String posixTimezone = defaultTz;
File file = LittleFS.open("/timezones.json", "r");
if (! file) {
Serial.println("Failed to open timezones.json; defaulting to UTC");
configTzTime(defaultTz, sntpServer);
return;
}
size_t size = file.size();
std::unique_ptr<char[]> buffer(new char[size]);
file.readBytes(buffer.get(), size);
file.close();
JsonDocument timezones;
DeserializationError error = deserializeJson(timezones, buffer.get());
if (error) {
Serial.println("Failed to parse timezones.json: " + String(error.c_str()));
configTzTime(defaultTz, sntpServer);
return;
}
for (JsonPair kv : timezones.as<JsonObject>()) {
String keyStr = kv.key().c_str();
String valueStr = kv.value().as<String>();
if (keyStr == timezoneSetting) {
posixTimezone = valueStr;
break;
}
}
Serial.println("POSIX Timezone set to: " + posixTimezone);
configTzTime(posixTimezone.c_str(), sntpServer);
}
// Totally didn't use AI to make these functions
// Function to get current minute as a string
String SplitFlapWebServer::getCurrentMinute() {
struct tm timeinfo;
if (! getLocalTime(&timeinfo)) {
return "";
}
char minuteStr[3]; // Max "59" + null terminator
sprintf(minuteStr, "%02d", timeinfo.tm_min); // Format as two-digit string
return String(minuteStr);
}
// Function to get current hour as a string
String SplitFlapWebServer::getCurrentHour() {
struct tm timeinfo;
if (! getLocalTime(&timeinfo)) {
return "";
}
char hourStr[3]; // Max "59" + null terminator
sprintf(hourStr, "%02d", timeinfo.tm_hour); // Format as two-digit string
return String(hourStr);
}
// Function to get the first n characters of the day
String SplitFlapWebServer::getDayPrefix(int n) {
struct tm timeinfo;
if (! getLocalTime(&timeinfo)) {
return "Err"; // Return error if time not available
}
// Get full weekday name
char fullDay[10]; // Buffer for full day name
strftime(fullDay, sizeof(fullDay), "%A", &timeinfo);
// Extract first n characters
char dayPrefix[n + 1];
strncpy(dayPrefix, fullDay, n);
dayPrefix[n] = '\0'; // Null-terminate the string
return String(dayPrefix);
}
// Function to get the first n characters of the month
String SplitFlapWebServer::getMonthPrefix(int n) {
struct tm timeinfo;
if (! getLocalTime(&timeinfo)) {
return "Err"; // Return error if time not available
}
// Get full month name
char fullMonth[10]; // Buffer for full month name
strftime(fullMonth, sizeof(fullMonth), "%B", &timeinfo);
// Extract first n characters
char monthPrefix[n + 1];
strncpy(monthPrefix, fullMonth, n);
monthPrefix[n] = '\0'; // Null-terminate the string
return String(monthPrefix);
}
String SplitFlapWebServer::getCurrentDay() {
struct tm timeinfo;
if (! getLocalTime(&timeinfo)) {
return "Err"; // Return error if time is not available
}
char dayStr[3]; // Buffer for the day number (max "31" + null terminator)
sprintf(dayStr, "%02d", timeinfo.tm_mday); // Format as two-digit string
return String(dayStr);
}
void SplitFlapWebServer::setMode(int targetMode) {
settings.putInt("mode", targetMode);
}
int SplitFlapWebServer::getMode() {
return settings.getInt("mode");
}
void SplitFlapWebServer::checkWiFi() {
if (connectionMode == 1) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Wi-Fi lost! Forcing reconnect...");
WiFi.disconnect();
WiFi.reconnect();
}
}
}
bool SplitFlapWebServer::loadWiFiCredentials() {
// Allow WIFI_SSID and WIFI_PASS to be overridden by compile-time definitions
String ssid = String(WIFI_SSID).isEmpty() ? settings.getString("ssid") : String(WIFI_SSID);
String password = String(WIFI_PASS).isEmpty() ? settings.getString("password") : String(WIFI_PASS);
if (ssid != "" && password != "") {
Serial.println("Wi-Fi credentials loaded successfully.");
Serial.print("Connecting to Network: ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
#ifdef WIFI_TX_POWER
delay(100);
WiFi.setTxPower((wifi_power_t) WIFI_TX_POWER);
#endif
WiFi.begin(ssid.c_str(), password.c_str());
return true; // Return true if credentials exist
}
return false; // Return false if no credentials were found
}
void SplitFlapWebServer::checkRebootRequired() {
if (rebootRequired) {
Serial.println("Reboot required. Restarting...");
delay(1000);
ESP.restart();
}
}
void SplitFlapWebServer::handleOta() {
ArduinoOTA.handle();
}
void SplitFlapWebServer::enableOta() {
// Skip OTA initialisation if no password is set
if (settings.getString("otaPass") == "") {
return;
}
ArduinoOTA.setHostname(settings.getString("mdns").c_str()); // otherwise mdns name gets overwritten with default
ArduinoOTA.setPassword(settings.getString("otaPass").c_str());
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_LITTLEFS
type = "filesystem";
LittleFS.end(); // Unmount the filesystem before update
}
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
LittleFS.begin(); // Remount filesystem
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
}).onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
LittleFS.begin(); // Remount filesystem
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
Serial.println("OTA Initialized");
}
bool SplitFlapWebServer::connectToWifi() {
if (loadWiFiCredentials()) {
unsigned long startAttemptTime = millis();
const unsigned long timeout = 20000; // 20 seconds
unsigned long lastPrintTime = startAttemptTime;
while (WiFi.status() != WL_CONNECTED) {
if (millis() - startAttemptTime >= timeout) {
Serial.println("_");
Serial.println("Wi-Fi connection failed! Timeout reached.");
return false; // Return false if unable to connect in 30 seconds
}
if ((millis() - lastPrintTime) > 1000) {
Serial.print(".");
lastPrintTime = millis();
}
yield();
}
// connected succesfully
connectionMode = 1;
WiFi.softAPdisconnect(); // Turns off SoftAP mode only after connected to
// actual network
WiFi.setAutoReconnect(true);
WiFi.persistent(true); // Saves Wi-Fi settings to flash memory
WiFi.setSleep(false);
Serial.println("Connected to Wi-Fi!");
Serial.println("IP Address: http://" + WiFi.localIP().toString());
return true;
}
return false;
}
void SplitFlapWebServer::startAccessPoint() {
connectionMode = 0;
const char *apSSID = AP_SSID;
WiFi.softAP(apSSID);
#ifdef WIFI_TX_POWER
delay(100);
WiFi.setTxPower((wifi_power_t) WIFI_TX_POWER);
#endif
Serial.println("AP Mode Started!");
Serial.println("Connect to: " + String(apSSID));
Serial.println("AP IP Address: http://" + WiFi.softAPIP().toString());
}
void fourOhFour(AsyncWebServerRequest *request) {
Serial.println("Request: " + request->url());
Serial.println("Method: " + String(request->methodToString()));
request->send(404);
}
void SplitFlapWebServer::endMDNS() {
MDNS.end();
Serial.println("mDNS responder stopped");
}
void SplitFlapWebServer::startMDNS() {
if (! MDNS.begin(settings.getString("mdns").c_str())) {
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);
}
}
Serial.println("mDNS: http://" + settings.getString("mdns") + ".local");
}
void SplitFlapWebServer::startWebServer() {
server.on("/", HTTP_GET, [this](AsyncWebServerRequest *request) { request->redirect("/index.html"); });
File root = LittleFS.open("/");
if (! root || ! root.isDirectory()) {
Serial.println("Failed to open directory or not a directory");
return;
}
File file = root.openNextFile();
while (file) {
if (String(file.name()).endsWith(".gz")) {
const char *filename = file.name();
String tempFilename = (String("/") + String(filename));
tempFilename.replace(".gz", "");
filename = tempFilename.c_str();
server.serveStatic(filename, LittleFS, filename, "max-age=600");
}
file = root.openNextFile();
}
server.on("/settings", HTTP_GET, [this](AsyncWebServerRequest *request) {
request->send(200, "application/json", settings.toJson().as<String>());
});
server.on("/settings/reset", HTTP_POST, [this](AsyncWebServerRequest *request) {
settings.reset();
JsonDocument response;
response["message"] = "Settings reset successfully! Reconnect to the " + String(AP_SSID) + " network";
response["persistent"] = true;
request->send(200, "application/json", response.as<String>());
this->attemptReconnect = true;
});
server.addHandler(new AsyncCallbackJsonWebHandler(
"/settings",
[this](AsyncWebServerRequest *request, JsonVariant &json) {
if (request->method() != HTTP_POST) {
return request->send(405, "application/json", "{\"error\":\"Method Not Allowed\"}");
}
Serial.println("Received settings update request");
Serial.println(json.as<String>());
bool rebootRequired = false;
bool reconnect = false;
JsonDocument response;
response["message"] = "Settings saved successfully!";
// TODO Refactor this it's gross
if ((json["ssid"].is<String>() && json["ssid"].as<String>() != settings.getString("ssid")) ||
(json["password"].is<String>() && json["password"].as<String>() != settings.getString("password"))) {
reconnect = true;
response["message"] = "Settings updated successfully, Network " "settings have changed, reconnect to the " +
json["ssid"].as<String>() + " network";
}
if (json["otaPass"].is<String>() && json["otaPass"].as<String>() != settings.getString("otaPass")) {
rebootRequired = true; // OTA password change can only be applied by rebooting
response["message"] = "Settings updated successfully, OTA Password has changed. Rebooting...";
}
if (json["mdns"].is<String>() && json["mdns"].as<String>() != settings.getString("mdns")) {
reconnect = true;
response["message"] =
"Settings updated successfully, mDNS name has changed, " "automatically redirecting to http://" +
json["mdns"].as<String>() + ".local...";
response["redirect"] = "http://" + json["mdns"].as<String>() + ".local/settings.html";
}
if ((json["mqtt_server"].is<String>() && json["mqtt_server"].as<String>() != settings.getString("mqtt_server")
) ||
(json["mqtt_port"].is<int>() && json["mqtt_port"].as<int>() != settings.getInt("mqtt_port")) ||
(json["mqtt_user"].is<String>() && json["mqtt_user"].as<String>() != settings.getString("mqtt_user")) ||
(json["mqtt_pass"].is<String>() && json["mqtt_pass"].as<String>() != settings.getString("mqtt_pass"))) {
response["message"] = "Mqtt settings have changed, reconnecting...";
reconnect = true;
}
if (! settings.fromJson(json)) {
response["message"] = "Failed to save settings";
response["type"] = "error";
response["errors"]["key"] = settings.getLastValidationKey();
response["errors"]["message"] = settings.getLastValidationError();
return request->send(400, "application/json", response.as<String>());
}
response["type"] = "success";
response["persistent"] = reconnect;
request->send(200, "application/json", response.as<String>());
this->rebootRequired = rebootRequired;
this->attemptReconnect = reconnect;
}
));
server
.addHandler(new AsyncCallbackJsonWebHandler("/text", [this](AsyncWebServerRequest *request, JsonVariant &json) {
if (request->method() != HTTP_POST) {
return request->send(405, "application/json", "{\"error\":\"Method Not Allowed\"}");
}
Serial.println("Received text update request");
Serial.println(json.as<String>());
// {"mode":"single","words":["adfasdf"],"delay":1,"center":false}
// {"mode":"multiple","words":["asdf","asdfasdf","fffff"],"delay":"14","center":true}
JsonDocument response;
if (! json["mode"].is<String>()) {
response["message"] = "Invalid mode type";
}
if (! json["words"].is<JsonArray>()) {
response["message"] = "Invalid words array";
}
float delay = json["delay"].as<float>();
if (delay < 1) {
response["message"] = "Invalid delay type / value";
}
if (! json["center"].is<bool>()) {
response["message"] = "Invalid center type";
}
if (response["message"].is<String>()) {
response["type"] = "error";
return request->send(400, "application/json", response.as<String>());
}
this->setMultiDelay(delay * 1000);
Serial.println("Delay: " + String(this->getMultiWordDelay()));
centering = json["center"].as<bool>() ? 1 : 0;
Serial.println("centering: " + String(centering ? "true" : "false"));
if (json["mode"] == "single") {
String word = decodeURIComponent(json["words"][0].as<String>());
Serial.println("Single Word: " + word);
this->setInputString(word);
this->setMode(0); // change mode last once all variables updated
}
if (json["mode"] == "multiple") {
JsonArray wordsArray = json["words"].as<JsonArray>();
String words = "";
for (JsonVariant v : wordsArray) {
words += decodeURIComponent(v.as<String>()) + ",";
}
if (words.length() > 0) {
words.remove(words.length() - 1);
}
this->setMultiInputString(words);
this->numMultiWords = wordsArray.size();
Serial.println("Multiple Words: " + words);
Serial.println("Number of Words: " + String(this->numMultiWords));
this->setMode(1);
}
response["message"] = "Text updated successfully!";
response["type"] = "success";
request->send(200, "application/json", response.as<String>());
}));
server.onNotFound(fourOhFour);
server.begin();
}
String SplitFlapWebServer::decodeURIComponent(String encodedString) {
String decodedString = encodedString;
// Replace common URL-encoded characters with their actual symbols
decodedString.replace("%20", " "); // space
decodedString.replace("%21", "!"); // exclamation mark
decodedString.replace("%22", "\""); // double quote
decodedString.replace("%23", "#"); // hash
decodedString.replace("%24", "$"); // dollar sign
decodedString.replace("%25", "%"); // percent
decodedString.replace("%26", "&"); // ampersand
decodedString.replace("%27", "'"); // single quote
decodedString.replace("%28", "("); // left parenthesis
decodedString.replace("%29", ")"); // right parenthesis
decodedString.replace("%2A", "*"); // asterisk
decodedString.replace("%2B", "+"); // plus
decodedString.replace("%2C", ","); // comma
decodedString.replace("%2D", "-"); // hyphen
decodedString.replace("%2E", "."); // period
decodedString.replace("%2F", "/"); // forward slash
decodedString.replace("%3A", ":"); // colon
decodedString.replace("%3B", ";"); // semicolon
decodedString.replace("%3C", "<"); // less than
decodedString.replace("%3D", "="); // equal sign
decodedString.replace("%3E", ">"); // greater than
decodedString.replace("%3F", "?"); // question mark
decodedString.replace("%40", "@"); // at symbol
decodedString.replace("%5B", "["); // left bracket
decodedString.replace("%5C", "\\"); // backslash
decodedString.replace("%5D", "]"); // right bracket
decodedString.replace("%5E", "^"); // caret
decodedString.replace("%5F", "_"); // underscore
decodedString.replace("%60", "`"); // grave accent
decodedString.replace("%7B", "{"); // left brace
decodedString.replace("%7C", "|"); // vertical bar
decodedString.replace("%7D", "}"); // right brace
decodedString.replace("%7E", "~"); // tilde
return decodedString;
}