#include "web_service.h"
WebService::WebService(DashboardState& state, DiagnosticsState& diag_state)
:
Service("Web", 10),
dashboardState(state),
diagnosticsState(diag_state),
server(80),
webSocket(81)
{
}
String formatUptime() {
unsigned long seconds = millis() / 1000;
unsigned long days = seconds / 86400;
seconds %= 86400;
unsigned long hours = seconds / 3600;
seconds %= 3600;
unsigned long minutes = seconds / 60;
seconds %= 60;
char buffer[64];
snprintf(buffer, sizeof(buffer),
"%lu days %02lu:%02lu:%02lu",
days, hours, minutes, seconds);
return String(buffer);
}
String ROOT_HTML = R"rawliteral(
ESP32 Dashboard
ESP32 Dashboard OTA
Device uptime:
Loading...
Hub Diagnostics:
Free heap:
Loading...
Minimum free heap:
Loading...
CPU Frequency:
Loading...
)rawliteral";
void WebService::broadcastState()
{
DiagnosticSample diagnostics = diagnosticsState.getCurrent();
Serial.println(diagnostics.freeHeap);
String json = "{";
// Opening system tag:
json += "\"system\":{";
json += "\"uptime\":\"";
json += dashboardState.uptime;
json += "\",";
json += "\"version\":\"";
json += dashboardState.firmwareVersion;
json += "\"";
json += "},"; // Close system tag
// Opening diagnostic tag:
json += "\"diagnostics\":{";
json += "\"free_heap\":\"";
json += diagnostics.freeHeap;
//json += ESP.getFreeHeap();
json += "\",";
json += "\"minimum_free_heap\":\"";
json += diagnostics.minimumFreeHeap;
json += "\",";
json += "\"cpu_frequency\":\"";
json += diagnostics.cpuFrequency;
json += "\"";
json += "}"; // Clost diagnostic tag
// Final close bracket
json += "}";
//Serial.println(json);
webSocket.broadcastTXT(json);
}
void WebService::begin() {
// Root path
server.on("/", [this](){
server.send(
200,
"text/html",
ROOT_HTML
);
});
#ifdef DEBUGGING
// Prints out paths that are requested but not found.
server.onNotFound([this]() {
Serial.print("HTTP not found: ");
Serial.println(server.uri());
server.send(404, "text/plain", "Not found");
});
#endif
server.begin();
webSocket.begin();
// Manage web socket connections
webSocket.onEvent(
[this](uint8_t clientNum,
WStype_t type,
uint8_t *payload,
size_t length)
{
if (type == WStype_TEXT) {
handleWebSocketMessage(
clientNum,
payload,
length
);
}
if (type == WStype_CONNECTED) {
Serial.println("WebSocket client connected");
broadcastState();
}
}
);
Serial.println("Web server started");
}
void WebService::handleWebSocketMessage(uint8_t clientNum, uint8_t *payload, size_t length) {
Serial.println("Client number: " + clientNum);
Serial.println("Sent a message of length: " + length);
Serial.print("Saying: ");
for (int ii = 0; ii < length; ii ++) {
Serial.print(payload[ii]);
}
Serial.print("\n\n\n");
}
void WebService::update()
{
dashboardState.update();
server.handleClient();
webSocket.loop();
// Brodcast updates once per second
static unsigned long lastUpdate = 0;
if (millis() - lastUpdate >= 1000)
{
lastUpdate = millis();
broadcastState();
}
}