|
|
@@ -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
|
|
|
from mcu.capture.smart_shooter_class import SmartShooter
|
|
|
# mcu命令
|
|
|
@@ -29,6 +30,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
|
|
|
@@ -201,6 +203,44 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
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))
|
|
|
@@ -856,7 +896,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)
|
|
|
@@ -869,7 +909,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、进行临时连接,并发送命令,成功后,自动连接对应设备
|
|
|
@@ -890,9 +930,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:
|
|
|
@@ -905,7 +945,7 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
data=None,
|
|
|
)
|
|
|
serial_handle.close()
|
|
|
- return
|
|
|
+ return False
|
|
|
print("尝试写入数据")
|
|
|
|
|
|
buf = bytearray(b"")
|
|
|
@@ -918,9 +958,9 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
except serial.SerialTimeoutException:
|
|
|
print("写入数据错误")
|
|
|
serial_handle.close()
|
|
|
- return
|
|
|
+ return False
|
|
|
|
|
|
- time.sleep(0.3)
|
|
|
+ await asyncio.sleep(0.3)
|
|
|
print("尝试接收命令")
|
|
|
receive_data = self.read_cmd(serial_handle)
|
|
|
device_id = 0
|
|
|
@@ -933,10 +973,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",
|
|
|
@@ -946,32 +987,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链接是否正常
|
|
|
@@ -980,11 +1055,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)
|
|
|
@@ -999,7 +1074,7 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
)
|
|
|
self.serial_ins = None
|
|
|
self.connect_state = False
|
|
|
- return False
|
|
|
+ return False,None
|
|
|
|
|
|
except:
|
|
|
message_data = {
|
|
|
@@ -1012,7 +1087,7 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
)
|
|
|
self.serial_ins = None
|
|
|
self.connect_state = False
|
|
|
- return False
|
|
|
+ return False, None
|
|
|
|
|
|
message_data = {
|
|
|
"_type": "show_info",
|
|
|
@@ -1029,7 +1104,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(
|
|
|
@@ -1153,6 +1228,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":
|
|
|
@@ -1405,7 +1483,7 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
image_counts += 1
|
|
|
# 批量插入
|
|
|
image_deal_mode = 0 if action_info == "执行左脚程序" else 1
|
|
|
- state,record_id = insert_photo_records(
|
|
|
+ state,record_id = await insert_photo_records(
|
|
|
image_deal_mode=image_deal_mode,
|
|
|
goods_art_no=goods_art_no,
|
|
|
image_index=idx,
|
|
|
@@ -1434,7 +1512,7 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
for index, action in enumerate(config_list):
|
|
|
# await asyncio.sleep(0.01)
|
|
|
if self.is_stop_action == True:
|
|
|
- self.is_stop_action = False
|
|
|
+ print("停止命令接收,立即终止")
|
|
|
break
|
|
|
# action_is_take_picture = action["take_picture"]
|
|
|
record_id = action["record_id"]
|
|
|
@@ -1478,6 +1556,27 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
)
|
|
|
self.msg_type = "mcu"
|
|
|
# 在第三张图时检查是否有对应图片生成
|
|
|
+ # if index == 3:
|
|
|
+ # # if not self.image_process_data.check_photo_is_get():
|
|
|
+ # self.sendSocketMessage(
|
|
|
+ # code=1,
|
|
|
+ # msg="未获取到图片数据",
|
|
|
+ # device_status=0,
|
|
|
+ # )
|
|
|
+ # self.action_state = 2
|
|
|
+ # return
|
|
|
+ if index == total_len - 1:
|
|
|
+ # 最后一个初始化处理
|
|
|
+ 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"
|
|
|
@@ -1534,6 +1633,26 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
|
|
|
code=1, msg="未查询到重拍记录得配置信息,请确认", device_status=0
|
|
|
)
|
|
|
|
|
|
+ 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:
|
|
|
@@ -1568,8 +1687,10 @@ 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))
|
|
|
print("MCU断开连接,已释放")
|