From e459b53472db2003ea037451dd88432b4ab897cd Mon Sep 17 00:00:00 2001 From: Manuel Weiser Date: Thu, 27 Mar 2025 18:01:15 +0100 Subject: [PATCH] feat: add functionality to remove Bambu credentials and update API handling --- html/spoolman.html | 21 +++++++++++++++++++++ src/bambu.cpp | 26 ++++++++++++++++++++++++++ src/bambu.h | 1 + src/commonFS.cpp | 14 ++++++++++++++ src/commonFS.h | 1 + src/website.cpp | 9 +++++++++ 6 files changed, 72 insertions(+) diff --git a/html/spoolman.html b/html/spoolman.html index 1b615f7..a961874 100644 --- a/html/spoolman.html +++ b/html/spoolman.html @@ -57,6 +57,26 @@ toggleOctoFields(); }; + function removeBambuCredentials() { + fetch('/api/bambu?remove=true') + .then(response => response.json()) + .then(data => { + if (data.success) { + document.getElementById('bambuIp').value = ''; + document.getElementById('bambuSerial').value = ''; + document.getElementById('bambuCode').value = ''; + document.getElementById('autoSend').checked = false; + document.getElementById('autoSendTime').value = ''; + document.getElementById('bambuStatusMessage').innerText = 'Bambu Credentials removed!'; + } else { + document.getElementById('bambuStatusMessage').innerText = 'Error while removing Bambu Credentials.'; + } + }) + .catch(error => { + document.getElementById('bambuStatusMessage').innerText = 'Error while removing: ' + error.message; + }); + } + function checkSpoolmanInstance() { const url = document.getElementById('spoolmanUrl').value; const spoolmanOctoEnabled = document.getElementById('spoolmanOctoEnabled').checked; @@ -162,6 +182,7 @@ +

diff --git a/src/bambu.cpp b/src/bambu.cpp index 56325a1..818f92d 100644 --- a/src/bambu.cpp +++ b/src/bambu.cpp @@ -38,6 +38,32 @@ int ams_count = 0; String amsJsonData; // Speichert das fertige JSON für WebSocket-Clients AMSData ams_data[MAX_AMS]; // Definition des Arrays; +bool removeBambuCredentials() { + if (BambuMqttTask) { + vTaskDelete(BambuMqttTask); + } + + if (!removeJsonValue("/bambu_credentials.json")) { + Serial.println("Fehler beim Löschen der Bambu-Credentials."); + return false; + } + // Löschen der globalen Variablen + g_bambu_ip = ""; + g_bambu_accesscode = ""; + g_bambu_serialnr = ""; + bambu_ip = nullptr; + bambu_accesscode = nullptr; + bambu_serialnr = nullptr; + autoSendToBambu = false; + autoSetToBambuSpoolId = 0; + ams_count = 0; + amsJsonData = ""; + + bambuDisabled = true; + + return true; +} + bool saveBambuCredentials(const String& ip, const String& serialnr, const String& accesscode, bool autoSend, const String& autoSendTime) { if (BambuMqttTask) { vTaskDelete(BambuMqttTask); diff --git a/src/bambu.h b/src/bambu.h index 1e8de4e..aef6c3b 100644 --- a/src/bambu.h +++ b/src/bambu.h @@ -32,6 +32,7 @@ extern bool autoSendToBambu; extern int autoSetToBambuSpoolId; extern bool bambuDisabled; +bool removeBambuCredentials(); bool loadBambuCredentials(); bool saveBambuCredentials(const String& bambu_ip, const String& bambu_serialnr, const String& bambu_accesscode, const bool autoSend, const String& autoSendTime); bool setupMqtt(); diff --git a/src/commonFS.cpp b/src/commonFS.cpp index 7940b22..b58f68d 100644 --- a/src/commonFS.cpp +++ b/src/commonFS.cpp @@ -1,6 +1,20 @@ #include "commonFS.h" #include +bool removeJsonValue(const char* filename) { + File file = LittleFS.open(filename, "r"); + if (!file) { + return true; + } + file.close(); + if (!LittleFS.remove(filename)) { + Serial.print("Fehler beim Löschen der Datei: "); + Serial.println(filename); + return false; + } + return true; +} + bool saveJsonValue(const char* filename, const JsonDocument& doc) { File file = LittleFS.open(filename, "w"); if (!file) { diff --git a/src/commonFS.h b/src/commonFS.h index d46b1e5..6cc2725 100644 --- a/src/commonFS.h +++ b/src/commonFS.h @@ -5,6 +5,7 @@ #include #include +bool removeJsonValue(const char* filename); bool saveJsonValue(const char* filename, const JsonDocument& doc); bool loadJsonValue(const char* filename, JsonDocument& doc); void initializeFileSystem(); diff --git a/src/website.cpp b/src/website.cpp index a49bdf8..18ac882 100644 --- a/src/website.cpp +++ b/src/website.cpp @@ -313,6 +313,15 @@ void setupWebserver(AsyncWebServer &server) { // Route für das Überprüfen der Bambu-Instanz server.on("/api/bambu", HTTP_GET, [](AsyncWebServerRequest *request){ + if (request->hasParam("remove")) { + if (removeBambuCredentials()) { + request->send(200, "application/json", "{\"success\": true}"); + } else { + request->send(500, "application/json", "{\"success\": false, \"error\": \"Fehler beim Löschen der Bambu-Credentials\"}"); + } + return; + } + if (!request->hasParam("bambu_ip") || !request->hasParam("bambu_serialnr") || !request->hasParam("bambu_accesscode")) { request->send(400, "application/json", "{\"success\": false, \"error\": \"Missing parameter\"}"); return;