forked from enesbcs/rpieasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P208_FPM.py
370 lines (362 loc) · 12 KB
/
_P208_FPM.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env python3
#############################################################################
################## Serial Fingerprint plugin for RPIEasy ####################
#############################################################################
#
# Plugin for serial Fingerprint Module reading
#
# Copyright (C) 2020 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import plugin
import webserver
import rpieGlobals
import rpieTime
import misc
import time
from pyfingerprint.pyfingerprint import PyFingerprint
import lib.lib_serial as rpiSerial
import hashlib
import webserver
import Settings
class Plugin(plugin.PluginProto):
PLUGIN_ID = 208
PLUGIN_NAME = "ID - Serial Fingerprint Module (EXPERIMENTAL)"
PLUGIN_VALUENAME1 = "ID"
def __init__(self,taskindex): # general init
plugin.PluginProto.__init__(self,taskindex)
self.dtype = rpieGlobals.DEVICE_TYPE_SER
self.vtype = rpieGlobals.SENSOR_TYPE_SINGLE
self.valuecount = 1
self.senddataoption = True
self.recdataoption = False
self.timeroption = True
self.timeroptional = True
self.formulaoption = True
self.fp = None
self.readinprogress=0
self.initialized=False
self.initcount = 0
def webform_load(self): # create html page for settings
choice1 = self.taskdevicepluginconfig[0]
options = rpiSerial.serial_portlist()
if len(options)>0:
webserver.addHtml("<tr><td>Serial Device:<td>")
webserver.addSelector_Head("p208_addr",False)
for o in range(len(options)):
webserver.addSelector_Item(options[o],options[o],(str(options[o])==str(choice1)),False)
webserver.addSelector_Foot()
webserver.addFormNote("Address of the FPM serial port")
else:
webserver.addFormNote("No serial ports found")
options = ["None", "Valid", "Position","SHA2"]
optionvalues = [0, 1, 2,3]
webserver.addFormSelector("Indicator1","plugin_208_ind0",len(options),options,optionvalues,None,self.taskdevicepluginconfig[1])
webserver.addFormSelector("Indicator2","plugin_208_ind1",len(options),options,optionvalues,None,self.taskdevicepluginconfig[2])
webserver.addFormSelector("Indicator3","plugin_208_ind2",len(options),options,optionvalues,None,self.taskdevicepluginconfig[3])
if self.enabled and self.initialized:
try:
webserver.addFormNote("Stored fingerprints: "+ str(self.fp.getTemplateCount())+"/"+str(self.fp.getStorageCapacity()))
except:
pass
webserver.addHtml("<tr><td><a href='/finger'>Management page</a>")
return True
def webform_save(self,params): # process settings post reply
try:
self.taskdevicepluginconfig[0] = str(webserver.arg("p208_addr",params)).strip()
for v in range(0,3):
par = webserver.arg("plugin_208_ind"+str(v),params)
if par == "":
par = -1
else:
par=int(par)
if str(self.taskdevicepluginconfig[v+1])!=str(par):
self.uservar[v] = 0
self.taskdevicepluginconfig[v+1] = par
if int(par)>0 and self.valuecount!=v+1:
self.valuecount = (v+1)
if self.valuecount == 1:
self.vtype = rpieGlobals.SENSOR_TYPE_SINGLE
elif self.valuecount == 2:
self.vtype = rpieGlobals.SENSOR_TYPE_DUAL
elif self.valuecount == 3:
self.vtype = rpieGlobals.SENSOR_TYPE_TRIPLE
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,str(e))
return True
def plugin_init(self,enableplugin=None):
plugin.PluginProto.plugin_init(self,enableplugin)
self.taskdevicepluginconfig[0] = str(self.taskdevicepluginconfig[0]).strip()
self.readinprogress=0
self.initialized=False
try:
if self.initcount > 2:
self.initcount = 0
self.enabled = False
except:
self.initcount = 0
if self.valuecount == 1:
self.vtype = rpieGlobals.SENSOR_TYPE_SINGLE
elif self.valuecount == 2:
self.vtype = rpieGlobals.SENSOR_TYPE_DUAL
elif self.valuecount == 3:
self.vtype = rpieGlobals.SENSOR_TYPE_TRIPLE
if self.enabled and self.taskdevicepluginconfig[0]!="" and self.taskdevicepluginconfig[0]!="0":
time.sleep(0.5)
try:
if self.fp is not None:
self.fp.__del__()
except:
pass
try:
time.sleep(2)
self.fp = PyFingerprint(self.taskdevicepluginconfig[0],57600,0xFFFFFFFF,0)
time.sleep(0.5)
if self.fp.verifyPassword()==False:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"FPM password wrong")
self.fp = None
self.initialized = True
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"FPM initialized")
except Exception as e:
self.fp = None
self.initialized = False
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"FPM init error: "+str(e))
if self.initialized==False:
self.initcount += 1
time.sleep(3)
self.plugin_init()
def plugin_read(self): # deal with data processing at specified time interval
result = False
# nochange = (self.interval>0)
change = False
if self.initialized and self.readinprogress==0 and self.enabled:
self.readinprogress = 1
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"FPM scan")
if self.interval>0 and self.interval<2:
scantime = 0.8
else:
scantime = 2
pos = -1
try:
# print("readimg") #debug
st = time.time()
readok = True
while (self.fp.readImage() == False):
if time.time()-st>=scantime:
readok = False
break
self.fp.convertImage(0x01)
result = self.fp.searchTemplate()
pos = result[0]
score = result[1]
except Exception as e:
pass
value = "0"
if readok:
for v in range(0,3):
vtype = int(self.taskdevicepluginconfig[v+1])
if vtype == 1:
if pos>-1:
value = 1
else:
value = 0
elif vtype == 2:
value = pos
elif vtype == 3:
if pos>-1:
value = "0"
try:
self.fp.loadTemplate(pos,0x01)
chars = str(self.fp.downloadCharacteristics(0x01)).encode('utf-8')
value = hashlib.sha256(chars).hexdigest()
except:
value = "0"
if vtype in [1,2,3]:
if str(self.uservar[v]) != str(value):
self.set_value(v+1,value,False)
change = True
if change:
self.plugin_senddata()
if readok==False and self.interval==0:
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"FPM read failed")
self._lastdataservetime = rpieTime.millis()
result = True
self.readinprogress = 0
return result
@webserver.WebServer.route('/finger')
def handle_fpm(self):
webserver.navMenuIndex=4
if (not webserver.isLoggedIn(self.get,self.cookie)):
return self.redirect('/login')
webserver.TXBuffer=""
fpm = None
options = []
optionvalues = []
for x in range(0,len(Settings.Tasks)):
if (Settings.Tasks[x]) and type(Settings.Tasks[x]) is not bool:
try:
if Settings.Tasks[x].enabled:
if Settings.Tasks[x].pluginid==208:
optionvalues.append(x)
options.append(str(x)+" / "+Settings.Tasks[x].gettaskname())
except:
pass
if self.type == "GET":
responsearr = self.get
else:
responsearr = self.post
# print(optionvalues,options) # debug
if len(optionvalues)>0:
ftask = webserver.arg('fptask',responsearr)
if str(ftask)!="":
ftask = int(ftask)
if ftask>-1:
fpm = Settings.Tasks[ftask]
else:
ftask = optionvalues[0]
fpm = Settings.Tasks[ftask]
if (fpm is not None) and fpm.enabled and fpm.initialized:
try:
ffree = int(webserver.arg('ffree',responsearr))
except:
ffree = -1
try:
fpnum = int(webserver.arg('fpnum',responsearr))
except:
fpnum = ""
try:
add = webserver.arg('add',responsearr)
except:
add = ""
try:
fsearch = webserver.arg('search',responsearr)
except:
fsearch = ""
try:
fdel = webserver.arg('fdel',responsearr)
except:
fdel = ""
try:
fdelall = webserver.arg('fdelall',responsearr)
except:
fdelall = ""
try:
if add!="":
readok = True
st = time.time()
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"FPM enroll, place your finger onto the scanner")
time.sleep(0.5)
while (fpm.fp.readImage() == False):
if time.time()-st>=4:
readok = False
break
if readok:
fpm.fp.convertImage(0x01)
result = fpm.fp.searchTemplate()
posnum = result[0]
if (posnum>=0):
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"FPM template already exists")
else:
fpm.fp.convertImage(0x02)
fpm.fp.createTemplate()
if ffree>-1:
posnum = ffree
else:
posnum = fpm.fp.getTemplateCount()
if (fpm.fp.storeTemplate(posnum) == True):
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"FPM template "+str(posnum)+" added")
else:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"FPM template "+str(posnum)+" adding returned error")
else:
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"FPM read failed")
elif fsearch!="":
readok = True
st = time.time()
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"FPM search, place your finger onto the scanner")
time.sleep(0.5)
while (fpm.fp.readImage() == False):
if time.time()-st>=4:
readok = False
break
if readok:
fpm.fp.convertImage(0x01)
result = fpm.fp.searchTemplate()
posnum = result[0]
if (posnum>=0):
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"FPM template found at #"+str(posnum))
else:
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"FPM template not found")
else:
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"FPM read failed")
elif fdel!="":
if fpnum!="":
if (fpm.fp.deleteTemplate(int(fpnum)) == True):
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"FPM template "+str(fpnum)+" deleted")
else:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"FPM template "+str(fpnum)+" deletion failed")
elif fdelall!="":
if (fpm.fp.clearDatabase() == True):
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"FPM all templates deleted")
else:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Template database deletion failed")
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"FPM error: "+str(e))
webserver.sendHeadandTail("TmplStd",webserver._HEAD)
webserver.TXBuffer += "<form name='frmtask' method='post'><table class='normal'>"
webserver.addFormHeader("Fingerprint Module")
webserver.addFormSelector("Fingerprint device task","fptask",len(options),options,optionvalues,None,ftask)
webserver.addFormSubHeader("Stored fingerprints")
if fpm is not None:
webserver.TXBuffer += "<TR><TH>ID<TH>SHA2<TH></TR>"
maxv = fpm.fp.getStorageCapacity()
fc = fpm.fp.getTemplateCount()
if fc > maxv:
maxv = fc
cter = 0
ffree = -1
for f in range(0,maxv):
fid = f
try:
fpm.fp.loadTemplate(f,0x01)
chars = str(fpm.fp.downloadCharacteristics(0x01)).encode('utf-8')
value = hashlib.sha256(chars).hexdigest()
except:
fid = -1
if fid>-1:
cter = cter + 1
webserver.TXBuffer += "<tr><td><input type='radio' name='fpnum' value='"+ str(fid)+"'>"+str(fid)
webserver.TXBuffer += "<td>"+str(value)+"</tr>"
elif ffree == -1:
ffree = f
if cter>fc:
break
if ffree==-1:
ffree = maxv
webserver.TXBuffer += "</table><p>"
webserver.addSubmitButton("Enroll new fingerprint", "add")
webserver.TXBuffer += "<BR>"
webserver.addSubmitButton("Search for fingerprint", "search")
webserver.TXBuffer += "<BR>"
webserver.addSubmitButton("Delete selected fingerprint", "fdel")
webserver.TXBuffer += "<BR>"
webserver.addSubmitButton("Delete all fingerprints", "fdelall")
webserver.TXBuffer += "<input type='hidden' name='ffree' value='" +str(ffree) + "'>"
webserver.TXBuffer += "</form>"
try:
webserver.TXBuffer += "<p><table width='100%'><TR><TD colspan='2'>Results<BR><textarea readonly rows='10' wrap='on'>"
lc = len(misc.SystemLog)
if lc>5:
ls = lc-5
else:
ls = 0
for l in range(ls,lc):
webserver.TXBuffer += '\r\n'+str(misc.SystemLog[l]["t"])+" : "+ str(misc.SystemLog[l]["l"])
webserver.TXBuffer += "</textarea>"
except Exception as e:
print(e)
webserver.TXBuffer += "</table>"
webserver.sendHeadandTail("TmplStd",webserver._TAIL)
return webserver.TXBuffer
else:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"FPM module uninitialized!")
time.sleep(1)
return self.redirect('/log')