diff --git a/.gitignore b/.gitignore index 70e6997..88ba600 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ build/ dist/ *.egg-info/ -*.test \ No newline at end of file +*.test +*.bak \ No newline at end of file diff --git a/README.md b/README.md index 2cd3acb..014c3c4 100644 --- a/README.md +++ b/README.md @@ -309,9 +309,11 @@ DeviceName : iClock260 (no capture data - probably similar problem as the latest If you have another version tested and it worked, please inform me to update this list! -# Related Project (TODO: check compatibility with this fork) +# Related Project -* [zkcluster](https://github.com/fananimi/zkcluster/ "zkcluster project") is a django apps to manage multiple fingerprint devices. +* [zkcluster](https://github.com/kurenai-ryu/zkcluster/ "zkcluster project") is a django apps to manage multiple fingerprint devices. (Initial support form the [original project](https://github.com/fananimi/zkcluster/)) + +# Related Project (TODO: check compatibility with this fork) * [Driji](https://github.com/fananimi/driji/ "Driji project") is an attendance apps based fingerprint for school diff --git a/test_backup_restore.py b/test_backup_restore.py new file mode 100755 index 0000000..b47fe9f --- /dev/null +++ b/test_backup_restore.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python2 +# # -*- coding: utf-8 -*- +import sys +import traceback +import argparse +import time +import datetime +import codecs +from builtins import input + +import pickle + +sys.path.append("zk") + +from zk import ZK, const +from zk.user import User +from zk.finger import Finger +from zk.attendance import Attendance +from zk.exception import ZKErrorResponse, ZKNetworkError + +class BasicException(Exception): + pass + +conn = None + +parser = argparse.ArgumentParser(description='ZK Basic Backup/Restore Tool') +parser.add_argument('-a', '--address', + help='ZK device Address [192.168.1.201]', default='192.168.1.201') +parser.add_argument('-p', '--port', type=int, + help='ZK device port [4370]', default=4370) +parser.add_argument('-T', '--timeout', type=int, + help='Default [10] seconds (0: disable timeout)', default=10) +parser.add_argument('-P', '--password', type=int, + help='Device code/password', default=0) +parser.add_argument('-f', '--force-udp', action="store_true", + help='Force UDP communication') +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='') + +args = parser.parse_args() + + + +zk = ZK(args.address, port=args.port, timeout=args.timeout, password=args.password, force_udp=args.force_udp, verbose=args.verbose) +try: + print('Connecting to device ...') + conn = zk.connect() + serialnumber = conn.get_serialnumber() + fp_version = conn.get_fp_version() + print ('Serial Number : {}'.format(serialnumber)) + print ('Finger Version : {}'.format(fp_version)) + filename = args.filename if args.filename else "{}.bak".format(serialnumber) + print ('') + if not args.restore: + print ('--- sizes & capacity ---') + conn.read_sizes() + print (conn) + print ('--- Get User ---') + inicio = time.time() + users = conn.get_users() + final = time.time() + print ('Read {} users took {:.3f}[s]'.format(len(users), final - inicio)) + print ("Read Templates...") + inicio = time.time() + templates = conn.get_templates() + final = time.time() + print ('Read {} templates took {:.3f}[s]'.format(len(templates), final - inicio)) + #save to file! + print ('') + print ('Saving to file {} ...'.format(filename)) + output = open(filename, 'wb') + data = { + 'version':'1.00ut', + 'serial': serialnumber, + 'fp_version': fp_version, + 'users': users, + 'templates':templates + } + pickle.dump(data, output, -1) + output.close() + else: + print ('Reading file {}'.format(filename)) + infile = open(filename, 'rb') + data = pickle.load(infile) + infile.close() + #compare versions... + if data['version'] != '1.00ut': + 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'])) + 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') +except BasicException as e: + print (e) + print ('') +except Exception as e: + print ("Process terminate : {}".format(e)) + print ("Error: %s" % sys.exc_info()[0]) + print ('-'*60) + traceback.print_exc(file=sys.stdout) + print ('-'*60) +finally: + if conn: + print ('Enabling device ...') + conn.enable_device() + conn.disconnect() + print ('ok bye!') + print ('') diff --git a/test_machine.py b/test_machine.py index d1492ff..4af5724 100755 --- a/test_machine.py +++ b/test_machine.py @@ -21,14 +21,13 @@ class BasicException(Exception): conn = None - parser = argparse.ArgumentParser(description='ZK Basic Reading Tests') parser.add_argument('-a', '--address', help='ZK device Address [192.168.1.201]', default='192.168.1.201') parser.add_argument('-p', '--port', type=int, help='ZK device port [4370]', default=4370) parser.add_argument('-T', '--timeout', type=int, - help='Default [10] seconds (0: disable timeout)', default=1) + help='Default [10] seconds (0: disable timeout)', default=10) parser.add_argument('-P', '--password', type=int, help='Device code/password', default=0) parser.add_argument('-b', '--basic', action="store_true", @@ -246,6 +245,7 @@ try: print ('') except BasicException as e: print (e) + print ('') except Exception as e: print ("Process terminate : {}".format(e)) print ("Error: %s" % sys.exc_info()[0]) diff --git a/zk/attendance.py b/zk/attendance.py index 4d68746..3ffd9d7 100644 --- a/zk/attendance.py +++ b/zk/attendance.py @@ -8,7 +8,7 @@ class Attendance(object): self.punch = punch def __str__(self): - return ': {} : {}'.format(self.user_id, self.timestamp) + return ': {} : {} ({}, {})'.format(self.user_id, self.timestamp, self.status, self.punch) def __repr__(self): - return ': {} : {}'.format(self.user_id, self.timestamp) + return ': {} : {} ({}, {})'.format(self.user_id, self.timestamp,self.status, self.punch) diff --git a/zk/base.py b/zk/base.py index 1f90185..830d11c 100644 --- a/zk/base.py +++ b/zk/base.py @@ -973,7 +973,7 @@ class ZK(object): if self.verbose: print("WRN: no user data") # debug return [] total_size = unpack('i', templatedata[0:4])[0] - print ("get template total size {}, size {} len {}".format(total_size, size, len(templatedata))) + if self.verbose: print ("get template total size {}, size {} len {}".format(total_size, size, len(templatedata))) templatedata = templatedata[4:] #total size not used # ZKFinger VX10.0 the only finger firmware tested while total_size: