-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnfc.py
192 lines (170 loc) · 7.08 KB
/
nfc.py
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
from pyscan import Pyscan
from MFRC630 import MFRC630
from machine import Pin, PWM
import time
import config
import _thread
import struct
import json
buzzer = Pin(config.BUZZER)
tim = PWM(0, frequency=300)
ch = tim.channel(2, duty_cycle=0, pin=buzzer)
in_jingle = [2430, 2637, 0]
out_jingle = [2637, 2430, 0]
err_jingle = [500, 0]
filename = '/flash/devices.txt'
def write_file(data, filename):
with open(filename, 'wt') as f:
f.write(json.dumps(data))
def read_file(filename):
with open(filename, 'r') as f:
data = json.loads(f.read())
return data
def add_card(uid, filename, lora_socket):
print("add card to dict")
device = str(uid).replace("'", "").replace("\\x", "").replace("bytearray(b", "").replace(")", "")
print("device:", device)
try:
checkedin_devices_dict = read_file(filename)
except Exception as e:
print("error:", e)
checkedin_devices_dict = {}
print("checkedin_devices_dict:", checkedin_devices_dict)
checkedin_devices_dict[device] = time.time()
write_file(checkedin_devices_dict, filename)
amount = len(checkedin_devices_dict)
print("amount:", amount)
lora_socket.send(uid + struct.pack("!h", amount))
print("{} checking in.".format(device))
print("checkedin_devices_dict:", checkedin_devices_dict)
def delete_card(uid, filename, lora_socket):
device = str(uid).replace("'", "").replace("\\x", "").replace("bytearray(b", "").replace(")", "")
print("delete card:", device)
try:
checkedin_devices_dict = read_file(filename)
except Exception as e:
print("error:", e)
checkedin_devices_dict = {}
checkedin_devices_dict.pop(device, None)
write_file(checkedin_devices_dict, filename)
print("{} deleted".format(device))
def checkout_card(uid, filename, lora_socket):
device = str(uid).replace("'", "").replace("\\x", "").replace("bytearray(b", "").replace(")", "")
print(checkout_card)
try:
checkedin_devices_dict = read_file(filename)
except Exception as e:
print("error:", e)
checkedin_devices_dict = {}
try:
time_diff = time.time() - checkedin_devices_dict[device]
amount = len(checkedin_devices_dict) - 1
print("amount:", amount)
lora_socket.send(uid + struct.pack("!h", amount) + struct.pack("!h", time_diff))
print("sending:", struct.pack("!I", time_diff))
except Exception as e:
print(e)
delete_card(uid, filename, lora_socket)
print("len dict:", len(checkedin_devices_dict))
print("{} checking out after: {} s".format(device, time_diff))
def check_for_orphaned_cards(filename):
print("check for orphaned cards")
try:
print("loading dict")
checkedin_devices_dict = read_file(filename)
except Exception as e:
print("loading dict error:", e)
checkedin_devices_dict = {}
for device in checkedin_devices_dict.keys():
try:
print("calc time_diff")
time_diff = time.time() - checkedin_devices_dict[device]
if time_diff > 32400:
# delete device from dict
print("removing", device)
checkedin_devices_dict.pop(device, None)
write_file(checkedin_devices_dict, filename)
print("{} deleted".format(device))
except Exception as e:
print("removing device error", e)
def play_jingle(jingle):
for i in jingle:
if i == 0:
ch.duty_cycle(0)
else:
tim = PWM(0, frequency=i) # change frequency for change tone
ch.duty_cycle(0.5)
time.sleep(0.2)
def discovery_loop(nfc, lora_socket):
try:
checkedin_devices_dict = read_file(filename)
print("file loading ok.")
except Exception as e:
print("error:", e)
checkedin_devices_dict = {}
print("Start up @{} s.".format(time.time()))
amount = len(checkedin_devices_dict)
lora_socket.send(struct.pack("!I", time.time()) + struct.pack("!h", amount))
while True:
# send a hearbeat message every hour (3600s)
if time.time() % 3600 == 0:
try:
check_for_orphaned_cards(filename)
checkedin_devices_dict = read_file(filename)
print("file loading ok.")
print("test:", checkedin_devices_dict)
except Exception as e:
print("error:", e)
checkedin_devices_dict = {}
print("Heartbeat @{} s.".format(time.time()))
amount = len(checkedin_devices_dict)
print("Heartbeat @{} s w/ amount of {}.".format(time.time(), amount))
lora_socket.send(struct.pack("!I", time.time()) + struct.pack("!h", amount))
time.sleep(2)
# Send REQA for ISO14443A card type
atqa = nfc.mfrc630_iso14443a_WUPA_REQA(nfc.MFRC630_ISO14443_CMD_REQA)
if (atqa != 0):
# A card has been detected, read uid and try to authenticate
uid = bytearray(10)
nfc.mfrc630_cmd_load_key(config.MF_AUTH_KEY_A)
uid_len = nfc.mfrc630_iso14443a_select(uid)
if (uid_len > 0) and (nfc.mfrc630_MF_auth(uid, nfc.MFRC630_MF_AUTH_KEY_A, 0)):
print("UID [%d]: %s, ts: %d s" % (uid_len, nfc.format_block(uid, uid_len), time.time()))
device = str(uid).replace("'", "").replace("\\x", "").replace("bytearray(b", "").replace(")", "")
try:
checkedin_devices_dict = read_file(filename)
except Exception as e:
print("error:", e)
checkedin_devices_dict = {}
if len(checkedin_devices_dict) > 0:
# in case the dict is not empty search for device
if device in checkedin_devices_dict.keys():
print("len dict:", len(checkedin_devices_dict))
time_diff = time.time() - checkedin_devices_dict[device]
if time_diff < 20:
print("Same card presented twice.")
play_jingle(err_jingle)
else:
# check out device and transmit duration
checkout_card(uid, filename, lora_socket)
play_jingle(out_jingle)
else:
# otherwise add it to the dict
add_card(uid, filename, lora_socket)
play_jingle(in_jingle)
else:
# otherwise add it to the dict
add_card(uid, filename, lora_socket)
play_jingle(in_jingle)
nfc.mfrc630_MF_deauth()
nfc.mfrc630_cmd_reset()
time.sleep(.5)
nfc.mfrc630_cmd_init()
def start_thread(lora_socket):
# Setup Pyscan
py = Pyscan()
# Setup NFC reader
nfc = MFRC630(py)
# Initialise the MFRC630 with some settings
nfc.mfrc630_cmd_init()
_thread.start_new_thread(discovery_loop, (nfc, lora_socket))