-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.py
More file actions
213 lines (188 loc) · 7.2 KB
/
Copy pathscanner.py
File metadata and controls
213 lines (188 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import subprocess
import threading
import time
from nfc_utils import canonicalize_nfc_uid
try:
from pynfc import Nfc, Timeout
except Exception as exc:
Nfc = None
class Timeout(Exception):
pass
NFC_IMPORT_ERROR = exc
else:
NFC_IMPORT_ERROR = None
def find_acr122u_usb_port():
try:
lsusb_output = subprocess.check_output(["lsusb"]).decode("utf-8", errors="ignore")
for line in lsusb_output.splitlines():
if "ACR122U" in line:
parts = line.split()
return parts[1]
except Exception as exc:
print(f"[USB] lsusb error: {exc}")
return None
class NFCStandbyReader:
def __init__(self, on_tap, debounce_seconds=1.0, cooldown_seconds=2.0):
self.on_tap = on_tap
self.debounce_seconds = debounce_seconds
self.cooldown_seconds = cooldown_seconds
self._lock = threading.Lock()
self._cond = threading.Condition(self._lock)
self._connected = False
self._last_error = None
self._device_string = None
self._tap_counter = 0
self._last_uid = None
self._last_uid_time = 0.0
self._last_tap_timestamp = None
self._last_message = None
self._last_log_id = None
self._last_payload = None
self._cooldown = {}
self._stop = threading.Event()
self._thread = threading.Thread(target=self._run, daemon=True)
def start(self):
if not self._thread.is_alive():
self._thread.start()
print("[NFC] Thread started")
def status(self):
with self._lock:
return {
"connected": self._connected,
"device": self._device_string,
"last_uid": self._last_uid,
"last_tap_timestamp": self._last_tap_timestamp,
"last_message": self._last_message,
"tap_counter": self._tap_counter,
"last_error": self._last_error,
"last_log_id": self._last_log_id,
"last_payload": self._last_payload,
}
def latest_tap(self, since_counter):
with self._lock:
if self._tap_counter == since_counter:
return None
return {
"tap_counter": self._tap_counter,
"uid": self._last_uid,
"tap_timestamp": self._last_tap_timestamp,
"message": self._last_message,
"log_id": self._last_log_id,
**(self._last_payload or {}),
}
def update_user_state(self, uid, checked_in):
with self._lock:
if self._last_uid != uid or not self._last_payload:
return
user = self._last_payload.get("user")
if isinstance(user, dict):
user["checked_in"] = bool(checked_in)
def cache_registered_user(self, uid, user):
"""Replace a just-registered card's stale unknown-card cache."""
with self._lock:
if self._last_uid != uid:
return False
safe_user = {
"firstname": user.get("firstname"),
"fullname": user.get("fullname"),
"student_no": user.get("student_no"),
"course": user.get("course"),
"checked_in": bool(user.get("checked_in", False)),
}
self._last_payload = {"user": safe_user}
self._last_message = f"{safe_user.get('fullname') or 'Student'} · ID registered"
self._cooldown.pop(uid, None)
return True
def _set_error(self, message):
with self._lock:
self._last_error = message
def _set_connected(self, connected, device=None):
with self._lock:
self._connected = connected
if device is not None:
self._device_string = device
def _publish_tap(self, uid, message, log_id=None, payload=None):
with self._cond:
self._last_uid = uid
self._last_uid_time = time.time()
self._last_tap_timestamp = time.time()
self._last_message = message
self._last_log_id = log_id
self._last_payload = payload
self._tap_counter += 1
self._cond.notify_all()
def _accept_tap(self, uid):
uid = canonicalize_nfc_uid(uid)
if not uid:
self._set_error("NFC reader returned an empty UID")
return
now = time.time()
with self._lock:
if uid == self._last_uid and (now - self._last_uid_time) < self.debounce_seconds:
return
last_logged = self._cooldown.get(uid, 0.0)
if (now - last_logged) < self.cooldown_seconds:
self._publish_tap(
uid,
self._last_message or f"NFC captured: {uid}",
payload=self._last_payload,
)
return
log_id = None
payload = None
try:
result = self.on_tap(uid)
if isinstance(result, dict):
message = result.get("message", "Tap recorded")
log_id = result.get("log_id")
payload = result.get("payload")
else:
message = result
self._cooldown[uid] = now
except Exception as exc:
message = f"Log failed: {exc}"
self._set_error(str(exc))
self._publish_tap(uid, message, log_id, payload)
def _run(self):
if Nfc is None:
self._set_error(f"pynfc is not installed/importable: {NFC_IMPORT_ERROR}")
print(f"[NFC] pynfc import failed: {NFC_IMPORT_ERROR}")
return
backoff = 1.0
while not self._stop.is_set():
port = find_acr122u_usb_port()
if not port:
self._set_connected(False, device=None)
self._set_error("ACR122U not found in lsusb")
time.sleep(1)
continue
device_candidates = [f"acr122_usb:{port}", "acr122_usb"]
self._set_connected(False, device=device_candidates[0])
try:
self._set_error(None)
nfc = None
last_open_error = None
for device in device_candidates:
try:
nfc = Nfc(device)
break
except Exception as exc:
last_open_error = exc
if nfc is None:
raise last_open_error or RuntimeError("Unable to open ACR122U")
self._set_connected(True, device=device)
backoff = 1.0
for target in nfc.poll():
if self._stop.is_set():
break
try:
self._accept_tap(target.uid)
except Timeout:
pass
except Exception as exc:
self._set_error(f"Read/decode error: {exc}")
except Exception as exc:
self._set_connected(False, device=device)
self._set_error(f"Open/poll failed: {exc}")
time.sleep(backoff)
backoff = min(backoff * 2, 10.0)