pyztk/zk/base.py

603 lines
20 KiB
Python
Raw Normal View History

2016-05-23 13:50:54 +07:00
# -*- coding: utf-8 -*-
2016-06-12 15:46:20 +07:00
from datetime import datetime
2016-06-24 19:00:55 +07:00
from socket import AF_INET, SOCK_DGRAM, socket
2016-05-23 13:50:54 +07:00
from struct import pack, unpack
from zk import const
2016-06-12 16:35:02 +07:00
from zk.attendance import Attendance
2016-06-24 19:00:55 +07:00
from zk.exception import ZKErrorResponse, ZKNetworkError
2016-05-25 16:28:55 +07:00
from zk.user import User
2016-05-23 13:50:54 +07:00
def make_commkey(key, session_id, ticks=50):
"""take a password and session_id and scramble them to send to the time
clock.
copied from commpro.c - MakeKey"""
key = int(key)
session_id = int(session_id)
k = 0
for i in range(32):
if (key & (1 << i)):
k = (k << 1 | 1)
else:
k = k << 1
k += session_id
k = pack(b'I', k)
k = unpack(b'BBBB', k)
k = pack(
b'BBBB',
k[0] ^ ord('Z'),
k[1] ^ ord('K'),
k[2] ^ ord('S'),
k[3] ^ ord('O'))
k = unpack(b'HH', k)
k = pack(b'HH', k[1], k[0])
B = 0xff & ticks
k = unpack(b'BBBB', k)
k = pack(
b'BBBB',
k[0] ^ B,
k[1] ^ B,
B,
k[3] ^ B)
return k
2016-06-24 19:00:55 +07:00
2016-05-23 13:50:54 +07:00
class ZK(object):
2016-05-26 12:45:28 +07:00
is_connect = False
2016-05-23 13:50:54 +07:00
2016-05-29 20:27:01 +07:00
__data_recv = None
__sesion_id = 0
__reply_id = 0
def __init__(self, ip, port=4370, timeout=60, password=0):
2017-12-06 21:11:22 +07:00
self.is_connect = False
2016-05-23 13:50:54 +07:00
self.__address = (ip, port)
self.__sock = socket(AF_INET, SOCK_DGRAM)
self.__sock.settimeout(timeout)
self.__password = password # passint
2016-05-23 13:50:54 +07:00
2016-05-29 20:27:01 +07:00
def __create_header(self, command, command_string, checksum, session_id, reply_id):
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-06-15 19:15:12 +07:00
buf = pack('HHHH', command, checksum, session_id, reply_id) + command_string
2016-06-24 19:00:55 +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-06-15 19:15:12 +07:00
2016-05-23 23:59:43 +07:00
while checksum > const.USHRT_MAX:
checksum -= const.USHRT_MAX
2016-06-15 19:15:12 +07:00
2016-05-23 23:59:43 +07:00
checksum = ~checksum
2016-06-15 19:15:12 +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-29 20:27:01 +07:00
def __send_command(self, command, command_string, checksum, session_id, reply_id, response_size):
2016-06-15 19:15:12 +07:00
'''
send command to the terminal
'''
2016-05-29 20:27:01 +07:00
buf = self.__create_header(command, command_string, checksum, session_id, reply_id)
2016-06-10 15:35:55 +07:00
try:
self.__sock.sendto(buf, self.__address)
self.__data_recv = self.__sock.recv(response_size)
except Exception, e:
raise ZKNetworkError(str(e))
2016-05-29 20:27:01 +07:00
self.__response = unpack('HHHH', self.__data_recv[:8])[0]
self.__reply_id = unpack('HHHH', self.__data_recv[:8])[3]
if self.__response in [const.CMD_ACK_OK, const.CMD_PREPARE_DATA]:
return {
'status': True,
'code': self.__response
}
else:
2016-05-23 23:59:43 +07:00
return {
'status': False,
2016-05-29 20:27:01 +07:00
'code': self.__response
2016-05-23 23:59:43 +07:00
}
2016-05-23 13:50:54 +07:00
2016-05-27 10:01:00 +07:00
def __get_data_size(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-06-12 15:46:20 +07:00
def __reverse_hex(self, hex):
data = ''
2016-06-24 19:00:55 +07:00
for i in reversed(xrange(len(hex) / 2)):
data += hex[i * 2:(i * 2) + 2]
2016-06-12 15:46:20 +07:00
return data
def __decode_time(self, t):
"""Decode a timestamp retrieved from the timeclock
copied from zkemsdk.c - DecodeTime"""
t = t.encode('hex')
t = int(self.__reverse_hex(t), 16)
2017-12-07 06:28:41 +07:00
#print "decode from %s "% format(t, '04x')
2016-06-12 15:46:20 +07:00
second = t % 60
t = t / 60
minute = t % 60
t = t / 60
hour = t % 24
t = t / 24
2016-06-24 19:00:55 +07:00
day = t % 31 + 1
2016-06-12 15:46:20 +07:00
t = t / 31
2016-06-24 19:00:55 +07:00
month = t % 12 + 1
2016-06-12 15:46:20 +07:00
t = t / 12
year = t + 2000
d = datetime(year, month, day, hour, minute, second)
return d
2017-12-07 06:28:41 +07:00
def __encode_time(self, t):
"""Encode a timestamp so that it can be read on the timeclock
"""
# formula taken from zkemsdk.c - EncodeTime
# can also be found in the technical manual
d = (
((t.year % 100) * 12 * 31 + ((t.month - 1) * 31) + t.day - 1) *
(24 * 60 * 60) + (t.hour * 60 + t.minute) * 60 + t.second
)
return d
2017-12-08 07:02:18 +07:00
2016-05-23 13:50:54 +07:00
def connect(self):
'''
2016-06-15 19:15:12 +07:00
connect to the device
2016-05-23 13:50:54 +07:00
'''
command = const.CMD_CONNECT
2016-05-29 20:27:01 +07:00
command_string = ''
checksum = 0
session_id = 0
reply_id = const.USHRT_MAX - 1
response_size = 8
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
self.__sesion_id = unpack('HHHH', self.__data_recv[:8])[2]
if cmd_response.get('code')==const.CMD_ACK_UNAUTH:
2017-12-07 06:28:41 +07:00
#print "try auth"
command_string = make_commkey(self.__password,self.__sesion_id)
cmd_response = self.__send_command(const.CMD_AUTH, command_string , checksum, self.__sesion_id, self.__reply_id, response_size)
2016-05-23 23:59:43 +07:00
if cmd_response.get('status'):
2016-05-26 12:45:28 +07:00
self.is_connect = True
2016-05-29 20:27:01 +07:00
# set the session id
2016-06-24 19:00:55 +07:00
return self
2016-05-23 23:59:43 +07:00
else:
print "connect err {} ".format(cmd_response["code"])
2016-06-10 15:35:55 +07:00
raise ZKErrorResponse("Invalid response")
2016-05-23 13:50:54 +07:00
def disconnect(self):
'''
2016-06-15 19:15:12 +07:00
diconnect from the connected device
2016-05-23 13:50:54 +07:00
'''
command = const.CMD_EXIT
2016-05-29 20:27:01 +07:00
command_string = ''
checksum = 0
session_id = self.__sesion_id
reply_id = self.__reply_id
response_size = 8
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
2016-05-23 23:59:43 +07:00
if cmd_response.get('status'):
2017-12-06 21:11:22 +07:00
self.is_connect = False
2016-05-29 20:27:01 +07:00
return True
2016-05-23 23:59:43 +07:00
else:
2016-06-10 15:35:55 +07:00
raise ZKErrorResponse("Invalid response")
2016-05-23 13:55:09 +07:00
2016-05-29 20:27:01 +07:00
def disable_device(self):
2016-05-27 08:54:17 +07:00
'''
2016-06-15 19:15:12 +07:00
disable (lock) device, ensure no activity when process run
2016-05-27 08:54:17 +07:00
'''
2016-05-30 09:47:58 +07:00
2016-05-29 20:27:01 +07:00
command = const.CMD_DISABLEDEVICE
command_string = ''
checksum = 0
session_id = self.__sesion_id
reply_id = self.__reply_id
response_size = 8
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
2016-05-23 23:59:43 +07:00
if cmd_response.get('status'):
2016-05-29 20:27:01 +07:00
return True
2016-05-23 23:59:43 +07:00
else:
2016-06-10 15:35:55 +07:00
raise ZKErrorResponse("Invalid response")
2016-05-23 14:41:39 +07:00
2016-05-29 20:27:01 +07:00
def enable_device(self):
2016-05-23 13:55:09 +07:00
'''
2016-06-15 19:15:12 +07:00
re-enable the connected device
2016-05-23 13:55:09 +07:00
'''
2016-05-30 09:47:58 +07:00
2016-05-29 20:27:01 +07:00
command = const.CMD_ENABLEDEVICE
command_string = ''
checksum = 0
session_id = self.__sesion_id
reply_id = self.__reply_id
response_size = 8
2016-05-23 13:55:09 +07:00
2016-05-29 20:27:01 +07:00
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
2016-05-23 23:59:43 +07:00
if cmd_response.get('status'):
2016-05-29 20:27:01 +07:00
return True
2016-05-23 23:59:43 +07:00
else:
2016-06-10 15:35:55 +07:00
raise ZKErrorResponse("Invalid response")
2016-05-23 13:55:09 +07:00
2016-05-29 20:27:01 +07:00
def get_firmware_version(self):
2016-05-23 13:55:09 +07:00
'''
2016-06-15 19:15:12 +07:00
return the firmware version
2016-05-23 13:55:09 +07:00
'''
2016-05-30 09:47:58 +07:00
2016-05-29 20:27:01 +07:00
command = const.CMD_GET_VERSION
command_string = ''
checksum = 0
session_id = self.__sesion_id
reply_id = self.__reply_id
response_size = 1024
2016-05-23 13:55:09 +07:00
2016-05-29 20:27:01 +07:00
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
2016-05-23 23:59:43 +07:00
if cmd_response.get('status'):
firmware_version = self.__data_recv[8:].split('\x00')[0]
2016-05-29 20:27:01 +07:00
return firmware_version
2016-05-23 23:59:43 +07:00
else:
2016-06-10 15:35:55 +07:00
raise ZKErrorResponse("Invalid response")
2016-05-23 21:00:43 +07:00
2016-06-09 11:20:39 +07:00
def get_serialnumber(self):
2016-06-15 19:15:12 +07:00
'''
return the serial number
'''
2016-06-10 11:27:49 +07:00
command = const.CMD_OPTIONS_RRQ
2016-06-09 11:20:39 +07:00
command_string = '~SerialNumber'
checksum = 0
session_id = self.__sesion_id
reply_id = self.__reply_id
response_size = 1024
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
if cmd_response.get('status'):
serialnumber = self.__data_recv[8:].split('=')[-1].split('\x00')[0]
2016-06-09 13:37:19 +07:00
return serialnumber
2016-06-09 11:20:39 +07:00
else:
2016-06-10 15:35:55 +07:00
raise ZKErrorResponse("Invalid response")
2016-06-09 11:20:39 +07:00
2016-05-29 20:27:01 +07:00
def restart(self):
2016-05-27 08:54:17 +07:00
'''
2016-06-15 19:15:12 +07:00
restart the device
2016-05-27 08:54:17 +07:00
'''
2016-05-29 20:27:01 +07:00
command = const.CMD_RESTART
command_string = ''
checksum = 0
session_id = self.__sesion_id
reply_id = self.__reply_id
response_size = 8
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
2016-05-24 21:49:04 +07:00
if cmd_response.get('status'):
2016-05-29 20:27:01 +07:00
return True
2016-05-24 21:49:04 +07:00
else:
2016-06-10 15:35:55 +07:00
raise ZKErrorResponse("Invalid response")
2016-05-24 21:49:04 +07:00
2017-12-07 06:28:41 +07:00
def get_time(self):
"""obtener la hora del equipo"""
command = const.CMD_GET_TIME
command_string = ''
checksum = 0
session_id = self.__sesion_id
reply_id = self.__reply_id
response_size = 1032
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
if cmd_response.get('status'):
return self.__decode_time(self.__data_recv[8:12])
else:
raise ZKErrorResponse("Invalid response")
def set_time(self, timestamp):
""" colocar la hora del sistema al zk """
command = const.CMD_SET_TIME
command_string = pack(b'I', self.__encode_time(timestamp))
checksum = 0
session_id = self.__sesion_id
reply_id = self.__reply_id
response_size = 8
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
if cmd_response.get('status'):
return True
else:
raise ZKErrorResponse("Invalid response")
2016-06-10 14:16:22 +07:00
def poweroff(self):
2016-05-27 08:54:17 +07:00
'''
2016-06-15 19:15:12 +07:00
shutdown the device
2016-05-27 08:54:17 +07:00
'''
2016-05-29 20:27:01 +07:00
command = const.CMD_POWEROFF
command_string = ''
checksum = 0
session_id = self.__sesion_id
reply_id = self.__reply_id
2017-12-07 06:28:41 +07:00
response_size = 1032
2016-05-29 20:27:01 +07:00
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
2016-05-24 21:49:04 +07:00
if cmd_response.get('status'):
2016-05-29 20:27:01 +07:00
return True
2016-05-24 21:49:04 +07:00
else:
2016-06-10 15:35:55 +07:00
raise ZKErrorResponse("Invalid response")
2016-05-24 21:49:04 +07:00
2016-05-24 21:52:42 +07:00
def test_voice(self):
2016-05-27 08:54:17 +07:00
'''
play test voice
'''
2016-05-30 09:47:58 +07:00
2016-05-24 21:52:42 +07:00
command = const.CMD_TESTVOICE
2016-05-29 20:27:01 +07:00
command_string = ''
checksum = 0
session_id = self.__sesion_id
reply_id = self.__reply_id
response_size = 8
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
2016-05-24 21:52:42 +07:00
if cmd_response.get('status'):
2016-05-29 20:27:01 +07:00
return True
2016-05-24 21:52:42 +07:00
else:
2016-06-10 15:35:55 +07:00
raise ZKErrorResponse("Invalid response")
2016-05-24 21:52:42 +07:00
2016-05-25 12:27:13 +07:00
def set_user(self, uid, name, privilege, password='', group_id='', user_id=''):
2016-05-27 08:54:17 +07:00
'''
create or update user by uid
'''
2016-05-30 09:47:58 +07:00
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-29 20:27:01 +07:00
checksum = 0
session_id = self.__sesion_id
reply_id = self.__reply_id
response_size = 1024
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
2016-05-25 01:32:06 +07:00
if cmd_response.get('status'):
2016-05-29 20:27:01 +07:00
return True
2016-05-25 01:32:06 +07:00
else:
2016-06-10 15:35:55 +07:00
raise ZKErrorResponse("Invalid response")
2016-06-11 13:58:49 +07:00
def delete_user(self, uid):
2016-06-15 19:15:12 +07:00
'''
delete specific user by uid
'''
2016-06-11 13:58:49 +07:00
command = const.CMD_DELETE_USER
uid = chr(uid % 256) + chr(uid >> 8)
command_string = pack('2s', uid)
checksum = 0
session_id = self.__sesion_id
reply_id = self.__reply_id
response_size = 1024
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
if cmd_response.get('status'):
return True
else:
raise ZKErrorResponse("Invalid response")
2016-05-25 01:32:06 +07:00
2016-05-25 15:35:45 +07:00
def get_users(self):
2016-05-27 08:54:17 +07:00
'''
2016-06-15 19:15:12 +07:00
return all user
2016-05-27 08:54:17 +07:00
'''
2016-05-30 09:47:58 +07:00
2016-05-25 15:35:45 +07:00
command = const.CMD_USERTEMP_RRQ
2016-07-04 13:55:29 +07:00
command_string = chr(const.FCT_USER)
2016-05-29 20:27:01 +07:00
checksum = 0
session_id = self.__sesion_id
reply_id = self.__reply_id
response_size = 1024
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
users = []
2017-12-28 23:01:12 +07:00
pac = 0
2016-05-29 20:27:01 +07:00
if cmd_response.get('status'):
if cmd_response.get('code') == const.CMD_PREPARE_DATA:
bytes = self.__get_data_size()
userdata = []
2017-12-28 23:01:12 +07:00
while True:
2016-05-25 15:35:45 +07:00
data_recv = self.__sock.recv(1032)
2017-12-10 00:39:16 +07:00
response = unpack('HHHH', data_recv[:8])[0]
2017-12-28 23:01:12 +07:00
if response == const.CMD_DATA:
pac += 1
userdata.append(data_recv[8:]) #header turncated
bytes -= 1024
elif response == const.CMD_ACK_OK:
break #without problem.
else:
#truncado! continuar?
print "broken! with %s" % response
print "user still needs %s" % bytes
break
2016-05-25 16:28:55 +07:00
if response == const.CMD_ACK_OK:
2016-05-29 20:27:01 +07:00
if userdata:
2016-05-25 17:38:59 +07:00
# The first 4 bytes don't seem to be related to the user
userdata = ''.join(userdata)
2017-12-28 23:01:12 +07:00
userdata = userdata[4:]
2016-05-25 17:38:59 +07:00
while len(userdata) >= 72:
2017-12-28 23:01:12 +07:00
uid, privilege, password, name, sparator, group_id, user_id = unpack('hc8s28sc7sx24s', userdata.ljust(72)[:72])
#u1 = int(uid[0].encode("hex"), 16)
#u2 = int(uid[1].encode("hex"), 16)
#uid = u1 + (u2 * 256)
2016-05-25 16:28:55 +07:00
privilege = int(privilege.encode("hex"), 16)
password = unicode(password.split('\x00')[0], errors='ignore')
name = unicode(name.split('\x00')[0], errors='ignore')
group_id = unicode(group_id.split('\x00')[0], errors='ignore')
user_id = unicode(user_id.split('\x00')[0], errors='ignore')
2016-05-25 16:28:55 +07:00
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
else:
2016-06-10 15:35:55 +07:00
raise ZKErrorResponse("Invalid response")
2016-05-29 20:27:01 +07:00
return users
2016-05-25 21:35:50 +07:00
2016-05-26 12:45:28 +07:00
def cancel_capture(self):
2016-05-27 08:54:17 +07:00
'''
cancel capturing finger
'''
2016-05-30 09:47:58 +07:00
2016-05-26 12:45:28 +07:00
command = const.CMD_CANCELCAPTURE
cmd_response = self.__send_command(command=command)
print cmd_response
def verify_user(self):
2016-05-27 08:54:17 +07:00
'''
verify finger
'''
2016-05-30 09:47:58 +07:00
2016-05-26 12:45:28 +07:00
command = const.CMD_STARTVERIFY
# uid = chr(uid % 256) + chr(uid >> 8)
cmd_response = self.__send_command(command=command)
print cmd_response
def enroll_user(self, uid):
2016-05-27 08:54:17 +07:00
'''
start enroll user
'''
2016-05-30 09:47:58 +07:00
2016-05-26 12:45:28 +07:00
command = const.CMD_STARTENROLL
uid = chr(uid % 256) + chr(uid >> 8)
command_string = pack('2s', uid)
cmd_response = self.__send_command(command=command, command_string=command_string)
print cmd_response
2016-06-10 11:27:49 +07:00
def clear_data(self):
2016-05-25 21:35:50 +07:00
'''
2016-06-10 11:27:49 +07:00
clear all data (include: user, attendance report, finger database )
2016-05-25 21:35:50 +07:00
'''
2016-06-10 11:27:49 +07:00
command = const.CMD_CLEAR_DATA
command_string = ''
checksum = 0
session_id = self.__sesion_id
reply_id = self.__reply_id
response_size = 1024
2016-05-30 09:47:58 +07:00
2016-06-10 11:27:49 +07:00
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
if cmd_response.get('status'):
2016-06-15 19:41:59 +07:00
return True
2016-06-10 11:27:49 +07:00
else:
2016-06-10 15:35:55 +07:00
raise ZKErrorResponse("Invalid response")
2016-05-25 21:35:50 +07:00
def get_attendance(self):
2016-06-15 19:15:12 +07:00
'''
return all attendance record
'''
2016-06-12 15:46:20 +07:00
command = const.CMD_ATTLOG_RRQ
command_string = ''
checksum = 0
session_id = self.__sesion_id
reply_id = self.__reply_id
response_size = 1024
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
attendances = []
if cmd_response.get('status'):
if cmd_response.get('code') == const.CMD_PREPARE_DATA:
bytes = self.__get_data_size()
attendance_data = []
2017-12-10 00:39:16 +07:00
pac = 1
2017-12-28 23:01:12 +07:00
while True: #limitado por respuesta no por tamaƱo
2016-06-12 15:46:20 +07:00
data_recv = self.__sock.recv(1032)
2017-12-10 00:39:16 +07:00
response = unpack('HHHH', data_recv[:8])[0]
2017-12-28 23:01:12 +07:00
#print "# %s packet response is: %s" % (pac, response)
if response == const.CMD_DATA:
pac += 1
attendance_data.append(data_recv[8:]) #header turncated
bytes -= 1024
elif response == const.CMD_ACK_OK:
break #without problem.
else:
2017-12-10 00:39:16 +07:00
#truncado! continuar?
print "broken!"
break
2017-12-28 23:01:12 +07:00
2017-12-10 00:39:16 +07:00
#print "still needs %s" % bytes
2016-06-12 15:46:20 +07:00
if response == const.CMD_ACK_OK:
if attendance_data:
attendance_data = ''.join(attendance_data)
2017-12-28 23:01:12 +07:00
attendance_data = attendance_data[4:]
while len(attendance_data) >= 40:
uid, user_id, sparator, timestamp, status, space = unpack('h24sc4sc8s', attendance_data.ljust(40)[:40])
user_id = user_id.split('\x00')[0]
2016-06-12 15:46:20 +07:00
timestamp = self.__decode_time(timestamp)
status = int(status.encode("hex"), 16)
2016-06-12 16:35:02 +07:00
2017-12-28 23:01:12 +07:00
attendance = Attendance(uid, user_id, timestamp, status)
2016-06-12 16:35:02 +07:00
attendances.append(attendance)
2016-06-12 15:46:20 +07:00
attendance_data = attendance_data[40:]
else:
raise ZKErrorResponse("Invalid response")
return attendances
2016-05-30 09:47:58 +07:00
2016-05-25 21:35:50 +07:00
def clear_attendance(self):
'''
2016-06-15 19:15:12 +07:00
clear all attendance record
2016-05-25 21:35:50 +07:00
'''
2016-06-15 19:15:12 +07:00
command = const.CMD_CLEAR_ATTLOG
command_string = ''
checksum = 0
session_id = self.__sesion_id
reply_id = self.__reply_id
response_size = 1024
2016-05-30 09:47:58 +07:00
2016-06-15 19:15:12 +07:00
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
if cmd_response.get('status'):
return True
else:
raise ZKErrorResponse("Invalid response")