forked from bkbilly/mqtt_fingerprint_pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
340 lines (304 loc) · 12.4 KB
/
run.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import os
import time
import serial
import adafruit_fingerprint
import paho.mqtt.client as mqtt
import threading
import json
import yaml
class Fingerprint():
"""docstring for Fingerprint"""
LEDCOLOR_RED = 1
LEDCOLOR_BLUE = 2
LEDCOLOR_PURPLE = 3
LEDMODE_BREATH = 1
LEDMODE_BLINK = 2
LEDMODE_STILL = 3
LEDMODE_OFF = 4
def __init__(self, myserial="/dev/serial0"):
uart = serial.Serial(myserial, baudrate=57600, timeout=1)
self.finger = adafruit_fingerprint.Adafruit_Fingerprint(uart)
self.found_finger = lambda x, y: None
self.updated_templates = lambda: None
self.unauthorized = lambda: None
self.mode = "scan"
self.get_info()
threading.Thread(target=self.get_fingerprint, daemon=True).start()
def set_mode(self, mode, args=-1):
if args == "" or args is None:
args = -1
args = int(args)
if self.mode != mode:
print("changed mode to", mode)
self.mode = mode
if mode == "enroll":
self.enroll_new(args)
self.mode = "scan"
elif mode == "delete":
self.delete_model(args)
self.mode = "scan"
elif mode == "empty":
self.empty_library()
def get_info(self):
if self.finger.read_templates() != adafruit_fingerprint.OK:
raise RuntimeError("Failed to read templates")
# print("Fingerprint templates: ", self.finger.templates)
if self.finger.count_templates() != adafruit_fingerprint.OK:
raise RuntimeError("Failed to read templates")
# print("Number of templates found: ", self.finger.template_count)
if self.finger.read_sysparam() != adafruit_fingerprint.OK:
raise RuntimeError("Failed to get system parameters")
# print("Size of template library: ", self.finger.library_size)
return {
"templates": self.finger.templates,
"size": self.finger.library_size
}
def set_ledcolor(self, led_color=None, led_mode=None, action=None):
if action == "reset":
self.finger.set_led(color=self.LEDCOLOR_RED, mode=self.LEDMODE_OFF)
elif action == "error":
self.finger.set_led(color=self.LEDCOLOR_RED, mode=self.LEDMODE_STILL)
time.sleep(1)
self.set_ledcolor(action="reset")
elif action == "enroll":
self.finger.set_led(color=self.LEDCOLOR_PURPLE, mode=self.LEDMODE_BREATH)
elif action == "success":
self.finger.set_led(color=self.LEDCOLOR_BLUE, mode=self.LEDMODE_STILL)
time.sleep(1)
self.set_ledcolor(action="reset")
led_mode = self.LEDMODE_STILL
if led_color is not None and led_mode is not None:
self.finger.set_led(color=led_color, mode=led_mode)
def delete_model(self, location):
time.sleep(1)
if self.finger.delete_model(location) == adafruit_fingerprint.OK:
print("Deleted!")
self.finger.read_templates()
self.finger.read_sysparam()
self.updated_templates()
return True
else:
print("Failed to delete")
self.set_ledcolor(action="error")
return False
def empty_library(self):
time.sleep(1)
if self.finger.empty_library() == adafruit_fingerprint.OK:
print("Library emptied!")
self.finger.read_templates()
self.finger.read_sysparam()
self.updated_templates()
return True
else:
print("Failed to empty library")
self.set_ledcolor(action="error")
return False
def get_fingerprint(self):
"""Get a finger print image, template it, and see if it matches!"""
while True:
if self.mode == "scan":
try:
is_finger_ok = self.finger.get_image() == adafruit_fingerprint.OK
except:
continue
if is_finger_ok:
self.set_ledcolor(self.LEDCOLOR_PURPLE, self.LEDMODE_BLINK)
print("Templating...")
time.sleep(0.2)
if self.finger.image_2_tz(1) == adafruit_fingerprint.OK:
print("Searching...")
if self.finger.finger_search() == adafruit_fingerprint.OK:
print("Detected #", self.finger.finger_id, "with confidence", self.finger.confidence)
self.set_ledcolor(self.LEDCOLOR_BLUE, self.LEDMODE_STILL)
self.found_finger(self.finger.finger_id, self.finger.confidence)
time.sleep(1)
self.set_ledcolor(action="reset")
else:
self.unauthorized()
self.set_ledcolor(action="error")
else:
self.unauthorized()
self.set_ledcolor(action="error")
def enroll_finger(self, location, timeout=10):
"""Take a 2 finger images and template it, then store in 'location'"""
time.sleep(1)
for fingerimg in range(1, 3):
self.set_ledcolor(action="enroll")
if fingerimg == 1:
print("Place finger on sensor...", end="", flush=True)
else:
print("Place same finger again...", end="", flush=True)
start = time.time()
while True:
i = self.finger.get_image()
if i == adafruit_fingerprint.OK:
print("Image taken")
break
if i == adafruit_fingerprint.NOFINGER:
print(".", end="", flush=True)
if (time.time() - start) > timeout:
self.set_ledcolor(action="error")
return False
elif i == adafruit_fingerprint.IMAGEFAIL:
print("Imaging error")
self.set_ledcolor(action="error")
return False
else:
print("Other error")
self.set_ledcolor(action="error")
return False
print("Templating...", end="", flush=True)
i = self.finger.image_2_tz(fingerimg)
if i == adafruit_fingerprint.OK:
self.set_ledcolor(action="success")
print("Templated")
else:
if i == adafruit_fingerprint.IMAGEMESS:
print("Image too messy")
elif i == adafruit_fingerprint.FEATUREFAIL:
print("Could not identify features")
elif i == adafruit_fingerprint.INVALIDIMAGE:
print("Image invalid")
else:
print("Other error")
self.set_ledcolor(action="error")
return False
if fingerimg == 1:
print("Remove finger")
time.sleep(1)
while i != adafruit_fingerprint.NOFINGER:
i = self.finger.get_image()
print("Creating model...", end="", flush=True)
i = self.finger.create_model()
if i == adafruit_fingerprint.OK:
print("Created")
else:
if i == adafruit_fingerprint.ENROLLMISMATCH:
print("Prints did not match")
else:
print("Other error")
self.set_ledcolor(action="error")
return False
print("Storing model #%d..." % location, end="", flush=True)
i = self.finger.store_model(location)
if i == adafruit_fingerprint.OK:
print("Stored")
else:
if i == adafruit_fingerprint.BADLOCATION:
print("Bad storage location")
elif i == adafruit_fingerprint.FLASHERR:
print("Flash storage error")
else:
print("Other error")
self.set_ledcolor(action="error")
return False
self.finger.read_templates()
self.finger.read_sysparam()
self.updated_templates()
return True
def enroll_new(self, position):
if position < 0:
empty_positions = list(set(range(0, self.finger.library_size - 1)) - set(self.finger.templates))
position = empty_positions[0]
print('ID of the new enlyst:', position)
fingerprint.enroll_finger(position)
return position
def read_devices():
devices = []
if os.path.exists('devices.yaml'):
with open('devices.yaml') as f:
devices = yaml.load(f, Loader=yaml.FullLoader)
if devices is None:
devices = []
devices_dict = {i['id']: i for i in devices}
return devices_dict
def on_connect(client, userdata, flags, rc, *args):
print("Connected with result code " + str(rc))
client.publish(f"{config.get('topic', 'fingerprint')}/mode", "scan")
client.subscribe(f"{config.get('topic', 'fingerprint')}/set/#")
updatedtemplates()
def on_message(client, userdata, msg, *args):
mode = msg.topic.split("/")[-1]
payload_arg = str(msg.payload.decode("utf-8"))
print(msg.topic + " " + payload_arg)
if "name_" in mode:
f_id = int(mode.split("_")[-1])
renamefinger(f_id, payload_arg)
else:
client.publish(f"{config.get('topic', 'fingerprint')}/mode", mode)
fingerprint.set_mode(mode, payload_arg)
client.publish(f"{config.get('topic', 'fingerprint')}/mode", "scan")
def renamefinger(f_id, name):
print(f_id, name)
devices_dict = read_devices()
devices_dict[f_id]['name'] = name
devices_list = []
for d_id, device in devices_dict.items():
devices_list.append(device)
with open('devices.yaml', 'w') as f:
yaml.dump(devices_list, f)
def foundfinger(f_id, confidence):
print(f_id, confidence)
devices_dict = read_devices()
devices_dict[f_id]['count'] += 1
device_publish = devices_dict[f_id]
device_publish['confidence'] = confidence
if config['timeout'] > 0:
if device_publish['id'] == device_publish['name']:
if (time.time() - device_publish['time']) > config['timeout']:
fingerprint.set_ledcolor(action="error")
device_publish['action'] = "timeout"
client.publish(f"{config.get('topic', 'fingerprint')}/finger", json.dumps(device_publish))
devices_list = []
for d_id, device in devices_dict.items():
devices_list.append(device)
with open('devices.yaml', 'w') as f:
yaml.dump(devices_list, f)
def unauthorized():
to_publish = {
'id': -1,
'name': "unauthorized",
'action': "unauthorized",
'time': int(time.time()),
'confidence': 0,
}
client.publish(f"{config.get('topic', 'fingerprint')}/finger", json.dumps(to_publish))
def updatedtemplates():
devices_list = []
devices_dict = read_devices()
for template in fingerprint.finger.templates:
if template in devices_dict:
devices_list.append({
'id': template,
'name': devices_dict[template]['name'],
'action': devices_dict[template]['action'],
'time': devices_dict[template]['time'],
'count': devices_dict[template]['count'],
})
else:
devices_list.append({
'id': template,
'name': template,
'action': "unlock",
'time': int(time.time()),
'count': 0,
})
with open('devices.yaml', 'w') as f:
yaml.dump(devices_list, f)
templates = json.dumps(fingerprint.finger.templates)
client.publish(f"{config.get('topic', 'fingerprint')}/templates", json.dumps(devices_list))
with open('config.yaml') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
fingerprint = Fingerprint(config['serial'])
if hasattr(mqtt, "CallbackAPIVersion"):
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
else:
client = mqtt.Client()
fingerprint.found_finger = foundfinger
fingerprint.updated_templates = updatedtemplates
fingerprint.unauthorized = unauthorized
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(config['mqtt']['user'], config['mqtt']['pass'])
client.connect(config['mqtt']['host'], 1883, 60)
client.loop_forever()