Introduces periodic Spoolman Healthcheck

Introduces a spoolman healthcheck that is executed every 60 seconds. Also fixes a bug with the periodic wifi update.
This commit is contained in:
Jan Philipp Ecker
2025-08-08 18:00:25 +02:00
parent 5509d98969
commit 4706152022
5 changed files with 249 additions and 215 deletions

View File

@@ -5,7 +5,7 @@
#include <Preferences.h> #include <Preferences.h>
#include "debug.h" #include "debug.h"
volatile spoolmanApiStateType spoolmanApiState = API_INIT; volatile spoolmanApiStateType spoolmanApiState = API_IDLE;
//bool spoolman_connected = false; //bool spoolman_connected = false;
String spoolmanUrl = ""; String spoolmanUrl = "";
bool octoEnabled = false; bool octoEnabled = false;
@@ -14,6 +14,8 @@ String octoUrl = "";
String octoToken = ""; String octoToken = "";
uint16_t remainingWeight = 0; uint16_t remainingWeight = 0;
bool spoolmanConnected = false; bool spoolmanConnected = false;
bool spoolmanExtraFieldsChecked = false;
TaskHandle_t* apiTask;
struct SendToApiParams { struct SendToApiParams {
SpoolmanApiRequestType requestType; SpoolmanApiRequestType requestType;
@@ -94,6 +96,11 @@ JsonDocument fetchSingleSpoolInfo(int spoolId) {
void sendToApi(void *parameter) { void sendToApi(void *parameter) {
HEAP_DEBUG_MESSAGE("sendToApi begin"); HEAP_DEBUG_MESSAGE("sendToApi begin");
// Wait until API is IDLE
while(spoolmanApiState != API_IDLE){
Serial.println("Waiting!");
yield();
}
spoolmanApiState = API_TRANSMITTING; spoolmanApiState = API_TRANSMITTING;
SendToApiParams* params = (SendToApiParams*)parameter; SendToApiParams* params = (SendToApiParams*)parameter;
@@ -236,7 +243,7 @@ bool updateSpoolTagId(String uidString, const char* payload) {
6144, // Stackgröße in Bytes 6144, // Stackgröße in Bytes
(void*)params, // Parameter (void*)params, // Parameter
0, // Priorität 0, // Priorität
NULL // Task-Handle (nicht benötigt) apiTask // Task-Handle (nicht benötigt)
); );
updateDoc.clear(); updateDoc.clear();
@@ -282,7 +289,7 @@ uint8_t updateSpoolWeight(String spoolId, uint16_t weight) {
6144, // Stackgröße in Bytes 6144, // Stackgröße in Bytes
(void*)params, // Parameter (void*)params, // Parameter
0, // Priorität 0, // Priorität
NULL // Task-Handle (nicht benötigt) apiTask // Task-Handle (nicht benötigt)
); );
updateDoc.clear(); updateDoc.clear();
@@ -319,7 +326,8 @@ uint8_t updateSpoolLocation(String spoolId, String location){
params->spoolsUrl = spoolsUrl; params->spoolsUrl = spoolsUrl;
params->updatePayload = updatePayload; params->updatePayload = updatePayload;
if(spoolmanApiState == API_IDLE){
if(apiTask == nullptr){
// Erstelle die Task // Erstelle die Task
BaseType_t result = xTaskCreate( BaseType_t result = xTaskCreate(
sendToApi, // Task-Funktion sendToApi, // Task-Funktion
@@ -327,9 +335,8 @@ uint8_t updateSpoolLocation(String spoolId, String location){
6144, // Stackgröße in Bytes 6144, // Stackgröße in Bytes
(void*)params, // Parameter (void*)params, // Parameter
0, // Priorität 0, // Priorität
NULL // Task-Handle (nicht benötigt) apiTask // Task-Handle
); );
}else{ }else{
Serial.println("Not spawning new task, API still active!"); Serial.println("Not spawning new task, API still active!");
} }
@@ -374,7 +381,7 @@ bool updateSpoolOcto(int spoolId) {
6144, // Stackgröße in Bytes 6144, // Stackgröße in Bytes
(void*)params, // Parameter (void*)params, // Parameter
0, // Priorität 0, // Priorität
NULL // Task-Handle (nicht benötigt) apiTask // Task-Handle (nicht benötigt)
); );
updateDoc.clear(); updateDoc.clear();
@@ -427,7 +434,7 @@ bool updateSpoolBambuData(String payload) {
6144, // Stackgröße in Bytes 6144, // Stackgröße in Bytes
(void*)params, // Parameter (void*)params, // Parameter
0, // Priorität 0, // Priorität
NULL // Task-Handle (nicht benötigt) apiTask // Task-Handle (nicht benötigt)
); );
return true; return true;
@@ -435,6 +442,8 @@ bool updateSpoolBambuData(String payload) {
// #### Spoolman init // #### Spoolman init
bool checkSpoolmanExtraFields() { bool checkSpoolmanExtraFields() {
// Only check extra fields if they have not been checked before
if(!spoolmanExtraFieldsChecked){
HTTPClient http; HTTPClient http;
String checkUrls[] = { String checkUrls[] = {
spoolmanUrl + apiUrl + "/field/spool", spoolmanUrl + apiUrl + "/field/spool",
@@ -582,14 +591,23 @@ bool checkSpoolmanExtraFields() {
http.end(); http.end();
spoolmanExtraFieldsChecked = true;
return true;
}else{
return true; return true;
} }
}
bool checkSpoolmanInstance(const String& url) { bool checkSpoolmanInstance() {
HTTPClient http; HTTPClient http;
String healthUrl = url + apiUrl + "/health"; bool returnValue = false;
Serial.print("Überprüfe Spoolman-Instanz unter: "); // Only do the spoolman instance check if there is no active API request going on
if(spoolmanApiState == API_IDLE){
spoolmanApiState = API_TRANSMITTING;
String healthUrl = spoolmanUrl + apiUrl + "/health";
Serial.print("Checking spoolman instance: ");
Serial.println(healthUrl); Serial.println(healthUrl);
http.begin(healthUrl); http.begin(healthUrl);
@@ -617,16 +635,29 @@ bool checkSpoolmanInstance(const String& url) {
spoolmanApiState = API_IDLE; spoolmanApiState = API_IDLE;
oledShowTopRow(); oledShowTopRow();
spoolmanConnected = true; spoolmanConnected = true;
return strcmp(status, "healthy") == 0; returnValue = strcmp(status, "healthy") == 0;
}else{
spoolmanConnected = false;
} }
doc.clear(); doc.clear();
}else{
spoolmanConnected = false;
} }
} else { } else {
spoolmanConnected = false;
Serial.println("Error contacting spoolman instance! HTTP Code: " + String(httpCode)); Serial.println("Error contacting spoolman instance! HTTP Code: " + String(httpCode));
} }
http.end(); http.end();
return false; returnValue = false;
spoolmanApiState = API_IDLE;
}else{
// If the check is skipped, return the previous status
Serial.println("Skipping spoolman healthcheck, API is active.");
returnValue = spoolmanConnected;
}
Serial.println("Healthcheck completed!");
return returnValue;
} }
bool saveSpoolmanUrl(const String& url, bool octoOn, const String& octo_url, const String& octoTk) { bool saveSpoolmanUrl(const String& url, bool octoOn, const String& octo_url, const String& octoTk) {
@@ -639,12 +670,13 @@ bool saveSpoolmanUrl(const String& url, bool octoOn, const String& octo_url, con
preferences.end(); preferences.end();
//TBD: This could be handled nicer in the future //TBD: This could be handled nicer in the future
spoolmanExtraFieldsChecked = false;
spoolmanUrl = url; spoolmanUrl = url;
octoEnabled = octoOn; octoEnabled = octoOn;
octoUrl = octo_url; octoUrl = octo_url;
octoToken = octoTk; octoToken = octoTk;
return true; return checkSpoolmanInstance();
} }
String loadSpoolmanUrl() { String loadSpoolmanUrl() {
@@ -664,15 +696,10 @@ String loadSpoolmanUrl() {
bool initSpoolman() { bool initSpoolman() {
oledShowProgressBar(3, 7, DISPLAY_BOOT_TEXT, "Spoolman init"); oledShowProgressBar(3, 7, DISPLAY_BOOT_TEXT, "Spoolman init");
spoolmanUrl = loadSpoolmanUrl(); spoolmanUrl = loadSpoolmanUrl();
spoolmanUrl.trim();
if (spoolmanUrl == "") {
Serial.println("Keine Spoolman-URL gefunden.");
return false;
}
bool success = checkSpoolmanInstance(spoolmanUrl); bool success = checkSpoolmanInstance();
if (!success) { if (!success) {
Serial.println("Spoolman nicht erreichbar."); Serial.println("Spoolman not available");
return false; return false;
} }

View File

@@ -29,7 +29,7 @@ extern String octoUrl;
extern String octoToken; extern String octoToken;
extern bool spoolmanConnected; extern bool spoolmanConnected;
bool checkSpoolmanInstance(const String& url); bool checkSpoolmanInstance();
bool saveSpoolmanUrl(const String& url, bool octoOn, const String& octoWh, const String& octoTk); bool saveSpoolmanUrl(const String& url, bool octoOn, const String& octoWh, const String& octoTk);
String loadSpoolmanUrl(); // Neue Funktion zum Laden der URL String loadSpoolmanUrl(); // Neue Funktion zum Laden der URL
bool checkSpoolmanExtraFields(); // Neue Funktion zum Überprüfen der Extrafelder bool checkSpoolmanExtraFields(); // Neue Funktion zum Überprüfen der Extrafelder

View File

@@ -5,7 +5,6 @@
#define BAMBU_DEFAULT_AUTOSEND_TIME 60 #define BAMBU_DEFAULT_AUTOSEND_TIME 60
#define NVS_NAMESPACE_API "api" #define NVS_NAMESPACE_API "api"
#define NVS_KEY_SPOOLMAN_URL "spoolmanUrl" #define NVS_KEY_SPOOLMAN_URL "spoolmanUrl"
#define NVS_KEY_OCTOPRINT_ENABLED "octoEnabled" #define NVS_KEY_OCTOPRINT_ENABLED "octoEnabled"
@@ -34,6 +33,9 @@
#define SCREEN_PROGRESS_BAR_HEIGHT 12U #define SCREEN_PROGRESS_BAR_HEIGHT 12U
#define DISPLAY_BOOT_TEXT "FilaMan" #define DISPLAY_BOOT_TEXT "FilaMan"
#define WIFI_CHECK_INTERVAL 60000U
#define DISPLAY_UPDATE_INTERVAL 1000U
#define SPOOLMAN_HEALTHCHECK_INTERVAL 60000U
extern const uint8_t PN532_IRQ; extern const uint8_t PN532_IRQ;
extern const uint8_t PN532_RESET; extern const uint8_t PN532_RESET;

View File

@@ -97,7 +97,8 @@ int16_t lastWeight = 0;
// WIFI check variables // WIFI check variables
unsigned long lastWifiCheckTime = 0; unsigned long lastWifiCheckTime = 0;
const unsigned long wifiCheckInterval = 60000; // Überprüfe alle 60 Sekunden (60000 ms) unsigned long lastTopRowUpdateTime = 0;
unsigned long lastSpoolmanHealcheckTime = 0;
// Button debounce variables // Button debounce variables
unsigned long lastButtonPress = 0; unsigned long lastButtonPress = 0;
@@ -115,17 +116,23 @@ void loop() {
} }
// Überprüfe regelmäßig die WLAN-Verbindung // Überprüfe regelmäßig die WLAN-Verbindung
if (intervalElapsed(currentMillis, lastWifiCheckTime, wifiCheckInterval)) if (intervalElapsed(currentMillis, lastWifiCheckTime, WIFI_CHECK_INTERVAL))
{ {
checkWiFiConnection(); checkWiFiConnection();
} }
// Periodic display update // Periodic display update
if (intervalElapsed(currentMillis, lastWifiCheckTime, 1000)) if (intervalElapsed(currentMillis, lastTopRowUpdateTime, DISPLAY_UPDATE_INTERVAL))
{ {
oledShowTopRow(); oledShowTopRow();
} }
// Periodic spoolman health check
if (intervalElapsed(currentMillis, lastSpoolmanHealcheckTime, SPOOLMAN_HEALTHCHECK_INTERVAL))
{
checkSpoolmanInstance();
}
// Wenn Bambu auto set Spool aktiv // Wenn Bambu auto set Spool aktiv
if (bambuCredentials.autosend_enable && autoSetToBambuSpoolId > 0) if (bambuCredentials.autosend_enable && autoSetToBambuSpoolId > 0)
{ {

View File

@@ -241,8 +241,6 @@ bool decodeNdefAndReturnJson(const byte* encodedMessage) {
Serial.println("SPOOL-ID gefunden: " + doc["sm_id"].as<String>()); Serial.println("SPOOL-ID gefunden: " + doc["sm_id"].as<String>());
activeSpoolId = doc["sm_id"].as<String>(); activeSpoolId = doc["sm_id"].as<String>();
lastSpoolId = activeSpoolId; lastSpoolId = activeSpoolId;
Serial.println("Api state: " + String(spoolmanApiState));
} }
else if(doc["location"].is<String>() && doc["location"] != "") else if(doc["location"].is<String>() && doc["location"] != "")
{ {