2016-05-23 13:50:54 +07:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-01-31 05:17:08 +07:00
|
|
|
import sys
|
2018-04-28 07:00:13 +07:00
|
|
|
#from builtins import str
|
2016-06-12 15:46:20 +07:00
|
|
|
from datetime import datetime
|
2018-05-03 02:12:08 +07:00
|
|
|
from socket import AF_INET, SOCK_DGRAM, SOCK_STREAM, socket, timeout
|
2016-05-23 13:50:54 +07:00
|
|
|
from struct import pack, unpack
|
2018-05-05 02:26:46 +07:00
|
|
|
import codecs
|
2016-05-23 13:50:54 +07:00
|
|
|
|
|
|
|
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
|
2018-03-21 06:59:04 +07:00
|
|
|
from zk.finger import Finger
|
2016-05-23 13:50:54 +07:00
|
|
|
|
2018-06-08 21:17:27 +07:00
|
|
|
def safe_cast(val, to_type, default=None):
|
|
|
|
#https://stackoverflow.com/questions/6330071/safe-casting-in-python
|
|
|
|
try:
|
|
|
|
return to_type(val)
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
return default
|
|
|
|
|
|
|
|
|
2017-11-29 05:57:17 +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
|
2018-04-30 21:25:43 +07:00
|
|
|
|
2018-04-26 07:42:04 +07:00
|
|
|
class ZK_helper(object):
|
|
|
|
""" helper class """
|
|
|
|
def __init__(self, ip, port=4370):
|
|
|
|
self.address = (ip, port)
|
|
|
|
self.ip = ip
|
|
|
|
self.port = port
|
|
|
|
#self.timeout = timeout
|
|
|
|
#self.password = password # passint
|
|
|
|
#self.firmware = int(firmware) #TODO check minor version?
|
|
|
|
#self.tcp = tcp
|
2018-04-30 21:25:43 +07:00
|
|
|
|
2018-04-26 07:42:04 +07:00
|
|
|
def test_ping(self):
|
|
|
|
"""
|
|
|
|
Returns True if host responds to a ping request
|
|
|
|
"""
|
|
|
|
import subprocess, platform
|
|
|
|
|
|
|
|
# Ping parameters as function of OS
|
2018-05-08 02:26:31 +07:00
|
|
|
ping_str = "-n 1" if platform.system().lower()=="windows" else "-c 1 -W 5"
|
2018-04-26 07:42:04 +07:00
|
|
|
args = "ping " + " " + ping_str + " " + self.ip
|
|
|
|
need_sh = False if platform.system().lower()=="windows" else True
|
|
|
|
# Ping
|
|
|
|
return subprocess.call(args,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
shell=need_sh) == 0
|
2018-04-30 21:25:43 +07:00
|
|
|
|
2018-04-26 07:42:04 +07:00
|
|
|
def test_tcp(self):
|
|
|
|
self.client = socket(AF_INET, SOCK_STREAM)
|
|
|
|
self.client.settimeout(10) # fixed test
|
|
|
|
res = self.client.connect_ex(self.address)
|
|
|
|
self.client.close()
|
|
|
|
return res
|
2018-04-30 21:25:43 +07:00
|
|
|
|
2018-05-04 02:58:52 +07:00
|
|
|
def test_udp(self): # WIP:
|
2018-04-26 07:42:04 +07:00
|
|
|
self.client = socket(AF_INET, SOCK_DGRAM)
|
|
|
|
self.client.settimeout(10) # fixed test
|
2018-04-30 21:25:43 +07:00
|
|
|
|
2016-05-23 13:50:54 +07:00
|
|
|
class ZK(object):
|
2018-04-12 06:38:37 +07:00
|
|
|
""" Clase ZK """
|
2018-05-03 04:09:33 +07:00
|
|
|
def __init__(self, ip, port=4370, timeout=60, password=0, force_udp=False, ommit_ping=False, verbose=False):
|
2018-04-12 06:38:37 +07:00
|
|
|
""" initialize instance """
|
2017-12-06 21:11:22 +07:00
|
|
|
self.is_connect = False
|
2018-05-03 02:12:08 +07:00
|
|
|
self.is_enabled = True #let's asume
|
2018-04-26 07:42:04 +07:00
|
|
|
self.helper = ZK_helper(ip, port)
|
2016-05-23 13:50:54 +07:00
|
|
|
self.__address = (ip, port)
|
|
|
|
self.__sock = socket(AF_INET, SOCK_DGRAM)
|
|
|
|
self.__sock.settimeout(timeout)
|
2018-04-26 07:42:04 +07:00
|
|
|
self.__timeout = timeout
|
2017-11-29 05:57:17 +07:00
|
|
|
self.__password = password # passint
|
2018-04-26 07:42:04 +07:00
|
|
|
#self.firmware = int(firmware) #dummy
|
|
|
|
self.force_udp = force_udp
|
|
|
|
self.ommit_ping = ommit_ping
|
2018-05-03 04:09:33 +07:00
|
|
|
self.verbose = verbose
|
2018-04-26 07:42:04 +07:00
|
|
|
self.tcp = False
|
2018-03-21 06:59:04 +07:00
|
|
|
self.users = 0
|
|
|
|
self.fingers = 0
|
|
|
|
self.records = 0
|
|
|
|
self.dummy = 0
|
|
|
|
self.cards = 0
|
|
|
|
self.fingers_cap = 0
|
|
|
|
self.users_cap = 0
|
|
|
|
self.rec_cap = 0
|
2018-04-21 06:16:31 +07:00
|
|
|
self.faces = 0
|
|
|
|
self.faces_cap = 0
|
2018-03-21 06:59:04 +07:00
|
|
|
self.fingers_av = 0
|
|
|
|
self.users_av = 0
|
|
|
|
self.rec_av = 0
|
2018-05-12 08:12:22 +07:00
|
|
|
self.next_uid = 1
|
|
|
|
self.next_user_id='1'
|
2018-04-26 07:42:04 +07:00
|
|
|
self.user_packet_size = 28 # default zk6
|
2018-05-07 19:52:22 +07:00
|
|
|
self.end_live_capture = False
|
2018-04-12 06:38:37 +07:00
|
|
|
self.__session_id = 0
|
|
|
|
self.__reply_id = const.USHRT_MAX-1
|
|
|
|
self.__data_recv = None
|
2018-04-28 07:00:13 +07:00
|
|
|
self.__data = None
|
2018-04-30 21:25:43 +07:00
|
|
|
|
2018-04-26 07:42:04 +07:00
|
|
|
def __create_socket(self):
|
|
|
|
""" based on self.tcp"""
|
|
|
|
if self.tcp:
|
|
|
|
self.__sock = socket(AF_INET, SOCK_STREAM)
|
|
|
|
self.__sock.connect_ex(self.__address)
|
|
|
|
else:
|
|
|
|
self.__sock = socket(AF_INET, SOCK_DGRAM)
|
2018-04-30 21:25:43 +07:00
|
|
|
|
2018-04-26 07:42:04 +07:00
|
|
|
def __create_tcp_top(self, packet):
|
|
|
|
""" witch the complete packet set top header """
|
|
|
|
length = len(packet)
|
|
|
|
top = pack('<HHI', const.MACHINE_PREPARE_DATA_1, const.MACHINE_PREPARE_DATA_2, length)
|
|
|
|
return top + packet
|
2018-04-30 21:25:43 +07:00
|
|
|
|
2018-04-12 06:38:37 +07:00
|
|
|
def __create_header(self, command, command_string, 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
|
2018-04-12 06:38:37 +07:00
|
|
|
|
|
|
|
MODIFIED now, without initial checksum
|
2016-05-23 13:50:54 +07:00
|
|
|
'''
|
2018-04-12 06:38:37 +07:00
|
|
|
#checksum = 0 always? for calculating
|
|
|
|
buf = pack('HHHH', command, 0, 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)
|
|
|
|
|
2018-05-05 02:26:46 +07:00
|
|
|
def __test_tcp_top(self, packet):
|
|
|
|
""" return size!"""
|
|
|
|
if len(packet)<=8:
|
|
|
|
return 0 # invalid packet
|
|
|
|
tcp_header = unpack('<HHI', packet[:8])
|
|
|
|
if tcp_header[0] == const.MACHINE_PREPARE_DATA_1 and tcp_header[1] == const.MACHINE_PREPARE_DATA_2:
|
|
|
|
return tcp_header[2]
|
|
|
|
return 0 #never everis 0!
|
|
|
|
|
2018-04-28 07:00:13 +07:00
|
|
|
def __send_command(self, command, command_string=b'', response_size=8):
|
2016-06-15 19:15:12 +07:00
|
|
|
'''
|
|
|
|
send command to the terminal
|
|
|
|
'''
|
2018-04-12 06:38:37 +07:00
|
|
|
|
|
|
|
buf = self.__create_header(command, command_string, self.__session_id, self.__reply_id)
|
2016-06-10 15:35:55 +07:00
|
|
|
try:
|
2018-04-26 07:42:04 +07:00
|
|
|
if self.tcp:
|
|
|
|
top = self.__create_tcp_top(buf)
|
|
|
|
self.__sock.send(top)
|
|
|
|
self.__tcp_data_recv = self.__sock.recv(response_size + 8)
|
2018-05-05 02:26:46 +07:00
|
|
|
self.__tcp_length = self.__test_tcp_top(self.__tcp_data_recv)
|
|
|
|
if self.__tcp_length == 0:
|
|
|
|
raise ZKNetworkError("TCP Packet invalid")
|
2018-04-26 07:42:04 +07:00
|
|
|
self.__header = unpack('HHHH', self.__tcp_data_recv[8:16])
|
|
|
|
self.__data_recv = self.__tcp_data_recv[8:] # dirty hack
|
|
|
|
else:
|
|
|
|
self.__sock.sendto(buf, self.__address)
|
|
|
|
self.__data_recv = self.__sock.recv(response_size)
|
|
|
|
self.__header = unpack('HHHH', self.__data_recv[:8])
|
2018-04-28 07:00:13 +07:00
|
|
|
except Exception as e:
|
2016-06-10 15:35:55 +07:00
|
|
|
raise ZKNetworkError(str(e))
|
|
|
|
|
2018-04-26 07:42:04 +07:00
|
|
|
self.__response = self.__header[0]
|
|
|
|
self.__reply_id = self.__header[3]
|
2018-04-28 07:00:13 +07:00
|
|
|
self.__data = self.__data_recv[8:] #could be empty
|
2018-04-12 06:38:37 +07:00
|
|
|
if self.__response in [const.CMD_ACK_OK, const.CMD_PREPARE_DATA, const.CMD_DATA]:
|
2016-05-29 20:27:01 +07:00
|
|
|
return {
|
|
|
|
'status': True,
|
|
|
|
'code': self.__response
|
|
|
|
}
|
2018-04-12 06:38:37 +07:00
|
|
|
return {
|
|
|
|
'status': False,
|
|
|
|
'code': self.__response
|
|
|
|
}
|
2018-04-30 21:25:43 +07:00
|
|
|
|
2018-04-12 06:38:37 +07:00
|
|
|
def __ack_ok(self):
|
2018-04-26 07:42:04 +07:00
|
|
|
""" event ack ok """
|
2018-04-28 07:00:13 +07:00
|
|
|
buf = self.__create_header(const.CMD_ACK_OK, b'', self.__session_id, const.USHRT_MAX - 1)
|
2018-04-12 06:38:37 +07:00
|
|
|
try:
|
2018-04-26 07:42:04 +07:00
|
|
|
if self.tcp:
|
|
|
|
top = self.__create_tcp_top(buf)
|
|
|
|
self.__sock.send(top)
|
|
|
|
else:
|
|
|
|
self.__sock.sendto(buf, self.__address)
|
2018-04-28 07:00:13 +07:00
|
|
|
except Exception as e:
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKNetworkError(str(e))
|
|
|
|
|
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:
|
2018-04-28 07:00:13 +07:00
|
|
|
size = unpack('I', self.__data[:4])[0]
|
2016-05-27 10:01:00 +07:00
|
|
|
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"""
|
2018-04-28 07:00:13 +07:00
|
|
|
"""
|
2016-06-12 15:46:20 +07:00
|
|
|
t = t.encode('hex')
|
|
|
|
t = int(self.__reverse_hex(t), 16)
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("decode from %s "% format(t, '04x'))
|
2018-04-28 07:00:13 +07:00
|
|
|
"""
|
|
|
|
t = unpack("<I", t)[0]
|
2016-06-12 15:46:20 +07:00
|
|
|
second = t % 60
|
2018-04-28 07:00:13 +07:00
|
|
|
t = t // 60
|
2016-06-12 15:46:20 +07:00
|
|
|
|
|
|
|
minute = t % 60
|
2018-04-28 07:00:13 +07:00
|
|
|
t = t // 60
|
2016-06-12 15:46:20 +07:00
|
|
|
|
|
|
|
hour = t % 24
|
2018-04-28 07:00:13 +07:00
|
|
|
t = t // 24
|
2016-06-12 15:46:20 +07:00
|
|
|
|
2016-06-24 19:00:55 +07:00
|
|
|
day = t % 31 + 1
|
2018-04-28 07:00:13 +07:00
|
|
|
t = t // 31
|
2016-06-12 15:46:20 +07:00
|
|
|
|
2016-06-24 19:00:55 +07:00
|
|
|
month = t % 12 + 1
|
2018-04-28 07:00:13 +07:00
|
|
|
t = t // 12
|
2016-06-12 15:46:20 +07:00
|
|
|
|
|
|
|
year = t + 2000
|
|
|
|
|
|
|
|
d = datetime(year, month, day, hour, minute, second)
|
|
|
|
|
|
|
|
return d
|
2018-05-03 02:12:08 +07:00
|
|
|
def __decode_timehex(self, timehex):
|
|
|
|
"""timehex string of six bytes"""
|
|
|
|
year, month, day, hour, minute, second = unpack("BBBBBB", timehex)
|
|
|
|
year += 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
|
|
|
'''
|
2018-05-24 19:48:50 +07:00
|
|
|
self.end_live_capture = False # jic
|
2018-04-26 07:42:04 +07:00
|
|
|
if not self.ommit_ping and not self.helper.test_ping():
|
|
|
|
raise ZKNetworkError("can't reach device (ping %s)" % self.__address[0])
|
|
|
|
if not self.force_udp and self.helper.test_tcp() == 0: #ok
|
|
|
|
self.tcp = True
|
|
|
|
self.user_packet_size = 72 # default zk8
|
|
|
|
self.__create_socket()# tcp based
|
2018-04-12 06:38:37 +07:00
|
|
|
self.__session_id = 0
|
|
|
|
self.__reply_id = const.USHRT_MAX - 1
|
|
|
|
cmd_response = self.__send_command(const.CMD_CONNECT)
|
2018-04-26 07:42:04 +07:00
|
|
|
self.__session_id = self.__header[2]
|
|
|
|
if cmd_response.get('code') == const.CMD_ACK_UNAUTH:
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("try auth")
|
2018-04-12 06:38:37 +07:00
|
|
|
command_string = make_commkey(self.__password, self.__session_id)
|
|
|
|
cmd_response = self.__send_command(const.CMD_AUTH, command_string)
|
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
|
2018-04-28 07:00:13 +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:
|
2018-04-26 07:42:04 +07:00
|
|
|
if cmd_response["code"] == const.CMD_ACK_UNAUTH:
|
|
|
|
raise ZKErrorResponse("Unauthenticated")
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("connect err response {} ".format(cmd_response["code"]))
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKErrorResponse("Invalid response: Can't connect")
|
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
|
|
|
'''
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(const.CMD_EXIT)
|
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
|
2018-04-26 07:42:04 +07:00
|
|
|
if self.__sock:
|
|
|
|
self.__sock.close() #leave to GC
|
2016-05-29 20:27:01 +07:00
|
|
|
return True
|
2016-05-23 23:59:43 +07:00
|
|
|
else:
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKErrorResponse("can't disconnect")
|
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
|
|
|
'''
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(const.CMD_DISABLEDEVICE)
|
2016-05-23 23:59:43 +07:00
|
|
|
if cmd_response.get('status'):
|
2018-05-03 02:12:08 +07:00
|
|
|
self.is_enabled = False
|
2016-05-29 20:27:01 +07:00
|
|
|
return True
|
2016-05-23 23:59:43 +07:00
|
|
|
else:
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKErrorResponse("Can't Disable")
|
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
|
|
|
'''
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(const.CMD_ENABLEDEVICE)
|
2016-05-23 23:59:43 +07:00
|
|
|
if cmd_response.get('status'):
|
2018-05-03 02:12:08 +07:00
|
|
|
self.is_enabled = True
|
2016-05-29 20:27:01 +07:00
|
|
|
return True
|
2016-05-23 23:59:43 +07:00
|
|
|
else:
|
2018-04-26 07:42:04 +07:00
|
|
|
raise ZKErrorResponse("Can't enable device")
|
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
|
|
|
'''
|
2018-04-28 07:00:13 +07:00
|
|
|
cmd_response = self.__send_command(const.CMD_GET_VERSION,b'', 1024)
|
2016-05-23 23:59:43 +07:00
|
|
|
if cmd_response.get('status'):
|
2018-04-28 07:00:13 +07:00
|
|
|
firmware_version = self.__data.split(b'\x00')[0]
|
|
|
|
return firmware_version.decode()
|
2016-05-23 23:59:43 +07:00
|
|
|
else:
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKErrorResponse("Can't read frimware version")
|
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
|
2018-04-28 07:00:13 +07:00
|
|
|
command_string = b'~SerialNumber'
|
2016-06-09 11:20:39 +07:00
|
|
|
response_size = 1024
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(command, command_string, response_size)
|
2016-06-09 11:20:39 +07:00
|
|
|
if cmd_response.get('status'):
|
2018-06-07 05:12:22 +07:00
|
|
|
serialnumber = self.__data.split(b'=', 1)[-1].split(b'\x00')[0]
|
2018-04-28 07:00:13 +07:00
|
|
|
return serialnumber.decode() # string?
|
2016-06-09 11:20:39 +07:00
|
|
|
else:
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKErrorResponse("can't read serial number")
|
2016-06-09 11:20:39 +07:00
|
|
|
|
2018-03-17 07:00:24 +07:00
|
|
|
def get_platform(self):
|
|
|
|
'''
|
|
|
|
return the serial number
|
|
|
|
'''
|
|
|
|
command = const.CMD_OPTIONS_RRQ
|
2018-04-28 07:00:13 +07:00
|
|
|
command_string = b'~Platform'
|
2018-03-17 07:00:24 +07:00
|
|
|
response_size = 1024
|
|
|
|
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(command, command_string, response_size)
|
2018-03-17 07:00:24 +07:00
|
|
|
if cmd_response.get('status'):
|
2018-06-07 05:12:22 +07:00
|
|
|
platform = self.__data.split(b'=', 1)[-1].split(b'\x00')[0]
|
2018-04-28 07:00:13 +07:00
|
|
|
return platform.decode()
|
2018-03-17 07:00:24 +07:00
|
|
|
else:
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKErrorResponse("can't get platform")
|
2018-03-17 07:00:24 +07:00
|
|
|
|
2018-04-20 07:23:23 +07:00
|
|
|
def get_mac(self):
|
|
|
|
'''
|
|
|
|
return the serial number
|
|
|
|
'''
|
|
|
|
command = const.CMD_OPTIONS_RRQ
|
2018-04-28 07:00:13 +07:00
|
|
|
command_string = b'MAC'
|
2018-04-20 07:23:23 +07:00
|
|
|
response_size = 1024
|
|
|
|
|
|
|
|
cmd_response = self.__send_command(command, command_string, response_size)
|
|
|
|
if cmd_response.get('status'):
|
2018-06-07 05:12:22 +07:00
|
|
|
mac = self.__data.split(b'=', 1)[-1].split(b'\x00')[0]
|
2018-04-28 07:00:13 +07:00
|
|
|
return mac.decode()
|
2018-04-20 07:23:23 +07:00
|
|
|
else:
|
|
|
|
raise ZKErrorResponse("can't get mac")
|
|
|
|
|
2018-03-17 07:00:24 +07:00
|
|
|
def get_device_name(self):
|
|
|
|
'''
|
|
|
|
return the serial number
|
|
|
|
'''
|
|
|
|
command = const.CMD_OPTIONS_RRQ
|
2018-04-28 07:00:13 +07:00
|
|
|
command_string = b'~DeviceName'
|
2018-03-17 07:00:24 +07:00
|
|
|
response_size = 1024
|
|
|
|
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(command, command_string, response_size)
|
2018-03-17 07:00:24 +07:00
|
|
|
if cmd_response.get('status'):
|
2018-06-07 05:12:22 +07:00
|
|
|
device = self.__data.split(b'=', 1)[-1].split(b'\x00')[0]
|
2018-04-28 07:00:13 +07:00
|
|
|
return device.decode()
|
2018-03-17 07:00:24 +07:00
|
|
|
else:
|
2018-05-08 06:48:19 +07:00
|
|
|
return "" #no name
|
|
|
|
#raise ZKErrorResponse("can't read device name")
|
2018-03-17 07:00:24 +07:00
|
|
|
|
2018-04-27 02:35:54 +07:00
|
|
|
def get_face_version(self):
|
|
|
|
'''
|
|
|
|
return the face version
|
|
|
|
'''
|
|
|
|
command = const.CMD_OPTIONS_RRQ
|
2018-04-28 07:00:13 +07:00
|
|
|
command_string = b'ZKFaceVersion'
|
2018-04-27 02:35:54 +07:00
|
|
|
response_size = 1024
|
|
|
|
|
|
|
|
cmd_response = self.__send_command(command, command_string, response_size)
|
|
|
|
if cmd_response.get('status'):
|
2018-06-07 05:12:22 +07:00
|
|
|
response = self.__data.split(b'=', 1)[-1].split(b'\x00')[0]
|
2018-06-08 21:17:27 +07:00
|
|
|
return safe_cast(response, int, 0) if response else 0
|
2018-04-27 02:35:54 +07:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_fp_version(self):
|
|
|
|
'''
|
|
|
|
return the fingerprint version
|
|
|
|
'''
|
|
|
|
command = const.CMD_OPTIONS_RRQ
|
2018-04-28 07:00:13 +07:00
|
|
|
command_string = b'~ZKFPVersion'
|
2018-04-27 02:35:54 +07:00
|
|
|
response_size = 1024
|
|
|
|
|
|
|
|
cmd_response = self.__send_command(command, command_string, response_size)
|
|
|
|
if cmd_response.get('status'):
|
2018-06-07 05:12:22 +07:00
|
|
|
response = self.__data.split(b'=', 1)[-1].split(b'\x00')[0]
|
2018-06-08 21:17:27 +07:00
|
|
|
return safe_cast(response, int, 0) if response else 0
|
2018-04-27 02:35:54 +07:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2018-04-21 06:16:31 +07:00
|
|
|
def get_extend_fmt(self):
|
|
|
|
'''
|
|
|
|
determine extend fmt
|
|
|
|
'''
|
|
|
|
command = const.CMD_OPTIONS_RRQ
|
2018-04-28 07:00:13 +07:00
|
|
|
command_string = b'~ExtendFmt'
|
2018-04-21 06:16:31 +07:00
|
|
|
response_size = 1024
|
|
|
|
|
|
|
|
cmd_response = self.__send_command(command, command_string, response_size)
|
|
|
|
if cmd_response.get('status'):
|
2018-06-07 05:12:22 +07:00
|
|
|
fmt = (self.__data.split(b'=', 1)[-1].split(b'\x00')[0])
|
2018-04-21 06:16:31 +07:00
|
|
|
#definitivo? seleccionar firmware aqui?
|
2018-06-08 21:17:27 +07:00
|
|
|
return safe_cast(fmt, int, 0) if fmt else 0
|
2018-04-21 06:16:31 +07:00
|
|
|
else:
|
|
|
|
raise ZKErrorResponse("can't read extend fmt")
|
|
|
|
|
2018-04-27 02:35:54 +07:00
|
|
|
def get_user_extend_fmt(self):
|
|
|
|
'''
|
2018-05-04 02:58:52 +07:00
|
|
|
determine user extend fmt
|
2018-04-27 02:35:54 +07:00
|
|
|
'''
|
|
|
|
command = const.CMD_OPTIONS_RRQ
|
2018-04-28 07:00:13 +07:00
|
|
|
command_string = b'~UserExtFmt'
|
2018-04-27 02:35:54 +07:00
|
|
|
response_size = 1024
|
|
|
|
|
|
|
|
cmd_response = self.__send_command(command, command_string, response_size)
|
|
|
|
if cmd_response.get('status'):
|
2018-06-07 05:12:22 +07:00
|
|
|
fmt = (self.__data.split(b'=', 1)[-1].split(b'\x00')[0])
|
2018-04-27 02:35:54 +07:00
|
|
|
#definitivo? seleccionar firmware aqui?
|
2018-06-08 21:17:27 +07:00
|
|
|
return safe_cast(fmt, int, 0) if fmt else 0
|
2018-04-27 02:35:54 +07:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_face_fun_on(self):
|
|
|
|
'''
|
|
|
|
determine extend fmt
|
|
|
|
'''
|
|
|
|
command = const.CMD_OPTIONS_RRQ
|
2018-04-28 07:00:13 +07:00
|
|
|
command_string = b'FaceFunOn'
|
2018-04-27 02:35:54 +07:00
|
|
|
response_size = 1024
|
|
|
|
|
|
|
|
cmd_response = self.__send_command(command, command_string, response_size)
|
|
|
|
if cmd_response.get('status'):
|
2018-06-07 05:12:22 +07:00
|
|
|
response = (self.__data.split(b'=', 1)[-1].split(b'\x00')[0])
|
2018-04-27 02:35:54 +07:00
|
|
|
#definitivo? seleccionar firmware aqui?
|
2018-04-28 07:00:13 +07:00
|
|
|
return int(response) if response else 0
|
2018-04-27 02:35:54 +07:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_compat_old_firmware(self):
|
|
|
|
'''
|
2018-05-04 02:58:52 +07:00
|
|
|
determine old firmware
|
2018-04-27 02:35:54 +07:00
|
|
|
'''
|
|
|
|
command = const.CMD_OPTIONS_RRQ
|
2018-04-28 07:00:13 +07:00
|
|
|
command_string = b'CompatOldFirmware'
|
2018-04-27 02:35:54 +07:00
|
|
|
response_size = 1024
|
|
|
|
|
|
|
|
cmd_response = self.__send_command(command, command_string, response_size)
|
|
|
|
if cmd_response.get('status'):
|
2018-06-07 05:12:22 +07:00
|
|
|
response = (self.__data.split(b'=', 1)[-1].split(b'\x00')[0])
|
2018-04-27 02:35:54 +07:00
|
|
|
#definitivo? seleccionar firmware aqui?
|
2018-06-08 21:17:27 +07:00
|
|
|
return safe_cast(response, int, 0) if response else 0
|
2018-04-27 02:35:54 +07:00
|
|
|
else:
|
|
|
|
return None
|
2018-04-27 07:17:48 +07:00
|
|
|
|
2018-04-27 02:35:54 +07:00
|
|
|
def get_network_params(self):
|
|
|
|
ip = self.__address[0]
|
2018-04-28 07:00:13 +07:00
|
|
|
mask = b''
|
|
|
|
gate = b''
|
|
|
|
cmd_response = self.__send_command(const.CMD_OPTIONS_RRQ, b'IPAddress', 1024)
|
2018-04-27 02:35:54 +07:00
|
|
|
if cmd_response.get('status'):
|
2018-06-07 05:12:22 +07:00
|
|
|
ip = (self.__data.split(b'=', 1)[-1].split(b'\x00')[0])
|
2018-04-28 07:00:13 +07:00
|
|
|
cmd_response = self.__send_command(const.CMD_OPTIONS_RRQ, b'NetMask', 1024)
|
2018-04-27 02:35:54 +07:00
|
|
|
if cmd_response.get('status'):
|
2018-06-07 05:12:22 +07:00
|
|
|
mask = (self.__data.split(b'=', 1)[-1].split(b'\x00')[0])
|
2018-04-28 07:00:13 +07:00
|
|
|
cmd_response = self.__send_command(const.CMD_OPTIONS_RRQ, b'GATEIPAddress', 1024)
|
2018-04-27 02:35:54 +07:00
|
|
|
if cmd_response.get('status'):
|
2018-06-07 05:12:22 +07:00
|
|
|
gate = (self.__data.split(b'=', 1)[-1].split(b'\x00')[0])
|
2018-04-28 07:00:13 +07:00
|
|
|
return {'ip': ip.decode(), 'mask': mask.decode(), 'gateway': gate.decode()}
|
2018-04-27 07:17:48 +07:00
|
|
|
|
2018-03-17 07:00:24 +07:00
|
|
|
def get_pin_width(self):
|
|
|
|
'''
|
|
|
|
return the serial number
|
|
|
|
'''
|
|
|
|
command = const.CMD_GET_PINWIDTH
|
2018-04-28 07:00:13 +07:00
|
|
|
command_string = b' P'
|
2018-04-12 06:38:37 +07:00
|
|
|
response_size = 9
|
|
|
|
cmd_response = self.__send_command(command, command_string, response_size)
|
2018-03-17 07:00:24 +07:00
|
|
|
if cmd_response.get('status'):
|
2018-04-28 07:00:13 +07:00
|
|
|
width = self.__data.split(b'\x00')[0]
|
2018-03-21 06:59:04 +07:00
|
|
|
return bytearray(width)[0]
|
2018-03-17 07:00:24 +07:00
|
|
|
else:
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKErrorResponse("can0t get pin width")
|
2018-03-17 07:00:24 +07:00
|
|
|
|
2018-03-21 06:59:04 +07:00
|
|
|
def free_data(self):
|
|
|
|
""" clear buffer"""
|
|
|
|
command = const.CMD_FREE_DATA
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(command)
|
2018-03-21 06:59:04 +07:00
|
|
|
if cmd_response.get('status'):
|
|
|
|
return True
|
|
|
|
else:
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKErrorResponse("can't free data")
|
2018-03-21 06:59:04 +07:00
|
|
|
|
|
|
|
def read_sizes(self):
|
|
|
|
""" read sizes """
|
|
|
|
command = const.CMD_GET_FREE_SIZES
|
|
|
|
response_size = 1024
|
2018-04-28 07:00:13 +07:00
|
|
|
cmd_response = self.__send_command(command,b'', response_size)
|
2018-03-21 06:59:04 +07:00
|
|
|
if cmd_response.get('status'):
|
2018-06-06 19:48:30 +07:00
|
|
|
if self.verbose: print(codecs.encode(self.__data,'hex'))
|
2018-04-28 07:00:13 +07:00
|
|
|
size = len(self.__data)
|
2018-04-21 06:16:31 +07:00
|
|
|
if size == 80:
|
2018-04-28 07:00:13 +07:00
|
|
|
fields = unpack('iiiiiiiiiiiiiiiiiiii', self.__data)
|
2018-04-21 06:16:31 +07:00
|
|
|
else: #92?
|
2018-06-06 19:48:30 +07:00
|
|
|
fields = unpack('iiiiiiiiiiiiiiiiiiiiiii', self.__data[:92]) #dirty hack! we need more information
|
2018-03-21 06:59:04 +07:00
|
|
|
self.users = fields[4]
|
|
|
|
self.fingers = fields[6]
|
|
|
|
self.records = fields[8]
|
|
|
|
self.dummy = fields[10] #???
|
|
|
|
self.cards = fields[12]
|
|
|
|
self.fingers_cap = fields[14]
|
|
|
|
self.users_cap = fields[15]
|
|
|
|
self.rec_cap = fields[16]
|
|
|
|
self.fingers_av = fields[17]
|
|
|
|
self.users_av = fields[18]
|
|
|
|
self.rec_av = fields[19]
|
2018-04-21 06:16:31 +07:00
|
|
|
if len(fields) > 20:
|
|
|
|
self.faces = fields[20]
|
|
|
|
self.faces_cap = fields[22]
|
2018-03-21 06:59:04 +07:00
|
|
|
return True
|
|
|
|
else:
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKErrorResponse("can't read sizes")
|
2018-03-21 06:59:04 +07:00
|
|
|
|
2018-06-07 02:10:18 +07:00
|
|
|
def unlock(self, time=3):
|
|
|
|
'''
|
|
|
|
:param time: define time in seconds
|
|
|
|
:return:
|
|
|
|
thanks to https://github.com/SoftwareHouseMerida/pyzk/
|
|
|
|
'''
|
|
|
|
command = const.CMD_UNLOCK
|
|
|
|
command_string = pack("I",int(time)*10)
|
|
|
|
cmd_response = self.__send_command(command, command_string)
|
|
|
|
if cmd_response.get('status'):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
raise ZKErrorResponse("can't open door")
|
|
|
|
|
2018-03-21 06:59:04 +07:00
|
|
|
def __str__(self):
|
|
|
|
""" for debug"""
|
2018-04-26 07:42:04 +07:00
|
|
|
return "ZK %s://%s:%s users[%i]:%i/%i fingers:%i/%i, records:%i/%i faces:%i/%i" % (
|
|
|
|
"tcp" if self.tcp else "udp", self.__address[0], self.__address[1],
|
|
|
|
self.user_packet_size, self.users, self.users_cap,
|
|
|
|
self.fingers, self.fingers_cap,
|
|
|
|
self.records, self.rec_cap,
|
|
|
|
self.faces, self.faces_cap
|
2018-03-21 06:59:04 +07:00
|
|
|
)
|
2018-03-17 07:00:24 +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
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(command)
|
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:
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKErrorResponse("can't restart device")
|
2016-05-24 21:49:04 +07:00
|
|
|
|
2017-12-07 06:28:41 +07:00
|
|
|
def get_time(self):
|
2018-05-04 02:58:52 +07:00
|
|
|
"""get Device Time"""
|
2017-12-07 06:28:41 +07:00
|
|
|
command = const.CMD_GET_TIME
|
|
|
|
response_size = 1032
|
2018-04-28 07:00:13 +07:00
|
|
|
cmd_response = self.__send_command(command, b'', response_size)
|
2017-12-07 06:28:41 +07:00
|
|
|
if cmd_response.get('status'):
|
2018-04-28 07:00:13 +07:00
|
|
|
return self.__decode_time(self.__data[:4])
|
2017-12-07 06:28:41 +07:00
|
|
|
else:
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKErrorResponse("can't get time")
|
2018-03-21 06:59:04 +07:00
|
|
|
|
2017-12-07 06:28:41 +07:00
|
|
|
def set_time(self, timestamp):
|
2018-05-04 02:58:52 +07:00
|
|
|
""" set Device time (pass datetime object)"""
|
2017-12-07 06:28:41 +07:00
|
|
|
command = const.CMD_SET_TIME
|
|
|
|
command_string = pack(b'I', self.__encode_time(timestamp))
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(command, command_string)
|
2017-12-07 06:28:41 +07:00
|
|
|
if cmd_response.get('status'):
|
|
|
|
return True
|
|
|
|
else:
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKErrorResponse("can't set time")
|
2017-12-07 06:28:41 +07:00
|
|
|
|
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
|
2018-04-28 07:00:13 +07:00
|
|
|
command_string = b''
|
2017-12-07 06:28:41 +07:00
|
|
|
response_size = 1032
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(command, command_string, 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:
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKErrorResponse("can't poweroff")
|
2016-05-24 21:49:04 +07:00
|
|
|
|
2018-04-07 07:01:35 +07:00
|
|
|
def refresh_data(self):
|
|
|
|
'''
|
|
|
|
shutdown the device
|
|
|
|
'''
|
|
|
|
command = const.CMD_REFRESHDATA
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(command)
|
2018-04-07 07:01:35 +07:00
|
|
|
if cmd_response.get('status'):
|
|
|
|
return True
|
|
|
|
else:
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKErrorResponse("can't refresh data")
|
2018-04-07 07:01:35 +07:00
|
|
|
|
2018-04-13 06:10:01 +07:00
|
|
|
def test_voice(self, index=0):
|
2016-05-27 08:54:17 +07:00
|
|
|
'''
|
|
|
|
play test voice
|
2018-05-04 02:58:52 +07:00
|
|
|
0 acceso correcto / acceso correcto
|
2018-04-21 06:16:31 +07:00
|
|
|
1 password incorrecto / clave incorrecta
|
|
|
|
2 la memoria del terminal estĆ” llena / acceso denegado
|
|
|
|
3 usuario invalido /codigo no valido
|
|
|
|
4 intente de nuevo por favor / intente de nuevo por favor *
|
|
|
|
5 reintroduszca codigo de usuario /reintroduszca codigo
|
|
|
|
6 memoria del terminal llena /-
|
|
|
|
7 memoria de alm fich llena /-
|
|
|
|
8 huella duplicada / huella duplicada
|
|
|
|
9 acceso denegado / ya ha sido registrado
|
2018-05-03 05:18:05 +07:00
|
|
|
10 *beep* / beep kuko
|
|
|
|
11 el sistema vuelve al modo de verificacion / beep siren
|
2018-04-21 06:16:31 +07:00
|
|
|
12 por favor coloque su dedo o acerque tarjeta /-
|
2018-05-03 05:18:05 +07:00
|
|
|
13 acerca su tarjeta de nuevo /beep bell
|
2018-04-21 06:16:31 +07:00
|
|
|
14 excedido tiempo p esta operacion /-
|
|
|
|
15 coloque su dedo de nuevo /-
|
|
|
|
16 coloque su dedo por ultima vez /-
|
|
|
|
17 ATN numero de tarjeta estĆ” repetida /-
|
|
|
|
18 proceso de registro correcto * /-
|
|
|
|
19 borrado correcto /-
|
|
|
|
20 Numero de usuario / ponga la caja de ojos
|
|
|
|
21 ATN se ha llegado al max num usuarios /-
|
|
|
|
22 verificacion de usuarios /-
|
|
|
|
23 usuario no registrado /-
|
|
|
|
24 ATN se ha llegado al num max de registros /-
|
|
|
|
25 ATN la puerta no esta cerrada /-
|
|
|
|
26 registro de usuarios /-
|
|
|
|
27 borrado de usuarios /-
|
|
|
|
28 coloque su dedo /-
|
|
|
|
29 registre la tarjeta de administrador /-
|
|
|
|
30 0 /-
|
|
|
|
31 1 /-
|
|
|
|
32 2 /-
|
|
|
|
33 3 /-
|
|
|
|
34 4 /-
|
|
|
|
35 5 /-
|
|
|
|
36 6 /-
|
|
|
|
37 7 /-
|
|
|
|
38 8 /-
|
|
|
|
39 9 /-
|
|
|
|
40 PFV seleccione numero de usuario /-
|
|
|
|
41 registrar /-
|
|
|
|
42 operacion correcta /-
|
|
|
|
43 PFV acerque su tarjeta /-
|
|
|
|
43 la tarjeta ha sido registrada /-
|
|
|
|
45 error en operacion /-
|
|
|
|
46 PFV acerque tarjeta de administracion, p confirmacion /-
|
|
|
|
47 descarga de fichajes /-
|
|
|
|
48 descarga de usuarios /-
|
|
|
|
49 carga de usuarios /-
|
|
|
|
50 actualizan de firmware /-
|
|
|
|
51 ejeuctar ficheros de configuracion /-
|
|
|
|
52 confirmaciĆ³n de clave de acceso correcta /-
|
|
|
|
53 error en operacion de tclado /-
|
|
|
|
54 borrar todos los usuarios /-
|
|
|
|
55 restaurar terminal con configuracion por defecto /-
|
|
|
|
56 introduzca numero de usuario /-
|
|
|
|
57 teclado bloqueado /-
|
|
|
|
58 error en la gestiĆ³n de la tarjeta /-
|
|
|
|
59 establezca una clave de acceso /-
|
|
|
|
60 pulse el teclado /-
|
|
|
|
61 zona de accceso invalida /-
|
|
|
|
62 acceso combinado invÄŗlido /-
|
|
|
|
63 verificaciĆ³n multiusuario /-
|
|
|
|
64 modo de verificaciĆ³n invĆ”lido /-
|
|
|
|
65 - /-
|
2018-04-13 06:10:01 +07:00
|
|
|
|
2016-05-27 08:54:17 +07:00
|
|
|
'''
|
2018-04-12 06:38:37 +07:00
|
|
|
command = const.CMD_TESTVOICE
|
2018-04-13 06:10:01 +07:00
|
|
|
command_string = pack("I", index)
|
|
|
|
cmd_response = self.__send_command(command, command_string)
|
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:
|
2018-05-05 02:26:46 +07:00
|
|
|
return False #some devices doesn't support sound
|
|
|
|
#raise ZKErrorResponse("can't test voice")
|
2016-05-24 21:52:42 +07:00
|
|
|
|
2018-03-28 06:32:56 +07:00
|
|
|
def set_user(self, uid, name, privilege=0, password='', group_id='', user_id='', card=0):
|
2016-05-27 08:54:17 +07:00
|
|
|
'''
|
|
|
|
create or update user by uid
|
|
|
|
'''
|
2016-05-25 01:32:06 +07:00
|
|
|
command = const.CMD_USER_WRQ
|
2018-03-24 06:44:00 +07:00
|
|
|
if not user_id:
|
|
|
|
user_id = str(uid) #ZK6 needs uid2 == uid
|
2018-01-31 05:17:08 +07:00
|
|
|
#uid = chr(uid % 256) + chr(uid >> 8)
|
2016-05-25 12:02:56 +07:00
|
|
|
if privilege not in [const.USER_DEFAULT, const.USER_ADMIN]:
|
|
|
|
privilege = const.USER_DEFAULT
|
2018-04-26 07:42:04 +07:00
|
|
|
privilege = int(privilege)
|
|
|
|
if self.user_packet_size == 28: #self.firmware == 6:
|
|
|
|
if not group_id:
|
|
|
|
group_id = 0
|
2018-01-31 05:17:08 +07:00
|
|
|
try:
|
2018-04-28 07:00:13 +07:00
|
|
|
command_string = pack('HB5s8sIxBHI', uid, privilege, password.encode(), name.encode(), card, int(group_id), 0, int(user_id))
|
|
|
|
except Exception as e:
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print("s_h Error pack: %s" % e)
|
|
|
|
if self.verbose: print("Error pack: %s" % sys.exc_info()[0])
|
2018-04-07 07:01:35 +07:00
|
|
|
raise ZKErrorResponse("Cant pack user")
|
2018-01-31 05:17:08 +07:00
|
|
|
else:
|
2018-05-03 04:09:33 +07:00
|
|
|
name_pad = name.encode().ljust(24, b'\x00')[:24]
|
2018-04-19 06:52:38 +07:00
|
|
|
card_str = pack('i', int(card))[:4]
|
2018-04-28 07:00:13 +07:00
|
|
|
command_string = pack('HB8s24s4sx7sx24s', uid, privilege, password.encode(), name_pad, card_str, group_id.encode(), user_id.encode())
|
2018-04-26 07:42:04 +07:00
|
|
|
response_size = 1024 #TODO check response?
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(command, command_string, response_size)
|
2018-04-26 07:42:04 +07:00
|
|
|
if not cmd_response.get('status'):
|
2018-04-07 07:01:35 +07:00
|
|
|
raise ZKErrorResponse("Cant set user")
|
2018-04-26 07:42:04 +07:00
|
|
|
self.refresh_data()
|
2018-05-24 19:48:50 +07:00
|
|
|
if self.next_uid == uid:
|
|
|
|
self.next_uid += 1 # better recalculate again
|
|
|
|
if self.next_user_id == user_id:
|
|
|
|
self.next_user_id = str(self.next_uid)
|
2018-04-26 07:42:04 +07:00
|
|
|
|
2018-04-07 07:01:35 +07:00
|
|
|
def save_user_template(self, user, fingers=[]):
|
|
|
|
""" save user and template """
|
2018-04-19 07:22:03 +07:00
|
|
|
#TODO: grabado global
|
2018-04-07 07:01:35 +07:00
|
|
|
# armar paquete de huellas
|
2018-04-30 21:25:43 +07:00
|
|
|
if not isinstance(user, User):
|
|
|
|
#try uid
|
|
|
|
users = self.get_users()
|
2018-05-05 06:18:23 +07:00
|
|
|
tusers = list(filter(lambda x: x.uid==user, users))
|
|
|
|
if len(tusers) == 1:
|
|
|
|
user = tusers[0]
|
2018-04-30 21:25:43 +07:00
|
|
|
else:
|
2018-05-05 06:18:23 +07:00
|
|
|
tusers = list(filter(lambda x: x.user_id==str(user), users))
|
|
|
|
if len(tusers) == 1:
|
|
|
|
user = tusers[0]
|
|
|
|
else:
|
|
|
|
raise ZKErrorResponse("Cant find user")
|
2018-04-07 07:01:35 +07:00
|
|
|
if isinstance(fingers, Finger):
|
2018-04-30 21:25:43 +07:00
|
|
|
fingers = [fingers]
|
2018-04-07 07:01:35 +07:00
|
|
|
fpack = ""
|
|
|
|
table = ""
|
|
|
|
fnum = 0x10 # possibly flag
|
|
|
|
tstart = 0
|
|
|
|
for finger in fingers:
|
|
|
|
tfp = finger.repack_only()
|
2018-04-19 07:22:03 +07:00
|
|
|
table += pack("<bHbI", 2, user.uid, fnum + finger.fid, tstart)
|
2018-04-07 07:01:35 +07:00
|
|
|
tstart += len(tfp)
|
|
|
|
fpack += tfp
|
2018-04-26 07:42:04 +07:00
|
|
|
if self.user_packet_size == 28: #self.firmware == 6:
|
2018-04-21 06:16:31 +07:00
|
|
|
upack = user.repack29()
|
2018-04-26 07:42:04 +07:00
|
|
|
else: # 72
|
2018-04-21 06:16:31 +07:00
|
|
|
upack = user.repack73()
|
2018-04-07 07:01:35 +07:00
|
|
|
head = pack("III", len(upack), len(table), len(fpack))
|
|
|
|
packet = head + upack + table + fpack
|
|
|
|
self._send_with_buffer(packet)
|
|
|
|
command = 110 # Unknown
|
|
|
|
command_string = pack('<IHH', 12,0,8) # ??? write? WRQ user data?
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(command, command_string)
|
2018-04-07 07:01:35 +07:00
|
|
|
if not cmd_response.get('status'):
|
|
|
|
raise ZKErrorResponse("Cant save utemp")
|
|
|
|
self.refresh_data()
|
|
|
|
|
|
|
|
def _send_with_buffer(self, buffer):
|
|
|
|
MAX_CHUNK = 1024
|
|
|
|
size = len(buffer)
|
|
|
|
#free_Data
|
|
|
|
self.free_data()
|
|
|
|
# send prepare_data
|
|
|
|
command = const.CMD_PREPARE_DATA
|
|
|
|
command_string = pack('I', size)
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(command, command_string)
|
2018-04-07 07:01:35 +07:00
|
|
|
if not cmd_response.get('status'):
|
|
|
|
raise ZKErrorResponse("Cant prepare data")
|
|
|
|
remain = size % MAX_CHUNK
|
2018-04-28 07:00:13 +07:00
|
|
|
packets = (size - remain) // MAX_CHUNK
|
2018-04-07 07:01:35 +07:00
|
|
|
start = 0
|
|
|
|
for _wlk in range(packets):
|
|
|
|
self.__send_chunk(buffer[start:start+MAX_CHUNK])
|
|
|
|
start += MAX_CHUNK
|
|
|
|
if remain:
|
|
|
|
self.__send_chunk(buffer[start:start+remain])
|
2016-06-11 13:58:49 +07:00
|
|
|
|
2018-04-07 07:01:35 +07:00
|
|
|
def __send_chunk(self, command_string):
|
|
|
|
command = const.CMD_DATA
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(command, command_string)
|
2018-04-07 07:01:35 +07:00
|
|
|
if cmd_response.get('status'):
|
|
|
|
return True #refres_data (1013)?
|
|
|
|
else:
|
|
|
|
raise ZKErrorResponse("Cant send chunk")
|
|
|
|
|
2018-05-05 06:18:23 +07:00
|
|
|
def delete_user_template(self, uid=0, temp_id=0, user_id=''):
|
2018-03-28 06:32:56 +07:00
|
|
|
"""
|
|
|
|
Delete specific template
|
2018-05-03 04:09:33 +07:00
|
|
|
for tcp via user_id:
|
2018-03-28 06:32:56 +07:00
|
|
|
"""
|
2018-06-06 19:48:30 +07:00
|
|
|
if self.tcp and user_id:
|
|
|
|
command = 134 # unknown?
|
|
|
|
command_string = pack('<24sB', str(user_id), temp_id)
|
|
|
|
cmd_response = self.__send_command(command, command_string)
|
|
|
|
# users = list(filter(lambda x: x.uid==uid, users))
|
|
|
|
if cmd_response.get('status'):
|
|
|
|
return True #refres_data (1013)?
|
|
|
|
else:
|
|
|
|
return False # probably empty!
|
2018-05-05 06:18:23 +07:00
|
|
|
if not uid:
|
|
|
|
users = self.get_users()
|
|
|
|
users = list(filter(lambda x: x.user_id==str(user_id), users))
|
|
|
|
if not users:
|
|
|
|
return False
|
|
|
|
uid = users[0].uid
|
2018-03-28 06:32:56 +07:00
|
|
|
command = const.CMD_DELETE_USERTEMP
|
|
|
|
command_string = pack('hb', uid, temp_id)
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(command, command_string)
|
2018-05-05 02:26:46 +07:00
|
|
|
# users = list(filter(lambda x: x.uid==uid, users))
|
2018-03-28 06:32:56 +07:00
|
|
|
if cmd_response.get('status'):
|
|
|
|
return True #refres_data (1013)?
|
|
|
|
else:
|
2018-04-07 07:01:35 +07:00
|
|
|
return False # probably empty!
|
2018-03-28 06:32:56 +07:00
|
|
|
|
2018-04-26 07:42:04 +07:00
|
|
|
def delete_user(self, uid=0, user_id=''):
|
2016-06-15 19:15:12 +07:00
|
|
|
'''
|
|
|
|
delete specific user by uid
|
|
|
|
'''
|
2018-04-26 07:42:04 +07:00
|
|
|
"""if self.tcp: should work but not tested
|
|
|
|
if not user_id:
|
|
|
|
#we need user_id (uid2)
|
|
|
|
users = self.get_users()
|
|
|
|
if len(users) == 1:
|
|
|
|
user_id = users[0].user_id
|
|
|
|
else: #double? posibly empty
|
|
|
|
return False #can't enrool
|
|
|
|
command = 133 #const.CMD_DELETE_USER_2
|
|
|
|
command_string = pack('24s',str(user_id))
|
|
|
|
else:"""
|
2018-05-05 06:18:23 +07:00
|
|
|
if not uid:
|
|
|
|
users = self.get_users()
|
|
|
|
users = list(filter(lambda x: x.user_id==str(user_id), users))
|
|
|
|
if not users:
|
|
|
|
return False
|
|
|
|
uid = users[0].uid
|
2016-06-11 13:58:49 +07:00
|
|
|
command = const.CMD_DELETE_USER
|
2018-04-07 07:01:35 +07:00
|
|
|
command_string = pack('h', uid)
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(command, command_string)
|
2018-04-26 07:42:04 +07:00
|
|
|
if not cmd_response.get('status'):
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKErrorResponse("can't delete user")
|
2018-04-26 07:42:04 +07:00
|
|
|
self.refresh_data()
|
2018-06-06 19:48:30 +07:00
|
|
|
if uid == (self.next_uid - 1):
|
|
|
|
self.next_uid = uid # quick undo
|
2018-03-28 06:32:56 +07:00
|
|
|
|
2018-05-05 06:18:23 +07:00
|
|
|
def get_user_template(self, uid, temp_id=0, user_id=''):
|
2018-05-03 04:09:33 +07:00
|
|
|
""" ZKFinger VX10.0
|
|
|
|
for tcp:
|
2018-05-04 02:58:52 +07:00
|
|
|
command = const.CMD_USERTEMP_RRQ (doesn't work always)
|
|
|
|
command_string = pack('hb', uid, temp_id)
|
2018-05-03 04:09:33 +07:00
|
|
|
"""
|
2018-05-05 06:18:23 +07:00
|
|
|
if not uid:
|
|
|
|
users = self.get_users()
|
|
|
|
users = list(filter(lambda x: x.user_id==str(user_id), users))
|
|
|
|
if not users:
|
|
|
|
return False
|
|
|
|
uid = users[0].uid
|
|
|
|
for _retries in range(3):
|
2018-05-26 03:44:38 +07:00
|
|
|
command = 88 # comando secreto!!! GET_USER_TEMPLATE
|
2018-05-05 06:18:23 +07:00
|
|
|
command_string = pack('hb', uid, temp_id)
|
|
|
|
response_size = 1024 + 8
|
|
|
|
cmd_response = self.__send_command(command, command_string, response_size)
|
|
|
|
data = self.__recieve_chunk()
|
|
|
|
if data is not None:
|
|
|
|
resp = data[:-1] # 01, valid byte?
|
|
|
|
if resp[-6:] == b'\x00\x00\x00\x00\x00\x00': # padding? bug?
|
|
|
|
resp = resp[:-6]
|
|
|
|
return Finger(uid, temp_id, 1, resp)
|
|
|
|
if self.verbose: print ("retry get_user_template")
|
|
|
|
else:
|
|
|
|
if self.verbose: print ("can't read/find finger")
|
|
|
|
return None
|
|
|
|
#-----------------------------------------------------------------
|
2018-03-28 06:32:56 +07:00
|
|
|
if not cmd_response.get('status'):
|
2018-04-13 06:10:01 +07:00
|
|
|
return None #("can't get user template")
|
2018-03-28 06:32:56 +07:00
|
|
|
#else
|
2018-05-03 04:09:33 +07:00
|
|
|
if cmd_response.get('code') == const.CMD_DATA: # less than 1024!!!
|
2018-05-04 02:58:52 +07:00
|
|
|
resp = self.__data[:-1]
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.tcp:
|
2018-05-04 02:58:52 +07:00
|
|
|
if resp[-6:] == b'\x00\x00\x00\x00\x00\x00': # padding? bug?
|
|
|
|
resp = resp[:-6]
|
|
|
|
if self.verbose: print("tcp too small!")
|
|
|
|
return Finger(uid, temp_id, 1, resp)
|
2018-03-28 06:32:56 +07:00
|
|
|
if cmd_response.get('code') == const.CMD_PREPARE_DATA:
|
2018-05-03 04:09:33 +07:00
|
|
|
data = []
|
2018-03-28 06:32:56 +07:00
|
|
|
bytes = self.__get_data_size() #TODO: check with size
|
2018-05-04 02:58:52 +07:00
|
|
|
size = bytes
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.tcp:
|
|
|
|
data_recv = self.__sock.recv(bytes + 32)
|
2018-05-05 02:26:46 +07:00
|
|
|
tcp_length = self.__test_tcp_top(data_recv)
|
|
|
|
if tcp_length == 0:
|
2018-05-05 06:18:23 +07:00
|
|
|
print ("Incorrect tcp packet")
|
2018-05-05 02:26:46 +07:00
|
|
|
return None
|
2018-05-03 04:09:33 +07:00
|
|
|
recieved = len(data_recv)
|
2018-05-26 03:44:38 +07:00
|
|
|
if self.verbose: print ("recieved {}, size {} rec {}".format(recieved, size, data_recv.encode('hex'))) #todo python3
|
2018-05-03 04:09:33 +07:00
|
|
|
tcp_length = unpack('HHI', data_recv[:8])[2] #bytes+8
|
|
|
|
if tcp_length < (bytes + 8):
|
|
|
|
if self.verbose: print ("request chunk too big!")
|
|
|
|
response = unpack('HHHH', data_recv[8:16])[0]
|
|
|
|
if recieved >= (bytes + 32): #complete
|
|
|
|
if response == const.CMD_DATA:
|
2018-05-04 02:58:52 +07:00
|
|
|
resp = data_recv[16:bytes+16][:size-1] # no ack?
|
|
|
|
if resp[-6:] == b'\x00\x00\x00\x00\x00\x00': # padding? bug?
|
2018-05-05 02:26:46 +07:00
|
|
|
if self.verbose: print ("cutting from",len(resp))
|
2018-05-04 02:58:52 +07:00
|
|
|
resp = resp[:-6]
|
|
|
|
if self.verbose: print ("resp len1", len(resp))
|
|
|
|
return Finger(uid, temp_id, 1, resp) #mistery
|
2018-05-03 04:09:33 +07:00
|
|
|
else:
|
|
|
|
if self.verbose: print("broken packet!!!")
|
|
|
|
return None #broken
|
|
|
|
else: # incomplete
|
2018-05-05 02:26:46 +07:00
|
|
|
if self.verbose: print ("try incomplete")
|
2018-05-03 04:09:33 +07:00
|
|
|
data.append(data_recv[16:]) # w/o tcp and header
|
|
|
|
bytes -= recieved-16
|
|
|
|
while bytes>0: #jic
|
|
|
|
data_recv = self.__sock.recv(bytes) #ideal limit?
|
|
|
|
recieved = len(data_recv)
|
2018-05-05 02:26:46 +07:00
|
|
|
if self.verbose: print ("partial recv {}".format(recieved))
|
2018-05-03 04:09:33 +07:00
|
|
|
data.append(data_recv) # w/o tcp and header
|
|
|
|
bytes -= recieved
|
|
|
|
data_recv = self.__sock.recv(16)
|
|
|
|
response = unpack('HHHH', data_recv[8:16])[0]
|
|
|
|
if response == const.CMD_ACK_OK:
|
2018-05-04 02:58:52 +07:00
|
|
|
resp = b''.join(data)[:size-1] # testing
|
|
|
|
if resp[-6:] == b'\x00\x00\x00\x00\x00\x00':
|
|
|
|
resp = resp[:-6]
|
|
|
|
if self.verbose: print ("resp len2", len(resp))
|
2018-05-03 04:09:33 +07:00
|
|
|
return Finger(uid, temp_id, 1, resp)
|
|
|
|
#data_recv[bytes+16:].encode('hex') #included CMD_ACK_OK
|
|
|
|
if self.verbose: print("bad response %s" % data_recv)
|
|
|
|
if self.verbose: print(data)
|
|
|
|
return None
|
|
|
|
#else udp
|
2018-03-28 06:32:56 +07:00
|
|
|
size = bytes
|
|
|
|
while True: #limitado por respuesta no por tamaƱo
|
2018-05-03 04:09:33 +07:00
|
|
|
data_recv = self.__sock.recv(response_size)
|
2018-03-28 06:32:56 +07:00
|
|
|
response = unpack('HHHH', data_recv[:8])[0]
|
2018-05-05 02:26:46 +07:00
|
|
|
if self.verbose: print("# packet response is: {}".format(response))
|
2018-03-28 06:32:56 +07:00
|
|
|
if response == const.CMD_DATA:
|
|
|
|
data.append(data_recv[8:]) #header turncated
|
|
|
|
bytes -= 1024
|
|
|
|
elif response == const.CMD_ACK_OK:
|
|
|
|
break #without problem.
|
|
|
|
else:
|
|
|
|
#truncado! continuar?
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print("broken!")
|
2018-03-28 06:32:56 +07:00
|
|
|
break
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print("still needs %s" % bytes)
|
2018-05-05 02:26:46 +07:00
|
|
|
data = b''.join(data)[:-1]
|
|
|
|
if data[-6:] == b'\x00\x00\x00\x00\x00\x00': # padding? bug?
|
|
|
|
data = data[:-6]
|
2018-03-28 06:32:56 +07:00
|
|
|
#uid 32 fing 03, starts with 4d-9b-53-53-32-31
|
2018-05-04 02:58:52 +07:00
|
|
|
#CMD_USERTEMP_RRQ desn't need [:-1]
|
2018-05-05 02:26:46 +07:00
|
|
|
return Finger(uid, temp_id, 1, data)
|
2018-03-24 06:44:00 +07:00
|
|
|
|
2018-03-21 06:59:04 +07:00
|
|
|
def get_templates(self):
|
2018-03-28 06:32:56 +07:00
|
|
|
""" return array of all fingers """
|
2018-03-24 06:44:00 +07:00
|
|
|
templates = []
|
|
|
|
templatedata, size = self.read_with_buffer(const.CMD_DB_RRQ, const.FCT_FINGERTMP)
|
|
|
|
if size < 4:
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print("WRN: no user data") # debug
|
2018-03-24 06:44:00 +07:00
|
|
|
return []
|
|
|
|
total_size = unpack('i', templatedata[0:4])[0]
|
2018-05-04 02:58:52 +07:00
|
|
|
print ("get template total size {}, size {} len {}".format(total_size, size, len(templatedata)))
|
2018-03-24 06:44:00 +07:00
|
|
|
templatedata = templatedata[4:] #total size not used
|
2018-04-26 07:42:04 +07:00
|
|
|
# ZKFinger VX10.0 the only finger firmware tested
|
|
|
|
while total_size:
|
2018-05-04 02:58:52 +07:00
|
|
|
#print ("total_size {}".format(total_size))
|
2018-04-26 07:42:04 +07:00
|
|
|
size, uid, fid, valid = unpack('HHbb',templatedata[:6])
|
|
|
|
template = unpack("%is" % (size-6), templatedata[6:size])[0]
|
2018-05-03 04:09:33 +07:00
|
|
|
finger = Finger(uid, fid, valid, template)
|
|
|
|
if self.verbose: print(finger) # test
|
2018-04-26 07:42:04 +07:00
|
|
|
templates.append(finger)
|
|
|
|
templatedata = templatedata[size:]
|
|
|
|
total_size -= size
|
2018-03-24 06:44:00 +07:00
|
|
|
return templates
|
2016-05-25 01:32:06 +07:00
|
|
|
|
2018-04-26 07:42:04 +07:00
|
|
|
def get_users(self): #ALWAYS CALL TO GET correct user_packet_size
|
2018-03-24 06:44:00 +07:00
|
|
|
""" return all user """
|
2018-04-26 07:42:04 +07:00
|
|
|
self.read_sizes() # last update
|
2018-05-12 08:12:22 +07:00
|
|
|
if self.users == 0: #lazy
|
|
|
|
self.next_uid = 1
|
|
|
|
self.next_user_id='1'
|
2018-04-26 07:42:04 +07:00
|
|
|
return []
|
2018-03-24 06:44:00 +07:00
|
|
|
users = []
|
2018-05-12 08:12:22 +07:00
|
|
|
max_uid = 0
|
2018-03-24 06:44:00 +07:00
|
|
|
userdata, size = self.read_with_buffer(const.CMD_USERTEMP_RRQ, const.FCT_USER)
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print("user size %i" % size)
|
2018-04-26 07:42:04 +07:00
|
|
|
if size <= 4:
|
2018-05-12 08:12:22 +07:00
|
|
|
print("WRN: missing user data")# debug
|
2018-03-24 06:44:00 +07:00
|
|
|
return []
|
2018-04-26 07:42:04 +07:00
|
|
|
total_size = unpack("I",userdata[:4])[0]
|
|
|
|
self.user_packet_size = total_size / self.users
|
|
|
|
if not self.user_packet_size in [28, 72]:
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print("WRN packet size would be %i" % self.user_packet_size)
|
2018-04-26 07:42:04 +07:00
|
|
|
userdata = userdata[4:]
|
|
|
|
if self.user_packet_size == 28:
|
2018-03-24 06:44:00 +07:00
|
|
|
while len(userdata) >= 28:
|
2018-04-28 07:00:13 +07:00
|
|
|
uid, privilege, password, name, card, group_id, timezone, user_id = unpack('<HB5s8sIxBhI',userdata.ljust(28, b'\x00')[:28])
|
2018-05-12 08:12:22 +07:00
|
|
|
if uid > max_uid: max_uid = uid
|
2018-04-28 07:00:13 +07:00
|
|
|
password = (password.split(b'\x00')[0]).decode(errors='ignore')
|
|
|
|
name = (name.split(b'\x00')[0]).decode(errors='ignore').strip()
|
|
|
|
#card = unpack('I', card)[0] #or hex value?
|
2018-03-24 06:44:00 +07:00
|
|
|
group_id = str(group_id)
|
|
|
|
user_id = str(user_id)
|
|
|
|
#TODO: check card value and find in ver8
|
|
|
|
if not name:
|
|
|
|
name = "NN-%s" % user_id
|
2018-04-07 07:01:35 +07:00
|
|
|
user = User(uid, name, privilege, password, group_id, user_id, card)
|
2018-03-24 06:44:00 +07:00
|
|
|
users.append(user)
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print("[6]user:",uid, privilege, password, name, card, group_id, timezone, user_id)
|
2018-03-24 06:44:00 +07:00
|
|
|
userdata = userdata[28:]
|
|
|
|
else:
|
|
|
|
while len(userdata) >= 72:
|
2018-04-28 07:00:13 +07:00
|
|
|
uid, privilege, password, name, card, group_id, user_id = unpack('<HB8s24sIx7sx24s', userdata.ljust(72, b'\x00')[:72])
|
2018-03-24 06:44:00 +07:00
|
|
|
#u1 = int(uid[0].encode("hex"), 16)
|
|
|
|
#u2 = int(uid[1].encode("hex"), 16)
|
|
|
|
#uid = u1 + (u2 * 256)
|
2018-04-28 07:00:13 +07:00
|
|
|
#privilege = int(privilege.encode("hex"), 16)
|
|
|
|
password = (password.split(b'\x00')[0]).decode(errors='ignore')
|
|
|
|
name = (name.split(b'\x00')[0]).decode(errors='ignore').strip()
|
|
|
|
group_id = (group_id.split(b'\x00')[0]).decode(errors='ignore').strip()
|
|
|
|
user_id = (user_id.split(b'\x00')[0]).decode(errors='ignore')
|
2018-05-12 08:12:22 +07:00
|
|
|
if uid > max_uid: max_uid = uid
|
2018-04-28 07:00:13 +07:00
|
|
|
#card = int(unpack('I', separator)[0])
|
2018-03-24 06:44:00 +07:00
|
|
|
if not name:
|
|
|
|
name = "NN-%s" % user_id
|
2018-04-18 07:31:18 +07:00
|
|
|
user = User(uid, name, privilege, password, group_id, user_id, card)
|
2018-03-24 06:44:00 +07:00
|
|
|
users.append(user)
|
|
|
|
userdata = userdata[72:]
|
2018-05-12 08:12:22 +07:00
|
|
|
#get limits!
|
|
|
|
max_uid += 1
|
|
|
|
self.next_uid = max_uid
|
|
|
|
self.next_user_id = str(max_uid)
|
|
|
|
#re check
|
|
|
|
while True:
|
|
|
|
if any(u for u in users if u.user_id == self.next_user_id):
|
|
|
|
max_uid += 1
|
|
|
|
self.next_user_id = str(max_uid)
|
|
|
|
else:
|
|
|
|
break
|
2018-03-24 06:44:00 +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-26 12:45:28 +07:00
|
|
|
command = const.CMD_CANCELCAPTURE
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(command)
|
2018-03-24 06:44:00 +07:00
|
|
|
return bool(cmd_response.get('status'))
|
2016-05-26 12:45:28 +07:00
|
|
|
|
|
|
|
def verify_user(self):
|
2016-05-27 08:54:17 +07:00
|
|
|
'''
|
2018-04-13 06:10:01 +07:00
|
|
|
start verify finger mode (after capture)
|
2016-05-27 08:54:17 +07:00
|
|
|
'''
|
2016-05-26 12:45:28 +07:00
|
|
|
command = const.CMD_STARTVERIFY
|
|
|
|
# uid = chr(uid % 256) + chr(uid >> 8)
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(command)
|
2018-04-07 07:01:35 +07:00
|
|
|
if cmd_response.get('status'):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
raise ZKErrorResponse("Cant Verify")
|
|
|
|
|
2018-04-12 06:38:37 +07:00
|
|
|
def reg_event(self, flags):
|
|
|
|
""" reg events, """
|
|
|
|
command = const.CMD_REG_EVENT
|
|
|
|
command_string = pack ("I", flags)
|
|
|
|
cmd_response = self.__send_command(command, command_string)
|
|
|
|
if not cmd_response.get('status'):
|
|
|
|
raise ZKErrorResponse("cant' reg events %i" % flags)
|
2018-04-20 07:23:23 +07:00
|
|
|
|
|
|
|
def set_sdk_build_1(self):
|
|
|
|
""" """
|
|
|
|
command = const.CMD_OPTIONS_WRQ
|
2018-04-28 07:00:13 +07:00
|
|
|
command_string = b"SDKBuild=1"
|
2018-04-20 07:23:23 +07:00
|
|
|
cmd_response = self.__send_command(command, command_string)
|
|
|
|
if not cmd_response.get('status'):
|
2018-04-27 07:17:48 +07:00
|
|
|
return False #raise ZKErrorResponse("can't set sdk build ")
|
|
|
|
return True
|
2018-05-03 02:12:08 +07:00
|
|
|
|
2018-04-26 07:42:04 +07:00
|
|
|
def enroll_user(self, uid=0, temp_id=0, user_id=''):
|
2016-05-27 08:54:17 +07:00
|
|
|
'''
|
|
|
|
start enroll user
|
2018-05-03 02:12:08 +07:00
|
|
|
we need user_id (uid2)
|
2016-05-27 08:54:17 +07:00
|
|
|
'''
|
2016-05-26 12:45:28 +07:00
|
|
|
command = const.CMD_STARTENROLL
|
2018-04-26 07:42:04 +07:00
|
|
|
done = False
|
2018-05-03 02:12:08 +07:00
|
|
|
if not user_id:
|
|
|
|
#we need user_id (uid2)
|
|
|
|
users = self.get_users()
|
2018-05-03 04:09:33 +07:00
|
|
|
users = list(filter(lambda x: x.uid==uid, users))
|
2018-05-03 02:12:08 +07:00
|
|
|
if len(users) >= 1:
|
|
|
|
user_id = users[0].user_id
|
|
|
|
else: #double? posibly empty
|
|
|
|
return False #can't enroll
|
2018-04-26 07:42:04 +07:00
|
|
|
if self.tcp:
|
2018-05-03 04:09:33 +07:00
|
|
|
command_string = pack('<24sbb',str(user_id).encode(), temp_id, 1) # el 1 es misterio
|
2018-04-26 07:42:04 +07:00
|
|
|
else:
|
2018-05-03 02:12:08 +07:00
|
|
|
command_string = pack('<Ib', int(user_id), temp_id) #
|
2018-04-26 07:42:04 +07:00
|
|
|
self.cancel_capture()
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(command, command_string)
|
2018-04-07 07:01:35 +07:00
|
|
|
if not cmd_response.get('status'):
|
|
|
|
raise ZKErrorResponse("Cant Enroll user #%i [%i]" %(uid, temp_id))
|
|
|
|
#retorna rapido toca esperar un reg event
|
2018-04-26 07:42:04 +07:00
|
|
|
self.__sock.settimeout(60)# default 1min for finger
|
2018-04-12 06:38:37 +07:00
|
|
|
attempts = 3
|
|
|
|
while attempts:
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print("A:%i esperando primer regevent" % attempts)
|
2018-04-12 06:38:37 +07:00
|
|
|
data_recv = self.__sock.recv(1032) # timeout? tarda bastante...
|
|
|
|
self.__ack_ok()
|
2018-05-04 02:58:52 +07:00
|
|
|
if self.verbose: print(codecs.encode(data_recv,'hex'))
|
2018-04-26 07:42:04 +07:00
|
|
|
if self.tcp:
|
|
|
|
if len(data_recv) > 16: #not empty
|
2018-04-28 07:00:13 +07:00
|
|
|
res = unpack("H", data_recv.ljust(24,b"\x00")[16:18])[0]
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print("res %i" % res)
|
2018-04-26 07:42:04 +07:00
|
|
|
if res == 0 or res == 6 or res == 4:
|
|
|
|
# 6 timeout, 4 mismatch error, 0 can't start(why?)
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("posible timeout o reg Fallido")
|
2018-04-26 07:42:04 +07:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
if len(data_recv) > 8: #not empty
|
2018-04-28 07:00:13 +07:00
|
|
|
res = unpack("H", data_recv.ljust(16,b"\x00")[8:10])[0]
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print("res %i" % res)
|
2018-04-26 07:42:04 +07:00
|
|
|
if res == 6 or res == 4:
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("posible timeout o reg Fallido")
|
2018-04-26 07:42:04 +07:00
|
|
|
break
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("A:%i esperando 2do regevent" % attempts)
|
2018-04-12 06:38:37 +07:00
|
|
|
data_recv = self.__sock.recv(1032) # timeout? tarda bastante...
|
|
|
|
self.__ack_ok()
|
2018-05-04 02:58:52 +07:00
|
|
|
if self.verbose: print (codecs.encode(data_recv, 'hex'))
|
2018-04-26 07:42:04 +07:00
|
|
|
if self.tcp:
|
|
|
|
if len(data_recv) > 8: #not empty
|
2018-04-28 07:00:13 +07:00
|
|
|
res = unpack("H", data_recv.ljust(24,b"\x00")[16:18])[0]
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print("res %i" % res)
|
2018-04-26 07:42:04 +07:00
|
|
|
if res == 6 or res == 4:
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("posible timeout o reg Fallido")
|
2018-04-26 07:42:04 +07:00
|
|
|
break
|
|
|
|
elif res == 0x64:
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("ok, continue?")
|
2018-04-26 07:42:04 +07:00
|
|
|
attempts -= 1
|
|
|
|
else:
|
|
|
|
if len(data_recv) > 8: #not empty
|
2018-04-28 07:00:13 +07:00
|
|
|
res = unpack("H", data_recv.ljust(16,b"\x00")[8:10])[0]
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print("res %i" % res)
|
2018-04-26 07:42:04 +07:00
|
|
|
if res == 6 or res == 4:
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("posible timeout o reg Fallido")
|
2018-04-26 07:42:04 +07:00
|
|
|
break
|
|
|
|
elif res == 0x64:
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("ok, continue?")
|
2018-04-26 07:42:04 +07:00
|
|
|
attempts -= 1
|
|
|
|
if attempts == 0:
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("esperando 3er regevent")
|
2018-04-26 07:42:04 +07:00
|
|
|
data_recv = self.__sock.recv(1032) # timeout? tarda bastante...
|
|
|
|
self.__ack_ok()
|
2018-05-04 02:58:52 +07:00
|
|
|
if self.verbose: print (codecs.encode(data_recv, 'hex'))
|
2018-04-26 07:42:04 +07:00
|
|
|
if self.tcp:
|
2018-04-28 07:00:13 +07:00
|
|
|
res = unpack("H", data_recv.ljust(24,b"\x00")[16:18])[0]
|
2018-04-26 07:42:04 +07:00
|
|
|
else:
|
2018-04-28 07:00:13 +07:00
|
|
|
res = unpack("H", data_recv.ljust(16,b"\x00")[8:10])[0]
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print("res %i" % res)
|
2018-05-03 02:12:08 +07:00
|
|
|
if res == 5:
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("huella duplicada")
|
2018-04-26 07:42:04 +07:00
|
|
|
if res == 6 or res == 4:
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("posible timeout o reg Fallido")
|
2018-04-26 07:42:04 +07:00
|
|
|
if res == 0:
|
2018-04-28 07:00:13 +07:00
|
|
|
size = unpack("H", data_recv.ljust(16,b"\x00")[10:12])[0]
|
|
|
|
pos = unpack("H", data_recv.ljust(16,b"\x00")[12:14])[0]
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print("enroll ok", size, pos)
|
2018-04-26 07:42:04 +07:00
|
|
|
done = True
|
|
|
|
self.__sock.settimeout(self.__timeout)
|
2018-05-03 04:09:33 +07:00
|
|
|
self.reg_event(0) # TODO: test
|
2018-04-26 07:42:04 +07:00
|
|
|
self.cancel_capture()
|
|
|
|
self.verify_user()
|
|
|
|
return done
|
2018-05-03 02:12:08 +07:00
|
|
|
|
|
|
|
def live_capture(self, new_timeout=10):# generator!
|
|
|
|
""" try live capture of events"""
|
|
|
|
was_enabled = self.is_enabled
|
|
|
|
users = self.get_users()
|
|
|
|
self.cancel_capture()
|
|
|
|
self.verify_user()
|
|
|
|
if not self.is_enabled:
|
|
|
|
self.enable_device()
|
|
|
|
self.reg_event(const.EF_ATTLOG) #0xFFFF
|
|
|
|
self.__sock.settimeout(new_timeout) # default 1 minute test?
|
|
|
|
self.end_live_capture = False
|
|
|
|
while not self.end_live_capture:
|
|
|
|
try:
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("esperando event")
|
2018-05-03 02:12:08 +07:00
|
|
|
data_recv = self.__sock.recv(1032)
|
|
|
|
self.__ack_ok()
|
|
|
|
if self.tcp:
|
|
|
|
size = unpack('<HHI', data_recv[:8])[2]
|
|
|
|
header = unpack('HHHH', data_recv[8:16])
|
|
|
|
data = data_recv[16:]
|
|
|
|
else:
|
|
|
|
size = len(data_recv)
|
|
|
|
header = unpack('HHHH', data_recv[:8])
|
|
|
|
data = data_recv[8:]
|
|
|
|
if not header[0] == const.CMD_REG_EVENT:
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print("not event!")
|
2018-05-03 02:12:08 +07:00
|
|
|
continue # or raise error?
|
|
|
|
if not len(data):
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("empty")
|
2018-05-03 02:12:08 +07:00
|
|
|
continue
|
|
|
|
"""if len (data) == 5:
|
|
|
|
|
|
|
|
continue"""
|
2018-06-12 05:28:52 +07:00
|
|
|
if len(data) == 12: #class 1 attendance #TODO: RETEST ZK6
|
|
|
|
user_id, status, punch, timehex = unpack('<IBB6s', data)
|
2018-05-05 06:18:23 +07:00
|
|
|
user_id = str(user_id)
|
2018-05-03 02:12:08 +07:00
|
|
|
timestamp = self.__decode_timehex(timehex)
|
2018-05-05 06:18:23 +07:00
|
|
|
tuser = list(filter(lambda x: x.user_id == user_id, users))
|
2018-05-03 02:12:08 +07:00
|
|
|
if not tuser:
|
2018-05-05 06:18:23 +07:00
|
|
|
uid = int(user_id)
|
2018-05-03 02:12:08 +07:00
|
|
|
else:
|
|
|
|
uid = tuser[0].uid
|
2018-06-13 07:38:35 +07:00
|
|
|
yield Attendance(user_id, timestamp, status, punch, uid)#punch test?
|
2018-05-03 02:12:08 +07:00
|
|
|
elif len(data) == 36: #class 2 attendance
|
2018-06-12 05:28:52 +07:00
|
|
|
user_id, status, punch, timehex, res = unpack('<24sBB6sI', data)
|
2018-05-03 02:12:08 +07:00
|
|
|
user_id = (user_id.split(b'\x00')[0]).decode(errors='ignore')
|
|
|
|
timestamp = self.__decode_timehex(timehex)
|
2018-05-05 06:18:23 +07:00
|
|
|
tuser = list(filter(lambda x: x.user_id == user_id, users))
|
2018-05-03 02:12:08 +07:00
|
|
|
if not tuser:
|
|
|
|
uid = int(user_id)
|
|
|
|
else:
|
|
|
|
uid = tuser[0].uid
|
2018-06-13 07:34:37 +07:00
|
|
|
yield Attendance(user_id, timestamp, status, punch, uid)
|
2018-05-03 02:12:08 +07:00
|
|
|
else:
|
2018-05-04 02:58:52 +07:00
|
|
|
if self.verbose: print (codecs.encode(data, 'hex')), len(data)
|
|
|
|
yield codecs.encode(data, 'hex')
|
2018-05-03 02:12:08 +07:00
|
|
|
except timeout:
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("time out")
|
2018-05-03 02:12:08 +07:00
|
|
|
yield None # return to keep watching
|
|
|
|
except (KeyboardInterrupt, SystemExit):
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("break")
|
2018-05-03 02:12:08 +07:00
|
|
|
break
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("exit gracefully")
|
|
|
|
self.__sock.settimeout(self.__timeout)
|
2018-05-03 02:12:08 +07:00
|
|
|
self.reg_event(0) # TODO: test
|
|
|
|
if not was_enabled:
|
|
|
|
self.disable_device()
|
|
|
|
|
2018-04-19 07:22:03 +07:00
|
|
|
def clear_data(self, clear_type=5): # FCT_USER
|
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 )
|
2018-04-19 07:22:03 +07:00
|
|
|
2 = FCT_FINGERTMP
|
2016-05-25 21:35:50 +07:00
|
|
|
'''
|
2016-06-10 11:27:49 +07:00
|
|
|
command = const.CMD_CLEAR_DATA
|
2018-04-19 07:22:03 +07:00
|
|
|
command_string = pack("B", clear_type)
|
|
|
|
cmd_response = self.__send_command(command, command_string)
|
2016-06-10 11:27:49 +07:00
|
|
|
if cmd_response.get('status'):
|
2016-06-15 19:41:59 +07:00
|
|
|
return True
|
2016-06-10 11:27:49 +07:00
|
|
|
else:
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKErrorResponse("can't clear data")
|
2016-05-25 21:35:50 +07:00
|
|
|
|
2018-05-05 06:18:23 +07:00
|
|
|
def __recieve_chunk(self):
|
|
|
|
""" recieve a chunk """
|
|
|
|
if self.__response == const.CMD_DATA: # less than 1024!!!
|
|
|
|
if self.verbose: print ("size was {} len is {}".format(size, len(self.__data)))
|
|
|
|
return self.__data #without headers
|
2018-05-26 03:44:38 +07:00
|
|
|
elif self.__response == const.CMD_PREPARE_DATA:
|
2018-05-05 06:18:23 +07:00
|
|
|
data = []
|
|
|
|
size = self.__get_data_size()
|
|
|
|
if self.verbose: print ("recieve chunk:data size is", size)
|
|
|
|
if self.tcp:
|
2018-06-07 02:10:18 +07:00
|
|
|
if len(self.__data)>=(8+size): #prepare data with actual data! should be 8+size+32
|
|
|
|
data_recv = self.__data[8:] # test, maybe -32
|
|
|
|
else:
|
|
|
|
data_recv = self.__sock.recv(size + 32)
|
2018-05-05 06:18:23 +07:00
|
|
|
tcp_length = self.__test_tcp_top(data_recv)
|
|
|
|
if tcp_length == 0:
|
|
|
|
print ("Incorrect tcp packet")
|
|
|
|
return None
|
|
|
|
recieved = len(data_recv)
|
|
|
|
if self.verbose: print ("recieved {}, size {}".format(recieved, size))
|
|
|
|
if tcp_length < (size + 8):
|
|
|
|
if self.verbose: print ("request chunk too big!")
|
|
|
|
response = unpack('HHHH', data_recv[8:16])[0]
|
2018-05-26 03:44:38 +07:00
|
|
|
if recieved >= (size + 32): #complete with ACK_OK included
|
2018-05-05 06:18:23 +07:00
|
|
|
if response == const.CMD_DATA:
|
|
|
|
resp = data_recv[16 : size + 16] # no ack?
|
|
|
|
if self.verbose: print ("resp complete len", len(resp))
|
|
|
|
return resp
|
|
|
|
else:
|
|
|
|
if self.verbose: print("broken packet!!! {}".format(response))
|
|
|
|
return None #broken
|
|
|
|
else: # incomplete
|
2018-05-26 03:44:38 +07:00
|
|
|
if self.verbose: print ("try incomplete (actual valid {})".format(recieved-16))
|
|
|
|
data.append(data_recv[16 : size+ 16 ]) # w/o DATA tcp and header
|
|
|
|
size -= recieved-16 # w/o DATA tcp and header
|
|
|
|
broken_header = b""
|
|
|
|
if size < 0: #broken ack header?
|
|
|
|
broken_header = data_recv[size:]
|
|
|
|
if self.verbose: print ("broken", (broken_header).encode('hex')) #TODO python3
|
2018-05-05 06:18:23 +07:00
|
|
|
while size>0: #jic
|
2018-05-26 03:44:38 +07:00
|
|
|
if self.verbose: print ("still need {}".format(size))
|
2018-05-05 06:18:23 +07:00
|
|
|
data_recv = self.__sock.recv(size) #ideal limit?
|
|
|
|
recieved = len(data_recv)
|
|
|
|
if self.verbose: print ("partial recv {}".format(recieved))
|
|
|
|
data.append(data_recv) # w/o tcp and header
|
|
|
|
size -= recieved
|
|
|
|
#get cmd_ack_ok
|
2018-05-26 03:44:38 +07:00
|
|
|
data_recv = broken_header + self.__sock.recv(16)
|
2018-05-05 06:18:23 +07:00
|
|
|
#could be broken
|
|
|
|
if len(data_recv) < 16:
|
2018-05-26 03:44:38 +07:00
|
|
|
print ("trying to complete broken ACK %s /16" % len(data_recv))
|
|
|
|
if self.verbose: print (data_recv.encode('hex')) #todo python3
|
2018-05-08 06:48:19 +07:00
|
|
|
data_recv += self.__sock.recv(16 - len(data_recv)) #TODO: CHECK HERE_!
|
2018-05-05 06:18:23 +07:00
|
|
|
if not self.__test_tcp_top(data_recv):
|
|
|
|
if self.verbose: print ("invalid tcp ACK OK")
|
|
|
|
return None #b''.join(data) # incomplete?
|
|
|
|
response = unpack('HHHH', data_recv[8:16])[0]
|
|
|
|
if response == const.CMD_ACK_OK:
|
|
|
|
return b''.join(data)
|
|
|
|
#data_recv[bytes+16:].encode('hex') #included CMD_ACK_OK
|
|
|
|
if self.verbose: print("bad response %s" % data_recv)
|
|
|
|
if self.verbose: print (data)
|
|
|
|
return None
|
|
|
|
#else udp
|
|
|
|
while True: #limitado por respuesta no por tamaƱo
|
2018-05-08 06:48:19 +07:00
|
|
|
data_recv = self.__sock.recv(1024+8)
|
2018-05-05 06:18:23 +07:00
|
|
|
response = unpack('HHHH', data_recv[:8])[0]
|
|
|
|
if self.verbose: print ("# packet response is: {}".format(response))
|
|
|
|
if response == const.CMD_DATA:
|
|
|
|
data.append(data_recv[8:]) #header turncated
|
|
|
|
size -= 1024 #UDP
|
|
|
|
elif response == const.CMD_ACK_OK:
|
|
|
|
break #without problem.
|
|
|
|
else:
|
|
|
|
#truncado! continuar?
|
|
|
|
if self.verbose: print ("broken!")
|
|
|
|
break
|
|
|
|
if self.verbose: print ("still needs %s" % size)
|
|
|
|
return b''.join(data)
|
|
|
|
else:
|
|
|
|
if self.verbose: print ("invalid response %s" % self.__response)
|
|
|
|
return None #("can't get user template")
|
|
|
|
|
2018-03-24 06:44:00 +07:00
|
|
|
def __read_chunk(self, start, size):
|
|
|
|
""" read a chunk from buffer """
|
2018-05-05 06:18:23 +07:00
|
|
|
for _retries in range(3):
|
|
|
|
command = 1504 #CMD_READ_BUFFER
|
|
|
|
command_string = pack('<ii', start, size)
|
2018-06-07 04:42:43 +07:00
|
|
|
if self.tcp:
|
|
|
|
response_size = size + 32
|
|
|
|
else:
|
|
|
|
response_size = 1024 + 8
|
2018-05-05 06:18:23 +07:00
|
|
|
cmd_response = self.__send_command(command, command_string, response_size)
|
|
|
|
data = self.__recieve_chunk()
|
|
|
|
if data is not None:
|
|
|
|
return data
|
|
|
|
else:
|
|
|
|
raise ZKErrorResponse("can't read chunk %i:[%i]" % (start, size))
|
|
|
|
#------------------------------------------
|
2018-03-24 06:44:00 +07:00
|
|
|
if not cmd_response.get('status'):
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKErrorResponse("can't read chunk %i:[%i]" % (start, size))
|
2018-03-24 06:44:00 +07:00
|
|
|
#else
|
2018-04-12 06:38:37 +07:00
|
|
|
if cmd_response.get('code') == const.CMD_DATA: # less than 1024!!!
|
2018-05-04 02:58:52 +07:00
|
|
|
if self.verbose: print ("size was {} len is {}".format(size, len(self.__data)))
|
|
|
|
return self.__data
|
2018-03-24 06:44:00 +07:00
|
|
|
if cmd_response.get('code') == const.CMD_PREPARE_DATA:
|
2018-04-12 06:38:37 +07:00
|
|
|
data = []
|
2018-03-24 06:44:00 +07:00
|
|
|
bytes = self.__get_data_size() #TODO: check with size
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("prepare data size is", bytes)
|
2018-04-26 07:42:04 +07:00
|
|
|
if self.tcp:
|
|
|
|
data_recv = self.__sock.recv(bytes + 32)
|
|
|
|
recieved = len(data_recv)
|
|
|
|
tcp_length = unpack('HHI', data_recv[:8])[2] #bytes+8
|
|
|
|
if tcp_length < (bytes + 8):
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("request chunk too big!")
|
2018-04-26 07:42:04 +07:00
|
|
|
response = unpack('HHHH', data_recv[8:16])[0]
|
|
|
|
if recieved >= (bytes + 32): #complete
|
|
|
|
if response == const.CMD_DATA:
|
|
|
|
resp = data_recv[16:bytes+16] # no ack?
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("resp len", len(resp))
|
2018-04-26 07:42:04 +07:00
|
|
|
return resp
|
|
|
|
else:
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print("broken packet!!!")
|
2018-04-26 07:42:04 +07:00
|
|
|
return '' #broken
|
|
|
|
else: # incomplete
|
|
|
|
data.append(data_recv[16:]) # w/o tcp and header
|
|
|
|
bytes -= recieved-16
|
|
|
|
while bytes>0: #jic
|
|
|
|
data_recv = self.__sock.recv(bytes) #ideal limit?
|
|
|
|
recieved = len(data_recv)
|
|
|
|
data.append(data_recv) # w/o tcp and header
|
|
|
|
bytes -= recieved
|
|
|
|
data_recv = self.__sock.recv(16)
|
|
|
|
response = unpack('HHHH', data_recv[8:16])[0]
|
|
|
|
if response == const.CMD_ACK_OK:
|
2018-04-28 07:00:13 +07:00
|
|
|
return b''.join(data)
|
2018-04-26 07:42:04 +07:00
|
|
|
#data_recv[bytes+16:].encode('hex') #included CMD_ACK_OK
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print("bad response %s" % data_recv)
|
|
|
|
if self.verbose: print (data)
|
2018-04-26 07:42:04 +07:00
|
|
|
return ''
|
|
|
|
#else udp
|
2018-03-24 06:44:00 +07:00
|
|
|
while True: #limitado por respuesta no por tamaƱo
|
2018-04-26 07:42:04 +07:00
|
|
|
data_recv = self.__sock.recv(response_size)
|
2018-03-24 06:44:00 +07:00
|
|
|
response = unpack('HHHH', data_recv[:8])[0]
|
2018-05-05 02:26:46 +07:00
|
|
|
if self.verbose: print ("# packet response is: {}".format(response))
|
2018-03-24 06:44:00 +07:00
|
|
|
if response == const.CMD_DATA:
|
|
|
|
data.append(data_recv[8:]) #header turncated
|
2018-04-26 07:42:04 +07:00
|
|
|
bytes -= 1024 #UDP
|
2018-03-24 06:44:00 +07:00
|
|
|
elif response == const.CMD_ACK_OK:
|
|
|
|
break #without problem.
|
|
|
|
else:
|
|
|
|
#truncado! continuar?
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("broken!")
|
2018-03-24 06:44:00 +07:00
|
|
|
break
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("still needs %s" % bytes)
|
2018-04-28 07:00:13 +07:00
|
|
|
return b''.join(data)
|
2018-03-24 06:44:00 +07:00
|
|
|
|
|
|
|
def read_with_buffer(self, command, fct=0 ,ext=0):
|
|
|
|
""" Test read info with buffered command (ZK6: 1503) """
|
2018-04-26 07:42:04 +07:00
|
|
|
if self.tcp:
|
|
|
|
MAX_CHUNK = 0xFFc0 #arbitrary, below 0x10008
|
|
|
|
else:
|
|
|
|
MAX_CHUNK = 16 * 1024
|
2018-03-24 06:44:00 +07:00
|
|
|
command_string = pack('<bhii', 1, command, fct, ext)
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("rwb cs", command_string)
|
2018-03-24 06:44:00 +07:00
|
|
|
response_size = 1024
|
|
|
|
data = []
|
|
|
|
start = 0
|
2018-04-12 06:38:37 +07:00
|
|
|
cmd_response = self.__send_command(1503, command_string, response_size)
|
2018-03-24 06:44:00 +07:00
|
|
|
if not cmd_response.get('status'):
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKErrorResponse("RWB Not supported")
|
2018-04-26 07:42:04 +07:00
|
|
|
if cmd_response['code'] == const.CMD_DATA:
|
|
|
|
#direct!!! small!!!
|
2018-04-28 07:00:13 +07:00
|
|
|
size = len(self.__data)
|
|
|
|
return self.__data, size
|
|
|
|
size = unpack('I', self.__data[1:5])[0] # extra info???
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("size fill be %i" % size)
|
2018-03-24 06:44:00 +07:00
|
|
|
remain = size % MAX_CHUNK
|
2018-04-28 07:00:13 +07:00
|
|
|
packets = (size-remain) // MAX_CHUNK # should be size /16k
|
2018-03-24 06:44:00 +07:00
|
|
|
for _wlk in range(packets):
|
|
|
|
data.append(self.__read_chunk(start,MAX_CHUNK))
|
|
|
|
start += MAX_CHUNK
|
|
|
|
if remain:
|
|
|
|
data.append(self.__read_chunk(start, remain))
|
|
|
|
start += remain # Debug
|
|
|
|
self.free_data()
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("_read w/chunk %i bytes" % start)
|
2018-04-28 07:00:13 +07:00
|
|
|
return b''.join(data), start
|
2018-03-24 06:44:00 +07:00
|
|
|
|
2016-05-25 21:35:50 +07:00
|
|
|
def get_attendance(self):
|
2018-03-24 06:44:00 +07:00
|
|
|
""" return attendance record """
|
2018-04-26 07:42:04 +07:00
|
|
|
self.read_sizes()
|
|
|
|
if self.records == 0: #lazy
|
|
|
|
return []
|
2018-05-03 02:12:08 +07:00
|
|
|
users = self.get_users()
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print (users)
|
2018-03-24 06:44:00 +07:00
|
|
|
attendances = []
|
|
|
|
attendance_data, size = self.read_with_buffer(const.CMD_ATTLOG_RRQ)
|
|
|
|
if size < 4:
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("WRN: no attendance data") # debug
|
2018-03-24 06:44:00 +07:00
|
|
|
return []
|
2018-04-26 07:42:04 +07:00
|
|
|
total_size = unpack("I", attendance_data[:4])[0]
|
|
|
|
record_size = total_size/self.records
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print ("record_size is ", record_size)
|
2018-03-24 06:44:00 +07:00
|
|
|
attendance_data = attendance_data[4:] #total size not used
|
2018-04-26 07:42:04 +07:00
|
|
|
if record_size == 8 : #ultra old format
|
2018-06-12 05:28:52 +07:00
|
|
|
while len(attendance_data) >= 8:#TODO RETEST ZK6!!!
|
|
|
|
uid, status, timestamp, punch = unpack('HB4sB', attendance_data.ljust(8, b'\x00')[:8])
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print (codecs.encode(attendance_data[:8], 'hex'))
|
2018-04-13 06:10:01 +07:00
|
|
|
attendance_data = attendance_data[8:]
|
2018-05-03 04:09:33 +07:00
|
|
|
tuser = list(filter(lambda x: x.uid == uid, users))
|
2018-05-03 02:12:08 +07:00
|
|
|
if not tuser:
|
|
|
|
user_id = str(uid) #TODO revisar pq
|
|
|
|
else:
|
|
|
|
user_id = tuser[0].user_id
|
2018-03-24 06:44:00 +07:00
|
|
|
timestamp = self.__decode_time(timestamp)
|
2018-06-13 07:38:35 +07:00
|
|
|
attendance = Attendance(user_id, timestamp, status, punch, uid) # punch?
|
2018-03-24 06:44:00 +07:00
|
|
|
attendances.append(attendance)
|
2018-04-26 07:42:04 +07:00
|
|
|
elif record_size == 16: # extended
|
2018-06-12 05:28:52 +07:00
|
|
|
while len(attendance_data) >= 16: #TODO RETEST ZK6
|
|
|
|
user_id, timestamp, status, punch, reserved, workcode = unpack('<I4sBB2sI', attendance_data.ljust(16, b'\x00')[:16])
|
2018-05-05 06:18:23 +07:00
|
|
|
user_id = str(user_id)
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print(codecs.encode(attendance_data[:16], 'hex'))
|
2018-04-26 07:42:04 +07:00
|
|
|
attendance_data = attendance_data[16:]
|
2018-05-05 06:18:23 +07:00
|
|
|
tuser = list(filter(lambda x: x.user_id == user_id, users))
|
2018-05-03 02:12:08 +07:00
|
|
|
if not tuser:
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print("no uid {}", user_id)
|
2018-05-03 02:12:08 +07:00
|
|
|
uid = str(user_id)
|
2018-05-03 04:09:33 +07:00
|
|
|
tuser = list(filter(lambda x: x.uid == user_id, users)) # refix
|
2018-05-03 02:12:08 +07:00
|
|
|
if not tuser:
|
|
|
|
uid = str(user_id) #TODO revisar pq
|
|
|
|
else:
|
|
|
|
uid = tuser[0].uid
|
|
|
|
user_id = tuser[0].user_id
|
|
|
|
else:
|
|
|
|
uid = tuser[0].uid
|
2018-04-26 07:42:04 +07:00
|
|
|
timestamp = self.__decode_time(timestamp)
|
2018-06-13 07:34:37 +07:00
|
|
|
attendance = Attendance(user_id, timestamp, status, punch, uid)
|
2018-04-26 07:42:04 +07:00
|
|
|
attendances.append(attendance)
|
2018-03-24 06:44:00 +07:00
|
|
|
else:
|
|
|
|
while len(attendance_data) >= 40:
|
2018-06-12 05:28:52 +07:00
|
|
|
uid, user_id, status, timestamp, punch, space = unpack('<H24sB4sB8s', attendance_data.ljust(40, b'\x00')[:40])
|
2018-05-03 04:09:33 +07:00
|
|
|
if self.verbose: print (codecs.encode(attendance_data[:40], 'hex'))
|
2018-05-03 02:12:08 +07:00
|
|
|
user_id = (user_id.split(b'\x00')[0]).decode(errors='ignore')
|
2018-03-24 06:44:00 +07:00
|
|
|
timestamp = self.__decode_time(timestamp)
|
2018-04-28 07:00:13 +07:00
|
|
|
#status = int(status.encode("hex"), 16)
|
2018-03-24 06:44:00 +07:00
|
|
|
|
2018-06-13 07:34:37 +07:00
|
|
|
attendance = Attendance(user_id, timestamp, status, punch, uid)
|
2018-03-24 06:44:00 +07:00
|
|
|
attendances.append(attendance)
|
|
|
|
attendance_data = attendance_data[40:]
|
|
|
|
return attendances
|
2018-04-26 07:42:04 +07:00
|
|
|
|
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
|
2018-04-20 07:23:23 +07:00
|
|
|
cmd_response = self.__send_command(command)
|
2016-06-15 19:15:12 +07:00
|
|
|
if cmd_response.get('status'):
|
|
|
|
return True
|
|
|
|
else:
|
2018-04-12 06:38:37 +07:00
|
|
|
raise ZKErrorResponse("can't clear response")
|