From 903b697912dc910b526ba96e256230b2d88e0574 Mon Sep 17 00:00:00 2001 From: Manuel Weiser Date: Sat, 22 Feb 2025 11:31:15 +0100 Subject: [PATCH] fix: update SPIFFS binary header and enhance WebSocket error handling --- .github/workflows/gitea-release.yml | 2 +- .github/workflows/github-release.yml | 2 +- src/website.cpp | 99 ++++++++++------------------ 3 files changed, 37 insertions(+), 66 deletions(-) diff --git a/.github/workflows/gitea-release.yml b/.github/workflows/gitea-release.yml index 7899e39..b832a47 100644 --- a/.github/workflows/gitea-release.yml +++ b/.github/workflows/gitea-release.yml @@ -41,7 +41,7 @@ jobs: cp .pio/build/esp32dev/firmware.bin .pio/build/esp32dev/upgrade_filaman_firmware_v${VERSION}.bin # Create SPIFFS binary with minimal ESP32 image header (chip version set to max supported) - echo -ne '\xE9\x01\x00\x00\x00\x00\x00\x00\x00\x30\x00\x00\xFF\xFF\x00\x00' > .pio/build/esp32dev/upgrade_filaman_website_v${VERSION}.bin + echo -ne '\xE9\x01\x00\x00\x00\x00\x00\x00\x00\x30\x00\x00\x00\x00\x00\x00' > .pio/build/esp32dev/upgrade_filaman_website_v${VERSION}.bin # Append the actual SPIFFS data cat .pio/build/esp32dev/spiffs.bin >> .pio/build/esp32dev/upgrade_filaman_website_v${VERSION}.bin 2>/dev/null diff --git a/.github/workflows/github-release.yml b/.github/workflows/github-release.yml index 3fca244..220292b 100644 --- a/.github/workflows/github-release.yml +++ b/.github/workflows/github-release.yml @@ -48,7 +48,7 @@ jobs: cp .pio/build/esp32dev/firmware.bin .pio/build/esp32dev/upgrade_filaman_firmware_v${VERSION}.bin # Create SPIFFS binary with minimal ESP32 image header (chip version set to max supported) - echo -ne '\xE9\x01\x00\x00\x00\x00\x00\x00\x00\x30\x00\x00\xFF\xFF\x00\x00' > .pio/build/esp32dev/upgrade_filaman_website_v${VERSION}.bin + echo -ne '\xE9\x01\x00\x00\x00\x00\x00\x00\x00\x30\x00\x00\x00\x00\x00\x00' > .pio/build/esp32dev/upgrade_filaman_website_v${VERSION}.bin # Append the actual SPIFFS data cat .pio/build/esp32dev/spiffs.bin >> .pio/build/esp32dev/upgrade_filaman_website_v${VERSION}.bin 2>/dev/null diff --git a/src/website.cpp b/src/website.cpp index 215023c..2e17798 100644 --- a/src/website.cpp +++ b/src/website.cpp @@ -8,6 +8,7 @@ #include "scale.h" #include "esp_task_wdt.h" #include +#include "display.h" #ifndef VERSION #define VERSION "1.1.0" @@ -32,6 +33,10 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp sendWriteResult(client, 3); } else if (type == WS_EVT_DISCONNECT) { Serial.println("Client getrennt."); + } else if (type == WS_EVT_ERROR) { + Serial.printf("WebSocket Client #%u error(%u): %s\n", client->id(), *((uint16_t*)arg), (char*)data); + } else if (type == WS_EVT_PONG) { + Serial.printf("WebSocket Client #%u pong\n", client->id()); } else if (type == WS_EVT_DATA) { String message = String((char*)data); JsonDocument doc; @@ -373,22 +378,36 @@ void setupWebserver(AsyncWebServer &server) { request->send(response); if (success) { + oledShowMessage("Upgrade successful Rebooting"); delay(500); ESP.restart(); } + else { + oledShowMessage("Upgrade failed"); + } }, [](AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final) { static size_t updateSize = 0; static int command = 0; + oledShowMessage("Upgrade please wait"); + + // TODO: Backup + if (!index) { updateSize = request->contentLength(); command = (filename.indexOf("spiffs") > -1) ? U_SPIFFS : U_FLASH; - Serial.printf("Update Start: %s\nSize: %u\nCommand: %d\n", filename.c_str(), updateSize, command); - + + if (command == U_SPIFFS) { + // Backup JSON config files before SPIFFS update + backupJsonConfigs(); + } + if (!Update.begin(updateSize, command)) { - Serial.printf("Update Begin Error: "); - Update.printError(Serial); + if (command == U_SPIFFS) { + // Restore JSON config files if update fails at start + restoreJsonConfigs(); + } String errorMsg = String("Update begin failed: ") + Update.errorString(); request->send(400, "application/json", "{\"success\":false,\"message\":\"" + errorMsg + "\"}"); return; @@ -397,24 +416,30 @@ void setupWebserver(AsyncWebServer &server) { if (len) { if (Update.write(data, len) != len) { - Serial.printf("Update Write Error: "); - Update.printError(Serial); + if (command == U_SPIFFS) { + // Restore JSON config files if update fails during write + restoreJsonConfigs(); + } String errorMsg = String("Write failed: ") + Update.errorString(); request->send(400, "application/json", "{\"success\":false,\"message\":\"" + errorMsg + "\"}"); return; } - Serial.printf("Progress: %u/%u\r", index + len, updateSize); + + // Sende den Fortschritt als JSON, um unerwünschte Zeilenumbrüche zu vermeiden + String progress = "{\"progress\":" + String((index + len) * 100 / updateSize) + "}"; + request->send(200, "application/json", progress); } if (final) { if (!Update.end(true)) { - Serial.printf("Update End Error: "); - Update.printError(Serial); + if (command == U_SPIFFS) { + // Restore JSON config files if update fails at end + restoreJsonConfigs(); + } String errorMsg = String("Update end failed: ") + Update.errorString(); request->send(400, "application/json", "{\"success\":false,\"message\":\"" + errorMsg + "\"}"); return; } - Serial.printf("\nUpdate Success: %uB\n", index+len); } } ); @@ -442,60 +467,6 @@ void setupWebserver(AsyncWebServer &server) { Serial.println("Webserver gestartet"); } -void handleOTAUpload(AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final) { - static bool isSpiffsUpdate = false; - if (!index) { - // Start eines neuen Uploads - Serial.println("Update Start: " + filename); - - // Überprüfe den Dateityp basierend auf dem Dateinamen - bool isFirmware = filename.startsWith("filaman_"); - isSpiffsUpdate = filename.startsWith("webpage_"); - - if (!isFirmware && !isSpiffsUpdate) { - request->send(400, "application/json", "{\"message\":\"Invalid file type. File must start with 'filaman_' or 'webpage_'\"}"); - return; - } - - // Wähle den Update-Typ basierend auf dem Dateinamen - if (isSpiffsUpdate) { - if (!Update.begin(SPIFFS.totalBytes(), U_SPIFFS)) { - Update.printError(Serial); - request->send(400, "application/json", "{\"message\":\"SPIFFS Update failed: " + String(Update.errorString()) + "\"}"); - return; - } - // Backup JSON configs before SPIFFS update - backupJsonConfigs(); - } else { - if (!Update.begin(UPDATE_SIZE_UNKNOWN, U_FLASH)) { - Update.printError(Serial); - request->send(400, "application/json", "{\"message\":\"Firmware Update failed: " + String(Update.errorString()) + "\"}"); - return; - } - } - } - - if (Update.write(data, len) != len) { - Update.printError(Serial); - request->send(400, "application/json", "{\"message\":\"Write failed: " + String(Update.errorString()) + "\"}"); - return; - } - - if (final) { - if (!Update.end(true)) { - Update.printError(Serial); - request->send(400, "application/json", "{\"message\":\"Update failed: " + String(Update.errorString()) + "\"}"); - return; - } - if (isSpiffsUpdate) { - // Restore JSON configs after SPIFFS update - restoreJsonConfigs(); - } - request->send(200, "application/json", "{\"message\":\"Update successful!\", \"restart\": true}"); - delay(500); - ESP.restart(); - } -} void backupJsonConfigs() { const char* configs[] = {"/bambu_credentials.json", "/spoolman_url.json"};