From 0302725bc7d9269ac2b06036c8b6696c6748ff4b Mon Sep 17 00:00:00 2001 From: tugsi Date: Sun, 30 Mar 2025 14:28:17 +0200 Subject: [PATCH] Added add forced cache refresh after removing and saving Bambu credentials (Manuel Weiser) add functionality to remove Bambu credentials and update API handling (Manuel Weiser) add rfid_bambu.html and update bambu connection handling (Manuel Weiser) Fixed handle Bambu connection state by introducing bambuDisabled flag (Manuel Weiser) fe4d2d7 - Fix rfid.js-Failure with X1-Series, if you wanna send a Spool to AMS: - Uncaught TypeError: Cannot read properties of undefined (reading 'replace') at handleSpoolIn (rfid.js:493:67) at HTMLButtonElement.onclick ((Index):1:1) handleSpoolIn @ rfid.js:493 onclick @ (Index):1 (tugsi) Changed docs: update changelog and header for version v1.4.7 (Manuel Weiser) docs: update platformio.ini for version v1.4.7 (Manuel Weiser) Merge branch 'testing' (Manuel Weiser) style: update remove button for Bambu credentials with red background (Manuel Weiser) Merge pull request #28 from tugsi/main (ManuelW) --- html/rfid.html | 9 --- html/rfid.js | 3 +- html/rfid_bambu.html | 165 +++++++++++++++++++++++++++++++++++++++++++ html/spoolman.html | 35 ++++++++- src/bambu.cpp | 40 +++++++++-- src/bambu.h | 2 + src/commonFS.cpp | 17 +++++ src/commonFS.h | 3 +- src/main.cpp | 9 +-- src/website.cpp | 21 ++++-- 10 files changed, 274 insertions(+), 30 deletions(-) create mode 100644 html/rfid_bambu.html diff --git a/html/rfid.html b/html/rfid.html index b3b2527..a481c04 100644 --- a/html/rfid.html +++ b/html/rfid.html @@ -141,15 +141,6 @@ - -
-
-

Bambu AMS

-
-
Wait for AMS-Data...
-
-
-
diff --git a/html/rfid.js b/html/rfid.js index 960c9a7..73ec371 100644 --- a/html/rfid.js +++ b/html/rfid.js @@ -685,5 +685,4 @@ function showNotification(message, isSuccess) { setTimeout(() => { notification.remove(); }, 300); - }, 3000); -} \ No newline at end of file + }, 3000);} \ No newline at end of file diff --git a/html/rfid_bambu.html b/html/rfid_bambu.html new file mode 100644 index 0000000..310e69c --- /dev/null +++ b/html/rfid_bambu.html @@ -0,0 +1,165 @@ + + + + + + + + FilaMan - Filament Management Tool + + + + + + + + + + + +
+
+ +
+
+
+

Statistics

+ +
+
+

Spools

+
+ total: + +
+ without Tag: + +
+
+ +
+
+

Overview

+
    +
  • + Manufacturer: + +
  • +
  • + Weight: + +
  • +
  • + Length: + +
  • +
+
+
+

Materials

+
    + +
+
+
+
+
+
+

NFC-Tag

+ +
+
+
+
+ + +
+
+

Spoolman Spools

+ +
+ + +
+
+ + +
+ + +
+
+

Bambu AMS

+
+
Wait for AMS-Data...
+
+
+
+
+
+ + + + + + + \ No newline at end of file diff --git a/html/spoolman.html b/html/spoolman.html index 1b615f7..dccfac5 100644 --- a/html/spoolman.html +++ b/html/spoolman.html @@ -57,6 +57,31 @@ 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!'; + // Reload with forced cache refresh after short delay + setTimeout(() => { + window.location.replace('/'); + location.reload(true); + }, 1500); + } 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; @@ -89,7 +114,12 @@ .then(data => { if (data.healthy) { document.getElementById('bambuStatusMessage').innerText = 'Bambu Credentials saved!'; - } else { + // Reload with forced cache refresh after short delay + setTimeout(() => { + window.location.replace('/'); + location.reload(true); + }, 1500); + } else { document.getElementById('bambuStatusMessage').innerText = 'Error while saving Bambu Credentials.'; } }) @@ -162,7 +192,8 @@ -

+ +

diff --git a/src/bambu.cpp b/src/bambu.cpp index 256749b..a9b9ecd 100644 --- a/src/bambu.cpp +++ b/src/bambu.cpp @@ -27,6 +27,7 @@ const char* bambu_serialnr = nullptr; String g_bambu_ip = ""; String g_bambu_accesscode = ""; String g_bambu_serialnr = ""; +bool bambuDisabled = false; bool bambu_connected = false; bool autoSendToBambu = false; @@ -37,6 +38,35 @@ 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); @@ -706,14 +736,13 @@ bool setupMqtt() { bool success = loadBambuCredentials(); if (!success) { - Serial.println("Failed to load Bambu credentials"); - oledShowMessage("Bambu Credentials Missing"); - vTaskDelay(2000 / portTICK_PERIOD_MS); + bambuDisabled = true; return false; } if (success && bambu_ip != "" && bambu_accesscode != "" && bambu_serialnr != "") { + bambuDisabled = false; sslClient.setCACert(root_ca); sslClient.setInsecure(); client.setServer(bambu_ip, 8883); @@ -770,10 +799,7 @@ bool setupMqtt() { } else { - Serial.println("Fehler: Keine MQTT-Daten vorhanden"); - oledShowMessage("Bambu Credentials Missing"); - oledShowTopRow(); - vTaskDelay(2000 / portTICK_PERIOD_MS); + bambuDisabled = true; return false; } return true; diff --git a/src/bambu.h b/src/bambu.h index 1dc7b3d..aef6c3b 100644 --- a/src/bambu.h +++ b/src/bambu.h @@ -30,7 +30,9 @@ extern int ams_count; extern AMSData ams_data[MAX_AMS]; 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..f725d21 100644 --- a/src/commonFS.cpp +++ b/src/commonFS.cpp @@ -1,6 +1,23 @@ #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..9ce9dc6 100644 --- a/src/commonFS.h +++ b/src/commonFS.h @@ -5,7 +5,8 @@ #include #include -bool saveJsonValue(const char* filename, const JsonDocument& doc); +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/main.cpp b/src/main.cpp index 8278fb2..4ab3e98 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -98,10 +98,11 @@ void loop() { // Wenn Bambu auto set Spool aktiv if (autoSendToBambu && autoSetToBambuSpoolId > 0) { - if (!bambu_connected) - { - bambu_restart(); - } + if (!bambuDisabled && !bambu_connected) + + { + bambu_restart(); + } if (intervalElapsed(currentMillis, lastAutoSetBambuAmsTime, autoSetBambuAmsInterval)) { if (hasReadRfidTag == 0) diff --git a/src/website.cpp b/src/website.cpp index d8d9ce4..5f43d71 100644 --- a/src/website.cpp +++ b/src/website.cpp @@ -29,7 +29,7 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp if (type == WS_EVT_CONNECT) { Serial.println("Neuer Client verbunden!"); // Sende die AMS-Daten an den neuen Client - sendAmsData(client); + if (!bambuDisabled) sendAmsData(client); sendNfcData(client); foundNfcTag(client, 0); sendWriteResult(client, 3); @@ -213,7 +213,9 @@ void setupWebserver(AsyncWebServer &server) { // Route für RFID server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ Serial.println("Anfrage für /rfid erhalten"); - AsyncWebServerResponse *response = request->beginResponse(LittleFS, "/rfid.html.gz", "text/html"); + + String page = (bambuDisabled) ? "/rfid.html.gz" : "/rfid_bambu.html.gz"; + AsyncWebServerResponse *response = request->beginResponse(LittleFS, page, "text/html"); response->addHeader("Content-Encoding", "gzip"); response->addHeader("Cache-Control", CACHE_CONTROL); request->send(response); @@ -311,7 +313,17 @@ void setupWebserver(AsyncWebServer &server) { }); // Route für das Überprüfen der Bambu-Instanz - server.on("/api/bambu", HTTP_GET, [](AsyncWebServerRequest *request){ + 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; @@ -335,8 +347,7 @@ void setupWebserver(AsyncWebServer &server) { bool success = saveBambuCredentials(bambu_ip, bambu_serialnr, bambu_accesscode, autoSend, autoSendTime); - request->send(200, "application/json", "{\"healthy\": " + String(success ? "true" : "false") + "}"); - }); + request->send(200, "application/json", "{\"healthy\": " + String(success ? "true" : "false") + "}"); }); // Route für das Überprüfen der Spoolman-Instanz server.on("/reboot", HTTP_GET, [](AsyncWebServerRequest *request){