Adds new feature to write and read location tags
Location tags can be written via the website. If a location tag is read after reading a spool tag, the location of the spool will be updated in spoolman to the location from the tag.
This commit is contained in:
52
src/api.cpp
52
src/api.cpp
@@ -11,6 +11,7 @@ String octoUrl = "";
|
||||
String octoToken = "";
|
||||
|
||||
struct SendToApiParams {
|
||||
SpoolmanApiRequestType requestType;
|
||||
String httpType;
|
||||
String spoolsUrl;
|
||||
String updatePayload;
|
||||
@@ -90,6 +91,7 @@ void sendToApi(void *parameter) {
|
||||
SendToApiParams* params = (SendToApiParams*)parameter;
|
||||
|
||||
// Extrahiere die Werte
|
||||
SpoolmanApiRequestType requestType = params->requestType;
|
||||
String httpType = params->httpType;
|
||||
String spoolsUrl = params->spoolsUrl;
|
||||
String updatePayload = params->updatePayload;
|
||||
@@ -118,12 +120,15 @@ void sendToApi(void *parameter) {
|
||||
Serial.print("Fehler beim Parsen der JSON-Antwort: ");
|
||||
Serial.println(error.c_str());
|
||||
} else {
|
||||
if (httpType == "PUT") {
|
||||
if (requestType == API_REQUEST_SPOOL_WEIGHT_UPDATE) {
|
||||
uint16_t remaining_weight = doc["remaining_weight"].as<float>();
|
||||
Serial.print("Aktuelles Gewicht: ");
|
||||
Serial.println(remaining_weight);
|
||||
oledShowMessage("Remaining: " + String(remaining_weight) + "g");
|
||||
}
|
||||
else if ( requestType == API_REQUEST_SPOOL_LOCATION_UPDATE) {
|
||||
oledShowMessage("Location updated!");
|
||||
}
|
||||
|
||||
vTaskDelay(3000 / portTICK_PERIOD_MS);
|
||||
doc.clear();
|
||||
@@ -178,6 +183,7 @@ bool updateSpoolTagId(String uidString, const char* payload) {
|
||||
Serial.println("Fehler: Kann Speicher für Task-Parameter nicht allokieren.");
|
||||
return false;
|
||||
}
|
||||
params->requestType = API_REQUEST_SPOOL_TAG_ID_UPDATE;
|
||||
params->httpType = "PATCH";
|
||||
params->spoolsUrl = spoolsUrl;
|
||||
params->updatePayload = updatePayload;
|
||||
@@ -219,6 +225,7 @@ uint8_t updateSpoolWeight(String spoolId, uint16_t weight) {
|
||||
Serial.println("Fehler: Kann Speicher für Task-Parameter nicht allokieren.");
|
||||
return 0;
|
||||
}
|
||||
params->requestType = API_REQUEST_SPOOL_WEIGHT_UPDATE;
|
||||
params->httpType = "PUT";
|
||||
params->spoolsUrl = spoolsUrl;
|
||||
params->updatePayload = updatePayload;
|
||||
@@ -238,6 +245,45 @@ uint8_t updateSpoolWeight(String spoolId, uint16_t weight) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t updateSpoolLocation(String spoolId, String location){
|
||||
String spoolsUrl = spoolmanUrl + apiUrl + "/spool/" + spoolId;
|
||||
Serial.print("Update Spule mit URL: ");
|
||||
Serial.println(spoolsUrl);
|
||||
|
||||
// Update Payload erstellen
|
||||
JsonDocument updateDoc;
|
||||
updateDoc["location"] = location;
|
||||
|
||||
String updatePayload;
|
||||
serializeJson(updateDoc, updatePayload);
|
||||
Serial.print("Update Payload: ");
|
||||
Serial.println(updatePayload);
|
||||
|
||||
SendToApiParams* params = new SendToApiParams();
|
||||
if (params == nullptr) {
|
||||
Serial.println("Fehler: Kann Speicher für Task-Parameter nicht allokieren.");
|
||||
return 0;
|
||||
}
|
||||
params->requestType = API_REQUEST_SPOOL_LOCATION_UPDATE;
|
||||
params->httpType = "PATCH";
|
||||
params->spoolsUrl = spoolsUrl;
|
||||
params->updatePayload = updatePayload;
|
||||
|
||||
// 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)
|
||||
);
|
||||
|
||||
updateDoc.clear();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool updateSpoolOcto(int spoolId) {
|
||||
String spoolsUrl = octoUrl + "/plugin/Spoolman/selectSpool";
|
||||
Serial.print("Update Spule in Octoprint mit URL: ");
|
||||
@@ -257,6 +303,7 @@ bool updateSpoolOcto(int spoolId) {
|
||||
Serial.println("Fehler: Kann Speicher für Task-Parameter nicht allokieren.");
|
||||
return false;
|
||||
}
|
||||
params->requestType = API_REQUEST_OCTO_SPOOL_UPDATE;
|
||||
params->httpType = "POST";
|
||||
params->spoolsUrl = spoolsUrl;
|
||||
params->updatePayload = updatePayload;
|
||||
@@ -306,6 +353,7 @@ bool updateSpoolBambuData(String payload) {
|
||||
Serial.println("Fehler: Kann Speicher für Task-Parameter nicht allokieren.");
|
||||
return false;
|
||||
}
|
||||
params->requestType = API_REQUEST_BAMBU_UPDATE;
|
||||
params->httpType = "PATCH";
|
||||
params->spoolsUrl = spoolsUrl;
|
||||
params->updatePayload = updatePayload;
|
||||
@@ -510,6 +558,8 @@ bool checkSpoolmanInstance(const String& url) {
|
||||
return strcmp(status, "healthy") == 0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Serial.println("Error contacting spoolman instance! HTTP Code: " + String(httpCode));
|
||||
}
|
||||
http.end();
|
||||
return false;
|
||||
|
@@ -12,6 +12,14 @@ typedef enum {
|
||||
API_TRANSMITTING
|
||||
} spoolmanApiStateType;
|
||||
|
||||
typedef enum {
|
||||
API_REQUEST_OCTO_SPOOL_UPDATE,
|
||||
API_REQUEST_BAMBU_UPDATE,
|
||||
API_REQUEST_SPOOL_TAG_ID_UPDATE,
|
||||
API_REQUEST_SPOOL_WEIGHT_UPDATE,
|
||||
API_REQUEST_SPOOL_LOCATION_UPDATE
|
||||
} SpoolmanApiRequestType;
|
||||
|
||||
extern volatile spoolmanApiStateType spoolmanApiState;
|
||||
extern bool spoolman_connected;
|
||||
extern String spoolmanUrl;
|
||||
@@ -26,6 +34,7 @@ bool checkSpoolmanExtraFields(); // Neue Funktion zum Überprüfen der Extrafeld
|
||||
JsonDocument fetchSingleSpoolInfo(int spoolId); // API-Funktion für die Webseite
|
||||
bool updateSpoolTagId(String uidString, const char* payload); // Neue Funktion zum Aktualisieren eines Spools
|
||||
uint8_t updateSpoolWeight(String spoolId, uint16_t weight); // Neue Funktion zum Aktualisieren des Gewichts
|
||||
uint8_t updateSpoolLocation(String spoolId, String location);
|
||||
bool initSpoolman(); // Neue Funktion zum Initialisieren von Spoolman
|
||||
bool updateSpoolBambuData(String payload); // Neue Funktion zum Aktualisieren der Bambu-Daten
|
||||
bool updateSpoolOcto(int spoolId); // Neue Funktion zum Aktualisieren der Octo-Daten
|
||||
|
18
src/nfc.cpp
18
src/nfc.cpp
@@ -218,11 +218,25 @@ bool decodeNdefAndReturnJson(const byte* encodedMessage) {
|
||||
// Sende die aktualisierten AMS-Daten an alle WebSocket-Clients
|
||||
Serial.println("JSON-Dokument erfolgreich verarbeitet");
|
||||
Serial.println(doc.as<String>());
|
||||
if (doc["sm_id"] != "")
|
||||
if (doc.containsKey("sm_id") && doc["sm_id"] != "")
|
||||
{
|
||||
Serial.println("SPOOL-ID gefunden: " + doc["sm_id"].as<String>());
|
||||
spoolId = doc["sm_id"].as<String>();
|
||||
}
|
||||
}
|
||||
else if(doc.containsKey("location") && doc["location"] != "")
|
||||
{
|
||||
Serial.println("Location Tag found!");
|
||||
String location = doc["location"].as<String>();
|
||||
if(spoolId != ""){
|
||||
updateSpoolLocation(spoolId, location);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("Location update tag scanned without scanning spool before!");
|
||||
oledShowMessage("No spool scanned before!");
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("Keine SPOOL-ID gefunden.");
|
||||
|
Reference in New Issue
Block a user