Compare commits
2 Commits
v2.0.0-bet
...
5cc58927a6
| Author | SHA1 | Date | |
|---|---|---|---|
| 5cc58927a6 | |||
| afde3f5f81 |
101
src/api.cpp
101
src/api.cpp
@@ -126,26 +126,67 @@ void sendToApi(void *parameter) {
|
|||||||
String spoolIdForWeight = params->spoolIdForWeight;
|
String spoolIdForWeight = params->spoolIdForWeight;
|
||||||
uint16_t weightValue = params->weightValue;
|
uint16_t weightValue = params->weightValue;
|
||||||
|
|
||||||
HTTPClient http;
|
// Retry mechanism with configurable parameters
|
||||||
http.setReuse(false);
|
const uint8_t MAX_RETRIES = 3;
|
||||||
|
const uint16_t RETRY_DELAY_MS = 1000; // 1 second between retries
|
||||||
|
const uint16_t HTTP_TIMEOUT_MS = 10000; // 10 second HTTP timeout
|
||||||
|
|
||||||
http.begin(spoolsUrl);
|
bool success = false;
|
||||||
http.addHeader("Content-Type", "application/json");
|
int httpCode = -1;
|
||||||
if (octoEnabled && octoToken != "") http.addHeader("X-Api-Key", octoToken);
|
String responsePayload = "";
|
||||||
|
|
||||||
int httpCode;
|
// Try request with retries
|
||||||
if (httpType == "PATCH") httpCode = http.PATCH(updatePayload);
|
for (uint8_t attempt = 1; attempt <= MAX_RETRIES && !success; attempt++) {
|
||||||
else if (httpType == "POST") httpCode = http.POST(updatePayload);
|
Serial.printf("API Request attempt %d/%d to: %s\n", attempt, MAX_RETRIES, spoolsUrl.c_str());
|
||||||
else if (httpType == "GET") httpCode = http.GET();
|
|
||||||
else httpCode = http.PUT(updatePayload);
|
|
||||||
|
|
||||||
if (httpCode == HTTP_CODE_OK) {
|
HTTPClient http;
|
||||||
|
http.setReuse(false);
|
||||||
|
http.setTimeout(HTTP_TIMEOUT_MS); // Set HTTP timeout
|
||||||
|
|
||||||
|
http.begin(spoolsUrl);
|
||||||
|
http.addHeader("Content-Type", "application/json");
|
||||||
|
if (octoEnabled && octoToken != "") http.addHeader("X-Api-Key", octoToken);
|
||||||
|
|
||||||
|
// Execute HTTP request based on type
|
||||||
|
if (httpType == "PATCH") httpCode = http.PATCH(updatePayload);
|
||||||
|
else if (httpType == "POST") httpCode = http.POST(updatePayload);
|
||||||
|
else if (httpType == "GET") httpCode = http.GET();
|
||||||
|
else httpCode = http.PUT(updatePayload);
|
||||||
|
|
||||||
|
// Check if request was successful
|
||||||
|
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_CREATED) {
|
||||||
|
responsePayload = http.getString();
|
||||||
|
success = true;
|
||||||
|
Serial.printf("API Request successful on attempt %d, HTTP Code: %d\n", attempt, httpCode);
|
||||||
|
} else {
|
||||||
|
Serial.printf("API Request failed on attempt %d, HTTP Code: %d (%s)\n",
|
||||||
|
attempt, httpCode, http.errorToString(httpCode).c_str());
|
||||||
|
|
||||||
|
// Don't retry on certain error codes (client errors)
|
||||||
|
if (httpCode >= 400 && httpCode < 500 && httpCode != 408 && httpCode != 429) {
|
||||||
|
Serial.println("Client error detected, stopping retries");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait before retry (except on last attempt)
|
||||||
|
if (attempt < MAX_RETRIES) {
|
||||||
|
Serial.printf("Waiting %dms before retry...\n", RETRY_DELAY_MS);
|
||||||
|
http.end();
|
||||||
|
vTaskDelay(RETRY_DELAY_MS / portTICK_PERIOD_MS);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
http.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process successful response
|
||||||
|
if (success) {
|
||||||
Serial.println("Spoolman Abfrage erfolgreich");
|
Serial.println("Spoolman Abfrage erfolgreich");
|
||||||
|
|
||||||
// Restgewicht der Spule auslesen
|
// Restgewicht der Spule auslesen
|
||||||
String payload = http.getString();
|
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
DeserializationError error = deserializeJson(doc, payload);
|
DeserializationError error = deserializeJson(doc, responsePayload);
|
||||||
if (error) {
|
if (error) {
|
||||||
Serial.print("Fehler beim Parsen der JSON-Antwort: ");
|
Serial.print("Fehler beim Parsen der JSON-Antwort: ");
|
||||||
Serial.println(error.c_str());
|
Serial.println(error.c_str());
|
||||||
@@ -226,9 +267,8 @@ void sendToApi(void *parameter) {
|
|||||||
Serial.println("Spoolman erfolgreich erstellt");
|
Serial.println("Spoolman erfolgreich erstellt");
|
||||||
|
|
||||||
// Parse response for created resources
|
// Parse response for created resources
|
||||||
String payload = http.getString();
|
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
DeserializationError error = deserializeJson(doc, payload);
|
DeserializationError error = deserializeJson(doc, responsePayload);
|
||||||
if (error) {
|
if (error) {
|
||||||
Serial.print("Fehler beim Parsen der JSON-Antwort: ");
|
Serial.print("Fehler beim Parsen der JSON-Antwort: ");
|
||||||
Serial.println(error.c_str());
|
Serial.println(error.c_str());
|
||||||
@@ -280,14 +320,17 @@ void sendToApi(void *parameter) {
|
|||||||
Serial.println(weightPayload);
|
Serial.println(weightPayload);
|
||||||
|
|
||||||
// Execute weight update
|
// Execute weight update
|
||||||
http.begin(weightUrl);
|
HTTPClient weightHttp;
|
||||||
http.addHeader("Content-Type", "application/json");
|
weightHttp.setReuse(false);
|
||||||
|
weightHttp.setTimeout(HTTP_TIMEOUT_MS);
|
||||||
|
weightHttp.begin(weightUrl);
|
||||||
|
weightHttp.addHeader("Content-Type", "application/json");
|
||||||
|
|
||||||
int weightHttpCode = http.PUT(weightPayload);
|
int weightHttpCode = weightHttp.PUT(weightPayload);
|
||||||
|
|
||||||
if (weightHttpCode == HTTP_CODE_OK) {
|
if (weightHttpCode == HTTP_CODE_OK) {
|
||||||
Serial.println("Weight update successful");
|
Serial.println("Weight update successful");
|
||||||
String weightResponse = http.getString();
|
String weightResponse = weightHttp.getString();
|
||||||
JsonDocument weightResponseDoc;
|
JsonDocument weightResponseDoc;
|
||||||
DeserializationError weightError = deserializeJson(weightResponseDoc, weightResponse);
|
DeserializationError weightError = deserializeJson(weightResponseDoc, weightResponse);
|
||||||
|
|
||||||
@@ -310,6 +353,7 @@ void sendToApi(void *parameter) {
|
|||||||
oledShowProgressBar(1, 1, "Failure!", "Weight update");
|
oledShowProgressBar(1, 1, "Failure!", "Weight update");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
weightHttp.end();
|
||||||
weightDoc.clear();
|
weightDoc.clear();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -325,14 +369,25 @@ void sendToApi(void *parameter) {
|
|||||||
case API_REQUEST_BAMBU_UPDATE:
|
case API_REQUEST_BAMBU_UPDATE:
|
||||||
oledShowProgressBar(1, 1, "Failure!", "Bambu update");
|
oledShowProgressBar(1, 1, "Failure!", "Bambu update");
|
||||||
break;
|
break;
|
||||||
|
case API_REQUEST_VENDOR_CHECK:
|
||||||
|
oledShowProgressBar(1, 1, "Failure!", "Vendor check");
|
||||||
|
foundVendorId = 0; // Set to 0 to indicate error/not found
|
||||||
|
break;
|
||||||
case API_REQUEST_VENDOR_CREATE:
|
case API_REQUEST_VENDOR_CREATE:
|
||||||
oledShowProgressBar(1, 1, "Failure!", "Vendor create");
|
oledShowProgressBar(1, 1, "Failure!", "Vendor create");
|
||||||
|
createdVendorId = 0; // Set to 0 to indicate error
|
||||||
|
break;
|
||||||
|
case API_REQUEST_FILAMENT_CHECK:
|
||||||
|
oledShowProgressBar(1, 1, "Failure!", "Filament check");
|
||||||
|
foundFilamentId = 0; // Set to 0 to indicate error/not found
|
||||||
break;
|
break;
|
||||||
case API_REQUEST_FILAMENT_CREATE:
|
case API_REQUEST_FILAMENT_CREATE:
|
||||||
oledShowProgressBar(1, 1, "Failure!", "Filament create");
|
oledShowProgressBar(1, 1, "Failure!", "Filament create");
|
||||||
|
createdFilamentId = 0; // Set to 0 to indicate error
|
||||||
break;
|
break;
|
||||||
case API_REQUEST_SPOOL_CREATE:
|
case API_REQUEST_SPOOL_CREATE:
|
||||||
oledShowProgressBar(1, 1, "Failure!", "Spool create");
|
oledShowProgressBar(1, 1, "Failure!", "Spool create");
|
||||||
|
createdSpoolId = 0; // Set to 0 to indicate error instead of hanging
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Serial.println("Fehler beim Senden an Spoolman! HTTP Code: " + String(httpCode));
|
Serial.println("Fehler beim Senden an Spoolman! HTTP Code: " + String(httpCode));
|
||||||
@@ -340,7 +395,6 @@ void sendToApi(void *parameter) {
|
|||||||
nfcReaderState = NFC_IDLE; // Reset NFC state to allow retry
|
nfcReaderState = NFC_IDLE; // Reset NFC state to allow retry
|
||||||
}
|
}
|
||||||
|
|
||||||
http.end();
|
|
||||||
vTaskDelay(50 / portTICK_PERIOD_MS);
|
vTaskDelay(50 / portTICK_PERIOD_MS);
|
||||||
|
|
||||||
// Speicher freigeben
|
// Speicher freigeben
|
||||||
@@ -953,6 +1007,13 @@ uint16_t createSpool(uint16_t vendorId, uint16_t filamentId, JsonDocument& paylo
|
|||||||
vTaskDelay(50 / portTICK_PERIOD_MS);
|
vTaskDelay(50 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if spool creation was successful
|
||||||
|
if (createdSpoolId == 0) {
|
||||||
|
Serial.println("ERROR: Spool creation failed");
|
||||||
|
nfcReaderState = NFC_IDLE; // Reset NFC state
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Write data to tag with startWriteJsonToTag
|
// Write data to tag with startWriteJsonToTag
|
||||||
// void startWriteJsonToTag(const bool isSpoolTag, const char* payload);
|
// void startWriteJsonToTag(const bool isSpoolTag, const char* payload);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user