From 909500fe2e5bded88540c83d9d35f484cabfbb91 Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Fri, 17 Nov 2023 22:09:28 +0100 Subject: [PATCH] Add light status API endpoint, UI update --- data | 2 +- platformio.ini | 1 + src/lib/config.cpp | 8 ++++++++ src/lib/webserver.cpp | 46 +++++++++++++++++++++++++++++++++++++------ src/lib/webserver.hpp | 2 ++ 5 files changed, 52 insertions(+), 7 deletions(-) diff --git a/data b/data index e14e854..58fa1e7 160000 --- a/data +++ b/data @@ -1 +1 @@ -Subproject commit e14e85425eb7a5fdc1d5aab6f4ae091f93ab45a7 +Subproject commit 58fa1e7ecb49388bc4b59444ab3f5af7b3219699 diff --git a/platformio.ini b/platformio.ini index 93878b5..4ab2d52 100644 --- a/platformio.ini +++ b/platformio.ini @@ -25,6 +25,7 @@ build_flags = !python scripts/git_rev.py -DLAST_BUILD_TIME=$UNIX_TIME -DARDUINO_USB_CDC_ON_BOOT + -DCORE_DEBUG_LEVEL=0 -fexceptions build_unflags = -Werror=all diff --git a/src/lib/config.cpp b/src/lib/config.cpp index 5913b7e..7d457e8 100644 --- a/src/lib/config.cpp +++ b/src/lib/config.cpp @@ -224,6 +224,14 @@ std::vector getScreenNameMap() void setupHardware() { + if (!LittleFS.begin(true)) + { + Serial.println(F("An Error has occurred while mounting LittleFS")); + } + if(!LittleFS.open("/index.html", "r")) { + Serial.println("Error loading WebUI"); + } + setupLeds(); WiFi.setHostname(getMyHostname().c_str()); diff --git a/src/lib/webserver.cpp b/src/lib/webserver.cpp index d1588b7..99f387e 100644 --- a/src/lib/webserver.cpp +++ b/src/lib/webserver.cpp @@ -6,11 +6,7 @@ TaskHandle_t eventSourceTaskHandle; void setupWebserver() { - if (!LittleFS.begin(true)) - { - Serial.println(F("An Error has occurred while mounting LittleFS")); - return; - } + events.onConnect([](AsyncEventSourceClient *client) { client->send("welcome", NULL, millis(), 1000); }); @@ -19,7 +15,7 @@ void setupWebserver() // server.serveStatic("/css", LittleFS, "/css/"); // server.serveStatic("/js", LittleFS, "/js/"); server.serveStatic("/build", LittleFS, "/build"); - server.serveStatic("/api.json", LittleFS, "/api.json"); + server.serveStatic("/swagger.json", LittleFS, "/swagger.json"); server.serveStatic("/api.html", LittleFS, "/api.html"); server.on("/", HTTP_GET, onIndex); @@ -45,6 +41,7 @@ void setupWebserver() server.on("/api/lights/off", HTTP_GET, onApiLightsOff); server.on("/api/lights/color", HTTP_GET, onApiLightsSetColor); + server.on("/api/lights", HTTP_GET, onApiLightsStatus); // server.on("^\\/api\\/lights\\/([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$", HTTP_GET, onApiLightsSetColor); @@ -108,12 +105,40 @@ StaticJsonDocument<768> getStatusObject() return root; } +StaticJsonDocument<512> getLedStatusObject() +{ + StaticJsonDocument<512> root; + JsonArray colors = root.createNestedArray("data"); +// Adafruit_NeoPixel pix = getPixels(); + + for (uint i = 0; i < pixels.numPixels(); i++) { + uint32_t pixColor = pixels.getPixelColor(i); + uint alpha = (pixColor >> 24) & 0xFF; + uint red = (pixColor >> 16) & 0xFF; + uint green = (pixColor >> 8) & 0xFF; + uint blue = pixColor & 0xFF; + char hexColor[8]; + sprintf(hexColor, "#%02X%02X%02X", red, green, blue); + + JsonObject object = colors.createNestedObject(); + object["red"] = red; + object["green"] = green; + object["blue"] = blue; + object["hex"] = hexColor; + } + + return root; +} + void eventSourceUpdate() { if (!events.count()) return; StaticJsonDocument<768> root = getStatusObject(); JsonArray data = root.createNestedArray("data"); + + root["leds"] = getLedStatusObject()["data"]; + String epdContent[NUM_SCREENS]; std::array retEpdContent = getCurrentEpdContent(); std::copy(std::begin(retEpdContent), std::end(retEpdContent), epdContent); @@ -648,6 +673,15 @@ void onApiSystemStatus(AsyncWebServerRequest *request) request->send(response); } +void onApiLightsStatus(AsyncWebServerRequest *request) +{ + AsyncResponseStream *response = request->beginResponseStream("application/json"); + + serializeJson(getLedStatusObject(), *response); + + request->send(response); +} + void onApiLightsOff(AsyncWebServerRequest *request) { setLights(0, 0, 0); diff --git a/src/lib/webserver.hpp b/src/lib/webserver.hpp index 595b2f8..b929b69 100644 --- a/src/lib/webserver.hpp +++ b/src/lib/webserver.hpp @@ -33,6 +33,7 @@ void onApiSettingsPost(AsyncWebServerRequest *request); void onApiSettingsPatch(AsyncWebServerRequest *request, JsonVariant &json); void onApiFullRefresh(AsyncWebServerRequest *request); +void onApiLightsStatus(AsyncWebServerRequest *request); void onApiLightsOff(AsyncWebServerRequest *request); void onApiLightsSetColor(AsyncWebServerRequest *request); @@ -42,6 +43,7 @@ void onApiRestart(AsyncWebServerRequest *request); void onIndex(AsyncWebServerRequest *request); void onNotFound(AsyncWebServerRequest *request); +StaticJsonDocument<512> getLedStatusObject(); StaticJsonDocument<768> getStatusObject(); void eventSourceUpdate(); void eventSourceTask(void *pvParameters); \ No newline at end of file