update get_user function

This commit is contained in:
Fanani M. Ihsan 2016-05-25 16:28:55 +07:00
parent 5b0c7c26a8
commit 8b8715944f
3 changed files with 70 additions and 35 deletions

39
test.py
View File

@ -10,6 +10,7 @@ print 'Connecting to device ...'
conn = zk.connect()
if conn.get('status'):
print conn
print 'Disabling device ...'
print zk.disable_device()
print 'Firmware Version: : {}'.format(zk.get_firmware_version())
# Load test create 2000 users
@ -18,27 +19,31 @@ if conn.get('status'):
# if i == 1:
# privilege = const.USER_ADMIN
# print zk.set_user(uid=i, name='user #{}'.format(i), privilege=privilege, password='12345678', group_id='', user_id='{}'.format(i))
# print 'Restarting device'
# print zk.restart()
# print 'Turning off device'
# print zk.power_off()
# users = zk.get_users()
# if users:
# for uid in users:
# if users[uid][2] == 14:
# level = 'Admin'
# else:
# 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 '--- Get User ---'
users = zk.get_users()
if users.get('status'):
for user in users.get('data'):
privilege = 'User'
if user.privilege == const.USER_ADMIN:
privilege = 'Admin'
print '- UID #{}'.format(user.uid)
print ' Name : {}'.format(user.name)
print ' Privilege : {}'.format(privilege)
print ' Password : {}'.format(user.password)
print ' Group ID : {}'.format(user.group_id)
print ' User ID : {}'.format(user.user_id)
print "Voice Test ..."
print zk.test_voice()
# print 'Restarting device ...'
# print zk.restart()
# print 'Turning off device ...'
# print zk.power_off()
print 'Enabling device ...'
print zk.enable_device()
print 'Disconnecting to device ...'
print 'Disconnecting to device ...'
print zk.disconnect()
# if status:
# print 'Disonnected !'
# else:
# print 'Disconnecting Error: {}'.format(message)
else:
print 'Connecting Error: {}'.format(conn.get('message'))

View File

@ -3,6 +3,7 @@ from struct import pack, unpack
from socket import socket, AF_INET, SOCK_DGRAM
from zk import const
from zk.user import User
class ZK(object):
@ -234,7 +235,6 @@ class ZK(object):
def get_users(self):
command = const.CMD_USERTEMP_RRQ
cmd_response = self.__send_command(command=command, response_size=1024)
cmd_response['data'] = ''
if cmd_response:
bytes = self.__get_size_user()
userdata = []
@ -244,24 +244,41 @@ class ZK(object):
userdata.append(data_recv)
bytes -= 1024
if len(userdata):
user_byte = ''.join(userdata)[12:]
while len(user_byte) >= 72:
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)
data_recv = self.__sock.recv(8)
response = unpack('HHHH', data_recv[:8])[0]
if response == const.CMD_ACK_OK:
users = []
if len(userdata):
user_byte = ''.join(userdata)[12:]
while len(user_byte) >= 72:
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)
uid = u1 + (u2*256)
print '---'
print uid
print privilege
print password
print name
print sparator
print group_id
print user_id
print '---'
uid = u1 + (u2*256)
name = unicode(name.strip('\x00|\x01\x10x'), errors='ignore')
privilege = int(privilege.encode("hex"), 16)
password = unicode(password.strip('\x00|\x01\x10x'), errors='ignore')
group_id = unicode(group_id.strip('\x00|\x01\x10x'), errors='ignore')
user_id = unicode(user_id.strip('\x00|\x01\x10x'), errors='ignore')
user_byte = user_byte[72:]
user = User(uid, name, privilege, password, group_id, user_id)
users.append(user)
user_byte = user_byte[72:]
cmd_response['status'] = True
cmd_response['code'] = response
cmd_response['message'] = 'success'
cmd_response['data'] = users
return cmd_response
else:
return {
'status': False,
'code': response,
'message': 'failed',
'data': ''
}
else:
cmd_response['data'] = ''
return cmd_response

13
zk/user.py Normal file
View File

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