create function __sending_packet to simplification the pakcet sending process
This commit is contained in:
parent
168c70a11d
commit
36f8643651
1
test.py
1
test.py
@ -8,6 +8,7 @@ zk = zk.ZK('192.168.1.201')
|
|||||||
status, message = zk.connect()
|
status, message = zk.connect()
|
||||||
if status:
|
if status:
|
||||||
print 'Firmware Version: : {}'.format(zk.get_firmware_version())
|
print 'Firmware Version: : {}'.format(zk.get_firmware_version())
|
||||||
|
zk.restart()
|
||||||
print zk.disconnect()
|
print zk.disconnect()
|
||||||
else:
|
else:
|
||||||
print message
|
print message
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
from base import ZK
|
from base import ZK
|
||||||
|
|
||||||
__all__ = ['ZK']
|
__all__ = ['ZK']
|
19
zk/base.py
19
zk/base.py
@ -53,6 +53,10 @@ class ZK(object):
|
|||||||
|
|
||||||
return pack('H', chksum)
|
return pack('H', chksum)
|
||||||
|
|
||||||
|
def __sending_packet(self, buf):
|
||||||
|
self.__sock.sendto(buf, self.__address)
|
||||||
|
self.__data_recv, addr = self.__sock.recvfrom(1024)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def __response(self):
|
def __response(self):
|
||||||
'''
|
'''
|
||||||
@ -76,8 +80,7 @@ class ZK(object):
|
|||||||
command = const.CMD_CONNECT
|
command = const.CMD_CONNECT
|
||||||
try:
|
try:
|
||||||
buf = self.__create_header(command=command)
|
buf = self.__create_header(command=command)
|
||||||
self.__sock.sendto(buf, self.__address)
|
self.__sending_packet(buf)
|
||||||
self.__data_recv, addr = self.__sock.recvfrom(1024)
|
|
||||||
|
|
||||||
if self.__response == const.CMD_ACK_OK:
|
if self.__response == const.CMD_ACK_OK:
|
||||||
return (True, self.__response)
|
return (True, self.__response)
|
||||||
@ -94,8 +97,7 @@ class ZK(object):
|
|||||||
command = const.CMD_EXIT
|
command = const.CMD_EXIT
|
||||||
try:
|
try:
|
||||||
buf = self.__create_header(command=command, session_id=self.__sesion_id, reply_id=self.__reply_id)
|
buf = self.__create_header(command=command, session_id=self.__sesion_id, reply_id=self.__reply_id)
|
||||||
self.__sock.sendto(buf, self.__address)
|
self.__sending_packet(buf)
|
||||||
self.__data_recv, addr = self.__sock.recvfrom(1024)
|
|
||||||
if self.__response == const.CMD_ACK_OK:
|
if self.__response == const.CMD_ACK_OK:
|
||||||
return (True, self.__response)
|
return (True, self.__response)
|
||||||
else:
|
else:
|
||||||
@ -107,8 +109,7 @@ class ZK(object):
|
|||||||
command = const.CMD_GET_VERSION
|
command = const.CMD_GET_VERSION
|
||||||
try:
|
try:
|
||||||
buf = self.__create_header(command=command, session_id=self.__sesion_id, reply_id=self.__reply_id)
|
buf = self.__create_header(command=command, session_id=self.__sesion_id, reply_id=self.__reply_id)
|
||||||
self.__sock.sendto(buf, self.__address)
|
self.__sending_packet(buf)
|
||||||
self.__data_recv, addr = self.__sock.recvfrom(1024)
|
|
||||||
if self.__response == const.CMD_ACK_OK:
|
if self.__response == const.CMD_ACK_OK:
|
||||||
return self.__data_recv[8:]
|
return self.__data_recv[8:]
|
||||||
else:
|
else:
|
||||||
@ -124,8 +125,7 @@ class ZK(object):
|
|||||||
command = const.CMD_RESTART
|
command = const.CMD_RESTART
|
||||||
try:
|
try:
|
||||||
buf = self.__create_header(command=command, session_id=self.__sesion_id, reply_id=self.__reply_id)
|
buf = self.__create_header(command=command, session_id=self.__sesion_id, reply_id=self.__reply_id)
|
||||||
self.__sock.sendto(buf, self.__address)
|
self.__sending_packet(buf)
|
||||||
self.__data_recv, addr = self.__sock.recvfrom(1024)
|
|
||||||
if self.__response == const.CMD_ACK_OK:
|
if self.__response == const.CMD_ACK_OK:
|
||||||
return (True, self.__response)
|
return (True, self.__response)
|
||||||
else:
|
else:
|
||||||
@ -142,8 +142,7 @@ class ZK(object):
|
|||||||
command = const.CMD_POWEROFF
|
command = const.CMD_POWEROFF
|
||||||
try:
|
try:
|
||||||
buf = self.__create_header(command=command, session_id=self.__sesion_id, reply_id=self.__reply_id)
|
buf = self.__create_header(command=command, session_id=self.__sesion_id, reply_id=self.__reply_id)
|
||||||
self.__sock.sendto(buf, self.__address)
|
self.__sending_packet(buf)
|
||||||
self.__data_recv, addr = self.__sock.recvfrom(1024)
|
|
||||||
if self.__response == const.CMD_ACK_OK:
|
if self.__response == const.CMD_ACK_OK:
|
||||||
return (True, self.__response)
|
return (True, self.__response)
|
||||||
else:
|
else:
|
||||||
|
Loading…
Reference in New Issue
Block a user