feat: implement scan request handling with timeout and error reporting

This commit is contained in:
Manuel Weiser committed 2026-05-28 18:58:10 +02:00
1 parent 9b93d219f9
commit 6a9acb9d27
2 files changed
+50 -7

No files matched your search

+11 -3
View File
@@ -199,9 +199,17 @@ void loop() {
mainTaskWasPaused = true;
}
// Während eines expliziten Scan-Requests darf kein regulärer Wiegevorgang
// an das Backend gesendet werden.
if (scanRequestActive) {
weightCounterToApi = 0;
weightSend = 0;
lastWeight = weight;
}
// Wenn Timer abgelaufen und nicht gerade ein RFID-Tag geschrieben wird
if (currentMillis - lastWeightReadTime >= weightReadInterval && nfcReaderState < NFC_WRITING)
if (!scanRequestActive && currentMillis - lastWeightReadTime >= weightReadInterval && nfcReaderState < NFC_WRITING)
{
lastWeightReadTime = currentMillis;
@@ -229,7 +237,7 @@ void loop() {
lastWeight = weight;
// Wenn ein Tag erkannt wurde und das Gewicht stabil ist (4+ seconds), an FilaMan senden
if (weightCounterToApi > 3 && weightSend == 0 && nfcReaderState == NFC_READ_SUCCESS && tagProcessed == false)
if (!scanRequestActive && weightCounterToApi > 3 && weightSend == 0 && nfcReaderState == NFC_READ_SUCCESS && tagProcessed == false)
{
tagProcessed = true;
@@ -251,7 +259,7 @@ void loop() {
}
// Handle successful tag write
if (nfcReaderState == NFC_WRITE_SUCCESS && tagProcessed == false)
if (!scanRequestActive && nfcReaderState == NFC_WRITE_SUCCESS && tagProcessed == false)
{
tagProcessed = true;
+39 -4
View File
@@ -28,9 +28,33 @@ volatile bool nfcReadingTaskSuspendRequest = false;
volatile bool nfcReadingTaskSuspendState = false;
volatile bool nfcWriteInProgress = false; // Prevent any tag operations during write
volatile bool scanRequestActive = false; // Set by website when frontend requests a tag scan
unsigned long scanRequestStartedAtMs = 0;
const unsigned long SCAN_REQUEST_TIMEOUT_MS = 30000;
void sendScanRequestErrorToBackend(const char* errorMessage) {
if (!scanRequestActive) {
return;
}
JsonDocument doc;
doc["scan_status"] = "error";
doc["error_message"] = errorMessage;
doc["source"] = "rfid_scan_request";
String payload;
serializeJson(doc, payload);
sendTagDataAsync(payload);
scanRequestActive = false;
scanRequestStartedAtMs = 0;
}
void setScanRequest(bool active) {
scanRequestActive = active;
if (active) {
scanRequestStartedAtMs = millis();
} else {
scanRequestStartedAtMs = 0;
}
}
volatile nfcReaderStateType nfcReaderState = NFC_IDLE;
@@ -1322,10 +1346,16 @@ bool decodeNdefAndReturnJson(const byte* encodedMessage, String uidString) {
Serial.println("JSON-Dokument erfolgreich verarbeitet");
if (doc["sm_id"].is<String>() && doc["sm_id"] != "" && doc["sm_id"] != "0")
{
oledShowProgressBar(2, 4, tr(STR_SPOOL_TAG), tr(STR_WEIGHING));
oledSetPriority(DISPLAY_PRIORITY_ACTION, 2000);
activeSpoolId = doc["sm_id"].as<String>();
lastSpoolId = activeSpoolId;
if (scanRequestActive) {
scanRequestActive = false;
sendTagDataAsync(nfcJsonData);
tagProcessed = true;
} else {
oledShowProgressBar(2, 4, tr(STR_SPOOL_TAG), tr(STR_WEIGHING));
oledSetPriority(DISPLAY_PRIORITY_ACTION, 2000);
activeSpoolId = doc["sm_id"].as<String>();
lastSpoolId = activeSpoolId;
}
}
else if(doc["location_id"].is<int>())
@@ -2055,6 +2085,11 @@ void scanRfidTask(void * parameter) {
esp_task_wdt_reset();
yield();
if (scanRequestActive && scanRequestStartedAtMs > 0 && (millis() - scanRequestStartedAtMs) > SCAN_REQUEST_TIMEOUT_MS) {
Serial.println("RFID scan-request timeout reached without tag");
sendScanRequestErrorToBackend("Timeout - no tag found");
}
// Skip scanning during write operations, but keep NFC interface active
if (nfcReaderState != NFC_WRITING && !nfcWriteInProgress && !nfcReadingTaskSuspendRequest && !booting)
{