Merge remote-tracking branch 'origin/master'

This commit is contained in:
ST-TEN12 2025-02-10 19:02:23 +01:00
commit f0f07b2a41

View File

@ -85,5 +85,39 @@ class RFID_PN532(Component):
self.rfid_error_signal.emit(False) self.rfid_error_signal.emit(False)
self.mutex.unlock() self.mutex.unlock()
def write_tag(self,data): def write_tag(self, data):
self.data_to_write=copy.deepcopy(data) self.data_to_write = copy.deepcopy(data)
if not self.simulate:
if not self.connected:
self.open_device()
if not self.connected:
self.log.error("Failed to connect to device")
return
if self.mutex.tryLock():
try:
target = self.clf.sense(RemoteTarget('106A'), RemoteTarget('106B'), RemoteTarget('212F'))
if target is not None:
tag = src.lib.nfc.tag.activate(self.clf, target)
if tag is not None and tag.ndef is not None:
self.log.info("Detected NFC tag, writing data...")
ndef_record = ndef.TextRecord(self.data_to_write)
# IMPORTANT: DON'T CLOSE DEVICE BEFORE WRITING
tag.ndef.records = [ndef_record] # Write the record to the tag
self.log.info(f"Data '{self.data_to_write}' written successfully to tag.")
else:
self.log.error("Tag is not NDEF-compatible or invalid")
else:
self.log.debug("No tag present for writing")
except Exception as e:
self.log.error(f"Error during tag writing: {e}")
finally:
# Move closing outside the try block
self.mutex.unlock() # Unlock mutex always
# Close AFTER writing and mutex release
if self.connected:
self.close_device() # close_device can raise exception