69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
from COM.COM_TCP import TCPClient
|
|
import queue
|
|
import json
|
|
from Model.RobotModel import DataAddress,DATARequest,DATAReply
|
|
|
|
class RobotClient(TCPClient):
|
|
|
|
def __init__(self, ip, port, command_quene, status_model:DataAddress):
|
|
super().__init__(ip, port)
|
|
self.command_quene = command_quene
|
|
self.status_model = status_model
|
|
self.errorCommands = {}
|
|
|
|
def add_sendQuene(self,command): #后面 命令分等级,紧急命令直接执行
|
|
self.command_quene.put(command)
|
|
return
|
|
|
|
def send_Command(self):
|
|
try:
|
|
if self.command_quene.qsize()!=0:
|
|
command = self.command_quene.get()
|
|
self.client_socket.send(command.encode('utf-8'))
|
|
|
|
if False:
|
|
response = self.client_socket.recv(1024).decode('utf-8')
|
|
response_message = json.loads(response)
|
|
if True:
|
|
print('正确相应')
|
|
else:
|
|
self.errorCommands[json.dumps(command).encode('utf-8')] = response_message
|
|
print('出错')
|
|
return True
|
|
else:
|
|
return True
|
|
except Exception as e:
|
|
print('连接断开')
|
|
return False
|
|
|
|
|
|
|
|
def send_Status(self):
|
|
request = DATARequest()
|
|
dataAddr = DataAddress()
|
|
request.queryAddr.append(dataAddr)
|
|
|
|
# 移除特殊属性和方法
|
|
request_status_json = request.toString()
|
|
# 转字符串
|
|
|
|
try:
|
|
self.client_socket.send(request_status_json.encode('utf-8'))
|
|
if True:
|
|
response = self.client_socket.recv(1024).decode('utf-8')
|
|
response_message = json.loads(response)
|
|
if True:
|
|
data_status = DATAReply()
|
|
data_status.__dict__ = response_message
|
|
data_address_array = data_status.queryAddr
|
|
data_Address = DataAddress()
|
|
data_Address.setPosition(*data_address_array)
|
|
self.status_model.setPosition(*data_address_array)
|
|
return True
|
|
except Exception as e:
|
|
return False
|
|
|
|
|
|
|
|
|