make api more easy
This commit is contained in:
parent
b94bf295b1
commit
88a408d92d
34
test.py
34
test.py
@ -5,18 +5,30 @@ sys.path.append("zk")
|
|||||||
import zk
|
import zk
|
||||||
|
|
||||||
zk = zk.ZK('192.168.1.201')
|
zk = zk.ZK('192.168.1.201')
|
||||||
status, message = zk.connect()
|
print 'Connecting to device ...'
|
||||||
if status:
|
conn = zk.connect()
|
||||||
|
if conn.get('status'):
|
||||||
|
print conn
|
||||||
print 'Firmware Version: : {}'.format(zk.get_firmware_version())
|
print 'Firmware Version: : {}'.format(zk.get_firmware_version())
|
||||||
users = zk.get_users()
|
# print 'Restarting device'
|
||||||
if users:
|
# print zk.restart()
|
||||||
for uid in users:
|
# print 'Turning off device'
|
||||||
if users[uid][2] == 14:
|
# print zk.power_off()
|
||||||
level = 'Admin'
|
# # users = zk.get_users()
|
||||||
else:
|
# # if users:
|
||||||
level = 'User'
|
# # for uid in users:
|
||||||
print "[UID %d]: ID: %s, Name: %s, Level: %s, Password: %s" % ( uid, users[uid][0], users[uid][1], level, users[uid][3] )
|
# # if users[uid][2] == 14:
|
||||||
|
# # level = 'Admin'
|
||||||
|
# # else:
|
||||||
|
# # level = 'User'
|
||||||
|
# # print "[UID %d]: ID: %s, Name: %s, Level: %s, Password: %s" % ( uid, users[uid][0], users[uid][1], level, users[uid][3] )
|
||||||
|
|
||||||
|
print 'Disconnecting to device ...'
|
||||||
print zk.disconnect()
|
print zk.disconnect()
|
||||||
|
# if status:
|
||||||
|
# print 'Disonnected !'
|
||||||
|
# else:
|
||||||
|
# print 'Disconnecting Error: {}'.format(message)
|
||||||
else:
|
else:
|
||||||
print message
|
print 'Connecting Error: {}'.format(conn.get('message'))
|
||||||
|
|
||||||
|
237
zk/base.py
237
zk/base.py
@ -13,45 +13,73 @@ class ZK(object):
|
|||||||
self.__sock = socket(AF_INET, SOCK_DGRAM)
|
self.__sock = socket(AF_INET, SOCK_DGRAM)
|
||||||
self.__sock.settimeout(timeout)
|
self.__sock.settimeout(timeout)
|
||||||
|
|
||||||
def __create_header(self, command, chksum=0, session_id=0, reply_id=65534, command_string=''):
|
def __create_header(self, command, checksum=0, session_id=0, reply_id=65534, command_string=''):
|
||||||
'''
|
'''
|
||||||
Puts a the parts that make up a packet together and packs them into a byte string
|
Puts a the parts that make up a packet together and packs them into a byte string
|
||||||
'''
|
'''
|
||||||
buf = pack('HHHH', command, chksum, session_id, reply_id) + command_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)
|
||||||
chksum = unpack('H', self.__create_checksum(buf))[0]
|
checksum = unpack('H', self.__create_checksum(buf))[0]
|
||||||
reply_id += 1
|
reply_id += 1
|
||||||
if reply_id >= const.USHRT_MAX:
|
if reply_id >= const.USHRT_MAX:
|
||||||
reply_id -= const.USHRT_MAX
|
reply_id -= const.USHRT_MAX
|
||||||
|
|
||||||
buf = pack('HHHH', command, chksum, session_id, reply_id)
|
buf = pack('HHHH', command, checksum, session_id, reply_id)
|
||||||
return buf + command_string
|
return buf + command_string
|
||||||
|
|
||||||
def __create_checksum(self, p):
|
def __create_checksum(self, p):
|
||||||
'''
|
'''
|
||||||
Calculates the chksum of the packet to be sent to the time clock
|
Calculates the checksum of the packet to be sent to the time clock
|
||||||
Copied from zkemsdk.c
|
Copied from zkemsdk.c
|
||||||
'''
|
'''
|
||||||
l = len(p)
|
l = len(p)
|
||||||
chksum = 0
|
checksum = 0
|
||||||
while l > 1:
|
while l > 1:
|
||||||
chksum += unpack('H', pack('BB', p[0], p[1]))[0]
|
checksum += unpack('H', pack('BB', p[0], p[1]))[0]
|
||||||
p = p[2:]
|
p = p[2:]
|
||||||
if chksum > const.USHRT_MAX:
|
if checksum > const.USHRT_MAX:
|
||||||
chksum -= const.USHRT_MAX
|
checksum -= const.USHRT_MAX
|
||||||
l -= 2
|
l -= 2
|
||||||
if l:
|
if l:
|
||||||
chksum = chksum + p[-1]
|
checksum = checksum + p[-1]
|
||||||
|
|
||||||
while chksum > const.USHRT_MAX:
|
while checksum > const.USHRT_MAX:
|
||||||
chksum -= const.USHRT_MAX
|
checksum -= const.USHRT_MAX
|
||||||
|
|
||||||
chksum = ~chksum
|
checksum = ~checksum
|
||||||
|
|
||||||
while chksum < 0:
|
while checksum < 0:
|
||||||
chksum += const.USHRT_MAX
|
checksum += const.USHRT_MAX
|
||||||
|
|
||||||
return pack('H', chksum)
|
return pack('H', checksum)
|
||||||
|
|
||||||
|
def __send_command(self, command, checksum=0, command_string='', response_size=8):
|
||||||
|
try:
|
||||||
|
buf = self.__create_header(command, checksum, self.__sesion_id, self.__reply_id, command_string)
|
||||||
|
self.__sending_packet(buf)
|
||||||
|
self.__receive_packet(response_size)
|
||||||
|
|
||||||
|
if self.__response == const.CMD_ACK_OK:
|
||||||
|
return {
|
||||||
|
'status': True,
|
||||||
|
'code': self.__response,
|
||||||
|
'message': 'success',
|
||||||
|
'data': self.__data_recv
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
return {
|
||||||
|
'status': False,
|
||||||
|
'code': self.__response,
|
||||||
|
'message': 'failed',
|
||||||
|
'data': self.__data_recv
|
||||||
|
}
|
||||||
|
except Exception, e:
|
||||||
|
return {
|
||||||
|
'status': False,
|
||||||
|
'code': const.CMD_ACK_ERROR,
|
||||||
|
'message': str(e),
|
||||||
|
'data': ''
|
||||||
|
}
|
||||||
|
|
||||||
def __sending_packet(self, buf):
|
def __sending_packet(self, buf):
|
||||||
self.__sock.sendto(buf, self.__address)
|
self.__sock.sendto(buf, self.__address)
|
||||||
@ -68,10 +96,14 @@ class ZK(object):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def __sesion_id(self):
|
def __sesion_id(self):
|
||||||
|
if not self.__data_recv:
|
||||||
|
return 0
|
||||||
return unpack('HHHH', self.__data_recv[:8])[2]
|
return unpack('HHHH', self.__data_recv[:8])[2]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def __reply_id(self):
|
def __reply_id(self):
|
||||||
|
if not self.__data_recv:
|
||||||
|
return const.USHRT_MAX - 1
|
||||||
return unpack('HHHH', self.__data_recv[:8])[3]
|
return unpack('HHHH', self.__data_recv[:8])[3]
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
@ -80,17 +112,13 @@ class ZK(object):
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
command = const.CMD_CONNECT
|
command = const.CMD_CONNECT
|
||||||
try:
|
cmd_response = self.__send_command(command)
|
||||||
buf = self.__create_header(command=command)
|
cmd_response['data'] = ''
|
||||||
self.__sending_packet(buf)
|
if cmd_response.get('status'):
|
||||||
self.__receive_packet(8)
|
cmd_response['message'] = 'connected'
|
||||||
|
return cmd_response
|
||||||
if self.__response == const.CMD_ACK_OK:
|
|
||||||
return (True, self.__response)
|
|
||||||
else:
|
else:
|
||||||
return (False, self.__response)
|
return cmd_response
|
||||||
except Exception, e:
|
|
||||||
return (False, e)
|
|
||||||
|
|
||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
'''
|
'''
|
||||||
@ -98,30 +126,23 @@ class ZK(object):
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
command = const.CMD_EXIT
|
command = const.CMD_EXIT
|
||||||
try:
|
cmd_response = self.__send_command(command)
|
||||||
buf = self.__create_header(command=command, session_id=self.__sesion_id, reply_id=self.__reply_id)
|
cmd_response['data'] = ''
|
||||||
self.__sending_packet(buf)
|
if cmd_response.get('status'):
|
||||||
self.__receive_packet(8)
|
cmd_response['message'] = 'disconnected'
|
||||||
if self.__response == const.CMD_ACK_OK:
|
return cmd_response
|
||||||
return (True, self.__response)
|
|
||||||
else:
|
else:
|
||||||
return (False, self.__response)
|
return cmd_response
|
||||||
except Exception, e:
|
|
||||||
return (False, e)
|
|
||||||
|
|
||||||
def get_firmware_version(self):
|
def get_firmware_version(self):
|
||||||
command = const.CMD_GET_VERSION
|
command = const.CMD_GET_VERSION
|
||||||
try:
|
cmd_response = self.__send_command(command, response_size=1024)
|
||||||
buf = self.__create_header(command=command, session_id=self.__sesion_id, reply_id=self.__reply_id)
|
if cmd_response.get('status'):
|
||||||
self.__sending_packet(buf)
|
cmd_response['data'] = cmd_response.get('data')[8:].strip('\x00|\x01\x10x')
|
||||||
self.__receive_packet(1024)
|
return cmd_response
|
||||||
if self.__response == const.CMD_ACK_OK:
|
|
||||||
version = self.__data_recv[8:]
|
|
||||||
return version
|
|
||||||
else:
|
else:
|
||||||
return (False, self.__response)
|
cmd_response['data'] = ''
|
||||||
except Exception, e:
|
return cmd_response
|
||||||
return (False, e)
|
|
||||||
|
|
||||||
def restart(self):
|
def restart(self):
|
||||||
'''
|
'''
|
||||||
@ -129,16 +150,13 @@ class ZK(object):
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
command = const.CMD_RESTART
|
command = const.CMD_RESTART
|
||||||
try:
|
cmd_response = self.__send_command(command)
|
||||||
buf = self.__create_header(command=command, session_id=self.__sesion_id, reply_id=self.__reply_id)
|
cmd_response['data'] = ''
|
||||||
self.__sending_packet(buf)
|
if cmd_response.get('status'):
|
||||||
self.__receive_packet(8)
|
cmd_response['message'] = 'device restarted'
|
||||||
if self.__response == const.CMD_ACK_OK:
|
return cmd_response
|
||||||
return (True, self.__response)
|
|
||||||
else:
|
else:
|
||||||
return (False, self.__response)
|
return cmd_response
|
||||||
except Exception, e:
|
|
||||||
return (False, e)
|
|
||||||
|
|
||||||
def power_off(self):
|
def power_off(self):
|
||||||
'''
|
'''
|
||||||
@ -146,79 +164,76 @@ class ZK(object):
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
command = const.CMD_POWEROFF
|
command = const.CMD_POWEROFF
|
||||||
try:
|
cmd_response = self.__send_command(command)
|
||||||
buf = self.__create_header(command=command, session_id=self.__sesion_id, reply_id=self.__reply_id)
|
cmd_response['data'] = ''
|
||||||
self.__sending_packet(buf)
|
if cmd_response.get('status'):
|
||||||
self.__receive_packet(8)
|
cmd_response['message'] = 'device turning off'
|
||||||
if self.__response == const.CMD_ACK_OK:
|
return cmd_response
|
||||||
return (True, self.__response)
|
|
||||||
else:
|
else:
|
||||||
return (False, self.__response)
|
return cmd_response
|
||||||
except Exception, e:
|
|
||||||
return (False, e)
|
|
||||||
|
|
||||||
def __get_size_user(self):
|
# def __get_size_user(self):
|
||||||
"""Checks a returned packet to see if it returned CMD_PREPARE_DATA,
|
# """Checks a returned packet to see if it returned CMD_PREPARE_DATA,
|
||||||
indicating that data packets are to be sent
|
# indicating that data packets are to be sent
|
||||||
|
|
||||||
Returns the amount of bytes that are going to be sent"""
|
# Returns the amount of bytes that are going to be sent"""
|
||||||
response = self.__response
|
# response = self.__response
|
||||||
if response == const.CMD_PREPARE_DATA:
|
# if response == const.CMD_PREPARE_DATA:
|
||||||
size = unpack('I', self.__data_recv[8:12])[0]
|
# size = unpack('I', self.__data_recv[8:12])[0]
|
||||||
return size
|
# return size
|
||||||
else:
|
# else:
|
||||||
return 0
|
# return 0
|
||||||
|
|
||||||
def get_users(self):
|
# def get_users(self):
|
||||||
command = const.CMD_USERTEMP_RRQ
|
# command = const.CMD_USERTEMP_RRQ
|
||||||
command_string = chr(5)
|
# command_string = chr(5)
|
||||||
|
|
||||||
try:
|
# try:
|
||||||
buf = self.__create_header(command=command, session_id=self.__sesion_id, reply_id=self.__reply_id, command_string=command_string)
|
# buf = self.__create_header(command=command, session_id=self.__sesion_id, reply_id=self.__reply_id, command_string=command_string)
|
||||||
self.__sending_packet(buf)
|
# self.__sending_packet(buf)
|
||||||
self.__receive_packet(1024)
|
# self.__receive_packet(1024)
|
||||||
|
|
||||||
bytes = self.__get_size_user()
|
# bytes = self.__get_size_user()
|
||||||
userdata = []
|
# userdata = []
|
||||||
|
|
||||||
if bytes:
|
# if bytes:
|
||||||
while bytes > 0:
|
# while bytes > 0:
|
||||||
data_recv, addr = self.__sock.recvfrom(1032)
|
# data_recv, addr = self.__sock.recvfrom(1032)
|
||||||
userdata.append(data_recv)
|
# userdata.append(data_recv)
|
||||||
bytes -= 1024
|
# bytes -= 1024
|
||||||
|
|
||||||
self.__receive_packet(8)
|
# self.__receive_packet(8)
|
||||||
|
|
||||||
users = {}
|
# users = {}
|
||||||
if len(userdata) > 0:
|
# if len(userdata) > 0:
|
||||||
# The first 4 bytes don't seem to be related to the user
|
# # The first 4 bytes don't seem to be related to the user
|
||||||
for x in xrange(len(userdata)):
|
# for x in xrange(len(userdata)):
|
||||||
if x > 0:
|
# if x > 0:
|
||||||
userdata[x] = userdata[x][8:]
|
# userdata[x] = userdata[x][8:]
|
||||||
|
|
||||||
userdata = ''.join(userdata)
|
# userdata = ''.join(userdata)
|
||||||
userdata = userdata[11:]
|
# userdata = userdata[11:]
|
||||||
|
|
||||||
while len(userdata) > 72:
|
# while len(userdata) > 72:
|
||||||
uid, role, password, name, userid = unpack( '2s2s8s28sx31s', userdata.ljust(72)[:72])
|
# uid, role, password, name, userid = unpack( '2s2s8s28sx31s', userdata.ljust(72)[:72])
|
||||||
|
|
||||||
uid = int( uid.encode("hex"), 16)
|
# uid = int( uid.encode("hex"), 16)
|
||||||
# Clean up some messy characters from the user name
|
# # Clean up some messy characters from the user name
|
||||||
password = password.split('\x00', 1)[0]
|
# password = password.split('\x00', 1)[0]
|
||||||
password = unicode(password.strip('\x00|\x01\x10x'), errors='ignore')
|
# password = unicode(password.strip('\x00|\x01\x10x'), errors='ignore')
|
||||||
|
|
||||||
userid = unicode(userid.strip('\x00|\x01\x10x'), errors='ignore')
|
# userid = unicode(userid.strip('\x00|\x01\x10x'), errors='ignore')
|
||||||
|
|
||||||
name = name.split('\x00', 1)[0]
|
# name = name.split('\x00', 1)[0]
|
||||||
|
|
||||||
if not name:
|
# if not name:
|
||||||
name = uid
|
# name = uid
|
||||||
|
|
||||||
users[uid] = (userid, name, int( role.encode("hex"), 16 ), password)
|
# users[uid] = (userid, name, int( role.encode("hex"), 16 ), password)
|
||||||
|
|
||||||
userdata = userdata[72:]
|
# userdata = userdata[72:]
|
||||||
|
|
||||||
return users
|
# return users
|
||||||
except Exception, e:
|
# except Exception, e:
|
||||||
return (False, e)
|
# return (False, e)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user