Files
AutoControlSystem-git/COM/COM_Robot.py
2024-12-03 23:25:17 +08:00

104 lines
3.8 KiB
Python

import logging
from enum import Enum
from turtle import Turtle
from numpy.array_api import trunc
import Constant
from COM.COM_TCP import TCPClient
import queue
import json
from Model.RobotModel import DataAddress,DATARequest,DATAReply
from Util.util_log import log
class DetectType(Enum):
EyeOnHand = 0
EyeOutHand = 1
class RobotClient(TCPClient):
def __init__(self, ip, port, photo_locs,command_quene, status_model:DataAddress,con_ios, time_delay_take,time_delay_put,time_delay_shake):
super().__init__(ip, port)
self.command_quene = command_quene
self.status_model = status_model
self.errorCommands = {}
self.photo_locs = photo_locs
self.con_ios = con_ios
self.time_delay_take= time_delay_take
self.time_delay_put = time_delay_put
self.time_delay_shake = time_delay_shake
self.type_detection = DetectType.EyeOutHand
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())
if True:
response = self.client_socket.recv(1024).decode('utf-8')
# response_message = json.loads(response)
return True
else:
return True
except Exception as e:
log.log_message(logging.ERROR, Constant.str_tcp_robot_connect_fail)
raise
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())
if True:
response = self.client_socket.recv(1024).decode('utf-8')
response_message = json.loads(response)
if True:
try:
data_status = DATAReply()
# if response_message.keys().contains('cmdReply'):
# return
data_status.__dict__ = response_message # {'cmdReply': ['AddRCC', 'ok'], 'dsID': 'www.hc-system.com.HCRemoteCommand', 'reqType': 'AddRCC'}
data_address_array = data_status.queryData
self.status_model.curMode = int(data_address_array[0])
self.status_model.setPosition(*data_address_array[1:13])
self.status_model.curAlarm = int(data_address_array[13])
self.status_model.isMoving = int(data_address_array[14])
self.status_model.RemoteCmdLen = int(data_address_array[15])
self.status_model.toolCoord=int(data_address_array[16])
self.status_model.input_n = int(data_address_array[19])
self.status_model.output_n = int(data_address_array[20])
self.status_model.curSpeed_n = int(data_address_array[21])
except:
if response_message.keys().__contains__('cmdReply'):
return
log.log_message(logging.ERROR,Constant.str_tcp_robot_data_error)
return True
except json.JSONDecodeError as e:
log.log_message(logging.WARNING,Constant.str_sys_json_error+response)
return True
except Exception as e:
log.log_message(logging.ERROR,f'{e}')
raise
def get_origin_position(self):
return self.status_model.getRealPosition()
pass