update get_user function
This commit is contained in:
parent
5b0c7c26a8
commit
8b8715944f
39
test.py
39
test.py
@ -10,6 +10,7 @@ print 'Connecting to device ...'
|
|||||||
conn = zk.connect()
|
conn = zk.connect()
|
||||||
if conn.get('status'):
|
if conn.get('status'):
|
||||||
print conn
|
print conn
|
||||||
|
print 'Disabling device ...'
|
||||||
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
|
||||||
@ -18,27 +19,31 @@ if conn.get('status'):
|
|||||||
# 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='12345678', 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 '--- Get User ---'
|
||||||
# print zk.restart()
|
users = zk.get_users()
|
||||||
# print 'Turning off device'
|
if users.get('status'):
|
||||||
# print zk.power_off()
|
for user in users.get('data'):
|
||||||
# users = zk.get_users()
|
privilege = 'User'
|
||||||
# if users:
|
if user.privilege == const.USER_ADMIN:
|
||||||
# for uid in users:
|
privilege = 'Admin'
|
||||||
# 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 '- 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 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 zk.enable_device()
|
||||||
print 'Disconnecting to device ...'
|
print 'Disconnecting to device ...'
|
||||||
print zk.disconnect()
|
print zk.disconnect()
|
||||||
# if status:
|
|
||||||
# print 'Disonnected !'
|
|
||||||
# else:
|
|
||||||
# print 'Disconnecting Error: {}'.format(message)
|
|
||||||
else:
|
else:
|
||||||
print 'Connecting Error: {}'.format(conn.get('message'))
|
print 'Connecting Error: {}'.format(conn.get('message'))
|
||||||
|
|
||||||
|
53
zk/base.py
53
zk/base.py
@ -3,6 +3,7 @@ from struct import pack, unpack
|
|||||||
from socket import socket, AF_INET, SOCK_DGRAM
|
from socket import socket, AF_INET, SOCK_DGRAM
|
||||||
|
|
||||||
from zk import const
|
from zk import const
|
||||||
|
from zk.user import User
|
||||||
|
|
||||||
class ZK(object):
|
class ZK(object):
|
||||||
|
|
||||||
@ -234,7 +235,6 @@ class ZK(object):
|
|||||||
def get_users(self):
|
def get_users(self):
|
||||||
command = const.CMD_USERTEMP_RRQ
|
command = const.CMD_USERTEMP_RRQ
|
||||||
cmd_response = self.__send_command(command=command, response_size=1024)
|
cmd_response = self.__send_command(command=command, response_size=1024)
|
||||||
cmd_response['data'] = ''
|
|
||||||
if cmd_response:
|
if cmd_response:
|
||||||
bytes = self.__get_size_user()
|
bytes = self.__get_size_user()
|
||||||
userdata = []
|
userdata = []
|
||||||
@ -244,24 +244,41 @@ class ZK(object):
|
|||||||
userdata.append(data_recv)
|
userdata.append(data_recv)
|
||||||
bytes -= 1024
|
bytes -= 1024
|
||||||
|
|
||||||
if len(userdata):
|
data_recv = self.__sock.recv(8)
|
||||||
user_byte = ''.join(userdata)[12:]
|
response = unpack('HHHH', data_recv[:8])[0]
|
||||||
while len(user_byte) >= 72:
|
if response == const.CMD_ACK_OK:
|
||||||
uid, privilege, password, name, sparator, group_id, user_id = unpack('2sc8s28sc7sx24s', user_byte[0:72])
|
users = []
|
||||||
u1 = int( uid[0].encode("hex"), 16)
|
if len(userdata):
|
||||||
u2 = int( uid[1].encode("hex"), 16)
|
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)
|
uid = u1 + (u2*256)
|
||||||
print '---'
|
name = unicode(name.strip('\x00|\x01\x10x'), errors='ignore')
|
||||||
print uid
|
privilege = int(privilege.encode("hex"), 16)
|
||||||
print privilege
|
password = unicode(password.strip('\x00|\x01\x10x'), errors='ignore')
|
||||||
print password
|
group_id = unicode(group_id.strip('\x00|\x01\x10x'), errors='ignore')
|
||||||
print name
|
user_id = unicode(user_id.strip('\x00|\x01\x10x'), errors='ignore')
|
||||||
print sparator
|
|
||||||
print group_id
|
|
||||||
print user_id
|
|
||||||
print '---'
|
|
||||||
|
|
||||||
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:
|
else:
|
||||||
|
cmd_response['data'] = ''
|
||||||
return cmd_response
|
return cmd_response
|
||||||
|
13
zk/user.py
Normal file
13
zk/user.py
Normal 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
|
Loading…
Reference in New Issue
Block a user