feat: implement filament and spool creation in Spoolman API
This commit is contained in:
390
src/api.cpp
390
src/api.cpp
@@ -5,8 +5,21 @@
|
|||||||
#include <Preferences.h>
|
#include <Preferences.h>
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include "scale.h"
|
#include "scale.h"
|
||||||
|
#include <time.h>
|
||||||
volatile spoolmanApiStateType spoolmanApiState = API_IDLE;
|
volatile spoolmanApiStateType spoolmanApiState = API_IDLE;
|
||||||
|
|
||||||
|
// Returns current date and time in ISO8601 format
|
||||||
|
String getCurrentDateISO8601() {
|
||||||
|
struct tm timeinfo;
|
||||||
|
if(!getLocalTime(&timeinfo)) {
|
||||||
|
Serial.println("Failed to obtain time");
|
||||||
|
return "1970-01-01T00:00:00Z";
|
||||||
|
}
|
||||||
|
char timeStringBuff[25];
|
||||||
|
strftime(timeStringBuff, sizeof(timeStringBuff), "%Y-%m-%dT%H:%M:%SZ", &timeinfo);
|
||||||
|
return String(timeStringBuff);
|
||||||
|
}
|
||||||
|
|
||||||
//bool spoolman_connected = false;
|
//bool spoolman_connected = false;
|
||||||
String spoolmanUrl = "";
|
String spoolmanUrl = "";
|
||||||
bool octoEnabled = false;
|
bool octoEnabled = false;
|
||||||
@@ -17,6 +30,8 @@ uint16_t remainingWeight = 0;
|
|||||||
uint16_t createdVendorId = 0; // Store ID of newly created vendor
|
uint16_t createdVendorId = 0; // Store ID of newly created vendor
|
||||||
uint16_t foundVendorId = 0; // Store ID of found vendor
|
uint16_t foundVendorId = 0; // Store ID of found vendor
|
||||||
uint16_t foundFilamentId = 0; // Store ID of found filament
|
uint16_t foundFilamentId = 0; // Store ID of found filament
|
||||||
|
uint16_t createdFilamentId = 0; // Store ID of newly created filament
|
||||||
|
uint16_t createdSpoolId = 0; // Store ID of newly created spool
|
||||||
bool spoolmanConnected = false;
|
bool spoolmanConnected = false;
|
||||||
bool spoolmanExtraFieldsChecked = false;
|
bool spoolmanExtraFieldsChecked = false;
|
||||||
TaskHandle_t* apiTask;
|
TaskHandle_t* apiTask;
|
||||||
@@ -199,6 +214,20 @@ void sendToApi(void *parameter) {
|
|||||||
Serial.println(foundFilamentId);
|
Serial.println(foundFilamentId);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case API_REQUEST_FILAMENT_CREATE:
|
||||||
|
Serial.println("Filament successfully created!");
|
||||||
|
createdFilamentId = doc["id"].as<uint16_t>();
|
||||||
|
Serial.print("Created Filament ID: ");
|
||||||
|
Serial.println(createdFilamentId);
|
||||||
|
oledShowProgressBar(1, 1, "Filament", "Created!");
|
||||||
|
break;
|
||||||
|
case API_REQUEST_SPOOL_CREATE:
|
||||||
|
Serial.println("Spool successfully created!");
|
||||||
|
createdSpoolId = doc["id"].as<uint16_t>();
|
||||||
|
Serial.print("Created Spool ID: ");
|
||||||
|
Serial.println(createdSpoolId);
|
||||||
|
oledShowProgressBar(1, 1, "Spool", "Created!");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
doc.clear();
|
doc.clear();
|
||||||
@@ -221,6 +250,20 @@ void sendToApi(void *parameter) {
|
|||||||
Serial.println(createdVendorId);
|
Serial.println(createdVendorId);
|
||||||
oledShowProgressBar(1, 1, "Vendor", "Created!");
|
oledShowProgressBar(1, 1, "Vendor", "Created!");
|
||||||
break;
|
break;
|
||||||
|
case API_REQUEST_FILAMENT_CREATE:
|
||||||
|
Serial.println("Filament successfully created!");
|
||||||
|
createdFilamentId = doc["id"].as<uint16_t>();
|
||||||
|
Serial.print("Created Filament ID: ");
|
||||||
|
Serial.println(createdFilamentId);
|
||||||
|
oledShowProgressBar(1, 1, "Filament", "Created!");
|
||||||
|
break;
|
||||||
|
case API_REQUEST_SPOOL_CREATE:
|
||||||
|
Serial.println("Spool successfully created!");
|
||||||
|
createdSpoolId = doc["id"].as<uint16_t>();
|
||||||
|
Serial.print("Created Spool ID: ");
|
||||||
|
Serial.println(createdSpoolId);
|
||||||
|
oledShowProgressBar(1, 1, "Spool", "Created!");
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
// Handle other create operations if needed
|
// Handle other create operations if needed
|
||||||
break;
|
break;
|
||||||
@@ -294,6 +337,12 @@ void sendToApi(void *parameter) {
|
|||||||
case API_REQUEST_VENDOR_CREATE:
|
case API_REQUEST_VENDOR_CREATE:
|
||||||
oledShowProgressBar(1, 1, "Failure!", "Vendor create");
|
oledShowProgressBar(1, 1, "Failure!", "Vendor create");
|
||||||
break;
|
break;
|
||||||
|
case API_REQUEST_FILAMENT_CREATE:
|
||||||
|
oledShowProgressBar(1, 1, "Failure!", "Filament create");
|
||||||
|
break;
|
||||||
|
case API_REQUEST_SPOOL_CREATE:
|
||||||
|
oledShowProgressBar(1, 1, "Failure!", "Spool create");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
Serial.println("Fehler beim Senden an Spoolman! HTTP Code: " + String(httpCode));
|
Serial.println("Fehler beim Senden an Spoolman! HTTP Code: " + String(httpCode));
|
||||||
|
|
||||||
@@ -563,67 +612,7 @@ bool updateSpoolBambuData(String payload) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// #### Filament Fabrik
|
// #### Brand Filament
|
||||||
uint16_t checkVendor(String vendor) {
|
|
||||||
// Check if vendor exists using task system
|
|
||||||
foundVendorId = 0; // Reset previous value
|
|
||||||
|
|
||||||
String spoolsUrl = spoolmanUrl + apiUrl + "/vendor?name=" + vendor;
|
|
||||||
Serial.print("Check vendor with URL: ");
|
|
||||||
Serial.println(spoolsUrl);
|
|
||||||
|
|
||||||
SendToApiParams* params = new SendToApiParams();
|
|
||||||
if (params == nullptr) {
|
|
||||||
Serial.println("Fehler: Kann Speicher für Task-Parameter nicht allokieren.");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
params->requestType = API_REQUEST_VENDOR_CHECK;
|
|
||||||
params->httpType = "GET";
|
|
||||||
params->spoolsUrl = spoolsUrl;
|
|
||||||
params->updatePayload = ""; // Empty for GET request
|
|
||||||
|
|
||||||
// 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!");
|
|
||||||
delete params;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for task completion
|
|
||||||
while(spoolmanApiState != API_IDLE) {
|
|
||||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if vendor was found
|
|
||||||
if (foundVendorId == 0) {
|
|
||||||
Serial.println("Vendor not found, creating new vendor...");
|
|
||||||
uint16_t vendorId = createVendor(vendor);
|
|
||||||
if (vendorId == 0) {
|
|
||||||
Serial.println("Failed to create vendor, returning 0.");
|
|
||||||
return 0; // Failed to create vendor
|
|
||||||
} else {
|
|
||||||
Serial.println("Vendor created with ID: " + String(vendorId));
|
|
||||||
checkFilament(vendorId);
|
|
||||||
return vendorId;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Serial.println("Vendor found: " + vendor);
|
|
||||||
Serial.print("Vendor ID: ");
|
|
||||||
Serial.println(foundVendorId);
|
|
||||||
return foundVendorId;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
||||||
@@ -685,11 +674,148 @@ uint16_t createVendor(String vendor) {
|
|||||||
return createdVendorId;
|
return createdVendorId;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t checkFilament(uint16_t vendorId) {
|
uint16_t checkVendor(String vendor) {
|
||||||
|
// Check if vendor exists using task system
|
||||||
|
foundVendorId = 0; // Reset previous value
|
||||||
|
|
||||||
|
String spoolsUrl = spoolmanUrl + apiUrl + "/vendor?name=" + vendor;
|
||||||
|
Serial.print("Check vendor with URL: ");
|
||||||
|
Serial.println(spoolsUrl);
|
||||||
|
|
||||||
|
SendToApiParams* params = new SendToApiParams();
|
||||||
|
if (params == nullptr) {
|
||||||
|
Serial.println("Fehler: Kann Speicher für Task-Parameter nicht allokieren.");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
params->requestType = API_REQUEST_VENDOR_CHECK;
|
||||||
|
params->httpType = "GET";
|
||||||
|
params->spoolsUrl = spoolsUrl;
|
||||||
|
params->updatePayload = ""; // Empty for GET request
|
||||||
|
|
||||||
|
// 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!");
|
||||||
|
delete params;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for task completion
|
||||||
|
while(spoolmanApiState != API_IDLE) {
|
||||||
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if vendor was found
|
||||||
|
if (foundVendorId == 0) {
|
||||||
|
Serial.println("Vendor not found, creating new vendor...");
|
||||||
|
uint16_t vendorId = createVendor(vendor);
|
||||||
|
if (vendorId == 0) {
|
||||||
|
Serial.println("Failed to create vendor, returning 0.");
|
||||||
|
return 0; // Failed to create vendor
|
||||||
|
} else {
|
||||||
|
Serial.println("Vendor created with ID: " + String(vendorId));
|
||||||
|
return vendorId;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Serial.println("Vendor found: " + vendor);
|
||||||
|
Serial.print("Vendor ID: ");
|
||||||
|
Serial.println(foundVendorId);
|
||||||
|
return foundVendorId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
createdFilamentId = 0; // Reset previous value
|
||||||
|
|
||||||
|
String spoolsUrl = spoolmanUrl + apiUrl + "/filament";
|
||||||
|
Serial.print("Create filament with URL: ");
|
||||||
|
Serial.println(spoolsUrl);
|
||||||
|
|
||||||
|
// Create JSON payload for filament creation
|
||||||
|
JsonDocument filamentDoc;
|
||||||
|
filamentDoc["name"] = payload["name"].as<String>();
|
||||||
|
filamentDoc["vendor_id"] = String(vendorId);
|
||||||
|
filamentDoc["material"] = payload["type"].as<String>();
|
||||||
|
filamentDoc["density"] = (payload["density"].is<String>() && payload["density"].as<String>().length() > 0) ? payload["density"].as<String>() : "1.24";
|
||||||
|
filamentDoc["diameter"] = (payload["diameter"].is<String>() && payload["diameter"].as<String>().length() > 0) ? payload["diameter"].as<String>() : "1.75";
|
||||||
|
filamentDoc["weight"] = payload["weight"].as<String>();
|
||||||
|
filamentDoc["spool_weight"] = payload["spool_weight"].as<String>();
|
||||||
|
filamentDoc["article_number"] = payload["artnr"].as<String>();
|
||||||
|
filamentDoc["comment"] = String("automatically generated");
|
||||||
|
filamentDoc["extruder_temp"] = payload["extruder_temp"].is<String>() ? payload["extruder_temp"].as<String>() : "";
|
||||||
|
filamentDoc["bed_temp"] = payload["bed_temp"].is<String>() ? payload["bed_temp"].as<String>() : "";
|
||||||
|
filamentDoc["external_id"] = payload["artnr"].as<String>();
|
||||||
|
|
||||||
|
if (payload["multi_color_hexes"].is<String>()) {
|
||||||
|
filamentDoc["multi_color_hexes"] = payload["multi_color_hexes"].as<String>();
|
||||||
|
filamentDoc["multi_color_direction"] = payload["multi_color_direction"].is<String>() ? payload["multi_color_direction"].as<String>() : "";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
filamentDoc["color_hex"] = (payload["color"].is<String>() && payload["color"].as<String>().length() >= 6) ? payload["color"].as<String>() : "FFFFFF";
|
||||||
|
}
|
||||||
|
|
||||||
|
String filamentPayload;
|
||||||
|
serializeJson(filamentDoc, filamentPayload);
|
||||||
|
Serial.print("Filament Payload: ");
|
||||||
|
Serial.println(filamentPayload);
|
||||||
|
|
||||||
|
SendToApiParams* params = new SendToApiParams();
|
||||||
|
if (params == nullptr) {
|
||||||
|
Serial.println("Fehler: Kann Speicher für Task-Parameter nicht allokieren.");
|
||||||
|
filamentDoc.clear();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
params->requestType = API_REQUEST_FILAMENT_CREATE;
|
||||||
|
params->httpType = "POST";
|
||||||
|
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!");
|
||||||
|
delete params;
|
||||||
|
filamentDoc.clear();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
filamentDoc.clear();
|
||||||
|
|
||||||
|
// Wait for task completion and return the created filament ID
|
||||||
|
// Note: createdFilamentId will be set by sendToApi when response is received
|
||||||
|
while(spoolmanApiState != API_IDLE) {
|
||||||
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||||
|
}
|
||||||
|
|
||||||
|
return createdFilamentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t checkFilament(uint16_t vendorId, const JsonDocument& payload) {
|
||||||
// Check if filament exists using task system
|
// Check if filament exists using task system
|
||||||
foundFilamentId = 0; // Reset previous value
|
foundFilamentId = 0; // Reset previous value
|
||||||
|
|
||||||
String spoolsUrl = spoolmanUrl + apiUrl + "/filament?vendor.id=" + String(vendorId);
|
String spoolsUrl = spoolmanUrl + apiUrl + "/filament?vendor.id=" + String(vendorId) + "&external_id=" + String(payload["artnr"].as<String>());
|
||||||
Serial.print("Check filament with URL: ");
|
Serial.print("Check filament with URL: ");
|
||||||
Serial.println(spoolsUrl);
|
Serial.println(spoolsUrl);
|
||||||
|
|
||||||
@@ -728,13 +854,12 @@ uint16_t checkFilament(uint16_t vendorId) {
|
|||||||
// Check if filament was found
|
// Check if filament was found
|
||||||
if (foundFilamentId == 0) {
|
if (foundFilamentId == 0) {
|
||||||
Serial.println("Filament not found, creating new filament...");
|
Serial.println("Filament not found, creating new filament...");
|
||||||
uint16_t filamentId = createFilament();
|
uint16_t filamentId = createFilament(vendorId, payload);
|
||||||
if (filamentId == 0) {
|
if (filamentId == 0) {
|
||||||
Serial.println("Failed to create filament, returning 0.");
|
Serial.println("Failed to create filament, returning 0.");
|
||||||
return 0; // Failed to create filament
|
return 0; // Failed to create filament
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Filament created with ID: " + String(filamentId));
|
Serial.println("Filament created with ID: " + String(filamentId));
|
||||||
checkSpool();
|
|
||||||
return filamentId;
|
return filamentId;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -745,54 +870,101 @@ uint16_t checkFilament(uint16_t vendorId) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool createFilament() {
|
uint16_t createSpool(uint16_t vendorId, uint16_t filamentId, JsonDocument& payload, String uidString) {
|
||||||
// {
|
// Create new spool in Spoolman database using task system
|
||||||
// "name": "PolyTerra Charcoal Black",
|
// Note: Due to async nature, the ID will be stored in createdSpoolId global variable
|
||||||
// "vendor_id": 0,
|
createdSpoolId = 0; // Reset previous value
|
||||||
// "material": "PLA",
|
|
||||||
// "price": 20,
|
|
||||||
// "density": 1.24,
|
|
||||||
// "diameter": 1.75,
|
|
||||||
// "weight": 1000,
|
|
||||||
// "spool_weight": 140,
|
|
||||||
// "article_number": "PM70820",
|
|
||||||
// "comment": "automatically generated",
|
|
||||||
// "settings_extruder_temp": 210,
|
|
||||||
// "settings_bed_temp": 60,
|
|
||||||
// "color_hex": "FF0000",
|
|
||||||
// "multi_color_hexes": "FF0000,00FF00,0000FF",
|
|
||||||
// "multi_color_direction": "coaxial",
|
|
||||||
// "external_id": "polymaker_pla_polysonicblack_1000_175",
|
|
||||||
// "extra": {
|
|
||||||
// "nozzle_temperature": "string"
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
String spoolsUrl = spoolmanUrl + apiUrl + "/spool";
|
||||||
|
Serial.print("Create spool with URL: ");
|
||||||
|
Serial.println(spoolsUrl);
|
||||||
|
String currentDate = getCurrentDateISO8601();
|
||||||
|
|
||||||
|
// Create JSON payload for spool creation
|
||||||
|
JsonDocument spoolDoc;
|
||||||
|
spoolDoc["first_used"] = String(currentDate);
|
||||||
|
spoolDoc["last_used"] = String(currentDate);
|
||||||
|
spoolDoc["filament_id"] = String(filamentId);
|
||||||
|
spoolDoc["initial_weight"] = weight > 10 ? String(weight) : "1000";
|
||||||
|
spoolDoc["spool_weight"] = (payload["spool_weight"].is<String>() && payload["spool_weight"].as<String>().length() > 0) ? payload["spool_weight"].as<String>() : "180";
|
||||||
|
spoolDoc["remaining_weight"] = (payload["weight"].is<String>() && payload["weight"].as<String>().length() > 0) ? payload["weight"].as<String>() : "1000";
|
||||||
|
spoolDoc["used_weight"] = "0";
|
||||||
|
spoolDoc["lot_nr"] = (payload["lotnr"].is<String>() && payload["lotnr"].as<String>().length() > 0) ? payload["lotnr"].as<String>() : "";
|
||||||
|
spoolDoc["comment"] = "automatically generated";
|
||||||
|
spoolDoc["extra"]["nfc_id"] = "\"" + uidString + "\"";
|
||||||
|
|
||||||
|
String spoolPayload;
|
||||||
|
serializeJson(spoolDoc, spoolPayload);
|
||||||
|
Serial.print("Spool Payload: ");
|
||||||
|
Serial.println(spoolPayload);
|
||||||
|
spoolDoc.clear();
|
||||||
|
|
||||||
|
SendToApiParams* params = new SendToApiParams();
|
||||||
|
if (params == nullptr) {
|
||||||
|
Serial.println("Fehler: Kann Speicher für Task-Parameter nicht allokieren.");
|
||||||
|
spoolDoc.clear();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
params->requestType = API_REQUEST_SPOOL_CREATE;
|
||||||
|
params->httpType = "POST";
|
||||||
|
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!");
|
||||||
|
delete params;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t checkSpool() {
|
// Wait for task completion and return the created spool ID
|
||||||
|
// Note: createdSpoolId will be set by sendToApi when response is received
|
||||||
|
while(spoolmanApiState != API_IDLE) {
|
||||||
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool createSpool() {
|
// daten mit startWriteJsonToTag schreiben
|
||||||
// Implement specific handling for Spool creation
|
// void startWriteJsonToTag(const bool isSpoolTag, const char* payload);
|
||||||
// {
|
payload["sm_id"].set(String(createdSpoolId));
|
||||||
// "first_used": "2019-08-24T14:15:22Z",
|
|
||||||
// "last_used": "2019-08-24T14:15:22Z",
|
String payloadString;
|
||||||
// "filament_id": 0,
|
serializeJson(payload, payloadString);
|
||||||
// "price": 20,
|
startWriteJsonToTag(true, payloadString.c_str());
|
||||||
// "initial_weight": 200,
|
|
||||||
// "spool_weight": 200,
|
return createdSpoolId;
|
||||||
// "remaining_weight": 800,
|
}
|
||||||
// "used_weight": 200,
|
|
||||||
// "location": "Shelf A",
|
bool createBrandFilament(JsonDocument& payload, String uidString) {
|
||||||
// "lot_nr": "52342",
|
uint16_t vendorId = checkVendor(payload["brand"].as<String>());
|
||||||
// "comment": "",
|
if (vendorId == 0) {
|
||||||
// "archived": false,
|
Serial.println("ERROR: Failed to create/find vendor");
|
||||||
// "extra": {
|
return false;
|
||||||
// "nfc_id": "string"
|
}
|
||||||
// }
|
|
||||||
// }
|
uint16_t filamentId = checkFilament(vendorId, payload);
|
||||||
|
if (filamentId == 0) {
|
||||||
|
Serial.println("ERROR: Failed to create/find filament");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t spoolId = createSpool(vendorId, filamentId, payload, uidString);
|
||||||
|
if (spoolId == 0) {
|
||||||
|
Serial.println("ERROR: Failed to create spool");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("SUCCESS: Brand filament created with Spool ID: " + String(spoolId));
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// #### Spoolman init
|
// #### Spoolman init
|
||||||
|
15
src/api.h
15
src/api.h
@@ -20,7 +20,9 @@ typedef enum {
|
|||||||
API_REQUEST_SPOOL_LOCATION_UPDATE,
|
API_REQUEST_SPOOL_LOCATION_UPDATE,
|
||||||
API_REQUEST_VENDOR_CREATE,
|
API_REQUEST_VENDOR_CREATE,
|
||||||
API_REQUEST_VENDOR_CHECK,
|
API_REQUEST_VENDOR_CHECK,
|
||||||
API_REQUEST_FILAMENT_CHECK
|
API_REQUEST_FILAMENT_CHECK,
|
||||||
|
API_REQUEST_FILAMENT_CREATE,
|
||||||
|
API_REQUEST_SPOOL_CREATE
|
||||||
} SpoolmanApiRequestType;
|
} SpoolmanApiRequestType;
|
||||||
|
|
||||||
extern volatile spoolmanApiStateType spoolmanApiState;
|
extern volatile spoolmanApiStateType spoolmanApiState;
|
||||||
@@ -30,9 +32,6 @@ extern bool octoEnabled;
|
|||||||
extern bool sendOctoUpdate;
|
extern bool sendOctoUpdate;
|
||||||
extern String octoUrl;
|
extern String octoUrl;
|
||||||
extern String octoToken;
|
extern String octoToken;
|
||||||
extern uint16_t createdVendorId; // ID of newly created vendor
|
|
||||||
extern uint16_t foundVendorId; // ID of found vendor
|
|
||||||
extern uint16_t foundFilamentId; // ID of found filament
|
|
||||||
extern bool spoolmanConnected;
|
extern bool spoolmanConnected;
|
||||||
|
|
||||||
bool checkSpoolmanInstance();
|
bool checkSpoolmanInstance();
|
||||||
@@ -46,12 +45,6 @@ uint8_t updateSpoolLocation(String spoolId, String location);
|
|||||||
bool initSpoolman(); // Neue Funktion zum Initialisieren von Spoolman
|
bool initSpoolman(); // Neue Funktion zum Initialisieren von Spoolman
|
||||||
bool updateSpoolBambuData(String payload); // Neue Funktion zum Aktualisieren der Bambu-Daten
|
bool updateSpoolBambuData(String payload); // Neue Funktion zum Aktualisieren der Bambu-Daten
|
||||||
bool updateSpoolOcto(int spoolId); // Neue Funktion zum Aktualisieren der Octo-Daten
|
bool updateSpoolOcto(int spoolId); // Neue Funktion zum Aktualisieren der Octo-Daten
|
||||||
uint16_t checkVendor(String vendor); // Check if vendor exists, return ID
|
bool createBrandFilament(JsonDocument& payload, String uidString);
|
||||||
uint16_t createVendor(String vendor); // Create vendor, return ID
|
|
||||||
uint16_t checkFilament(); // Check if filament exists, return ID
|
|
||||||
bool createFilament(); // Create filament
|
|
||||||
uint16_t checkSpool(); // Check if spool exists, return ID
|
|
||||||
bool createSpool(); // Create spool
|
|
||||||
void createFilamentFabrik(JsonDocument payload);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -33,6 +33,7 @@ AMSData ams_data[MAX_AMS]; // Definition des Arrays;
|
|||||||
bool removeBambuCredentials() {
|
bool removeBambuCredentials() {
|
||||||
if (BambuMqttTask) {
|
if (BambuMqttTask) {
|
||||||
vTaskDelete(BambuMqttTask);
|
vTaskDelete(BambuMqttTask);
|
||||||
|
BambuMqttTask = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Preferences preferences;
|
Preferences preferences;
|
||||||
@@ -63,6 +64,7 @@ bool removeBambuCredentials() {
|
|||||||
bool saveBambuCredentials(const String& ip, const String& serialnr, const String& accesscode, bool autoSend, const String& autoSendTime) {
|
bool saveBambuCredentials(const String& ip, const String& serialnr, const String& accesscode, bool autoSend, const String& autoSendTime) {
|
||||||
if (BambuMqttTask) {
|
if (BambuMqttTask) {
|
||||||
vTaskDelete(BambuMqttTask);
|
vTaskDelete(BambuMqttTask);
|
||||||
|
BambuMqttTask = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
bambuCredentials.ip = ip.c_str();
|
bambuCredentials.ip = ip.c_str();
|
||||||
@@ -593,6 +595,7 @@ void reconnect() {
|
|||||||
Serial.println("Disable Bambu MQTT Task after 5 retries");
|
Serial.println("Disable Bambu MQTT Task after 5 retries");
|
||||||
//vTaskSuspend(BambuMqttTask);
|
//vTaskSuspend(BambuMqttTask);
|
||||||
vTaskDelete(BambuMqttTask);
|
vTaskDelete(BambuMqttTask);
|
||||||
|
BambuMqttTask = NULL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -681,6 +684,7 @@ void bambu_restart() {
|
|||||||
|
|
||||||
if (BambuMqttTask) {
|
if (BambuMqttTask) {
|
||||||
vTaskDelete(BambuMqttTask);
|
vTaskDelete(BambuMqttTask);
|
||||||
|
BambuMqttTask = NULL;
|
||||||
delay(10);
|
delay(10);
|
||||||
}
|
}
|
||||||
setupMqtt();
|
setupMqtt();
|
||||||
|
42
src/nfc.cpp
42
src/nfc.cpp
@@ -39,12 +39,6 @@ volatile nfcReaderStateType nfcReaderState = NFC_IDLE;
|
|||||||
// 6 = reading
|
// 6 = reading
|
||||||
// ***** PN532
|
// ***** PN532
|
||||||
|
|
||||||
|
|
||||||
// ##### Recycling Fabrik #####
|
|
||||||
bool isRecyclingFabrik(const char* brand) {
|
|
||||||
return strcmp(brand, "Recycling Fabrik") == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ##### Funktionen für RFID #####
|
// ##### Funktionen für RFID #####
|
||||||
void payloadToJson(uint8_t *data) {
|
void payloadToJson(uint8_t *data) {
|
||||||
const char* startJson = strchr((char*)data, '{');
|
const char* startJson = strchr((char*)data, '{');
|
||||||
@@ -66,12 +60,6 @@ void payloadToJson(uint8_t *data) {
|
|||||||
int max_temp = doc["max_temp"];
|
int max_temp = doc["max_temp"];
|
||||||
const char* brand = doc["brand"];
|
const char* brand = doc["brand"];
|
||||||
|
|
||||||
// Recycling Fabrik
|
|
||||||
if (isRecyclingFabrik(brand)) {
|
|
||||||
// TODO: Implement specific handling for Recycling Fabrik
|
|
||||||
Serial.println("Recycling Fabrik erkannt.");
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.println("-----------------");
|
Serial.println("-----------------");
|
||||||
Serial.println("JSON-Parsed Data:");
|
Serial.println("JSON-Parsed Data:");
|
||||||
@@ -215,7 +203,7 @@ uint8_t ntag2xx_WriteNDEF(const char *payload) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool decodeNdefAndReturnJson(const byte* encodedMessage) {
|
bool decodeNdefAndReturnJson(const byte* encodedMessage, String uidString) {
|
||||||
oledShowProgressBar(1, octoEnabled?5:4, "Reading", "Decoding data");
|
oledShowProgressBar(1, octoEnabled?5:4, "Reading", "Decoding data");
|
||||||
|
|
||||||
byte typeLength = encodedMessage[3];
|
byte typeLength = encodedMessage[3];
|
||||||
@@ -266,12 +254,15 @@ bool decodeNdefAndReturnJson(const byte* encodedMessage) {
|
|||||||
oledShowProgressBar(1, 1, "Failure", "Scan spool first");
|
oledShowProgressBar(1, 1, "Failure", "Scan spool first");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Recycling Fabrik
|
// Brand Filament not registered to Spoolman
|
||||||
else if (isRecyclingFabrik(doc["type"].as<String>().c_str())) {
|
else if ((!doc["sm_id"].is<String>() || (doc["sm_id"].is<String>() && (doc["sm_id"] == "0" || doc["sm_id"] == "")))
|
||||||
// If no sm_id is present but the brand is Recycling Fabrik then
|
&& doc["brand"].is<String>() && doc["artnr"].is<String>())
|
||||||
|
{
|
||||||
|
doc["sm_id"] = "0"; // Ensure sm_id is set to 0
|
||||||
|
// If no sm_id is present but the brand is Brand Filament then
|
||||||
// create a new spool, maybe brand too, in Spoolman
|
// create a new spool, maybe brand too, in Spoolman
|
||||||
Serial.println("Recycling Fabrik Tag found!");
|
Serial.println("New Brand Filament Tag found!");
|
||||||
createFilamentFabrik(doc);
|
createBrandFilament(doc, uidString);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -393,6 +384,9 @@ void writeJsonToTag(void *parameter) {
|
|||||||
nfcReadingTaskSuspendRequest = false;
|
nfcReadingTaskSuspendRequest = false;
|
||||||
pauseBambuMqttTask = false;
|
pauseBambuMqttTask = false;
|
||||||
|
|
||||||
|
free(params->payload);
|
||||||
|
delete params;
|
||||||
|
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -451,6 +445,16 @@ void scanRfidTask(void * parameter) {
|
|||||||
|
|
||||||
//vTaskDelay(500 / portTICK_PERIOD_MS);
|
//vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||||
|
|
||||||
|
// create Tag UID string
|
||||||
|
String uidString = "";
|
||||||
|
for (uint8_t i = 0; i < uidLength; i++) {
|
||||||
|
//TBD: Rework to remove all the string operations
|
||||||
|
uidString += String(uid[i], HEX);
|
||||||
|
if (i < uidLength - 1) {
|
||||||
|
uidString += ":"; // Optional: Trennzeichen hinzufügen
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (uidLength == 7)
|
if (uidLength == 7)
|
||||||
{
|
{
|
||||||
uint16_t tagSize = readTagSize();
|
uint16_t tagSize = readTagSize();
|
||||||
@@ -480,7 +484,7 @@ void scanRfidTask(void * parameter) {
|
|||||||
vTaskDelay(pdMS_TO_TICKS(1));
|
vTaskDelay(pdMS_TO_TICKS(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!decodeNdefAndReturnJson(data))
|
if (!decodeNdefAndReturnJson(data, uidString))
|
||||||
{
|
{
|
||||||
oledShowProgressBar(1, 1, "Failure", "Unknown tag");
|
oledShowProgressBar(1, 1, "Failure", "Unknown tag");
|
||||||
nfcReaderState = NFC_READ_ERROR;
|
nfcReaderState = NFC_READ_ERROR;
|
||||||
|
@@ -48,9 +48,15 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp
|
|||||||
} else if (type == WS_EVT_PONG) {
|
} else if (type == WS_EVT_PONG) {
|
||||||
Serial.printf("WebSocket Client #%u pong\n", client->id());
|
Serial.printf("WebSocket Client #%u pong\n", client->id());
|
||||||
} else if (type == WS_EVT_DATA) {
|
} else if (type == WS_EVT_DATA) {
|
||||||
String message = String((char*)data);
|
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
deserializeJson(doc, message);
|
DeserializationError error = deserializeJson(doc, (char*)data, len);
|
||||||
|
//String message = String((char*)data);
|
||||||
|
//deserializeJson(doc, message);
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
Serial.println("JSON deserialization failed: " + String(error.c_str()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (doc["type"] == "heartbeat") {
|
if (doc["type"] == "heartbeat") {
|
||||||
// Sende Heartbeat-Antwort
|
// Sende Heartbeat-Antwort
|
||||||
|
Reference in New Issue
Block a user