2023-07-12 17:37:57 +00:00
|
|
|
import copy
|
2023-06-23 08:20:26 +00:00
|
|
|
import ctypes
|
|
|
|
|
import sys
|
|
|
|
|
import platform
|
|
|
|
|
from PyQt5.QtCore import QMutex, Qt, QTimer, pyqtSlot, pyqtSignal
|
|
|
|
|
from .component import Component
|
2023-06-23 17:25:41 +00:00
|
|
|
import ndef
|
2024-12-24 11:30:17 +00:00
|
|
|
import src.lib.nfc
|
|
|
|
|
from src.lib.nfc.clf import RemoteTarget
|
2023-06-23 08:20:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class RFID_PN532(Component):
|
2023-06-23 17:25:41 +00:00
|
|
|
new_id_signal = pyqtSignal(str)
|
2024-01-16 18:12:06 +00:00
|
|
|
|
2023-06-23 08:20:26 +00:00
|
|
|
def __init__(self, config=None, name=None, period=1, lazy=True, paused=False, threaded=True):
|
|
|
|
|
super().__init__(config=config, name=name, period=period, lazy=lazy, paused=paused, threaded=threaded)
|
2023-07-12 17:37:57 +00:00
|
|
|
self.data_to_write = None
|
2023-06-23 08:20:26 +00:00
|
|
|
self.mutex = QMutex()
|
|
|
|
|
self.simulate="--sim-rfid" in sys.argv
|
|
|
|
|
self.clf = None
|
|
|
|
|
self.connected=False
|
|
|
|
|
self.tag_present=False
|
|
|
|
|
self.current_data=None
|
2023-07-12 17:37:57 +00:00
|
|
|
self.is_win = platform.system().lower() == "windows"
|
2023-07-25 19:18:16 +00:00
|
|
|
self.dev_list = [f"{'com'if self.is_win else 'tty'}:{self.config['fixture_rfid']['port']}:pn532"]
|
2023-06-23 17:25:41 +00:00
|
|
|
self._period = 1
|
2023-06-23 08:20:26 +00:00
|
|
|
|
|
|
|
|
def open_device(self):
|
2024-12-24 11:30:17 +00:00
|
|
|
self.clf = src.lib.nfc.ContactlessFrontend()
|
2023-06-23 08:20:26 +00:00
|
|
|
for dev in self.dev_list:
|
2023-06-23 17:25:41 +00:00
|
|
|
self.connected = self.clf.open(dev)
|
|
|
|
|
if self.connected:
|
2023-06-23 08:20:26 +00:00
|
|
|
self.log.info(f"CONNECTED TO {dev}")
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
self.log.info(f"UNABLE TO CONNECT TO {dev}")
|
|
|
|
|
|
|
|
|
|
def close_device(self):
|
|
|
|
|
self.clf.close()
|
|
|
|
|
|
|
|
|
|
@pyqtSlot()
|
|
|
|
|
def start(self):
|
|
|
|
|
super().start()
|
|
|
|
|
|
|
|
|
|
@pyqtSlot()
|
2023-06-23 17:25:41 +00:00
|
|
|
def _get(self):
|
2023-08-24 10:10:01 +00:00
|
|
|
if not self.simulate:
|
|
|
|
|
if self.mutex.tryLock():
|
|
|
|
|
try:
|
|
|
|
|
if not self.connected:
|
|
|
|
|
self.open_device()
|
2023-07-12 17:37:57 +00:00
|
|
|
else:
|
2023-08-24 10:10:01 +00:00
|
|
|
target = self.clf.sense(RemoteTarget('106A'), RemoteTarget('106B'), RemoteTarget('212F'))
|
|
|
|
|
if target is not None:
|
2024-12-24 11:30:17 +00:00
|
|
|
tag = src.lib.nfc.tag.activate(self.clf, target)
|
2023-08-24 10:10:01 +00:00
|
|
|
if tag is not None:
|
|
|
|
|
self.log.debug("tag present")
|
|
|
|
|
if tag.ndef is not None:
|
|
|
|
|
tag_content=tag.ndef.records[0].text
|
|
|
|
|
if tag_content!=self.current_data:
|
|
|
|
|
self.log.info(f"new tag detected:{tag_content}")
|
|
|
|
|
self.current_data=tag_content
|
|
|
|
|
self.new_id_signal.emit(self.current_data)
|
|
|
|
|
else:
|
|
|
|
|
self.log.error("tag is not NDEF")
|
|
|
|
|
else:
|
|
|
|
|
if self.current_data:
|
|
|
|
|
self.log.info(f"tag removed:{self.current_data}")
|
|
|
|
|
self.current_data = None
|
|
|
|
|
self.new_id_signal.emit(None)
|
|
|
|
|
self.log.debug("no target present")
|
2023-07-12 17:37:57 +00:00
|
|
|
|
2023-08-24 10:10:01 +00:00
|
|
|
except Exception as e:
|
|
|
|
|
self.log.info(f"{e}")
|
|
|
|
|
self.connected = False
|
|
|
|
|
finally:
|
|
|
|
|
self.mutex.unlock()
|
2023-06-23 08:20:26 +00:00
|
|
|
|
2023-07-12 17:37:57 +00:00
|
|
|
def write_tag(self,data):
|
|
|
|
|
self.data_to_write=copy.deepcopy(data)
|