From 9f0267b107dc4dabbff459af7401b4473ce917a1 Mon Sep 17 00:00:00 2001 From: Arturo Hernandez Date: Wed, 2 May 2018 15:12:08 -0400 Subject: [PATCH] fix enroll_user (keeps using uid, but now it searchs for user_id first) testing live capture! --- test_machine.py | 35 ++++++++++-- zk/base.py | 140 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 147 insertions(+), 28 deletions(-) diff --git a/test_machine.py b/test_machine.py index 4d3aa67..31a325d 100755 --- a/test_machine.py +++ b/test_machine.py @@ -35,10 +35,14 @@ parser.add_argument('-r', '--records', action="store_true", help='get records') parser.add_argument('-u', '--updatetime', action="store_true", help='Update Date / Time') +parser.add_argument('-l', '--live-capture', action="store_true", + help='Live Event Capture') parser.add_argument('-D', '--deleteuser', type=int, - help='Delete a User', default=0) + help='Delete a User (uid)', default=0) parser.add_argument('-A', '--adduser', type=int, - help='Add a User', default=0) + help='Add a User (uid)', default=0) +parser.add_argument('-E', '--enrolluser', type=int, + help='Enroll a User (uid)', default=0) parser.add_argument('-F', '--finger', type=int, help='Finger for register', default=0) @@ -137,15 +141,23 @@ try: conn.delete_user(uid) #borrado previo try: conn.set_user(uid, name, privilege, password, '', user_id, card) + args.enrolluser = uid except ZKErrorResponse as e: print ("error: %s" % e) #try new format zk_user = User(uid, name, privilege, password, '', user_id, card) - conn.save_user_template(zk_user) + conn.save_user_template(zk_user)# forced creation + args.enrolluser = uid + conn.refresh_data() + if args.enrolluser: + uid = int(args.enrolluser) + print ('--- Enrolling User #{} ---'.format(uid)) conn.delete_user_template(uid, args.finger) conn.reg_event(0xFFFF) # if conn.enroll_user(uid, args.finger): conn.test_voice(18) # register ok + tem = conn.get_user_template(uid, args.finger) + print (tem) else: conn.test_voice(23) # not registered conn.refresh_data() @@ -162,11 +174,25 @@ try: i = 0 for att in attendance: i +=1 - print ("ATT {:>6}: uid:{:>3}, t: {}".format(i, att.uid, att.timestamp)) + print ("ATT {:>6}: uid:{:>3}, user_id:{:>8} t: {}, s:{}".format(i, att.uid, att.user_id, att.timestamp, att.status)) print ('') print ('--- sizes & capacity ---') conn.read_sizes() print (conn) + if args.live_capture: + print ('') + print ('--- Live Capture! (press ctrl+C to break)---') #TODO how? + counter = 0 + for att in conn.live_capture(): + if att is None: + #counter += 1 + print ("timeout {}".format(counter)) + else: + print ("ATT {:>6}: uid:{:>3}, user_id:{:>8} t: {}, s:{}".format(counter, att.uid, att.user_id, att.timestamp, att.status)) + if counter >= 10: + conn.end_live_capture = True + print('') + print('--- capture End!---') print ('') except Exception as e: print ("Process terminate : {}".format(e)) @@ -179,4 +205,5 @@ finally: print ('Enabling device ...') conn.enable_device() conn.disconnect() + print ('ok bye!') print ('') diff --git a/zk/base.py b/zk/base.py index c40b3e1..f7badf0 100644 --- a/zk/base.py +++ b/zk/base.py @@ -2,7 +2,7 @@ import sys #from builtins import str from datetime import datetime -from socket import AF_INET, SOCK_DGRAM, SOCK_STREAM, socket +from socket import AF_INET, SOCK_DGRAM, SOCK_STREAM, socket, timeout from struct import pack, unpack from zk import const @@ -89,6 +89,7 @@ class ZK(object): def __init__(self, ip, port=4370, timeout=60, password=0, force_udp=False, ommit_ping=False): """ initialize instance """ self.is_connect = False + self.is_enabled = True #let's asume self.helper = ZK_helper(ip, port) self.__address = (ip, port) self.__sock = socket(AF_INET, SOCK_DGRAM) @@ -186,7 +187,7 @@ class ZK(object): top = self.__create_tcp_top(buf) self.__sock.send(top) self.__tcp_data_recv = self.__sock.recv(response_size + 8) - self.__tcp_header = unpack('HHI', self.__tcp_data_recv[:8]) + self.__tcp_header = unpack('= 1: + user_id = users[0].user_id + else: #double? posibly empty + return False #can't enroll if self.tcp: - if not user_id: - #we need user_id (uid2) - users = self.get_users() - users = filter(lambda x: x.uid==uid, users) - if len(users) == 1: - user_id = users[0].user_id - else: #double? posibly empty - return False #can't enroll command_string = pack('<24sbb',str(user_id), temp_id, 1) # el 1 es misterio else: - if not uid: - #we need uid - users = self.get_users() - users = filter(lambda x: x.user_id==user_id, users) - if len(users) >= 1: - uid = users[0].uid #only one - else: # posibly empty - return False #can't enroll - command_string = pack('hhb', int(uid), 0, temp_id) # el 0 es misterio + command_string = pack('= 8: uid, status, timestamp = unpack('HH4s', attendance_data.ljust(8, b'\x00')[:8]) + #print attendance_data[:8].encode('hex') attendance_data = attendance_data[8:] - user_id = str(uid) #TODO revisar posibles valores cruzar con userdata + tuser = filter(lambda x: x.uid == uid, users) + if not tuser: + user_id = str(uid) #TODO revisar pq + else: + user_id = tuser[0].user_id timestamp = self.__decode_time(timestamp) attendance = Attendance(uid, user_id, timestamp, status) attendances.append(attendance) elif record_size == 16: # extended while len(attendance_data) >= 16: - uid, timestamp, status, verified, reserved, workcode = unpack('= 40: uid, user_id, sparator, timestamp, status, space = unpack('