diff --git a/test.py b/test.py old mode 100644 new mode 100755 index e96debd..841a8af --- a/test.py +++ b/test.py @@ -1,3 +1,6 @@ +#!/usr/bin/env python2 +# # -*- coding: utf-8 -*- + import sys import os import unittest diff --git a/test_backup_restore.py b/test_backup_restore.py index b47fe9f..7be520f 100755 --- a/test_backup_restore.py +++ b/test_backup_restore.py @@ -38,8 +38,8 @@ parser.add_argument('-v', '--verbose', action="store_true", help='Print debug information') parser.add_argument('-r', '--restore', action="store_true", help='Restore from backup') -parser.add_argument('-F', '--filename', - help='backup filename', default='') +parser.add_argument('filename', nargs='?', + help='backup filename (default [serialnumber].bak)', default='') args = parser.parse_args() @@ -64,6 +64,8 @@ try: users = conn.get_users() final = time.time() print ('Read {} users took {:.3f}[s]'.format(len(users), final - inicio)) + if len(users) == 0: + raise BasicException("Empty user list, aborting...") print ("Read Templates...") inicio = time.time() templates = conn.get_templates() @@ -92,13 +94,28 @@ try: raise BasicException("file with different version... aborting!") if data['fp_version'] != fp_version: raise BasicException("fingerprint version mismmatch {} != {} ... aborting!".format(fp_version, data['fp_version'])) + #TODO: check data consistency... + print ("INFO: ready to write {} users".format(len(data['users']))) + print ("INFO: ready to write {} templates".format(len(data['templates']))) + #input serial number to corroborate. print ('WARNING! the next step will erase the current device content.') print ('Please input the serialnumber of this device [{}] to acknowledge the ERASING!'.format(serialnumber)) new_serial = input ('Serial Number : ') if new_serial != serialnumber: raise BasicException('Serial number mismatch') - print ('Erasing device!!! Work in progress') - print ('Restoring Data... Work in progress') + conn.disable_device() + print ('Erasing device...') + conn.clear_data() + print ('Restoring Data...') + for u in data['users']: + #look for Templates + temps = list(filter(lambda f: f.uid ==u.uid, data['templates'])) + #print ("user {} has {} fingers".format(u.uid, len(temps))) + conn.save_user_template(u,temps) + conn.enable_device() + print ('--- final sizes & capacity ---') + conn.read_sizes() + print (conn) except BasicException as e: print (e) print ('') diff --git a/zk/base.py b/zk/base.py index aa387da..c0f8b04 100644 --- a/zk/base.py +++ b/zk/base.py @@ -109,6 +109,7 @@ class ZK(object): self.ommit_ping = ommit_ping self.verbose = verbose self.encoding = encoding + User.encoding = encoding self.tcp = False self.users = 0 self.fingers = 0 @@ -775,14 +776,18 @@ class ZK(object): return False #some devices doesn't support sound #raise ZKErrorResponse("can't test voice") - def set_user(self, uid, name, privilege=0, password='', group_id='', user_id='', card=0): + def set_user(self, uid=None, name='', privilege=0, password='', group_id='', user_id='', card=0): ''' create or update user by uid ''' command = const.CMD_USER_WRQ + if uid is None: + uid = self.next_uid # keeps uid=0 + if not user_id: + user_id = self.next_user_id # else... if not user_id: user_id = str(uid) #ZK6 needs uid2 == uid - #uid = chr(uid % 256) + chr(uid >> 8) + #TODO: check what happens if name is missing... if privilege not in [const.USER_DEFAULT, const.USER_ADMIN]: privilege = const.USER_DEFAULT privilege = int(privilege) @@ -827,8 +832,8 @@ class ZK(object): raise ZKErrorResponse("Can't find user") if isinstance(fingers, Finger): fingers = [fingers] - fpack = "" - table = "" + fpack = b"" + table = b"" fnum = 0x10 # possibly flag tstart = 0 for finger in fingers: diff --git a/zk/user.py b/zk/user.py index 265953f..7a5fbe6 100644 --- a/zk/user.py +++ b/zk/user.py @@ -1,21 +1,25 @@ # -*- coding: utf-8 -*- from struct import pack #, unpack class User(object): + encoding = 'UTF-8' def __init__(self, uid, name, privilege, password='', group_id='', user_id='', card=0): self.uid = uid self.name = str(name) self.privilege = privilege self.password = str(password) - self.group_id = group_id + self.group_id = str(group_id) self.user_id = user_id self.card = card # 64 int to 40 bit int + def repack29(self): # with 02 for zk6 (size 29) - return pack(" 7sx group id, timezone? - return pack(": [uid:{}, name:{} user_id:{}]'.format(self.uid, self.name, self.user_id)