Further improvements on NFC writing
Fixes some issues related to tag writing. Allos writing of tags that are already on the scale when pressing the write button, but introduces a confirmation dialog before doing so. Also first test to fix reset issue when trying to write tags.
This commit is contained in:
		
							
								
								
									
										133
									
								
								html/rfid.js
									
									
									
									
									
								
							
							
						
						
									
										133
									
								
								html/rfid.js
									
									
									
									
									
								
							| @@ -7,6 +7,7 @@ let heartbeatTimer = null; | ||||
| let lastHeartbeatResponse = Date.now(); | ||||
| const HEARTBEAT_TIMEOUT = 20000; | ||||
| let reconnectTimer = null; | ||||
| let spoolDetected = false; | ||||
|  | ||||
| // WebSocket Funktionen | ||||
| function startHeartbeat() { | ||||
| @@ -508,12 +509,15 @@ function updateNfcStatusIndicator(data) { | ||||
|     if (data.found === 0) { | ||||
|         // Kein NFC Tag gefunden | ||||
|         indicator.className = 'status-circle'; | ||||
|         spoolDetected = false; | ||||
|     } else if (data.found === 1) { | ||||
|         // NFC Tag erfolgreich gelesen | ||||
|         indicator.className = 'status-circle success'; | ||||
|         spoolDetected = true; | ||||
|     } else { | ||||
|         // Fehler beim Lesen | ||||
|         indicator.className = 'status-circle error'; | ||||
|         spoolDetected = true; | ||||
|     } | ||||
| } | ||||
|  | ||||
| @@ -618,78 +622,83 @@ function updateNfcData(data) { | ||||
| } | ||||
|  | ||||
| function writeNfcTag() { | ||||
|     const selectedText = document.getElementById("selected-filament").textContent; | ||||
|     if (selectedText === "Please choose...") { | ||||
|         alert('Please select a Spool first.'); | ||||
|         return; | ||||
|     } | ||||
|     if(!spoolDetected || confirm("Are you sure you want to overwrite the Tag?") == true){ | ||||
|         const selectedText = document.getElementById("selected-filament").textContent; | ||||
|         if (selectedText === "Please choose...") { | ||||
|             alert('Please select a Spool first.'); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|     const spoolsData = window.getSpoolData(); | ||||
|     const selectedSpool = spoolsData.find(spool =>  | ||||
|         `${spool.id} | ${spool.filament.name} (${spool.filament.material})` === selectedText | ||||
|     ); | ||||
|         const spoolsData = window.getSpoolData(); | ||||
|         const selectedSpool = spoolsData.find(spool =>  | ||||
|             `${spool.id} | ${spool.filament.name} (${spool.filament.material})` === selectedText | ||||
|         ); | ||||
|  | ||||
|     if (!selectedSpool) { | ||||
|         alert('Ausgewählte Spule konnte nicht gefunden werden.'); | ||||
|         return; | ||||
|     } | ||||
|         if (!selectedSpool) { | ||||
|             alert('Ausgewählte Spule konnte nicht gefunden werden.'); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|     // Temperaturwerte korrekt extrahieren | ||||
|     let minTemp = "175"; | ||||
|     let maxTemp = "275"; | ||||
|      | ||||
|     if (Array.isArray(selectedSpool.filament.nozzle_temperature) &&  | ||||
|         selectedSpool.filament.nozzle_temperature.length >= 2) { | ||||
|         minTemp = String(selectedSpool.filament.nozzle_temperature[0]); | ||||
|         maxTemp = String(selectedSpool.filament.nozzle_temperature[1]); | ||||
|     } | ||||
|         // Temperaturwerte korrekt extrahieren | ||||
|         let minTemp = "175"; | ||||
|         let maxTemp = "275"; | ||||
|          | ||||
|         if (Array.isArray(selectedSpool.filament.nozzle_temperature) &&  | ||||
|             selectedSpool.filament.nozzle_temperature.length >= 2) { | ||||
|             minTemp = String(selectedSpool.filament.nozzle_temperature[0]); | ||||
|             maxTemp = String(selectedSpool.filament.nozzle_temperature[1]); | ||||
|         } | ||||
|  | ||||
|     // Erstelle das NFC-Datenpaket mit korrekten Datentypen | ||||
|     const nfcData = { | ||||
|         color_hex: selectedSpool.filament.color_hex || "FFFFFF", | ||||
|         type: selectedSpool.filament.material, | ||||
|         min_temp: minTemp, | ||||
|         max_temp: maxTemp, | ||||
|         brand: selectedSpool.filament.vendor.name, | ||||
|         sm_id: String(selectedSpool.id) // Konvertiere zu String | ||||
|     }; | ||||
|         // Erstelle das NFC-Datenpaket mit korrekten Datentypen | ||||
|         const nfcData = { | ||||
|             color_hex: selectedSpool.filament.color_hex || "FFFFFF", | ||||
|             type: selectedSpool.filament.material, | ||||
|             min_temp: minTemp, | ||||
|             max_temp: maxTemp, | ||||
|             brand: selectedSpool.filament.vendor.name, | ||||
|             sm_id: String(selectedSpool.id) // Konvertiere zu String | ||||
|         }; | ||||
|  | ||||
|     if (socket?.readyState === WebSocket.OPEN) { | ||||
|         const writeButton = document.getElementById("writeNfcButton"); | ||||
|         writeButton.classList.add("writing"); | ||||
|         writeButton.textContent = "Writing"; | ||||
|         socket.send(JSON.stringify({ | ||||
|             type: 'writeNfcTag', | ||||
|             tagType: 'spool', | ||||
|             payload: nfcData | ||||
|         })); | ||||
|     } else { | ||||
|         alert('Not connected to Server. Please check connection.'); | ||||
|         if (socket?.readyState === WebSocket.OPEN) { | ||||
|             const writeButton = document.getElementById("writeNfcButton"); | ||||
|             writeButton.classList.add("writing"); | ||||
|             writeButton.textContent = "Writing"; | ||||
|             socket.send(JSON.stringify({ | ||||
|                 type: 'writeNfcTag', | ||||
|                 tagType: 'spool', | ||||
|                 payload: nfcData | ||||
|             })); | ||||
|         } else { | ||||
|             alert('Not connected to Server. Please check connection.'); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| function writeLocationNfcTag() { | ||||
|     const selectedText = document.getElementById("locationSelect").value; | ||||
|     if (selectedText === "Please choose...") { | ||||
|         alert('Please select a location first.'); | ||||
|         return; | ||||
|     } | ||||
|     // Erstelle das NFC-Datenpaket mit korrekten Datentypen | ||||
|     const nfcData = { | ||||
|         location: String(selectedText) | ||||
|     }; | ||||
|     if(!spoolDetected || confirm("Are you sure you want to overwrite the Tag?") == true){ | ||||
|         const selectedText = document.getElementById("locationSelect").value; | ||||
|         if (selectedText === "Please choose...") { | ||||
|             alert('Please select a location first.'); | ||||
|             return; | ||||
|         } | ||||
|         // Erstelle das NFC-Datenpaket mit korrekten Datentypen | ||||
|         const nfcData = { | ||||
|             location: String(selectedText) | ||||
|         }; | ||||
|  | ||||
|     if (socket?.readyState === WebSocket.OPEN) { | ||||
|         const writeButton = document.getElementById("writeLocationNfcButton"); | ||||
|         writeButton.classList.add("writing"); | ||||
|         writeButton.textContent = "Writing"; | ||||
|         socket.send(JSON.stringify({ | ||||
|             type: 'writeNfcTag', | ||||
|             tagType: 'location', | ||||
|             payload: nfcData | ||||
|         })); | ||||
|     } else { | ||||
|         alert('Not connected to Server. Please check connection.'); | ||||
|  | ||||
|         if (socket?.readyState === WebSocket.OPEN) { | ||||
|             const writeButton = document.getElementById("writeLocationNfcButton"); | ||||
|             writeButton.classList.add("writing"); | ||||
|             writeButton.textContent = "Writing"; | ||||
|             socket.send(JSON.stringify({ | ||||
|                 type: 'writeNfcTag', | ||||
|                 tagType: 'location', | ||||
|                 payload: nfcData | ||||
|             })); | ||||
|         } else { | ||||
|             alert('Not connected to Server. Please check connection.'); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
							
								
								
									
										28
									
								
								src/nfc.cpp
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								src/nfc.cpp
									
									
									
									
									
								
							| @@ -20,6 +20,7 @@ String lastSpoolId = ""; | ||||
| String nfcJsonData = ""; | ||||
| bool tagProcessed = false; | ||||
| volatile bool pauseBambuMqttTask = false; | ||||
| volatile bool suspendNfcReading = false; | ||||
|  | ||||
| struct NfcWriteParameterType { | ||||
|   bool tagType; | ||||
| @@ -278,8 +279,10 @@ void writeJsonToTag(void *parameter) { | ||||
|   Serial.println(params->payload); | ||||
|  | ||||
|   nfcReaderState = NFC_WRITING; | ||||
|   vTaskSuspend(RfidReaderTask); | ||||
|   vTaskDelay(50 / portTICK_PERIOD_MS); | ||||
|   suspendNfcReading = true; | ||||
|   //vTaskSuspend(RfidReaderTask); | ||||
|   // make sure to wait 600ms, after that the reading task should be waiting | ||||
|   vTaskDelay(1000 / portTICK_PERIOD_MS); | ||||
|  | ||||
|   //pauseBambuMqttTask = true; | ||||
|   // aktualisieren der Website wenn sich der Status ändert | ||||
| @@ -372,7 +375,8 @@ void writeJsonToTag(void *parameter) { | ||||
|   sendWriteResult(nullptr, success); | ||||
|   sendNfcData(); | ||||
|  | ||||
|   vTaskResume(RfidReaderTask); | ||||
|   suspendNfcReading = false; | ||||
|   //vTaskResume(RfidReaderTask); | ||||
|   pauseBambuMqttTask = false; | ||||
|  | ||||
|   vTaskDelete(NULL); | ||||
| @@ -384,7 +388,7 @@ void startWriteJsonToTag(const bool isSpoolTag, const char* payload) { | ||||
|   parameters->payload = strdup(payload); | ||||
|    | ||||
|   // Task nicht mehrfach starten | ||||
|   if (nfcReaderState == NFC_IDLE) { | ||||
|   if (nfcReaderState == NFC_IDLE || nfcReaderState == NFC_READ_ERROR || nfcReaderState == NFC_READ_SUCCESS) { | ||||
|     oledShowProgressBar(0, 1, "Write Tag", "Place tag now"); | ||||
|     // Erstelle die Task | ||||
|     xTaskCreate( | ||||
| @@ -405,7 +409,7 @@ void scanRfidTask(void * parameter) { | ||||
|   Serial.println("RFID Task gestartet"); | ||||
|   for(;;) { | ||||
|     // Wenn geschrieben wird Schleife aussetzen | ||||
|     if (nfcReaderState != NFC_WRITING) | ||||
|     if (nfcReaderState != NFC_WRITING && !suspendNfcReading) | ||||
|     { | ||||
|       yield(); | ||||
|  | ||||
| @@ -413,7 +417,7 @@ void scanRfidTask(void * parameter) { | ||||
|       uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID | ||||
|       uint8_t uidLength; | ||||
|  | ||||
|       success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 1000); | ||||
|       success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 500); | ||||
|  | ||||
|       foundNfcTag(nullptr, success); | ||||
|        | ||||
| @@ -430,8 +434,8 @@ void scanRfidTask(void * parameter) { | ||||
|  | ||||
|         oledShowProgressBar(0, octoEnabled?5:4, "Reading", "Detecting tag"); | ||||
|  | ||||
|         vTaskDelay(500 / portTICK_PERIOD_MS); | ||||
|  | ||||
|         //vTaskDelay(500 / portTICK_PERIOD_MS); | ||||
|          | ||||
|         if (uidLength == 7) | ||||
|         { | ||||
|           uint16_t tagSize = readTagSize(); | ||||
| @@ -487,7 +491,7 @@ void scanRfidTask(void * parameter) { | ||||
|         } | ||||
|       } | ||||
|  | ||||
|       if (!success && nfcReaderState != NFC_IDLE) | ||||
|       if (!success && nfcReaderState != NFC_IDLE && !suspendNfcReading) | ||||
|       { | ||||
|         nfcReaderState = NFC_IDLE; | ||||
|         //uidString = ""; | ||||
| @@ -500,6 +504,12 @@ void scanRfidTask(void * parameter) { | ||||
|       // aktualisieren der Website wenn sich der Status ändert | ||||
|       sendNfcData(); | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|       // TBD: Debug only: | ||||
|       Serial.println("NFC Reading disabled"); | ||||
|       vTaskDelay(1000 / portTICK_PERIOD_MS); | ||||
|     } | ||||
|     yield(); | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user