From 95433b4842e0b3b42de96e469c93b01294033579 Mon Sep 17 00:00:00 2001 From: Manuel Weiser Date: Fri, 29 Aug 2025 10:02:48 +0200 Subject: [PATCH] refactor: improve task synchronization in vendor, filament, and spool creation functions --- src/api.cpp | 80 +++++++++++++++++++++++++-------------------------- src/scale.cpp | 4 ++- 2 files changed, 43 insertions(+), 41 deletions(-) diff --git a/src/api.cpp b/src/api.cpp index 156ba3a..7a1bbcf 100644 --- a/src/api.cpp +++ b/src/api.cpp @@ -616,6 +616,7 @@ bool updateSpoolBambuData(String payload) { uint16_t createVendor(String vendor) { // Create new vendor in Spoolman database using task system // Note: Due to async nature, the ID will be stored in createdVendorId global variable + // Note: This function assumes that the caller has already ensured API is IDLE createdVendorId = 0; // Reset previous value String spoolsUrl = spoolmanUrl + apiUrl + "/vendor"; @@ -645,19 +646,18 @@ uint16_t createVendor(String vendor) { params->spoolsUrl = spoolsUrl; params->updatePayload = vendorPayload; - // Check if API is idle before creating task - if(spoolmanApiState == API_IDLE){ - // Erstelle die Task - BaseType_t result = xTaskCreate( - sendToApi, // Task-Funktion - "SendToApiTask", // Task-Name - 6144, // Stackgröße in Bytes - (void*)params, // Parameter - 0, // Priorität - NULL // Task-Handle (nicht benötigt) - ); - } else { - Serial.println("Not spawning new task, API still active!"); + // Create task without additional API state check since caller ensures synchronization + BaseType_t result = xTaskCreate( + sendToApi, // Task-Funktion + "SendToApiTask", // Task-Name + 6144, // Stackgröße in Bytes + (void*)params, // Parameter + 0, // Priorität + NULL // Task-Handle (nicht benötigt) + ); + + if (result != pdPASS) { + Serial.println("Failed to create vendor task!"); delete params; vendorDoc.clear(); return 0; @@ -736,6 +736,7 @@ uint16_t checkVendor(String vendor) { uint16_t createFilament(uint16_t vendorId, const JsonDocument& payload) { // Create new filament in Spoolman database using task system // Note: Due to async nature, the ID will be stored in createdFilamentId global variable + // Note: This function assumes that the caller has already ensured API is IDLE createdFilamentId = 0; // Reset previous value String spoolsUrl = spoolmanUrl + apiUrl + "/filament"; @@ -790,19 +791,18 @@ uint16_t createFilament(uint16_t vendorId, const JsonDocument& payload) { params->spoolsUrl = spoolsUrl; params->updatePayload = filamentPayload; - // Check if API is idle before creating task - if(spoolmanApiState == API_IDLE){ - // Erstelle die Task - BaseType_t result = xTaskCreate( - sendToApi, // Task-Funktion - "SendToApiTask", // Task-Name - 6144, // Stackgröße in Bytes - (void*)params, // Parameter - 0, // Priorität - NULL // Task-Handle (nicht benötigt) - ); - } else { - Serial.println("Not spawning new task, API still active!"); + // Create task without additional API state check since caller ensures synchronization + BaseType_t result = xTaskCreate( + sendToApi, // Task-Funktion + "SendToApiTask", // Task-Name + 6144, // Stackgröße in Bytes + (void*)params, // Parameter + 0, // Priorität + NULL // Task-Handle (nicht benötigt) + ); + + if (result != pdPASS) { + Serial.println("Failed to create filament task!"); delete params; filamentDoc.clear(); return 0; @@ -881,6 +881,7 @@ uint16_t checkFilament(uint16_t vendorId, const JsonDocument& payload) { uint16_t createSpool(uint16_t vendorId, uint16_t filamentId, JsonDocument& payload, String uidString) { // Create new spool in Spoolman database using task system // Note: Due to async nature, the ID will be stored in createdSpoolId global variable + // Note: This function assumes that the caller has already ensured API is IDLE createdSpoolId = 0; // Reset previous value String spoolsUrl = spoolmanUrl + apiUrl + "/spool"; @@ -918,19 +919,18 @@ uint16_t createSpool(uint16_t vendorId, uint16_t filamentId, JsonDocument& paylo params->spoolsUrl = spoolsUrl; params->updatePayload = spoolPayload; - // Check if API is idle before creating task - if(spoolmanApiState == API_IDLE){ - // Erstelle die Task - BaseType_t result = xTaskCreate( - sendToApi, // Task-Funktion - "SendToApiTask", // Task-Name - 6144, // Stackgröße in Bytes - (void*)params, // Parameter - 0, // Priorität - NULL // Task-Handle (nicht benötigt) - ); - } else { - Serial.println("Not spawning new task, API still active!"); + // Create task without additional API state check since caller ensures synchronization + BaseType_t result = xTaskCreate( + sendToApi, // Task-Funktion + "SendToApiTask", // Task-Name + 6144, // Stackgröße in Bytes + (void*)params, // Parameter + 0, // Priorität + NULL // Task-Handle (nicht benötigt) + ); + + if (result != pdPASS) { + Serial.println("Failed to create spool task!"); delete params; return 0; } @@ -941,7 +941,7 @@ uint16_t createSpool(uint16_t vendorId, uint16_t filamentId, JsonDocument& paylo vTaskDelay(100 / portTICK_PERIOD_MS); } - // daten mit startWriteJsonToTag schreiben + // Write data to tag with startWriteJsonToTag // void startWriteJsonToTag(const bool isSpoolTag, const char* payload); payload["sm_id"].set(String(createdSpoolId)); diff --git a/src/scale.cpp b/src/scale.cpp index 24d8797..91b7ea3 100644 --- a/src/scale.cpp +++ b/src/scale.cpp @@ -48,8 +48,10 @@ void scale_loop(void * parameter) { Serial.println("Scale Loop started"); Serial.println("++++++++++++++++++++++++++++++"); + vTaskDelay(pdMS_TO_TICKS(500)); scale.tare(); - + vTaskDelay(pdMS_TO_TICKS(500)); + for(;;) { if (scale.is_ready()) {