refactor: improve task synchronization in vendor, filament, and spool creation functions

This commit is contained in:
2025-08-29 10:02:48 +02:00
parent 54275f2ac9
commit 95433b4842
2 changed files with 43 additions and 41 deletions

View File

@@ -616,6 +616,7 @@ bool updateSpoolBambuData(String payload) {
uint16_t createVendor(String vendor) { uint16_t createVendor(String vendor) {
// Create new vendor in Spoolman database using task system // Create new vendor in Spoolman database using task system
// Note: Due to async nature, the ID will be stored in createdVendorId global variable // 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 createdVendorId = 0; // Reset previous value
String spoolsUrl = spoolmanUrl + apiUrl + "/vendor"; String spoolsUrl = spoolmanUrl + apiUrl + "/vendor";
@@ -645,9 +646,7 @@ uint16_t createVendor(String vendor) {
params->spoolsUrl = spoolsUrl; params->spoolsUrl = spoolsUrl;
params->updatePayload = vendorPayload; params->updatePayload = vendorPayload;
// Check if API is idle before creating task // Create task without additional API state check since caller ensures synchronization
if(spoolmanApiState == API_IDLE){
// Erstelle die Task
BaseType_t result = xTaskCreate( BaseType_t result = xTaskCreate(
sendToApi, // Task-Funktion sendToApi, // Task-Funktion
"SendToApiTask", // Task-Name "SendToApiTask", // Task-Name
@@ -656,8 +655,9 @@ uint16_t createVendor(String vendor) {
0, // Priorität 0, // Priorität
NULL // Task-Handle (nicht benötigt) NULL // Task-Handle (nicht benötigt)
); );
} else {
Serial.println("Not spawning new task, API still active!"); if (result != pdPASS) {
Serial.println("Failed to create vendor task!");
delete params; delete params;
vendorDoc.clear(); vendorDoc.clear();
return 0; return 0;
@@ -736,6 +736,7 @@ uint16_t checkVendor(String vendor) {
uint16_t createFilament(uint16_t vendorId, const JsonDocument& payload) { uint16_t createFilament(uint16_t vendorId, const JsonDocument& payload) {
// Create new filament in Spoolman database using task system // Create new filament in Spoolman database using task system
// Note: Due to async nature, the ID will be stored in createdFilamentId global variable // 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 createdFilamentId = 0; // Reset previous value
String spoolsUrl = spoolmanUrl + apiUrl + "/filament"; String spoolsUrl = spoolmanUrl + apiUrl + "/filament";
@@ -790,9 +791,7 @@ uint16_t createFilament(uint16_t vendorId, const JsonDocument& payload) {
params->spoolsUrl = spoolsUrl; params->spoolsUrl = spoolsUrl;
params->updatePayload = filamentPayload; params->updatePayload = filamentPayload;
// Check if API is idle before creating task // Create task without additional API state check since caller ensures synchronization
if(spoolmanApiState == API_IDLE){
// Erstelle die Task
BaseType_t result = xTaskCreate( BaseType_t result = xTaskCreate(
sendToApi, // Task-Funktion sendToApi, // Task-Funktion
"SendToApiTask", // Task-Name "SendToApiTask", // Task-Name
@@ -801,8 +800,9 @@ uint16_t createFilament(uint16_t vendorId, const JsonDocument& payload) {
0, // Priorität 0, // Priorität
NULL // Task-Handle (nicht benötigt) NULL // Task-Handle (nicht benötigt)
); );
} else {
Serial.println("Not spawning new task, API still active!"); if (result != pdPASS) {
Serial.println("Failed to create filament task!");
delete params; delete params;
filamentDoc.clear(); filamentDoc.clear();
return 0; 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) { uint16_t createSpool(uint16_t vendorId, uint16_t filamentId, JsonDocument& payload, String uidString) {
// Create new spool in Spoolman database using task system // Create new spool in Spoolman database using task system
// Note: Due to async nature, the ID will be stored in createdSpoolId global variable // 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 createdSpoolId = 0; // Reset previous value
String spoolsUrl = spoolmanUrl + apiUrl + "/spool"; String spoolsUrl = spoolmanUrl + apiUrl + "/spool";
@@ -918,9 +919,7 @@ uint16_t createSpool(uint16_t vendorId, uint16_t filamentId, JsonDocument& paylo
params->spoolsUrl = spoolsUrl; params->spoolsUrl = spoolsUrl;
params->updatePayload = spoolPayload; params->updatePayload = spoolPayload;
// Check if API is idle before creating task // Create task without additional API state check since caller ensures synchronization
if(spoolmanApiState == API_IDLE){
// Erstelle die Task
BaseType_t result = xTaskCreate( BaseType_t result = xTaskCreate(
sendToApi, // Task-Funktion sendToApi, // Task-Funktion
"SendToApiTask", // Task-Name "SendToApiTask", // Task-Name
@@ -929,8 +928,9 @@ uint16_t createSpool(uint16_t vendorId, uint16_t filamentId, JsonDocument& paylo
0, // Priorität 0, // Priorität
NULL // Task-Handle (nicht benötigt) NULL // Task-Handle (nicht benötigt)
); );
} else {
Serial.println("Not spawning new task, API still active!"); if (result != pdPASS) {
Serial.println("Failed to create spool task!");
delete params; delete params;
return 0; return 0;
} }
@@ -941,7 +941,7 @@ uint16_t createSpool(uint16_t vendorId, uint16_t filamentId, JsonDocument& paylo
vTaskDelay(100 / portTICK_PERIOD_MS); vTaskDelay(100 / portTICK_PERIOD_MS);
} }
// daten mit startWriteJsonToTag schreiben // Write data to tag with startWriteJsonToTag
// void startWriteJsonToTag(const bool isSpoolTag, const char* payload); // void startWriteJsonToTag(const bool isSpoolTag, const char* payload);
payload["sm_id"].set(String(createdSpoolId)); payload["sm_id"].set(String(createdSpoolId));

View File

@@ -48,7 +48,9 @@ void scale_loop(void * parameter) {
Serial.println("Scale Loop started"); Serial.println("Scale Loop started");
Serial.println("++++++++++++++++++++++++++++++"); Serial.println("++++++++++++++++++++++++++++++");
vTaskDelay(pdMS_TO_TICKS(500));
scale.tare(); scale.tare();
vTaskDelay(pdMS_TO_TICKS(500));
for(;;) { for(;;) {
if (scale.is_ready()) if (scale.is_ready())