soporte inicial para firmware version 6

This commit is contained in:
Arturo Hernandez 2018-01-30 18:17:08 -04:00
parent 949ec91447
commit 0a5322528e
2 changed files with 78 additions and 48 deletions

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import sys
from datetime import datetime from datetime import datetime
from socket import AF_INET, SOCK_DGRAM, socket from socket import AF_INET, SOCK_DGRAM, socket
from struct import pack, unpack from struct import pack, unpack
@ -51,12 +52,13 @@ class ZK(object):
__sesion_id = 0 __sesion_id = 0
__reply_id = 0 __reply_id = 0
def __init__(self, ip, port=4370, timeout=60, password=0): def __init__(self, ip, port=4370, timeout=60, password=0, firmware=8):
self.is_connect = False self.is_connect = False
self.__address = (ip, port) self.__address = (ip, port)
self.__sock = socket(AF_INET, SOCK_DGRAM) self.__sock = socket(AF_INET, SOCK_DGRAM)
self.__sock.settimeout(timeout) self.__sock.settimeout(timeout)
self.__password = password # passint self.__password = password # passint
self.__firmware = int(firmware) #TODO check minor version?
def __create_header(self, command, command_string, checksum, session_id, reply_id): def __create_header(self, command, command_string, checksum, session_id, reply_id):
''' '''
@ -184,7 +186,6 @@ class ZK(object):
''' '''
connect to the device connect to the device
''' '''
command = const.CMD_CONNECT command = const.CMD_CONNECT
command_string = '' command_string = ''
checksum = 0 checksum = 0
@ -210,7 +211,6 @@ class ZK(object):
''' '''
diconnect from the connected device diconnect from the connected device
''' '''
command = const.CMD_EXIT command = const.CMD_EXIT
command_string = '' command_string = ''
checksum = 0 checksum = 0
@ -229,7 +229,6 @@ class ZK(object):
''' '''
disable (lock) device, ensure no activity when process run disable (lock) device, ensure no activity when process run
''' '''
command = const.CMD_DISABLEDEVICE command = const.CMD_DISABLEDEVICE
command_string = '' command_string = ''
checksum = 0 checksum = 0
@ -247,7 +246,6 @@ class ZK(object):
''' '''
re-enable the connected device re-enable the connected device
''' '''
command = const.CMD_ENABLEDEVICE command = const.CMD_ENABLEDEVICE
command_string = '' command_string = ''
checksum = 0 checksum = 0
@ -265,7 +263,6 @@ class ZK(object):
''' '''
return the firmware version return the firmware version
''' '''
command = const.CMD_GET_VERSION command = const.CMD_GET_VERSION
command_string = '' command_string = ''
checksum = 0 checksum = 0
@ -302,7 +299,6 @@ class ZK(object):
''' '''
restart the device restart the device
''' '''
command = const.CMD_RESTART command = const.CMD_RESTART
command_string = '' command_string = ''
checksum = 0 checksum = 0
@ -348,7 +344,6 @@ class ZK(object):
''' '''
shutdown the device shutdown the device
''' '''
command = const.CMD_POWEROFF command = const.CMD_POWEROFF
command_string = '' command_string = ''
checksum = 0 checksum = 0
@ -366,7 +361,6 @@ class ZK(object):
''' '''
play test voice play test voice
''' '''
command = const.CMD_TESTVOICE command = const.CMD_TESTVOICE
command_string = '' command_string = ''
checksum = 0 checksum = 0
@ -384,15 +378,29 @@ class ZK(object):
''' '''
create or update user by uid create or update user by uid
''' '''
command = const.CMD_USER_WRQ command = const.CMD_USER_WRQ
uid = chr(uid % 256) + chr(uid >> 8) #uid = chr(uid % 256) + chr(uid >> 8)
if privilege not in [const.USER_DEFAULT, const.USER_ADMIN]: if privilege not in [const.USER_DEFAULT, const.USER_ADMIN]:
privilege = const.USER_DEFAULT privilege = const.USER_DEFAULT
privilege = chr(privilege) privilege = chr(privilege)
if self.__firmware == 6:
command_string = pack('2sc8s28sc7sx24s', uid, privilege, password, name, chr(0), group_id, user_id) print "uid : %i" % uid
print "pri : %c" % privilege
print "pass: %s" % str(password)
print "name: %s" % str(name)
print type(name)
print "group %i" % int(group_id)
print "uid2: %i" % int(user_id)
try:
command_string = pack('Hc5s8s5sBHI', uid, privilege, str(password), str(name), chr(0), int(group_id), 0, int(user_id))
print "cmd : %s" % command_string
except Exception, e:
print "s_h Error pack: %s" % e
print "Error pack: %s" % sys.exc_info()[0]
raise ZKErrorResponse("Invalid response")
else:
command_string = pack('Hc8s28sc7sx24s', uid, privilege, password, name, chr(0), group_id, user_id)
checksum = 0 checksum = 0
session_id = self.__sesion_id session_id = self.__sesion_id
reply_id = self.__reply_id reply_id = self.__reply_id
@ -423,12 +431,12 @@ class ZK(object):
return True return True
else: else:
raise ZKErrorResponse("Invalid response") raise ZKErrorResponse("Invalid response")
#def get_user_template(self, uid, finger):
def get_users(self): def get_users(self):
''' '''
return all user return all user
''' '''
command = const.CMD_USERTEMP_RRQ command = const.CMD_USERTEMP_RRQ
command_string = chr(const.FCT_USER) command_string = chr(const.FCT_USER)
checksum = 0 checksum = 0
@ -454,8 +462,8 @@ class ZK(object):
break #without problem. break #without problem.
else: else:
#truncado! continuar? #truncado! continuar?
print "broken! with %s" % response #print "broken! with %s" % response
print "user still needs %s" % bytes #print "user still needs %s" % bytes
break break
if response == const.CMD_ACK_OK: if response == const.CMD_ACK_OK:
@ -463,31 +471,45 @@ class ZK(object):
# The first 4 bytes don't seem to be related to the user # The first 4 bytes don't seem to be related to the user
userdata = ''.join(userdata) userdata = ''.join(userdata)
userdata = userdata[4:] userdata = userdata[4:]
while len(userdata) >= 72: if self.__firmware == 6:
uid, privilege, password, name, sparator, group_id, user_id = unpack('hc8s28sc7sx24s', userdata.ljust(72)[:72]) while len(userdata) >= 28:
#u1 = int(uid[0].encode("hex"), 16) uid, privilege, password, name, card, group_id, timezone, user_id = unpack('HB5s8s5sBhI',userdata.ljust(28)[:28])
#u2 = int(uid[1].encode("hex"), 16) password = unicode(password.split('\x00')[0], errors='ignore')
#uid = u1 + (u2 * 256) name = unicode(name.split('\x00')[0], errors='ignore').strip()
privilege = int(privilege.encode("hex"), 16) card = unpack('Q', card.ljust(8,'\x00'))[0] #or hex value?
password = unicode(password.split('\x00')[0], errors='ignore') group_id = str(group_id)
name = unicode(name.split('\x00')[0], errors='ignore') user_id = str(user_id)
group_id = unicode(group_id.split('\x00')[0], errors='ignore') #TODO: check card value and find in ver8
user_id = unicode(user_id.split('\x00')[0], errors='ignore') if not name:
name = user_id
user = User(uid, name, privilege, password, group_id, user_id)
users.append(user)
print "[6]user:",uid, privilege, password, name, card, group_id, timezone, user_id
userdata = userdata[28:]
else:
while len(userdata) >= 72:
uid, privilege, password, name, sparator, group_id, user_id = unpack('Hc8s28sc7sx24s', userdata.ljust(72)[:72])
#u1 = int(uid[0].encode("hex"), 16)
#u2 = int(uid[1].encode("hex"), 16)
#uid = u1 + (u2 * 256)
privilege = int(privilege.encode("hex"), 16)
password = unicode(password.split('\x00')[0], errors='ignore')
name = unicode(name.split('\x00')[0], errors='ignore').strip()
group_id = unicode(group_id.split('\x00')[0], errors='ignore').strip()
user_id = unicode(user_id.split('\x00')[0], errors='ignore')
user = User(uid, name, privilege, password, group_id, user_id) user = User(uid, name, privilege, password, group_id, user_id)
users.append(user) users.append(user)
userdata = userdata[72:] userdata = userdata[72:]
else: else:
raise ZKErrorResponse("Invalid response") raise ZKErrorResponse("Invalid response")
return users return users
def cancel_capture(self): def cancel_capture(self):
''' '''
cancel capturing finger cancel capturing finger
''' '''
command = const.CMD_CANCELCAPTURE command = const.CMD_CANCELCAPTURE
cmd_response = self.__send_command(command=command) cmd_response = self.__send_command(command=command)
print cmd_response print cmd_response
@ -496,7 +518,6 @@ class ZK(object):
''' '''
verify finger verify finger
''' '''
command = const.CMD_STARTVERIFY command = const.CMD_STARTVERIFY
# uid = chr(uid % 256) + chr(uid >> 8) # uid = chr(uid % 256) + chr(uid >> 8)
cmd_response = self.__send_command(command=command) cmd_response = self.__send_command(command=command)
@ -506,11 +527,15 @@ class ZK(object):
''' '''
start enroll user start enroll user
''' '''
command = const.CMD_STARTENROLL command = const.CMD_STARTENROLL
uid = chr(uid % 256) + chr(uid >> 8) uid = chr(uid % 256) + chr(uid >> 8)
command_string = pack('2s', uid) command_string = pack('2s', uid)
cmd_response = self.__send_command(command=command, command_string=command_string) checksum = 0
session_id = self.__sesion_id
reply_id = self.__reply_id
response_size = 8
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
print cmd_response print cmd_response
def clear_data(self): def clear_data(self):
@ -560,28 +585,34 @@ class ZK(object):
break #without problem. break #without problem.
else: else:
#truncado! continuar? #truncado! continuar?
print "broken!" #print "broken!"
break break
#print "still needs %s" % bytes #print "still needs %s" % bytes
if response == const.CMD_ACK_OK: if response == const.CMD_ACK_OK:
if attendance_data: if attendance_data:
attendance_data = ''.join(attendance_data) attendance_data = ''.join(attendance_data)
attendance_data = attendance_data[4:] attendance_data = attendance_data[4:]
while len(attendance_data) >= 40: if self.__firmware == 6:
uid, user_id, sparator, timestamp, status, space = unpack('h24sc4sc8s', attendance_data.ljust(40)[:40]) while len(attendance_data) >= 8:
user_id = user_id.split('\x00')[0] uid, status, timestamp = unpack('HH4s', attendance_data.ljust(8)[:8])
timestamp = self.__decode_time(timestamp) user_id = str(uid) #TODO revisar posibles valores cruzar con userdata
status = int(status.encode("hex"), 16) timestamp = self.__decode_time(timestamp)
attendance = Attendance(uid, user_id, timestamp, status)
attendances.append(attendance)
attendance_data = attendance_data[8:]
else:
while len(attendance_data) >= 40:
uid, user_id, sparator, timestamp, status, space = unpack('H24sc4sc8s', attendance_data.ljust(40)[:40])
user_id = user_id.split('\x00')[0]
timestamp = self.__decode_time(timestamp)
status = int(status.encode("hex"), 16)
attendance = Attendance(uid, user_id, timestamp, status) attendance = Attendance(uid, user_id, timestamp, status)
attendances.append(attendance) attendances.append(attendance)
attendance_data = attendance_data[40:] attendance_data = attendance_data[40:]
else: else:
raise ZKErrorResponse("Invalid response") raise ZKErrorResponse("Invalid response")
return attendances return attendances
def clear_attendance(self): def clear_attendance(self):
@ -594,7 +625,6 @@ class ZK(object):
session_id = self.__sesion_id session_id = self.__sesion_id
reply_id = self.__reply_id reply_id = self.__reply_id
response_size = 1024 response_size = 1024
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size) cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
if cmd_response.get('status'): if cmd_response.get('status'):
return True return True

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
class User(object): class User(object):
def __init__(self, uid, name, privilege, password='', group_id='', user_id=''): def __init__(self, uid, name, privilege, password='', group_id='', user_id='', card=0):
self.uid = uid self.uid = uid
self.name = name self.name = name
self.privilege = privilege self.privilege = privilege