fixing get_user function

This commit is contained in:
Fanani M. Ihsan 2016-05-25 15:35:45 +07:00
parent 6444ad234c
commit 5b0c7c26a8
2 changed files with 54 additions and 73 deletions

26
test.py
View File

@ -13,23 +13,23 @@ if conn.get('status'):
print zk.disable_device() print zk.disable_device()
print 'Firmware Version: : {}'.format(zk.get_firmware_version()) print 'Firmware Version: : {}'.format(zk.get_firmware_version())
# Load test create 2000 users # Load test create 2000 users
for i in range(1, 2000+1): # for i in range(1, 2000+1):
privilege = const.USER_DEFAULT # privilege = const.USER_DEFAULT
if i == 1: # if i == 1:
privilege = const.USER_ADMIN # privilege = const.USER_ADMIN
print zk.set_user(uid=i, name='user #{}'.format(i), privilege=privilege, password='123456', group_id='', user_id='{}'.format(i)) # print zk.set_user(uid=i, name='user #{}'.format(i), privilege=privilege, password='12345678', group_id='', user_id='{}'.format(i))
# print 'Restarting device' # print 'Restarting device'
# print zk.restart() # print zk.restart()
# print 'Turning off device' # print 'Turning off device'
# print zk.power_off() # print zk.power_off()
# # users = zk.get_users() # users = zk.get_users()
# # if users: # if users:
# # for uid in users: # for uid in users:
# # if users[uid][2] == 14: # if users[uid][2] == 14:
# # level = 'Admin' # level = 'Admin'
# # else: # else:
# # level = 'User' # level = 'User'
# # print "[UID %d]: ID: %s, Name: %s, Level: %s, Password: %s" % ( uid, users[uid][0], users[uid][1], level, users[uid][3] ) # print "[UID %d]: ID: %s, Name: %s, Level: %s, Password: %s" % ( uid, users[uid][0], users[uid][1], level, users[uid][3] )
print zk.test_voice() print zk.test_voice()
print zk.enable_device() print zk.enable_device()

View File

@ -219,68 +219,49 @@ class ZK(object):
else: else:
return cmd_response return cmd_response
# def __get_size_user(self): def __get_size_user(self):
# """Checks a returned packet to see if it returned CMD_PREPARE_DATA, """Checks a returned packet to see if it returned CMD_PREPARE_DATA,
# indicating that data packets are to be sent indicating that data packets are to be sent
# Returns the amount of bytes that are going to be sent""" Returns the amount of bytes that are going to be sent"""
# response = self.__response response = self.__response
# if response == const.CMD_PREPARE_DATA: if response == const.CMD_PREPARE_DATA:
# size = unpack('I', self.__data_recv[8:12])[0] size = unpack('I', self.__data_recv[8:12])[0]
# return size return size
# else: else:
# return 0 return 0
# def get_users(self): def get_users(self):
# command = const.CMD_USERTEMP_RRQ command = const.CMD_USERTEMP_RRQ
# command_string = chr(5) cmd_response = self.__send_command(command=command, response_size=1024)
cmd_response['data'] = ''
if cmd_response:
bytes = self.__get_size_user()
userdata = []
if bytes:
while bytes > 0:
data_recv = self.__sock.recv(1032)
userdata.append(data_recv)
bytes -= 1024
# try: if len(userdata):
# buf = self.__create_header(command=command, session_id=self.__sesion_id, reply_id=self.__reply_id, command_string=command_string) user_byte = ''.join(userdata)[12:]
# self.__sending_packet(buf) while len(user_byte) >= 72:
# self.__receive_packet(1024) uid, privilege, password, name, sparator, group_id, user_id = unpack('2sc8s28sc7sx24s', user_byte[0:72])
u1 = int( uid[0].encode("hex"), 16)
u2 = int( uid[1].encode("hex"), 16)
# bytes = self.__get_size_user() uid = u1 + (u2*256)
# userdata = [] print '---'
print uid
# if bytes: print privilege
# while bytes > 0: print password
# data_recv, addr = self.__sock.recvfrom(1032) print name
# userdata.append(data_recv) print sparator
# bytes -= 1024 print group_id
print user_id
# self.__receive_packet(8) print '---'
# users = {}
# if len(userdata) > 0:
# # The first 4 bytes don't seem to be related to the user
# for x in xrange(len(userdata)):
# if x > 0:
# userdata[x] = userdata[x][8:]
# userdata = ''.join(userdata)
# userdata = userdata[11:]
# while len(userdata) > 72:
# uid, role, password, name, userid = unpack( '2s2s8s28sx31s', userdata.ljust(72)[:72])
# uid = int( uid.encode("hex"), 16)
# # Clean up some messy characters from the user name
# password = password.split('\x00', 1)[0]
# password = unicode(password.strip('\x00|\x01\x10x'), errors='ignore')
# userid = unicode(userid.strip('\x00|\x01\x10x'), errors='ignore')
# name = name.split('\x00', 1)[0]
# if not name:
# name = uid
# users[uid] = (userid, name, int( role.encode("hex"), 16 ), password)
# userdata = userdata[72:]
# return users
# except Exception, e:
# return (False, e)
user_byte = user_byte[72:]
else:
return cmd_response