-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathweb_config.cpp
More file actions
661 lines (583 loc) · 29.6 KB
/
web_config.cpp
File metadata and controls
661 lines (583 loc) · 29.6 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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
#include "web_config.h"
#include "web_config_html.h"
#include "build_info.h"
#include "system_monitor.h"
#include "network_manager.h"
#include "mqtt_manager.h"
#include "display_manager.h"
#include "crash_logger.h"
#include "logging.h"
#include <time.h>
// Global instance
WebConfig webConfig;
WebConfig::WebConfig() : server(nullptr), wsServer(nullptr), serverRunning(false), otaInProgress(false) {}
bool WebConfig::begin(int port) {
if (serverRunning) {
LOG_DEBUG_F("[WebServer] Already running on port %d\n", port);
return true;
}
try {
LOG_DEBUG_F("[WebServer] Initializing web server on port %d\n", port);
server = new WebServer(port);
if (!server) {
LOG_CRITICAL("[WebServer] Failed to allocate WebServer memory!");
return false;
}
LOG_DEBUG("[WebServer] Setting up HTTP routes");
// Setup routes
server->on("/", [this]() { handleRoot(); });
server->on("/console", [this]() { handleConsole(); });
server->on("/config/network", [this]() { handleNetworkConfig(); });
server->on("/config/mqtt", [this]() { handleMQTTConfig(); });
server->on("/config/images", [this]() { handleImageConfig(); });
server->on("/config/display", [this]() { handleDisplayConfig(); });
server->on("/config/system", [this]() { handleAdvancedConfig(); });
server->on("/config/commands", [this]() { handleSerialCommands(); });
server->on("/status", [this]() { handleStatus(); });
server->on("/api/save", HTTP_POST, [this]() { handleSaveConfig(); });
server->on("/api/add-source", HTTP_POST, [this]() { handleAddImageSource(); });
server->on("/api/remove-source", HTTP_POST, [this]() { handleRemoveImageSource(); });
server->on("/api/update-source", HTTP_POST, [this]() { handleUpdateImageSource(); });
server->on("/api/clear-sources", HTTP_POST, [this]() { handleClearImageSources(); });
server->on("/api/bulk-delete-sources", HTTP_POST, [this]() { handleBulkDeleteImageSources(); });
server->on("/api/next-image", HTTP_POST, [this]() { handleNextImage(); });
server->on("/api/force-refresh", HTTP_POST, [this]() { handleForceRefresh(); });
server->on("/api/update-transform", HTTP_POST, [this]() { handleUpdateImageTransform(); });
server->on("/api/copy-defaults", HTTP_POST, [this]() { handleCopyDefaultsToImage(); });
server->on("/api/apply-transform", HTTP_POST, [this]() { handleApplyTransform(); });
server->on("/api/toggle-image-enabled", HTTP_POST, [this]() { handleToggleImageEnabled(); });
server->on("/api/select-image", HTTP_POST, [this]() { handleSelectImage(); });
server->on("/api/clear-editing-state", HTTP_POST, [this]() { handleClearEditingState(); });
server->on("/api/update-image-duration", HTTP_POST, [this]() { handleUpdateImageDuration(); });
server->on("/api/restart", HTTP_POST, [this]() { handleRestart(); });
server->on("/api/factory-reset", HTTP_POST, [this]() { handleFactoryReset(); });
server->on("/api/set-log-severity", HTTP_POST, [this]() { handleSetLogSeverity(); });
server->on("/api/clear-crash-logs", HTTP_POST, [this]() { handleClearCrashLogs(); });
server->on("/api/force-brightness-update", HTTP_POST, [this]() { handleForceBrightnessUpdate(); });
server->on("/api/info", HTTP_GET, [this]() { handleGetAllInfo(); });
server->on("/api/current-image", HTTP_GET, [this]() { handleCurrentImage(); });
server->on("/api/health", HTTP_GET, [this]() { handleGetHealth(); });
server->on("/api/wifi-scan", HTTP_GET, [this]() { handleWiFiScan(); });
// Favicon handler (prevents 404 log clutter when browsers request favicon)
server->on("/favicon.ico", HTTP_GET, [this]() {
server->send(204); // No Content - silently ignore favicon requests
});
// Initialize ElegantOTA
ElegantOTA.begin(server);
ElegantOTA.onStart([]() {
LOG_INFO("ElegantOTA: Update started");
webConfig.setOTAInProgress(true); // Suppress WebSocket during OTA
displayManager.showOTAProgress("OTA Update", 0, "Starting...");
systemMonitor.forceResetWatchdog();
});
ElegantOTA.onProgress([](size_t current, size_t final) {
// Reset watchdog on every progress update to prevent timeout
systemMonitor.forceResetWatchdog();
// Only log progress to serial, don't update display
static uint8_t lastPercent = 0;
uint8_t percent = (current * 100) / final;
if (percent != lastPercent && percent % 10 == 0) {
LOG_DEBUG_F("ElegantOTA Progress: %u%%\n", percent);
lastPercent = percent;
}
});
ElegantOTA.onEnd([](bool success) {
systemMonitor.forceResetWatchdog();
webConfig.setOTAInProgress(false); // Re-enable WebSocket
if (success) {
LOG_INFO("ElegantOTA: Update successful!");
displayManager.showOTAProgress("OTA Complete!", 100, "Rebooting...");
delay(2000);
} else {
LOG_ERROR("ElegantOTA: Update failed!");
displayManager.showOTAProgress("OTA Failed", 0, "Update failed");
delay(3000);
}
});
server->on("/api-reference", [this]() { handleAPIReference(); });
server->onNotFound([this]() { handleNotFound(); });
LOG_DEBUG("Starting WebServer...");
server->begin();
// Initialize WebSocket server on port 81
LOG_DEBUG("[WebSocket] Starting WebSocket server on port 81");
LOG_DEBUG_F("[WebSocket] Free heap before allocation: %d bytes\n", ESP.getFreeHeap());
wsServer = new WebSocketsServer(81);
if (wsServer) {
LOG_DEBUG("[WebSocket] Server instance created successfully");
wsServer->begin();
wsServer->onEvent(webSocketEvent);
LOG_DEBUG("[WebSocket] ✓ Server started and event handler registered");
LOG_DEBUG_F("[WebSocket] Listening on port 81 (clients can connect to ws://%s:81)\n", WiFi.localIP().toString().c_str());
} else {
LOG_ERROR("[WebSocket] ERROR: Failed to allocate WebSocket server!");
LOG_ERROR_F("[WebSocket] Free heap: %d bytes, PSRAM: %d bytes\n", ESP.getFreeHeap(), ESP.getFreePsram());
}
if (!server) {
LOG_ERROR("ERROR: WebServer failed to start!");
return false;
}
serverRunning = true;
LOG_DEBUG_F("✓ Web configuration server started successfully on port %d\n", port);
return true;
} catch (const std::exception& e) {
LOG_ERROR_F("ERROR: Exception starting web server: %s\n", e.what());
return false;
} catch (...) {
LOG_ERROR("ERROR: Unknown exception starting web server!");
return false;
}
}
void WebConfig::handleClient() {
if (server && serverRunning) {
server->handleClient();
ElegantOTA.loop();
}
if (wsServer) {
wsServer->loop();
}
}
void WebConfig::loopWebSocket() {
if (wsServer) {
wsServer->loop();
}
}
bool WebConfig::isRunning() {
return serverRunning;
}
void WebConfig::stop() {
if (serverRunning && server) {
server->stop();
delete server;
server = nullptr;
serverRunning = false;
}
if (wsServer) {
wsServer->close();
delete wsServer;
wsServer = nullptr;
}
}
// Helper to start a chunked HTML response - sends header, nav, then returns for page content
// Sends CSS/JS from PROGMEM as separate chunks to avoid copying large strings into heap
void WebConfig::beginChunkedHtmlResponse(const String& title, const String& navPage) {
server->setContentLength(CONTENT_LENGTH_UNKNOWN);
server->send(200, "text/html", "");
// Send the HTML doctype and head opening separately from CSS to reduce peak heap usage
String headStart = "<!DOCTYPE html><html lang='en'><head>";
headStart += "<meta charset='UTF-8'><meta name='viewport' content='width=device-width,initial-scale=1'>";
headStart += "<title>" + title + "</title><style>";
server->sendContent(headStart);
// Send CSS directly from PROGMEM as its own chunk (~5KB) - avoids copying into a larger String
server->sendContent(FPSTR(HTML_CSS));
// Send the rest of the header (status badges, nav bar, etc.)
server->sendContent(generateHeaderBody(title));
server->sendContent(generateNavigation(navPage));
}
// Helper to finish a chunked HTML response - sends JS/modals from PROGMEM as separate chunks, then footer
void WebConfig::endChunkedHtmlResponse() {
// Send JavaScript from PROGMEM as its own chunk to avoid copying into a large String
server->sendContent("<script>");
server->sendContent(FPSTR(HTML_JAVASCRIPT));
server->sendContent("</script>");
// Send modals from PROGMEM as its own chunk
server->sendContent(FPSTR(HTML_MODALS));
// Send the footer bar (small, dynamic content)
server->sendContent(generateFooterBody());
server->sendContent(""); // End chunked transfer
}
// Route handlers - these call the page generators from web_config_pages.cpp
void WebConfig::handleRoot() {
size_t heapBefore = ESP.getFreeHeap();
LOG_DEBUG_F("[WebServer] Dashboard page accessed (heap before: %d bytes)\n", heapBefore);
beginChunkedHtmlResponse("Dashboard", "dashboard");
server->sendContent(generateMainPage());
endChunkedHtmlResponse();
size_t heapAfter = ESP.getFreeHeap();
int heapDelta = (int)heapBefore - (int)heapAfter;
if (heapDelta > 0) {
LOG_WARNING_F("[WebServer] Dashboard request used %d bytes heap (before: %d, after: %d)\n",
heapDelta, heapBefore, heapAfter);
} else {
LOG_DEBUG_F("[WebServer] Dashboard completed (heap after: %d bytes)\n", heapAfter);
}
}
void WebConfig::handleConsole() {
LOG_DEBUG("[WebServer] Serial console page accessed");
beginChunkedHtmlResponse("Serial Console", "console");
server->sendContent(generateConsolePage());
endChunkedHtmlResponse();
}
void WebConfig::handleNetworkConfig() {
LOG_DEBUG("[WebServer] Network configuration page accessed");
beginChunkedHtmlResponse("Network Configuration", "network");
server->sendContent(generateNetworkPage());
endChunkedHtmlResponse();
}
void WebConfig::handleMQTTConfig() {
LOG_DEBUG("[WebServer] MQTT configuration page accessed");
beginChunkedHtmlResponse("MQTT Configuration", "mqtt");
server->sendContent(generateMQTTPage());
endChunkedHtmlResponse();
}
void WebConfig::handleImageConfig() {
LOG_DEBUG("[WebServer] Image sources page accessed");
beginChunkedHtmlResponse("Image Sources", "images");
server->sendContent(generateImagePage());
endChunkedHtmlResponse();
}
void WebConfig::handleDisplayConfig() {
beginChunkedHtmlResponse("Display Configuration", "display");
server->sendContent(generateDisplayPage());
endChunkedHtmlResponse();
}
void WebConfig::handleAdvancedConfig() {
beginChunkedHtmlResponse("System Configuration", "system");
server->sendContent(generateAdvancedPage());
endChunkedHtmlResponse();
}
void WebConfig::handleSerialCommands() {
beginChunkedHtmlResponse("Serial Commands", "commands");
server->sendContent(generateSerialCommandsPage());
endChunkedHtmlResponse();
}
void WebConfig::handleStatus() {
String json = getSystemStatus();
sendResponse(200, "application/json", json);
}
void WebConfig::handleAPIReference() {
beginChunkedHtmlResponse("API Reference", "api");
server->sendContent(generateAPIReferencePage());
endChunkedHtmlResponse();
}
void WebConfig::handleNotFound() {
String uri = server->uri();
LOG_WARNING_F("[WebServer] 404 Not Found: %s\n", uri.c_str());
server->setContentLength(CONTENT_LENGTH_UNKNOWN);
server->send(404, "text/html", "");
server->sendContent(generateHeader("Page Not Found"));
server->sendContent("<div class='container'><div class='card error'>");
server->sendContent("<h2>Page Not Found</h2>");
server->sendContent("<p>The requested page could not be found.</p>");
server->sendContent("<a href='/' class='btn btn-primary'>Return to Dashboard</a>");
server->sendContent("</div></div>");
server->sendContent(generateFooter());
server->sendContent(""); // End chunked transfer
}
// HTML template generation functions
// generateHeader - returns full header including CSS (used by handleNotFound which doesn't use chunked helpers)
String WebConfig::generateHeader(const String& title) {
String html;
html.reserve(2000); // Pre-allocate ~2KB for header HTML (excluding CSS which is large)
html = "<!DOCTYPE html><html lang='en'><head>";
html += "<meta charset='UTF-8'><meta name='viewport' content='width=device-width,initial-scale=1'>";
html += "<title>" + title + "</title>";
html += "<style>" + String(FPSTR(HTML_CSS)) + "</style></head><body>";
// Header bar
html += "<div class='header'><div class='container'>";
html += "<div class='header-content'>";
html += "<div class='logo'><i class='fas fa-satellite'></i> " + configStorage.getDeviceName() + "</div>";
html += "<div class='status-badges'>";
html += "<a href='https://github.com/chvvkumar/ESP32-P4-Allsky-Display' target='_blank' class='github-link'><i class='github-icon fa-github'></i> GitHub</a>";
html += getConnectionStatus();
if (configStorage.getImageSourceCount() > 1) {
html += "<button type='button' class='github-link' style='cursor:pointer;border:none' onclick='nextImage(this)'><i class='fas fa-forward' style='margin-right:6px'></i> Next</button>";
}
html += "<button class='github-link' style='cursor:pointer;border:none;background:#3b82f6;border-color:#2563eb' onclick='restart()'><i class='fas fa-sync-alt' style='margin-right:6px'></i> Restart</button>";
html += "<button class='github-link' style='cursor:pointer;border:none;background:#ef4444;border-color:#dc2626' onclick='factoryReset()'><i class='fas fa-trash-alt' style='margin-right:6px'></i> Reset</button>";
html += "</div></div></div></div>";
return html;
}
// generateHeaderBody - returns only the </style></head><body> + header bar HTML
// Used by beginChunkedHtmlResponse which sends CSS as a separate chunk from PROGMEM
String WebConfig::generateHeaderBody(const String& title) {
String html;
html.reserve(1500);
html = "</style></head><body>";
// Header bar
html += "<div class='header'><div class='container'>";
html += "<div class='header-content'>";
html += "<div class='logo'><i class='fas fa-satellite'></i> " + configStorage.getDeviceName() + "</div>";
html += "<div class='status-badges'>";
html += "<a href='https://github.com/chvvkumar/ESP32-P4-Allsky-Display' target='_blank' class='github-link'><i class='github-icon fa-github'></i> GitHub</a>";
html += getConnectionStatus();
if (configStorage.getImageSourceCount() > 1) {
html += "<button type='button' class='github-link' style='cursor:pointer;border:none' onclick='nextImage(this)'><i class='fas fa-forward' style='margin-right:6px'></i> Next</button>";
}
html += "<button class='github-link' style='cursor:pointer;border:none;background:#3b82f6;border-color:#2563eb' onclick='restart()'><i class='fas fa-sync-alt' style='margin-right:6px'></i> Restart</button>";
html += "<button class='github-link' style='cursor:pointer;border:none;background:#ef4444;border-color:#dc2626' onclick='factoryReset()'><i class='fas fa-trash-alt' style='margin-right:6px'></i> Reset</button>";
html += "</div></div></div></div>";
return html;
}
String WebConfig::generateNavigation(const String& currentPage) {
String html;
html.reserve(1500); // Pre-allocate ~1.5KB for navigation HTML
html = "<div class='nav'><div class='container' style='position:relative'>";
html += "<button class='nav-toggle' onclick='toggleNav()' aria-label='Toggle navigation'><i class='fas fa-bars'></i></button>";
html += "<div class='nav-content'>";
static const char* const pages[] = {"dashboard", "images", "display", "network", "mqtt", "console", "system", "commands", "api"};
static const char* const labels[] = {"\xF0\x9F\x8F\xA0 Dashboard", "\xF0\x9F\x96\xBC\xEF\xB8\x8F Images", "\xF0\x9F\x92\xA1 Display", "\xF0\x9F\x93\xA1 Network", "\xF0\x9F\x94\x97 MQTT", "\xF0\x9F\x96\xA5\xEF\xB8\x8F Console", "\xE2\x9A\x99\xEF\xB8\x8F System", "\xF0\x9F\x93\x9F Commands", "\xF0\x9F\x93\x9A API"};
static const char* const urls[] = {"/", "/config/images", "/config/display", "/config/network", "/config/mqtt", "/console", "/config/system", "/config/commands", "/api-reference"};
for (int i = 0; i < 9; i++) {
const char* activeClass = (currentPage == pages[i]) ? " active" : "";
html += "<a href='";
html += urls[i];
html += "' class='nav-item";
html += activeClass;
html += "'>";
html += labels[i];
html += "</a>";
}
html += "</div></div></div>";
return html;
}
// generateFooter - returns full footer including JS/modals (used by handleNotFound)
String WebConfig::generateFooter() {
String html;
html.reserve(1000); // Pre-allocate ~1KB for footer HTML
html = "<script>" + String(FPSTR(HTML_JAVASCRIPT)) + "</script>";
html += String(FPSTR(HTML_MODALS));
html += "<div class='footer'><div class='container'>";
html += "<p style='margin-bottom:0.5rem'>" + configStorage.getDeviceName() + " Configuration Portal</p>";
html += "<p style='font-size:0.8rem;color:#64748b;margin:0.25rem 0'>";
html += "MD5: " + String(ESP.getSketchMD5().substring(0, 8)) + " | ";
html += "Build: " + formatBytes(ESP.getSketchSize()) + " | ";
html += "Free: " + formatBytes(ESP.getFreeSketchSpace());
html += "</p>";
html += "<p style='font-size:0.75rem;color:#475569;margin:0.25rem 0'>";
html += "Built: " + String(BUILD_DATE) + " " + String(BUILD_TIME) + " | ";
html += "Commit: <span style='font-family:monospace'>" + String(GIT_COMMIT_HASH) + "</span> (" + String(GIT_BRANCH) + ")";
html += "</p></div></div>";
html += "</body></html>";
return html;
}
// generateFooterBody - returns only the footer bar HTML (no JS/modals)
// Used by endChunkedHtmlResponse which sends JS/modals from PROGMEM as separate chunks
String WebConfig::generateFooterBody() {
String html;
html.reserve(512);
html = "<div class='footer'><div class='container'>";
html += "<p style='margin-bottom:0.5rem'>" + configStorage.getDeviceName() + " Configuration Portal</p>";
html += "<p style='font-size:0.8rem;color:#64748b;margin:0.25rem 0'>";
html += "MD5: " + String(ESP.getSketchMD5().substring(0, 8)) + " | ";
html += "Build: " + formatBytes(ESP.getSketchSize()) + " | ";
html += "Free: " + formatBytes(ESP.getFreeSketchSpace());
html += "</p>";
html += "<p style='font-size:0.75rem;color:#475569;margin:0.25rem 0'>";
html += "Built: " + String(BUILD_DATE) + " " + String(BUILD_TIME) + " | ";
html += "Commit: <span style='font-family:monospace'>" + String(GIT_COMMIT_HASH) + "</span> (" + String(GIT_BRANCH) + ")";
html += "</p></div></div>";
html += "</body></html>";
return html;
}
// Utility functions
String WebConfig::getSystemStatus() {
String json;
json.reserve(512); // Pre-allocate ~512 bytes for system status JSON
json = "{";
json += "\"wifi_connected\":" + String(wifiManager.isConnected() ? "true" : "false") + ",";
json += "\"wifi_ssid\":\"" + escapeJson(wifiManager.isConnected() ? String(WiFi.SSID()) : String("Not connected")) + "\",";
json += "\"wifi_ip\":\"" + (wifiManager.isConnected() ? WiFi.localIP().toString() : String("0.0.0.0")) + "\",";
json += "\"wifi_rssi\":" + String(WiFi.RSSI()) + ",";
json += "\"mqtt_connected\":" + String(mqttManager.isConnected() ? "true" : "false") + ",";
json += "\"free_heap\":" + String(systemMonitor.getCurrentFreeHeap()) + ",";
json += "\"free_psram\":" + String(systemMonitor.getCurrentFreePsram()) + ",";
json += "\"uptime\":" + String(millis()) + ",";
json += "\"brightness\":" + String(displayManager.getBrightness());
json += "}";
return json;
}
String WebConfig::formatUptime(unsigned long ms) {
unsigned long seconds = ms / 1000;
unsigned long minutes = seconds / 60;
unsigned long hours = minutes / 60;
unsigned long days = hours / 24;
if (days > 0) return String(days) + "d " + String(hours % 24) + "h";
else if (hours > 0) return String(hours) + "h " + String(minutes % 60) + "m";
else if (minutes > 0) return String(minutes) + "m " + String(seconds % 60) + "s";
else return String(seconds) + "s";
}
String WebConfig::formatBytes(size_t bytes) {
if (bytes < 1024) return String(bytes) + "B";
else if (bytes < 1024 * 1024) return String(bytes / 1024.0, 1) + "KB";
else return String(bytes / (1024.0 * 1024.0), 1) + "MB";
}
String WebConfig::getConnectionStatus() {
String html;
html.reserve(256); // Pre-allocate ~256 bytes for status badges
html = "";
if (wifiManager.isConnected()) html += "<span class='badge success'>WiFi ✓</span>";
else html += "<span class='badge error'>WiFi ✗</span>";
if (mqttManager.isConnected()) html += "<span class='badge success'>MQTT ✓</span>";
else html += "<span class='badge error'>MQTT ✗</span>";
if (systemMonitor.isSystemHealthy()) html += "<span class='badge success'>System ✓</span>";
else html += "<span class='badge warning'>System ⚠</span>";
return html;
}
String WebConfig::escapeHtml(const String& input) {
String output = input;
output.replace("&", "&");
output.replace("<", "<");
output.replace(">", ">");
output.replace("\"", """);
output.replace("'", "'");
return output;
}
String WebConfig::escapeJson(const String& input) {
String output;
output.reserve(input.length() + 16);
for (unsigned int i = 0; i < input.length(); i++) {
char c = input[i];
switch (c) {
case '\\': output += "\\\\"; break;
case '"': output += "\\\""; break;
case '\n': output += "\\n"; break;
case '\r': output += "\\r"; break;
case '\t': output += "\\t"; break;
case '\b': output += "\\b"; break;
case '\f': output += "\\f"; break;
default:
if (c < 0x20) {
// Escape other control characters as \u00XX
char buf[8];
snprintf(buf, sizeof(buf), "\\u%04x", (unsigned char)c);
output += buf;
} else {
output += c;
}
break;
}
}
return output;
}
void WebConfig::sendResponse(int code, const String& contentType, const String& content) {
if (server) {
server->send(code, contentType, content);
}
}
// WebSocket event handler (static)
void WebConfig::webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
LOG_DEBUG_F("[WebSocket] Client #%u disconnected\n", num);
LOG_DEBUG_F("[WebSocket] Active clients: %d\n", webConfig.wsServer->connectedClients());
break;
case WStype_CONNECTED:
{
IPAddress ip = webConfig.wsServer->remoteIP(num);
LOG_INFO_F("[WebSocket] Client #%u connected from %d.%d.%d.%d\n", num, ip[0], ip[1], ip[2], ip[3]);
LOG_DEBUG_F("[WebSocket] Total active clients: %d\n", webConfig.wsServer->connectedClients());
// Send welcome message
String welcome = "[SYSTEM] Console connected. Monitoring serial output...\n";
webConfig.wsServer->sendTXT(num, welcome);
LOG_DEBUG_F("[WebSocket] Welcome message sent to client #%u\n", num);
// Send buffered crash logs to new client
webConfig.sendCrashLogsToClient(num);
}
break;
case WStype_TEXT:
LOG_DEBUG_F("[WebSocket] Received from client #%u: %s\n", num, payload);
break;
case WStype_ERROR:
LOG_ERROR_F("[WebSocket] ERROR on client #%u\n", num);
break;
case WStype_PING:
LOG_DEBUG_F("[WebSocket] Ping from client #%u\n", num);
break;
case WStype_PONG:
LOG_DEBUG_F("[WebSocket] Pong from client #%u\n", num);
break;
default:
LOG_WARNING_F("[WebSocket] Unknown event type %d from client #%u\n", type, num);
break;
}
}
// Broadcast log message to all connected WebSocket clients with severity filtering
void WebConfig::broadcastLog(const char* message, uint16_t color, LogSeverity severity) {
if (!serverRunning || !wsServer || !message || otaInProgress) {
// Silent return - don't spam serial with these conditions
return;
}
// Check severity filter - only send messages at or above configured threshold
int minSeverity = configStorage.getMinLogSeverity();
if (severity < minSeverity) {
return; // Message filtered out by severity level
}
// Log broadcast failures for troubleshooting
static unsigned long lastBroadcastError = 0;
if (wsServer->connectedClients() == 0) {
// Only log "no clients" once per 30 seconds to avoid spam
if (millis() - lastBroadcastError > 30000) {
Serial.println("[WebSocket] DEBUG: No clients connected to broadcast to");
lastBroadcastError = millis();
}
return;
}
// Severity prefixes for visual identification
const char* severityPrefix = "";
switch(severity) {
case LOG_DEBUG: severityPrefix = "[DEBUG] "; break;
case LOG_INFO: severityPrefix = "[INFO] "; break;
case LOG_WARNING: severityPrefix = "[WARN] "; break;
case LOG_ERROR: severityPrefix = "[ERROR] "; break;
case LOG_CRITICAL: severityPrefix = "[CRITICAL] "; break;
}
// Use fixed buffer to avoid String heap fragmentation
char buffer[384];
// Get current time - always use real time
struct tm timeinfo;
char timeStr[32];
if (getLocalTime(&timeinfo, 0)) {
// Format: YYYY-MM-DD HH:MM:SS
strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", &timeinfo);
} else {
// Show clear message that time is not synced yet
snprintf(timeStr, sizeof(timeStr), "TIME_NOT_SYNCED");
}
int written = snprintf(buffer, sizeof(buffer), "[%s] %s%s",
timeStr, severityPrefix, message);
// Ensure newline termination if there's room
if (written > 0 && written < (int)sizeof(buffer) - 2) {
if (buffer[written - 1] != '\n') {
buffer[written] = '\n';
buffer[written + 1] = '\0';
}
}
wsServer->broadcastTXT(buffer);
}
// Send crash logs to a specific WebSocket client
void WebConfig::sendCrashLogsToClient(uint8_t clientNum) {
// Get recent logs from crash logger
String logs = crashLogger.getRecentLogs(6144); // Get up to 6KB of logs
if (logs.length() > 0) {
Serial.printf("[WebSocket] Sending %d bytes of crash logs to client #%u\n", logs.length(), clientNum);
// Send header to distinguish historical logs from live stream
String header = "\n╔══════════════════════════════════════════════════════════════╗\n";
header += "║ BUFFERED LOGS (Boot + Crash History) ║\n";
header += "║ These are preserved messages from boot and previous crashes ║\n";
header += "╚══════════════════════════════════════════════════════════════╝\n\n";
wsServer->sendTXT(clientNum, header);
delay(20);
// Send in chunks to avoid WebSocket buffer overflow
const size_t chunkSize = 1024;
size_t offset = 0;
while (offset < logs.length()) {
size_t remaining = logs.length() - offset;
size_t sendSize = (remaining < chunkSize) ? remaining : chunkSize;
String chunk = logs.substring(offset, offset + sendSize);
wsServer->sendTXT(clientNum, chunk);
offset += sendSize;
delay(10); // Small delay to prevent buffer overflow
}
// Send footer to mark end of historical logs
String footer = "\n╔══════════════════════════════════════════════════════════════╗\n";
footer += "║ END OF BUFFERED LOGS ║\n";
footer += "║ Live log streaming continues below... ║\n";
footer += "╚══════════════════════════════════════════════════════════════╝\n\n";
wsServer->sendTXT(clientNum, footer);
Serial.printf("[WebSocket] Crash logs sent to client #%u - now streaming live\n", clientNum);
} else {
Serial.printf("[WebSocket] No crash logs - client #%u will receive live stream only\n", clientNum);
String noLogs = "[SYSTEM] No buffered logs. Streaming live output...\n\n";
wsServer->sendTXT(clientNum, noLogs);
}
}