diff --git a/README.md b/README.md index 2aaf61b..8d3d3aa 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # pyzk -pyzk is unofficial library of zksoftware the fingerprint attendance machine. +pyzk is an unofficial library of zksoftware the fingerprint attendance machine. # Installation @@ -14,25 +14,24 @@ Complete documentation can be found at [Readthedocs](http://pyzk.readthedocs.io/ # Api Usage -Create ZK object and you will ready to call api. +Create an ZK instnace and you will ready to call api. * Basic Usage ``` -import zk -from zk import const +from zk import ZK, const -conn = False -zk = zk.ZK(ip='192.168.1.201', port=4370, timeout=5) +sys.path.append("zk") + +conn = None +zk = ZK('192.168.1.10', port=4370, timeout=5) try: + print 'Connecting to device ...' conn = zk.connect() - - # disable (lock) the device, make sure no activity when process run - zk.disable_device() - - # Do another task here - firmware_version = zk.get_firmware_version() - print 'Firmware Version: : {}'.format(firmware_version) - users = zk.get_users() + print 'Disabling device ...' + conn.disable_device() + print 'Firmware Version: : {}'.format(conn.get_firmware_version()) + # print '--- Get User ---' + users = conn.get_users() for user in users: privilege = 'User' if user.privilege == const.USER_ADMIN: @@ -45,67 +44,69 @@ try: print ' Group ID : {}'.format(user.group_id) print ' User ID : {}'.format(user.user_id) - zk.enable_device() + print "Voice Test ..." + conn.test_voice() + print 'Enabling device ...' + conn.enable_device() except Exception, e: print "Process terminate : {}".format(e) finally: if conn: - zk.disconnect() - + conn.disconnect() ``` * Connect/Disconnect ``` -zk.connect() -zk.disconnect() +conn = zk.connect() +conn.disconnect() ``` * Disable/Enable Connected Device ``` -zk.disable_device() -zk.enable_device() +conn.disable_device() +conn.enable_device() ``` * Ger Firmware Version ``` -zk.get_firmware_version() +conn.get_firmware_version() ``` * User Operation ``` # Create user -zk.set_user(uid=1, name='Fanani M. Ihsan', privilege=const.USER_ADMIN, password='12345678', group_id='', user_id='123') +conn.set_user(uid=1, name='Fanani M. Ihsan', privilege=const.USER_ADMIN, password='12345678', group_id='', user_id='123') # Get all users (will return list of User object) -users = zk.get_users() +users = conn.get_users() # Delete User -zk.delete_user(uid=1) +conn.delete_user(uid=1) ``` * Attendance Record ``` # Get attendances (will return list of Attendance object) -attendances = zk.get_attendance() +attendances = conn.get_attendance() # Clear attendances record -zk.clear_attendance() +conn.clear_attendance() ``` * Test voice ``` -zk.test_voice() +conn.test_voice() ``` * Device Maintenance ``` # shutdown connected device -zk.power_off() +conn.power_off() # restart connected device -zk.restart() +conn.restart() ``` # Related Project diff --git a/setup.py b/setup.py index 1e076b8..5504f8c 100644 --- a/setup.py +++ b/setup.py @@ -1,18 +1,19 @@ +# -*- coding: utf-8 -*- from setuptools import setup setup( name='pyzk', version='0.4', - description='an unofficial library of zksoftware the fingerprint attendance machine.', + description='an unofficial library of zksoftware fingerprint device', url='https://github.com/fananimi/pyzk', author='Fanani M. Ihsan', author_email='fanani.mi@gmail.com', license='LICENSE.txt', packages=['zk'], - keywords = [ - 'zksoftware', - 'pyzk', + keywords=[ 'zk', + 'pyzk', + 'zksoftware', 'attendance machine', 'fingerprint', 'biometrics', diff --git a/test.py b/test.py index 5310073..37bec2e 100644 --- a/test.py +++ b/test.py @@ -1,26 +1,20 @@ # -*- coding: utf-8 -*- import sys + +from zk import ZK, const + sys.path.append("zk") -import zk -from zk import const - -conn = False -zk = zk.ZK('192.168.1.201', port=4370, timeout=5) +conn = None +zk = ZK('192.168.1.10', port=4370, timeout=5) try: print 'Connecting to device ...' conn = zk.connect() print 'Disabling device ...' - zk.disable_device() - print 'Firmware Version: : {}'.format(zk.get_firmware_version()) - # Load test create 2000 users - # for i in range(1, 2000+1): - # privilege = const.USER_DEFAULT - # if i == 1: - # privilege = const.USER_ADMIN - # print zk.set_user(uid=i, name='user #{}'.format(i), privilege=privilege, password='12345678', group_id='', user_id='{}'.format(i)) + conn.disable_device() + print 'Firmware Version: : {}'.format(conn.get_firmware_version()) # print '--- Get User ---' - users = zk.get_users() + users = conn.get_users() for user in users: privilege = 'User' if user.privilege == const.USER_ADMIN: @@ -34,18 +28,11 @@ try: print ' User ID : {}'.format(user.user_id) print "Voice Test ..." - zk.test_voice() - # print 'Restarting device ...' - # zk.restart() - # print 'Turning off device ...' - # zk.power_off() + conn.test_voice() print 'Enabling device ...' - zk.enable_device() - print 'Disconnecting to device ...' - zk.disconnect() - + conn.enable_device() except Exception, e: print "Process terminate : {}".format(e) finally: if conn: - zk.disconnect() + conn.disconnect() diff --git a/zk/__init__.py b/zk/__init__.py index b5e932d..94900e7 100644 --- a/zk/__init__.py +++ b/zk/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- from base import ZK -VERSION = (0,3) +VERSION = (0, 3) __all__ = ['ZK'] diff --git a/zk/attendance.py b/zk/attendance.py index b609383..ff5d3b3 100644 --- a/zk/attendance.py +++ b/zk/attendance.py @@ -6,7 +6,7 @@ class Attendance(object): self.status = status def __str__(self): - return self.user_id + return ': {}'.format(self.user_id) def __repr__(self): return ': {}'.format(self.user_id) diff --git a/zk/base.py b/zk/base.py index fe84925..2aad767 100644 --- a/zk/base.py +++ b/zk/base.py @@ -1,13 +1,14 @@ # -*- coding: utf-8 -*- from datetime import datetime +from socket import AF_INET, SOCK_DGRAM, socket from struct import pack, unpack -from socket import socket, AF_INET, SOCK_DGRAM from zk import const -from zk.exception import ZKErrorResponse, ZKNetworkError from zk.attendance import Attendance +from zk.exception import ZKErrorResponse, ZKNetworkError from zk.user import User + class ZK(object): is_connect = False @@ -26,7 +27,7 @@ class ZK(object): Puts a the parts that make up a packet together and packs them into a byte string ''' buf = pack('HHHH', command, checksum, session_id, reply_id) + command_string - buf = unpack('8B'+'%sB' % len(command_string), buf) + buf = unpack('8B' + '%sB' % len(command_string), buf) checksum = unpack('H', self.__create_checksum(buf))[0] reply_id += 1 if reply_id >= const.USHRT_MAX: @@ -100,8 +101,8 @@ class ZK(object): def __reverse_hex(self, hex): data = '' - for i in reversed( xrange( len(hex)/2 ) ): - data += hex[i*2:(i*2)+2] + for i in reversed(xrange(len(hex) / 2)): + data += hex[i * 2:(i * 2) + 2] return data def __decode_time(self, t): @@ -120,10 +121,10 @@ class ZK(object): hour = t % 24 t = t / 24 - day = t % 31+1 + day = t % 31 + 1 t = t / 31 - month = t % 12+1 + month = t % 12 + 1 t = t / 12 year = t + 2000 @@ -149,7 +150,7 @@ class ZK(object): self.is_connect = True # set the session id self.__sesion_id = unpack('HHHH', self.__data_recv[:8])[2] - return True + return self else: raise ZKErrorResponse("Invalid response") @@ -377,11 +378,11 @@ class ZK(object): userdata = ''.join(userdata) userdata = userdata[11:] while len(userdata) >= 72: - uid, privilege, password, name, sparator, group_id, user_id = unpack( '2s2s8s28sc7sx23s', userdata.ljust(72)[:72]) + uid, privilege, password, name, sparator, group_id, user_id = unpack('2s2s8s28sc7sx23s', userdata.ljust(72)[:72]) u1 = int(uid[0].encode("hex"), 16) u2 = int(uid[1].encode("hex"), 16) - uid = u2 + (u1*256) + uid = u2 + (u1 * 256) name = unicode(name.strip('\x00|\x01\x10x'), errors='ignore') privilege = int(privilege.encode("hex"), 16) password = unicode(password.strip('\x00|\x01\x10x'), errors='ignore') @@ -478,7 +479,7 @@ class ZK(object): attendance_data = ''.join(attendance_data) attendance_data = attendance_data[14:] while len(attendance_data) >= 38: - user_id, sparator, timestamp, status, space = unpack( '24sc4sc10s', attendance_data.ljust(40)[:40]) + user_id, sparator, timestamp, status, space = unpack('24sc4sc10s', attendance_data.ljust(40)[:40]) user_id = user_id.strip('\x00|\x01\x10x') timestamp = self.__decode_time(timestamp) @@ -493,7 +494,6 @@ class ZK(object): return attendances - def clear_attendance(self): ''' clear all attendance record diff --git a/zk/exception.py b/zk/exception.py index b6fd0ce..223f52c 100644 --- a/zk/exception.py +++ b/zk/exception.py @@ -1,8 +1,11 @@ +# -*- coding: utf-8 -*- class ZKError(Exception): pass + class ZKErrorResponse(ZKError): pass + class ZKNetworkError(ZKError): pass diff --git a/zk/user.py b/zk/user.py index 66a5e6d..46fa14a 100644 --- a/zk/user.py +++ b/zk/user.py @@ -9,8 +9,8 @@ class User(object): self.group_id = group_id self.user_id = user_id - def __repr__(self): - return self.name + def __str__(self): + return ': {}'.format(self.name) def __repr__(self): - return ': {}'.format(self.name) \ No newline at end of file + return ': {}'.format(self.name)