|
|
@@ -15,6 +15,7 @@ from databases import insert_photo_records
|
|
|
from .McuDeviationSet import McuDeviationSet
|
|
|
from .OtherSet import OtherSet
|
|
|
from .DebugUart import DebugUart
|
|
|
+from .LineControl import LineControl
|
|
|
import copy
|
|
|
|
|
|
# mcu命令
|
|
|
@@ -27,6 +28,7 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
self.mcu_deviation_set = McuDeviationSet(self)
|
|
|
self.mcu_other_set = OtherSet(self)
|
|
|
self.debug_uart = DebugUart(self)
|
|
|
+ self.line_control = LineControl(websocket_manager)
|
|
|
self.m_t = 1
|
|
|
# 0未开始 1进行中 2已结束 99异常
|
|
|
self.action_state = 2
|
|
|
@@ -36,7 +38,6 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
self.state_turntable_steering = 3
|
|
|
self.state_overturn_steering = 3
|
|
|
self.state_move_turntable_steering = 3
|
|
|
-
|
|
|
self.last_from_mcu_move_respond_data = None
|
|
|
self.camera_motor_speed = 0
|
|
|
self.camera_motor_value = 0
|
|
|
@@ -52,6 +53,7 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
self.is_runn_action = False
|
|
|
self.is_stop_action = False
|
|
|
self.connect_state = False
|
|
|
+ self.is_init_while = False
|
|
|
self.device_name_dict = {
|
|
|
"camera_steering": 0,
|
|
|
"camera_high_motor": 1,
|
|
|
@@ -194,10 +196,50 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
# self.sign_data.emit(
|
|
|
# {"_type": "show_info", "plugins_mode": "mcu", "data": "MCU 连接失败"}
|
|
|
# )
|
|
|
+ print("串口未连接,请检查")
|
|
|
+ self.sendSocketMessage(code=1, msg="串口未连接,请检查",device_status=-1)
|
|
|
message = {"_type": "show_info", "plugins_mode": "mcu", "data": "MCU 连接失败"}
|
|
|
self.sendSocketMessage(code=1, msg="MCU 连接失败", data=message,device_status=-1)
|
|
|
self.close_connect()
|
|
|
|
|
|
+ async def initControlLine(self,serial_handle):
|
|
|
+ '''实例化有线控制设备'''
|
|
|
+ self.line_control.connect_state = True
|
|
|
+ self.line_control.serial_ins = serial_handle
|
|
|
+ await self.line_control.run()
|
|
|
+ # if self.init_state == True:
|
|
|
+ # print("已经初始化过,请勿重复初始化")
|
|
|
+ # self.sendSocketMessage(msg="设备初始化完成", device_status=2)
|
|
|
+ # return False
|
|
|
+ # self.serial_ins.clearn_flush()
|
|
|
+ # self.to_init_device_origin_point(device_name="mcu", is_force=is_force)
|
|
|
+ # print("MCU 开始循环~")
|
|
|
+ # while 1:
|
|
|
+ # await asyncio.sleep(0.01)
|
|
|
+ # if not self.serial_ins or not self.connect_state:
|
|
|
+ # break
|
|
|
+ # try:
|
|
|
+ # # print("mcu send_cmd")
|
|
|
+ # self.send_cmd()
|
|
|
+ # # time.sleep(0.01)
|
|
|
+ # self.get_basic_info_mcu()
|
|
|
+ # # self.close_other_window()
|
|
|
+ # except BaseException as e:
|
|
|
+ # print("121231298908", e)
|
|
|
+ # break
|
|
|
+
|
|
|
+ # self.is_running = False
|
|
|
+ # self.connect_state = False
|
|
|
+ # print("MCU 循环退出~")
|
|
|
+ # # self.sign_data.emit(
|
|
|
+ # # {"_type": "show_info", "plugins_mode": "mcu", "data": "MCU 连接失败"}
|
|
|
+ # # )
|
|
|
+ # message = {"_type": "show_info", "plugins_mode": "mcu", "data": "MCU 连接失败"}
|
|
|
+ # self.sendSocketMessage(
|
|
|
+ # code=1, msg="MCU 连接失败", data=message, device_status=-1
|
|
|
+ # )
|
|
|
+ # self.close_connect()
|
|
|
+
|
|
|
def stop_mcu(self):
|
|
|
buf = [self.command["stop_mcu"]]
|
|
|
buf.extend(self.encapsulation_data(data=1, len_data=1))
|
|
|
@@ -261,12 +303,16 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
|
|
|
async def getDeviationInfo(self):
|
|
|
await asyncio.sleep(0.01)
|
|
|
- # 发送获取偏移量
|
|
|
- data = [self.command["get_deviation"], 1]
|
|
|
- self.add_send_data_queue(data)
|
|
|
- # if self.serial_ins:
|
|
|
- # self.serial_ins.write_cmd(data)
|
|
|
- print("发送获取偏移量")
|
|
|
+ try:
|
|
|
+ # 发送获取偏移量
|
|
|
+ data = [self.command["get_deviation"], 1]
|
|
|
+ self.add_send_data_queue(data)
|
|
|
+ # if self.serial_ins:
|
|
|
+ # self.serial_ins.write_cmd(data)
|
|
|
+ print("发送获取偏移量")
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ print("getDeviationInfo", "暂未获取到self.command")
|
|
|
|
|
|
def set_deviation(self, device_name, _type=0, deviation=0):
|
|
|
# turntable----0 angle_ratio 1 turntable_steering_deviation
|
|
|
@@ -458,7 +504,7 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
self.connect_state = False
|
|
|
return False
|
|
|
if not receive_data:
|
|
|
- return
|
|
|
+ return False
|
|
|
# print("receive_data", receive_data)
|
|
|
# 数据 结构 command,按命令解析
|
|
|
# command 0(9) 相机高度1-2 相机角度3-4 转盘角度5-6 灯光状态7 激光指示器状态8,运行状态9
|
|
|
@@ -847,7 +893,7 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
}
|
|
|
self.sendSocketMessage(1, "串口被移除", data)
|
|
|
|
|
|
- def add_port_by_linkage(self, port_name):
|
|
|
+ async def add_port_by_linkage(self, port_name):
|
|
|
# port_value :串口基础信息
|
|
|
# todo 根据prot_value 信息自动进行连接
|
|
|
print("add", port_name)
|
|
|
@@ -860,7 +906,7 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
self.sendSocketMessage(
|
|
|
msg="开始识别接口:{}".format(port_name), data=message_data, device_status=1
|
|
|
)
|
|
|
- time.sleep(1)
|
|
|
+ await asyncio.sleep(1)
|
|
|
"""
|
|
|
步骤:
|
|
|
1、进行临时连接,并发送命令,成功后,自动连接对应设备
|
|
|
@@ -881,9 +927,9 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
device_status=-1,
|
|
|
)
|
|
|
print("串口:{} 被占用".format(port_name))
|
|
|
- return
|
|
|
+ return False
|
|
|
|
|
|
- time.sleep(2)
|
|
|
+ await asyncio.sleep(2)
|
|
|
print("开始发送命令")
|
|
|
data = [90, 1]
|
|
|
try:
|
|
|
@@ -896,7 +942,7 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
data=None,
|
|
|
)
|
|
|
serial_handle.close()
|
|
|
- return
|
|
|
+ return False
|
|
|
print("尝试写入数据")
|
|
|
|
|
|
buf = bytearray(b"")
|
|
|
@@ -909,11 +955,12 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
except serial.SerialTimeoutException:
|
|
|
print("写入数据错误")
|
|
|
serial_handle.close()
|
|
|
- return
|
|
|
+ return False
|
|
|
|
|
|
- time.sleep(0.3)
|
|
|
- print("尝试接收命令")
|
|
|
+ await asyncio.sleep(0.3)
|
|
|
+ # print("尝试接收命令")
|
|
|
receive_data = self.read_cmd(serial_handle)
|
|
|
+ print("尝试接收命令", receive_data)
|
|
|
device_id = 0
|
|
|
|
|
|
if receive_data:
|
|
|
@@ -924,10 +971,11 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
|
|
|
print("关闭串口:{}".format(port_name))
|
|
|
serial_handle.close()
|
|
|
-
|
|
|
+ loop = asyncio.get_event_loop()
|
|
|
+ print("device_id============>>>", device_id)
|
|
|
if device_id > 0:
|
|
|
if device_id == 1:
|
|
|
- self.to_connect_com(port_name)
|
|
|
+ await self.to_connect_com(port_name)
|
|
|
message_data = {
|
|
|
"_type": "show_info",
|
|
|
"plugins_mode": "auto_select_com",
|
|
|
@@ -937,32 +985,66 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
msg="MCU开始连接", data=message_data, device_status=1
|
|
|
)
|
|
|
self.connected_ports_dict[port_name] = "MCU"
|
|
|
- message_data = {
|
|
|
- "_type": "select_port_name",
|
|
|
- "plugins_mode": "auto_select_com",
|
|
|
- "data": {
|
|
|
- "device_name": "mcu" if device_id == 1 else "remote_control",
|
|
|
- "port_name": port_name,
|
|
|
- },
|
|
|
- }
|
|
|
- self.sendSocketMessage(
|
|
|
- msg="MCU连接成功", data=message_data, device_status=2
|
|
|
- )
|
|
|
- time.sleep(2)
|
|
|
- loop = asyncio.get_event_loop()
|
|
|
- loop.create_task(self.initDevice(), name="init_mcu")
|
|
|
+ message_data = {
|
|
|
+ "_type": "select_port_name",
|
|
|
+ "plugins_mode": "auto_select_com",
|
|
|
+ "data": {
|
|
|
+ "device_name": "mcu" if device_id == 1 else "remote_control",
|
|
|
+ "port_name": port_name,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ self.sendSocketMessage(
|
|
|
+ msg="MCU连接成功", data=message_data, device_status=2
|
|
|
+ )
|
|
|
+ await asyncio.sleep(2)
|
|
|
+ # await self.initDevice()
|
|
|
+ loop.create_task(
|
|
|
+ self.initDevice(port_name),
|
|
|
+ name="initDevice",
|
|
|
+ )
|
|
|
+ elif device_id == 2:
|
|
|
+ # 有线接收器
|
|
|
+ print("device_id", device_id)
|
|
|
+ loop.create_task(
|
|
|
+ self.line_control.to_connect_com(port_name),
|
|
|
+ name="line_control_to_connect_com",
|
|
|
+ )
|
|
|
+ # message_data = {
|
|
|
+ # "_type": "show_info",
|
|
|
+ # "plugins_mode": "auto_select_com",
|
|
|
+ # "data": {"text": "有线控制器开始连接"},
|
|
|
+ # }
|
|
|
+ # self.sendSocketMessage(
|
|
|
+ # msg="有线控制器开始连接", data=message_data, device_status=1
|
|
|
+ # )
|
|
|
+ # self.connected_ports_dict[port_name] = "MCU"
|
|
|
+ # message_data = {
|
|
|
+ # "_type": "select_port_name",
|
|
|
+ # "plugins_mode": "auto_select_com",
|
|
|
+ # "data": {
|
|
|
+ # "device_name": "mcu" if device_id == 1 else "remote_control",
|
|
|
+ # "port_name": port_name,
|
|
|
+ # },
|
|
|
+ # }
|
|
|
+ # self.sendSocketMessage(
|
|
|
+ # msg="有线控制器开始连接", data=message_data, device_status=2
|
|
|
+ # )
|
|
|
+ # await asyncio.sleep(2)
|
|
|
+ # await self.initControlLine(serial_handle)
|
|
|
# async def getBaseInfo():
|
|
|
# while True:
|
|
|
# await asyncio.sleep(1)
|
|
|
# # 异步循环获取设备信息
|
|
|
# self.to_get_mcu_base_info()
|
|
|
# asyncio.gather(getBaseInfo())
|
|
|
+ return True
|
|
|
else:
|
|
|
print("串口无法识别")
|
|
|
self.sendSocketMessage(
|
|
|
code=1,
|
|
|
msg="串口无法识别,请重新插拔拍照机USB", data=message_data, device_status=-1
|
|
|
)
|
|
|
+ return False
|
|
|
# 走其他途径处理
|
|
|
|
|
|
# 检查当前MCU链接是否正常
|
|
|
@@ -971,11 +1053,11 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
# 连接不上,记录为其他列表
|
|
|
def clearMyInstance(self):
|
|
|
SingletonType.clear_instance()
|
|
|
- def to_connect_com(self, port_name):
|
|
|
+ async def to_connect_com(self, port_name):
|
|
|
# 关闭串口
|
|
|
print("to_connect_com", port_name)
|
|
|
self.close_connect()
|
|
|
- time.sleep(0.3)
|
|
|
+ await asyncio.sleep(0.3)
|
|
|
self.connect_state = False
|
|
|
try:
|
|
|
self.serial_ins = SerialIns(port_name=port_name, baud=115200, timeout=0.1)
|
|
|
@@ -990,7 +1072,7 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
)
|
|
|
self.serial_ins = None
|
|
|
self.connect_state = False
|
|
|
- return False
|
|
|
+ return False,None
|
|
|
|
|
|
except:
|
|
|
message_data = {
|
|
|
@@ -1003,7 +1085,7 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
)
|
|
|
self.serial_ins = None
|
|
|
self.connect_state = False
|
|
|
- return False
|
|
|
+ return False, None
|
|
|
|
|
|
message_data = {
|
|
|
"_type": "show_info",
|
|
|
@@ -1020,7 +1102,7 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
self.serial_ins.write_cmd(data)
|
|
|
|
|
|
# 延迟接收数据
|
|
|
- time.sleep(0.3)
|
|
|
+ await asyncio.sleep(0.3)
|
|
|
receive_data = self.serial_ins.read_cmd(out_time=1)
|
|
|
if receive_data:
|
|
|
print(
|
|
|
@@ -1068,8 +1150,8 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
self.is_running = False
|
|
|
self.connect_state = False
|
|
|
self.connected_ports_dict = {} # 已连接的ports
|
|
|
- self.p_list = []
|
|
|
- self.temp_ports_dict = {}
|
|
|
+ # self.p_list = []
|
|
|
+ # self.temp_ports_dict = {}
|
|
|
self.init_state = False
|
|
|
print("关闭MCU")
|
|
|
|
|
|
@@ -1144,6 +1226,9 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
|
|
|
cmd = 1
|
|
|
device_id = self.device_name_dict[device_name]
|
|
|
+ # if device_id != 1:
|
|
|
+ # print("F非MCU设备,禁止处理")
|
|
|
+ # return
|
|
|
print("正在执行",device_name)
|
|
|
match device_name:
|
|
|
case "camera_high_motor":
|
|
|
@@ -1389,17 +1474,19 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
for idx, item in enumerate(config_list):
|
|
|
is_take_picture = item["take_picture"]
|
|
|
action_id = item["id"]
|
|
|
+ record_id = -1
|
|
|
if is_take_picture:
|
|
|
action_names.append(item["action_name"])
|
|
|
image_counts += 1
|
|
|
# 批量插入
|
|
|
image_deal_mode = 0 if action_info == "执行左脚程序" else 1
|
|
|
- insert_photo_records(
|
|
|
+ state,record_id = insert_photo_records(
|
|
|
image_deal_mode=image_deal_mode,
|
|
|
goods_art_no=goods_art_no,
|
|
|
image_index=idx,
|
|
|
action_id=action_id,
|
|
|
)
|
|
|
+ config_list[idx]["record_id"] = record_id
|
|
|
total_len = len(config_list)
|
|
|
self.action_state = 1
|
|
|
self.msg_type = "image_process"
|
|
|
@@ -1422,11 +1509,14 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
for index, action in enumerate(config_list):
|
|
|
await asyncio.sleep(0.1)
|
|
|
if self.is_stop_action == True:
|
|
|
- self.is_stop_action = False
|
|
|
+ print("停止命令接收,立即终止")
|
|
|
break
|
|
|
- action_is_take_picture = action["take_picture"]
|
|
|
+ # action_is_take_picture = action["take_picture"]
|
|
|
+ record_id = action["record_id"]
|
|
|
image_index = -1
|
|
|
- if action_is_take_picture:
|
|
|
+ if record_id == -1:
|
|
|
+ image_index = -1
|
|
|
+ else:
|
|
|
image_index = index
|
|
|
program_item = ProgramItem(
|
|
|
websocket_manager=self.websocket_manager,
|
|
|
@@ -1434,6 +1524,7 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
mcu=self,
|
|
|
goods_art_no=goods_art_no,
|
|
|
image_index=image_index,
|
|
|
+ record_id=record_id,
|
|
|
)
|
|
|
print("self.action_state===>", self.action_state)
|
|
|
if self.action_state != 1:
|
|
|
@@ -1472,6 +1563,14 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
# 最后一个初始化处理
|
|
|
pass
|
|
|
# self.action_state = 2
|
|
|
+ if self.is_stop_action==True:
|
|
|
+ self.msg_type = "run_mcu_stop"
|
|
|
+ self.sendSocketMessage(
|
|
|
+ code=0,
|
|
|
+ msg=f"货号:{goods_art_no},执行终止",
|
|
|
+ device_status=2,
|
|
|
+ )
|
|
|
+ self.is_stop_action = False
|
|
|
self.action_state = 2
|
|
|
self.is_runn_action = False
|
|
|
self.msg_type = "photo_take_finish"
|
|
|
@@ -1483,7 +1582,7 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
self.msg_type = "mcu"
|
|
|
self.controlDevice("laser_position", 1)
|
|
|
|
|
|
- async def run_mcu_config_single(self, config_info, goods_art_no,msg_type="run_mcu_single_finish",image_index=-1):
|
|
|
+ async def run_mcu_config_single(self, config_info, goods_art_no,msg_type="run_mcu_single_finish",image_index=-1,record_id=-1):
|
|
|
'''独立拍照 仅作测试用'''
|
|
|
if self.checkDevice() == False:
|
|
|
return
|
|
|
@@ -1497,6 +1596,7 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
mcu=self,
|
|
|
goods_art_no=goods_art_no,
|
|
|
image_index=image_index,
|
|
|
+ record_id=record_id,
|
|
|
)
|
|
|
print("self.action_state===>", self.action_state)
|
|
|
if self.action_state != 1:
|
|
|
@@ -1531,6 +1631,26 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
)
|
|
|
self.msg_type = "mcu"
|
|
|
|
|
|
+ async def only_take_photo(self, goods_art_no, image_index, record_id):
|
|
|
+ await asyncio.sleep(0.1)
|
|
|
+ print("only_take_photo=====>",goods_art_no, image_index, record_id)
|
|
|
+ if goods_art_no == "":
|
|
|
+ print("only_take_photo 数据查询异常")
|
|
|
+ return
|
|
|
+ # 关闭led
|
|
|
+ self.controlDevice("laser_position", 0)
|
|
|
+ program_item = ProgramItem(
|
|
|
+ websocket_manager=self.websocket_manager,
|
|
|
+ action_data={},
|
|
|
+ mcu=self,
|
|
|
+ goods_art_no=goods_art_no,
|
|
|
+ image_index=image_index,
|
|
|
+ record_id=record_id,
|
|
|
+ )
|
|
|
+ program_item.digicam_take_picture()
|
|
|
+ # 打开led
|
|
|
+ self.controlDevice("laser_position", 1)
|
|
|
+
|
|
|
|
|
|
async def checkMcuConnection(device_ctrl: DeviceControl):
|
|
|
if device_ctrl.is_running == True:
|
|
|
@@ -1542,9 +1662,12 @@ async def checkMcuConnection(device_ctrl: DeviceControl):
|
|
|
device_ctrl.device_status = 2
|
|
|
device_ctrl.sendSocketMessage(code=0, msg="MCU连接成功", data=message)
|
|
|
return
|
|
|
+ if device_ctrl.is_init_while:
|
|
|
+ return
|
|
|
+ device_ctrl.is_init_while = True
|
|
|
"""实时检测串口是否连接"""
|
|
|
while True:
|
|
|
- await asyncio.sleep(0.5)
|
|
|
+ await asyncio.sleep(1)
|
|
|
if device_ctrl.mcu_exit:
|
|
|
break
|
|
|
ports_dict = device_ctrl.scan_serial_port()
|
|
|
@@ -1554,8 +1677,6 @@ async def checkMcuConnection(device_ctrl: DeviceControl):
|
|
|
if device_ctrl.p_list:
|
|
|
_p = device_ctrl.p_list.pop()
|
|
|
device_ctrl.remove_port(_p)
|
|
|
- print("串口未连接,请检查")
|
|
|
- device_ctrl.sendSocketMessage(code=1, msg="串口未连接,请检查",device_status=-1)
|
|
|
continue
|
|
|
if ports_dict:
|
|
|
for index, _i in enumerate(device_ctrl.p_list):
|
|
|
@@ -1565,8 +1686,11 @@ async def checkMcuConnection(device_ctrl: DeviceControl):
|
|
|
for _port_name, _port_value in ports_dict.items():
|
|
|
if _port_name not in device_ctrl.p_list:
|
|
|
try:
|
|
|
+ flag = await device_ctrl.add_port_by_linkage(_port_name)
|
|
|
+ if flag == False:
|
|
|
+ continue
|
|
|
device_ctrl.p_list.append(_port_name)
|
|
|
- device_ctrl.add_port_by_linkage(_port_name)
|
|
|
except BaseException as e:
|
|
|
print("串口不存在{} {}".format(_port_name, e))
|
|
|
+ device_ctrl.is_init_while = False
|
|
|
print("MCU断开连接,已释放")
|