From 6a9acb9d276739303a4188cc5c2b907ff8869218 Mon Sep 17 00:00:00 2001 From: Manuel Weiser Date: Thu, 28 May 2026 18:58:10 +0200 Subject: [PATCH] feat: implement scan request handling with timeout and error reporting --- src/main.cpp | 14 +++++++++++--- src/nfc.cpp | 43 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index a5806b0..e6dd013 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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; diff --git a/src/nfc.cpp b/src/nfc.cpp index e2befd2..e97ffa8 100644 --- a/src/nfc.cpp +++ b/src/nfc.cpp @@ -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() && 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(); - 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(); + lastSpoolId = activeSpoolId; + } } else if(doc["location_id"].is()) @@ -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) {