clean up the get_sizes function,

This commit is contained in:
Arturo Hernandez 2018-06-13 10:14:21 -04:00
parent 6b102f8a0f
commit 4cedf3ee5b
2 changed files with 46 additions and 91 deletions

View File

@ -4,13 +4,26 @@ pyzk is an unofficial library of zksoftware the fingerprint attendance machine.
# Installation # Installation
[![Build Status](https://travis-ci.org/fananimi/pyzk.svg?branch=master)](https://travis-ci.org/fananimi/pyzk) [![Build Status](https://travis-ci.org/kurenai-ryu/pyzk.svg?branch=master)](https://travis-ci.org/kurenai-ryu/pyzk)
`pip install pyzk` replace original pyzk, if it was installed
`pip install -U git+https://github.com/kurenai-ryu/pyzk.git`
or clone and execute:
`python setup.py install`
or in your project, append the path of this project
```python
import sys
import os
sys.path.insert(1,os.path.abspath("./pyzk"))
from zk import ZK, const
```
# Documentation # Documentation
Complete documentation can be found at [Readthedocs](http://pyzk.readthedocs.io/en/latest/ "pyzk's readthedocs") . Complete documentation of the original project can be found at [Readthedocs](http://pyzk.readthedocs.io/en/latest/ "pyzk's readthedocs") .
# Api Usage # Api Usage

View File

@ -153,14 +153,14 @@ class ZK(object):
MODIFIED now, without initial checksum MODIFIED now, without initial checksum
''' '''
#checksum = 0 always? for calculating #checksum = 0 always? for calculating
buf = pack('HHHH', command, 0, session_id, reply_id) + command_string buf = pack('<4H', command, 0, session_id, reply_id) + command_string
buf = unpack('8B' + '%sB' % len(command_string), buf) buf = unpack('8B' + '%sB' % len(command_string), buf)
checksum = unpack('H', self.__create_checksum(buf))[0] checksum = unpack('H', self.__create_checksum(buf))[0]
reply_id += 1 reply_id += 1
if reply_id >= const.USHRT_MAX: if reply_id >= const.USHRT_MAX:
reply_id -= const.USHRT_MAX reply_id -= const.USHRT_MAX
buf = pack('HHHH', command, checksum, session_id, reply_id) buf = pack('<4H', command, checksum, session_id, reply_id)
return buf + command_string return buf + command_string
def __create_checksum(self, p): def __create_checksum(self, p):
@ -212,12 +212,12 @@ class ZK(object):
self.__tcp_length = self.__test_tcp_top(self.__tcp_data_recv) self.__tcp_length = self.__test_tcp_top(self.__tcp_data_recv)
if self.__tcp_length == 0: if self.__tcp_length == 0:
raise ZKNetworkError("TCP Packet invalid") raise ZKNetworkError("TCP Packet invalid")
self.__header = unpack('HHHH', self.__tcp_data_recv[8:16]) self.__header = unpack('<4H', self.__tcp_data_recv[8:16])
self.__data_recv = self.__tcp_data_recv[8:] # dirty hack self.__data_recv = self.__tcp_data_recv[8:] # dirty hack
else: else:
self.__sock.sendto(buf, self.__address) self.__sock.sendto(buf, self.__address)
self.__data_recv = self.__sock.recv(response_size) self.__data_recv = self.__sock.recv(response_size)
self.__header = unpack('HHHH', self.__data_recv[:8]) self.__header = unpack('<4H', self.__data_recv[:8])
except Exception as e: except Exception as e:
raise ZKNetworkError(str(e)) raise ZKNetworkError(str(e))
@ -296,7 +296,7 @@ class ZK(object):
return d return d
def __decode_timehex(self, timehex): def __decode_timehex(self, timehex):
"""timehex string of six bytes""" """timehex string of six bytes"""
year, month, day, hour, minute, second = unpack("BBBBBB", timehex) year, month, day, hour, minute, second = unpack("6B", timehex)
year += 2000 year += 2000
d = datetime(year, month, day, hour, minute, second) d = datetime(year, month, day, hour, minute, second)
return d return d
@ -586,10 +586,8 @@ class ZK(object):
if cmd_response.get('status'): if cmd_response.get('status'):
if self.verbose: print(codecs.encode(self.__data,'hex')) if self.verbose: print(codecs.encode(self.__data,'hex'))
size = len(self.__data) size = len(self.__data)
if size == 80: if len(self.__data) >= 80:
fields = unpack('iiiiiiiiiiiiiiiiiiii', self.__data) fields = unpack('20i', self.__data[:80])
else: #92?
fields = unpack('iiiiiiiiiiiiiiiiiiiiiii', self.__data[:92]) #dirty hack! we need more information
self.users = fields[4] self.users = fields[4]
self.fingers = fields[6] self.fingers = fields[6]
self.records = fields[8] self.records = fields[8]
@ -601,9 +599,11 @@ class ZK(object):
self.fingers_av = fields[17] self.fingers_av = fields[17]
self.users_av = fields[18] self.users_av = fields[18]
self.rec_av = fields[19] self.rec_av = fields[19]
if len(fields) > 20: self.__data = self.__data[80:]
self.faces = fields[20] if len(self.__data) >= 12: #face info
self.faces_cap = fields[22] fields = unpack('3i', self.__data[:12]) #dirty hack! we need more information
self.faces = fields[0]
self.faces_cap = fields[2]
return True return True
else: else:
raise ZKErrorResponse("can't read sizes") raise ZKErrorResponse("can't read sizes")
@ -979,7 +979,7 @@ class ZK(object):
return None return None
recieved = len(data_recv) recieved = len(data_recv)
if self.verbose: print ("recieved {}, size {} rec {}".format(recieved, size, data_recv.encode('hex'))) #todo python3 if self.verbose: print ("recieved {}, size {} rec {}".format(recieved, size, data_recv.encode('hex'))) #todo python3
tcp_length = unpack('HHI', data_recv[:8])[2] #bytes+8 tcp_length = unpack('<HHI', data_recv[:8])[2] #bytes+8
if tcp_length < (bytes + 8): if tcp_length < (bytes + 8):
if self.verbose: print ("request chunk too big!") if self.verbose: print ("request chunk too big!")
response = unpack('HHHH', data_recv[8:16])[0] response = unpack('HHHH', data_recv[8:16])[0]
@ -1005,7 +1005,7 @@ class ZK(object):
data.append(data_recv) # w/o tcp and header data.append(data_recv) # w/o tcp and header
bytes -= recieved bytes -= recieved
data_recv = self.__sock.recv(16) data_recv = self.__sock.recv(16)
response = unpack('HHHH', data_recv[8:16])[0] response = unpack('<4H', data_recv[8:16])[0]
if response == const.CMD_ACK_OK: if response == const.CMD_ACK_OK:
resp = b''.join(data)[:size-1] # testing resp = b''.join(data)[:size-1] # testing
if resp[-6:] == b'\x00\x00\x00\x00\x00\x00': if resp[-6:] == b'\x00\x00\x00\x00\x00\x00':
@ -1020,7 +1020,7 @@ class ZK(object):
size = bytes size = bytes
while True: #limitado por respuesta no por tamaño while True: #limitado por respuesta no por tamaño
data_recv = self.__sock.recv(response_size) data_recv = self.__sock.recv(response_size)
response = unpack('HHHH', data_recv[:8])[0] response = unpack('<4H', data_recv[:8])[0]
if self.verbose: print("# packet response is: {}".format(response)) if self.verbose: print("# packet response is: {}".format(response))
if response == const.CMD_DATA: if response == const.CMD_DATA:
data.append(data_recv[8:]) #header turncated data.append(data_recv[8:]) #header turncated
@ -1281,7 +1281,7 @@ class ZK(object):
data = data_recv[16:] data = data_recv[16:]
else: else:
size = len(data_recv) size = len(data_recv)
header = unpack('HHHH', data_recv[:8]) header = unpack('<4H', data_recv[:8])
data = data_recv[8:] data = data_recv[8:]
if not header[0] == const.CMD_REG_EVENT: if not header[0] == const.CMD_REG_EVENT:
if self.verbose: print("not event!") if self.verbose: print("not event!")
@ -1406,7 +1406,7 @@ class ZK(object):
#else udp #else udp
while True: #limitado por respuesta no por tamaño while True: #limitado por respuesta no por tamaño
data_recv = self.__sock.recv(1024+8) data_recv = self.__sock.recv(1024+8)
response = unpack('HHHH', data_recv[:8])[0] response = unpack('<4H', data_recv[:8])[0]
if self.verbose: print ("# packet response is: {}".format(response)) if self.verbose: print ("# packet response is: {}".format(response))
if response == const.CMD_DATA: if response == const.CMD_DATA:
data.append(data_recv[8:]) #header turncated data.append(data_recv[8:]) #header turncated
@ -1438,64 +1438,6 @@ class ZK(object):
return data return data
else: else:
raise ZKErrorResponse("can't read chunk %i:[%i]" % (start, size)) raise ZKErrorResponse("can't read chunk %i:[%i]" % (start, size))
#------------------------------------------
if not cmd_response.get('status'):
raise ZKErrorResponse("can't read chunk %i:[%i]" % (start, size))
#else
if cmd_response.get('code') == const.CMD_DATA: # less than 1024!!!
if self.verbose: print ("size was {} len is {}".format(size, len(self.__data)))
return self.__data
if cmd_response.get('code') == const.CMD_PREPARE_DATA:
data = []
bytes = self.__get_data_size() #TODO: check with size
if self.verbose: print ("prepare data size is", bytes)
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):
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:
resp = data_recv[16:bytes+16] # no ack?
if self.verbose: print ("resp len", len(resp))
return resp
else:
if self.verbose: print("broken packet!!!")
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:
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 ''
#else udp
while True: #limitado por respuesta no por tamaño
data_recv = self.__sock.recv(response_size)
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
bytes -= 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" % bytes)
return b''.join(data)
def read_with_buffer(self, command, fct=0 ,ext=0): def read_with_buffer(self, command, fct=0 ,ext=0):
""" Test read info with buffered command (ZK6: 1503) """ """ Test read info with buffered command (ZK6: 1503) """