pyztk/zk/base.py

291 lines
9.8 KiB
Python
Raw Normal View History

2016-05-23 13:50:54 +07:00
# -*- coding: utf-8 -*-
from struct import pack, unpack
from socket import socket, AF_INET, SOCK_DGRAM
from zk import const
2016-05-25 16:28:55 +07:00
from zk.user import User
2016-05-23 13:50:54 +07:00
class ZK(object):
__data_recv = None
def __init__(self, ip, port=4370, timeout=5):
self.__address = (ip, port)
self.__sock = socket(AF_INET, SOCK_DGRAM)
self.__sock.settimeout(timeout)
2016-05-23 23:59:43 +07:00
def __create_header(self, command, checksum=0, session_id=0, reply_id=65534, command_string=''):
2016-05-23 13:50:54 +07:00
'''
Puts a the parts that make up a packet together and packs them into a byte string
'''
2016-05-23 23:59:43 +07:00
buf = pack('HHHH', command, checksum, session_id, reply_id) + command_string
2016-05-23 13:50:54 +07:00
buf = unpack('8B'+'%sB' % len(command_string), buf)
2016-05-23 23:59:43 +07:00
checksum = unpack('H', self.__create_checksum(buf))[0]
2016-05-23 13:50:54 +07:00
reply_id += 1
if reply_id >= const.USHRT_MAX:
reply_id -= const.USHRT_MAX
2016-05-23 23:59:43 +07:00
buf = pack('HHHH', command, checksum, session_id, reply_id)
2016-05-23 13:50:54 +07:00
return buf + command_string
def __create_checksum(self, p):
'''
2016-05-23 23:59:43 +07:00
Calculates the checksum of the packet to be sent to the time clock
2016-05-23 13:50:54 +07:00
Copied from zkemsdk.c
'''
l = len(p)
2016-05-23 23:59:43 +07:00
checksum = 0
2016-05-23 13:50:54 +07:00
while l > 1:
2016-05-23 23:59:43 +07:00
checksum += unpack('H', pack('BB', p[0], p[1]))[0]
2016-05-23 13:50:54 +07:00
p = p[2:]
2016-05-23 23:59:43 +07:00
if checksum > const.USHRT_MAX:
checksum -= const.USHRT_MAX
2016-05-23 13:50:54 +07:00
l -= 2
if l:
2016-05-23 23:59:43 +07:00
checksum = checksum + p[-1]
2016-05-23 13:50:54 +07:00
2016-05-23 23:59:43 +07:00
while checksum > const.USHRT_MAX:
checksum -= const.USHRT_MAX
2016-05-23 13:50:54 +07:00
2016-05-23 23:59:43 +07:00
checksum = ~checksum
2016-05-23 13:50:54 +07:00
2016-05-23 23:59:43 +07:00
while checksum < 0:
checksum += const.USHRT_MAX
2016-05-23 13:50:54 +07:00
2016-05-23 23:59:43 +07:00
return pack('H', checksum)
2016-05-25 13:21:41 +07:00
def __send_command(self, command, command_string='', response_size=8, checksum=0):
2016-05-23 23:59:43 +07:00
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': ''
}
2016-05-23 13:50:54 +07:00
def __sending_packet(self, buf):
self.__sock.sendto(buf, self.__address)
2016-05-23 21:00:43 +07:00
def __receive_packet(self, buf_size):
self.__data_recv = self.__sock.recv(buf_size)
2016-05-23 13:50:54 +07:00
@property
def __response(self):
'''
Checks a returned packet to see if it returned `CMD_ACK_OK` indicating success
'''
return unpack('HHHH', self.__data_recv[:8])[0]
@property
def __sesion_id(self):
2016-05-23 23:59:43 +07:00
if not self.__data_recv:
return 0
2016-05-23 13:50:54 +07:00
return unpack('HHHH', self.__data_recv[:8])[2]
@property
def __reply_id(self):
2016-05-23 23:59:43 +07:00
if not self.__data_recv:
return const.USHRT_MAX - 1
2016-05-23 13:50:54 +07:00
return unpack('HHHH', self.__data_recv[:8])[3]
def connect(self):
'''
connect to device
'''
command = const.CMD_CONNECT
2016-05-23 23:59:43 +07:00
cmd_response = self.__send_command(command)
cmd_response['data'] = ''
if cmd_response.get('status'):
cmd_response['message'] = 'connected'
return cmd_response
else:
return cmd_response
2016-05-23 13:50:54 +07:00
def disconnect(self):
'''
diconnect from connected device
'''
command = const.CMD_EXIT
2016-05-23 23:59:43 +07:00
cmd_response = self.__send_command(command)
cmd_response['data'] = ''
if cmd_response.get('status'):
cmd_response['message'] = 'disconnected'
return cmd_response
else:
return cmd_response
2016-05-23 13:55:09 +07:00
2016-05-23 14:41:39 +07:00
def get_firmware_version(self):
command = const.CMD_GET_VERSION
2016-05-23 23:59:43 +07:00
cmd_response = self.__send_command(command, response_size=1024)
if cmd_response.get('status'):
cmd_response['data'] = cmd_response.get('data')[8:].strip('\x00|\x01\x10x')
return cmd_response
else:
cmd_response['data'] = ''
return cmd_response
2016-05-23 14:41:39 +07:00
2016-05-23 13:55:09 +07:00
def restart(self):
'''
shutdown device
'''
command = const.CMD_RESTART
2016-05-23 23:59:43 +07:00
cmd_response = self.__send_command(command)
cmd_response['data'] = ''
if cmd_response.get('status'):
cmd_response['message'] = 'device restarted'
return cmd_response
else:
return cmd_response
2016-05-23 13:55:09 +07:00
def power_off(self):
'''
shutdown device
'''
command = const.CMD_POWEROFF
2016-05-23 23:59:43 +07:00
cmd_response = self.__send_command(command)
cmd_response['data'] = ''
if cmd_response.get('status'):
cmd_response['message'] = 'device turning off'
return cmd_response
else:
return cmd_response
2016-05-23 21:00:43 +07:00
2016-05-24 21:49:04 +07:00
def disable_device(self):
command = const.CMD_DISABLEDEVICE
cmd_response = self.__send_command(command)
cmd_response['data'] = ''
if cmd_response.get('status'):
cmd_response['message'] = 'device disabled'
return cmd_response
else:
return cmd_response
def enable_device(self):
command = const.CMD_ENABLEDEVICE
cmd_response = self.__send_command(command)
cmd_response['data'] = ''
if cmd_response.get('status'):
cmd_response['message'] = 'device enabled'
return cmd_response
else:
return cmd_response
2016-05-24 21:52:42 +07:00
def test_voice(self):
command = const.CMD_TESTVOICE
cmd_response = self.__send_command(command)
cmd_response['data'] = ''
if cmd_response.get('status'):
cmd_response['message'] = 'voice detected'
return cmd_response
else:
return cmd_response
2016-05-25 12:27:13 +07:00
def set_user(self, uid, name, privilege, password='', group_id='', user_id=''):
2016-05-25 01:32:06 +07:00
command = const.CMD_USER_WRQ
2016-05-25 12:02:56 +07:00
uid = chr(uid % 256) + chr(uid >> 8)
if privilege not in [const.USER_DEFAULT, const.USER_ADMIN]:
privilege = const.USER_DEFAULT
privilege = chr(privilege)
command_string = pack('2sc8s28sc7sx24s', uid, privilege, password, name, chr(0), group_id, user_id)
2016-05-25 13:21:41 +07:00
cmd_response = self.__send_command(command, command_string, 1024)
2016-05-25 01:32:06 +07:00
cmd_response['data'] = ''
if cmd_response.get('status'):
cmd_response['message'] = 'new user created'
return cmd_response
else:
return cmd_response
2016-05-25 15:35:45 +07:00
def __get_size_user(self):
"""Checks a returned packet to see if it returned CMD_PREPARE_DATA,
indicating that data packets are to be sent
Returns the amount of bytes that are going to be sent"""
response = self.__response
if response == const.CMD_PREPARE_DATA:
size = unpack('I', self.__data_recv[8:12])[0]
return size
else:
return 0
2016-05-23 21:00:43 +07:00
2016-05-25 15:35:45 +07:00
def get_users(self):
command = const.CMD_USERTEMP_RRQ
cmd_response = self.__send_command(command=command, response_size=1024)
if cmd_response:
bytes = self.__get_size_user()
userdata = []
if bytes:
while bytes > 0:
data_recv = self.__sock.recv(1032)
userdata.append(data_recv)
bytes -= 1024
2016-05-25 16:28:55 +07:00
data_recv = self.__sock.recv(8)
response = unpack('HHHH', data_recv[:8])[0]
if response == const.CMD_ACK_OK:
users = []
if len(userdata):
2016-05-25 17:38:59 +07:00
# The first 4 bytes don't seem to be related to the user
for x in xrange(len(userdata)):
if x > 0:
userdata[x] = userdata[x][8:]
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])
2016-05-25 16:28:55 +07:00
u1 = int( uid[0].encode("hex"), 16)
u2 = int( uid[1].encode("hex"), 16)
2016-05-25 17:38:59 +07:00
uid = u2 + (u1*256)
2016-05-25 16:28:55 +07:00
name = unicode(name.strip('\x00|\x01\x10x'), errors='ignore')
privilege = int(privilege.encode("hex"), 16)
password = unicode(password.strip('\x00|\x01\x10x'), errors='ignore')
group_id = unicode(group_id.strip('\x00|\x01\x10x'), errors='ignore')
user_id = unicode(user_id.strip('\x00|\x01\x10x'), errors='ignore')
user = User(uid, name, privilege, password, group_id, user_id)
users.append(user)
2016-05-25 17:38:59 +07:00
userdata = userdata[72:]
2016-05-25 16:28:55 +07:00
cmd_response['status'] = True
cmd_response['code'] = response
cmd_response['message'] = 'success'
cmd_response['data'] = users
return cmd_response
else:
return {
'status': False,
'code': response,
'message': 'failed',
'data': ''
}
2016-05-25 15:35:45 +07:00
else:
2016-05-25 16:28:55 +07:00
cmd_response['data'] = ''
2016-05-25 15:35:45 +07:00
return cmd_response