From b0b3d41c8497da4e84e9d83d5466585f0f4ff283 Mon Sep 17 00:00:00 2001 From: Manuel Weiser Date: Fri, 21 Feb 2025 17:38:20 +0100 Subject: [PATCH] docs: add backup and restore functions for JSON configurations during OTA updates --- src/ota.cpp | 29 +++++++++++++++++++++++++++++ src/ota.h | 7 +++++-- src/website.cpp | 36 ++++++++++++++++++++++++++++++++---- 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/ota.cpp b/src/ota.cpp index de30e9f..4cbe122 100644 --- a/src/ota.cpp +++ b/src/ota.cpp @@ -29,12 +29,37 @@ void stopAllTasks() { Serial.println("All tasks stopped"); } +void backupJsonConfigs() { + const char* configs[] = {"/bambu_credentials.json", "/spoolman_url.json"}; + for (const char* config : configs) { + if (SPIFFS.exists(config)) { + String backupPath = String(config) + ".bak"; + SPIFFS.remove(backupPath); + SPIFFS.rename(config, backupPath); + } + } +} + +void restoreJsonConfigs() { + const char* configs[] = {"/bambu_credentials.json", "/spoolman_url.json"}; + for (const char* config : configs) { + String backupPath = String(config) + ".bak"; + if (SPIFFS.exists(backupPath)) { + SPIFFS.remove(config); + SPIFFS.rename(backupPath, config); + } + } +} + void performStageTwo() { if (!SPIFFS.begin(true)) { Serial.println("Error: Could not mount SPIFFS for stage 2"); return; } + // Backup JSON configs before update + backupJsonConfigs(); + File firmwareFile = SPIFFS.open("/firmware.bin", "r"); if (!firmwareFile) { Serial.println("Error: Could not open firmware.bin from SPIFFS"); @@ -71,6 +96,10 @@ void performStageTwo() { firmwareFile.close(); SPIFFS.remove("/firmware.bin"); // Cleanup + + // Restore JSON configs after update + restoreJsonConfigs(); + Serial.println("Stage 2 update successful, restarting..."); delay(500); ESP.restart(); diff --git a/src/ota.h b/src/ota.h index c126b76..ce646b5 100644 --- a/src/ota.h +++ b/src/ota.h @@ -8,8 +8,11 @@ #define UPDATE_SIZE_UNKNOWN 0xFFFFFFFF #endif -void stopAllTasks(); -void handleOTAUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final); +void handleOTAUpload(AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final); void checkForStagedUpdate(); +void performStageTwo(); +void stopAllTasks(); +void backupJsonConfigs(); +void restoreJsonConfigs(); #endif \ No newline at end of file diff --git a/src/website.cpp b/src/website.cpp index d5f452f..215023c 100644 --- a/src/website.cpp +++ b/src/website.cpp @@ -7,7 +7,6 @@ #include "nfc.h" #include "scale.h" #include "esp_task_wdt.h" -#include "ota.h" #include #ifndef VERSION @@ -444,26 +443,29 @@ void setupWebserver(AsyncWebServer &server) { } 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_"); - bool isWebpage = filename.startsWith("webpage_"); + isSpiffsUpdate = filename.startsWith("webpage_"); - if (!isFirmware && !isWebpage) { + 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 (isWebpage) { + 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); @@ -485,8 +487,34 @@ void handleOTAUpload(AsyncWebServerRequest *request, const String& filename, siz 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"}; + for (const char* config : configs) { + if (SPIFFS.exists(config)) { + String backupPath = String(config) + ".bak"; + SPIFFS.remove(backupPath); + SPIFFS.rename(config, backupPath); + } + } +} + +void restoreJsonConfigs() { + const char* configs[] = {"/bambu_credentials.json", "/spoolman_url.json"}; + for (const char* config : configs) { + String backupPath = String(config) + ".bak"; + if (SPIFFS.exists(backupPath)) { + SPIFFS.remove(config); + SPIFFS.rename(backupPath, config); + } + } +}