ProgramItem.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. import asyncio
  2. import json
  3. import os
  4. from .BaseClass import BaseClass
  5. import settings
  6. import time
  7. from .capture.module_digicam import DigiCam
  8. from .capture.module_watch_dog import FileEventHandler
  9. class ProgramItem(BaseClass):
  10. # program_sign = Signal(dict)
  11. # program_refresh_photo_list_sign = Signal()
  12. def __init__(
  13. self,
  14. websocket_manager,
  15. action_data: any,
  16. mcu,
  17. goods_art_no: str = None,
  18. image_index: int = -1,
  19. record_id: int = -1,
  20. smart_shooter=None,
  21. is_get_mcu_state=True,
  22. ):
  23. super().__init__(BaseClass)
  24. # 1 表示等待中,2表示没有等待
  25. self.wait_state = 2
  26. self.msg_type = "mcu"
  27. print("action_data====>", action_data)
  28. self.data = action_data
  29. print("ProgramItem smart_shooter", smart_shooter)
  30. self.smart_shooter = smart_shooter
  31. if smart_shooter == None:
  32. self.capture_one = DigiCam()
  33. captrure_folder_path = self.capture_one.getCaptureFolderPath()
  34. self.watch_dog = FileEventHandler()
  35. self.watch_dog.goods_art_no = goods_art_no
  36. self.watch_dog.image_index = image_index
  37. self.watch_dog.mcu = mcu
  38. self.watch_dog.start_observer(captrure_folder_path)
  39. else:
  40. print("使用smart_shooter")
  41. print("21 =========ProgramItem=======action_data=====")
  42. print(action_data)
  43. self.action_id = self.get_value(action_data, "id")
  44. self.mode_type = self.get_value(action_data, "mode_type")
  45. # self.action_type = self.get_value(action_data, "execution_type", "程序1")
  46. self.action_name = self.get_value(action_data, "action_name", "")
  47. self.is_wait = self.get_value(action_data, "is_wait", False)
  48. self.is_need_confirm = self.get_value(action_data, "is_need_confirm", False)
  49. self.image_index = self.get_value(action_data, "picture_index", 99)
  50. self.camera_height = self.get_value(action_data, "camera_height", 0)
  51. self.camera_angle = float(self.get_value(action_data, "camera_angle", 0.0))
  52. self.af_times = self.get_value(action_data, "number_focus", 0)
  53. self.shoe_overturn = self.get_value(action_data, "shoe_upturn", False)
  54. self.is_photograph = self.get_value(action_data, "take_picture", True)
  55. self.turntable_position = float(
  56. self.get_value(action_data, "turntable_position", 0.0)
  57. )
  58. self.is_get_mcu_state = is_get_mcu_state
  59. self.turntable_angle = float(
  60. self.get_value(action_data, "turntable_angle", 0.0)
  61. )
  62. self.delay_time = self.get_value(action_data, "pre_delay", 0.0)
  63. self.after_delay_time = self.get_value(action_data, "after_delay", 0.0)
  64. self.is_led = self.get_value(action_data, "led_switch", False)
  65. self.last_photograph_time = None # 最近一次拍照时间
  66. self.goods_art_no = goods_art_no # 货号
  67. self.record_id = record_id # 货号
  68. self.set_other()
  69. self.error_info_text = "" # 错误提示信息
  70. # self.setParent(parent)
  71. self.mcu = mcu
  72. # if is_show:
  73. # self.parent = parent
  74. # self.setupUi(self)
  75. # self.init()
  76. # self.show()
  77. def set_other(self):
  78. if self.mode_type == "其他配置":
  79. self.turntable_position = None
  80. self.turntable_angle = None
  81. self.delay_time = 0
  82. self.after_delay_time = 0
  83. self.is_led = False
  84. def get_value(self, data, key, default=None):
  85. if key not in data:
  86. data[key] = default
  87. print("取值模式==>获取到key:{}的值为:{}".format(key, data[key]))
  88. return default
  89. print("默认值模式==>获取到key:{}的值为:{}".format(key, data[key]))
  90. return data[key]
  91. def reset(self):
  92. self.set_state(state_value=0)
  93. self.error_info_text = "" # 错误提示信息
  94. def init(self):
  95. self.icon_dict = {
  96. 0: "resources/other_icon/0weikaishi.png",
  97. 1: "resources/other_icon/1jinxingzhong.png",
  98. 2: "resources/other_icon/2yijieshu.png",
  99. 99: "resources/other_icon/11yichang.png",
  100. }
  101. if self.is_wait:
  102. msg = "{}--等待".format(self.action_name)
  103. self.sendSocketMessage(msg=msg, device_status=0)
  104. else:
  105. msg = "{}".format(self.action_name)
  106. self.sendSocketMessage(msg=msg, device_status=2)
  107. tips_text = "程序:{},对焦:{}".format(1, 1)
  108. self.sendSocketMessage(msg=tips_text, device_status=2)
  109. self.set_state(state_value=0)
  110. # def next_step_clicked(self, *args, **kwargs):
  111. # self.windows.event.set()
  112. def set_state(self, state_value):
  113. self.state = state_value
  114. # icon = QPixmap(self.icon_dict[self.state])
  115. # icon = icon.scaled(
  116. # self.ui_icon.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation
  117. # )
  118. # self.ui_icon.setPixmap(icon)
  119. # if self.state == 0:
  120. # self.ui_retry.setText("")
  121. # self.ui_retry.setEnabled(False)
  122. # if self.state == 1:
  123. # self.ui_retry.setText("")
  124. # self.ui_retry.setEnabled(False)
  125. # if self.state == 2:
  126. # self.ui_retry.setText("单步")
  127. # self.ui_retry.setEnabled(True)
  128. # if self.state == 99:
  129. # self.ui_retry.setText("重试")
  130. # self.ui_retry.setEnabled(True)
  131. # self.ui_action_name.setToolTip(self.error_info_text)
  132. async def check_mcu_move_is_stop(self, re_check=False):
  133. self.error_info_text = ""
  134. # 发送基础数据信息
  135. # self.mcu.to_get_mcu_base_info()
  136. _s = time.time()
  137. last_num_1 = self.mcu.last_mcu_info_data["num"]
  138. await self.mcu.cleanAllReceiveData()
  139. while 1:
  140. print("\033[1;31m执行检查动作\033[0m", self.mcu.mcu_move_state)
  141. print("\033[1;31m执行检查动作\033[0m", self.mcu.action_state)
  142. if self.mcu.action_state != 1:
  143. # 外部终止,停止运行
  144. print("\033[1;31m执行结束\033[0m", "外部终止,停止运行")
  145. return False
  146. cr_time = time.time()
  147. print(cr_time - _s, cr_time, _s)
  148. if cr_time - _s > 8:
  149. self.error_info_text = "MCU检测运动未停止,自动退出"
  150. self.set_state(state_value=99) # 标记异常
  151. print("MCU检测运动未停止,自动退出")
  152. self.sendSocketMessage(msg=self.error_info_text, device_status=-1)
  153. print("\033[1;31m执行结束\033[0m", "MCU检测运动未停止,自动退出")
  154. return False
  155. # return True
  156. # 存在时间间隙,导致误认为所有设备已完成运动
  157. if self.mcu.mcu_move_state == 2:
  158. print("\033[1;31m执行结束\033[0m", "导致误认为所有设备已完成运动")
  159. return True
  160. else:
  161. self.mcu.to_get_mcu_base_info()
  162. asyncio.create_task(self.mcu.send_all_cmd())
  163. await asyncio.sleep(0.5)
  164. self.mcu.get_basic_info_mcu()
  165. # return True
  166. print("\033[1;31m执行结束\033[0m", self.mcu.action_state)
  167. # await asyncio.sleep(0.1)
  168. # self.mcu.to_get_mcu_base_info()
  169. async def run(self, total_len=5, *args):
  170. if total_len == 1:
  171. self.mode_type = "其他配置"
  172. self.set_other()
  173. print("1{} - is run".format(self.action_name))
  174. self.set_state(state_value=1)
  175. if settings.IS_TEST:
  176. await self.do_run()
  177. else:
  178. try:
  179. await self.do_run()
  180. except BaseException as e:
  181. self.sendSocketMessage(
  182. msg="p_item 错误:{}".format(e), device_status=-1
  183. )
  184. self.set_state(state_value=99)
  185. self.set_state(state_value=2)
  186. return True
  187. def run_only_mcu(self, *args):
  188. # ============连接MCU 处理步进电机与舵机等
  189. if settings.IS_MCU:
  190. if self.shoe_overturn:
  191. self.mcu.to_deal_device(device_name="overturn_steering")
  192. time.sleep(0.1)
  193. if self.camera_height is not None:
  194. self.mcu.to_device_move(
  195. device_name="camera_high_motor", value=self.camera_height
  196. )
  197. time.sleep(0.1)
  198. if self.camera_angle is not None:
  199. self.mcu.to_device_move(
  200. device_name="camera_steering", value=self.camera_angle
  201. )
  202. time.sleep(0.1)
  203. if self.turntable_position is not None:
  204. self.mcu.to_device_move(
  205. device_name="turntable_position_motor",
  206. value=self.turntable_position,
  207. )
  208. time.sleep(0.1)
  209. if self.turntable_angle is not None:
  210. self.mcu.to_device_move(
  211. device_name="turntable_steering", value=self.turntable_angle
  212. )
  213. time.sleep(0.1)
  214. loop = asyncio.get_event_loop()
  215. # self.mcu.send_all_cmd()
  216. loop.create_task(self.mcu.send_all_cmd())
  217. async def do_run(self, *args):
  218. await asyncio.sleep(0.001)
  219. # if not self.goods_art_no: # and self.action_name != "初始化位置"
  220. # return False
  221. start_time = time.time()
  222. self.mcu.is_get_mcu_state = self.is_get_mcu_state
  223. # ============连接MCU 处理步进电机与舵机等
  224. if settings.IS_MCU:
  225. if self.mode_type != "其他配置" and await self.check_mcu_move_is_stop() is False:
  226. # MCU运动是否有停止检查,设定超时时间
  227. return
  228. print(
  229. "{} 检查停止时间1:{}".format(
  230. self.action_name, time.time() - start_time
  231. )
  232. )
  233. if self.is_led:
  234. self.mcu.to_deal_device(device_name="laser_position", value=1)
  235. else:
  236. self.mcu.to_deal_device(device_name="laser_position", value=0)
  237. if self.shoe_overturn:
  238. self.mcu.to_deal_device(device_name="overturn_steering")
  239. # time.sleep(0.1)
  240. if self.camera_height is not None:
  241. self.mcu.to_device_move(
  242. device_name="camera_high_motor", value=self.camera_height
  243. )
  244. # time.sleep(0.1)
  245. if self.camera_angle is not None:
  246. self.mcu.to_device_move(
  247. device_name="camera_steering", value=self.camera_angle
  248. )
  249. # time.sleep(0.1)
  250. if self.turntable_position is not None:
  251. self.mcu.to_device_move(
  252. device_name="turntable_position_motor",
  253. value=self.turntable_position,
  254. )
  255. # time.sleep(0.1)
  256. if self.turntable_angle is not None:
  257. self.mcu.to_device_move(
  258. device_name="turntable_steering", value=self.turntable_angle
  259. )
  260. # time.sleep(0.1)
  261. # MCU运动是否有停止检查,设定超时时间
  262. asyncio.create_task(self.mcu.send_all_cmd())
  263. if self.mode_type != "其他配置":
  264. await asyncio.sleep(1.2)
  265. print("二次检查")
  266. if await self.check_mcu_move_is_stop(re_check=True) is False:
  267. print("MCU检测运动未停止,自动退出, 提前退出")
  268. return
  269. self.mcu.is_get_mcu_state = True
  270. if self.delay_time:
  271. await asyncio.sleep(self.delay_time)
  272. if self.is_photograph:
  273. # print("photograph==================")
  274. self.mcu.to_deal_device(device_name="buzzer", times=1)
  275. # 用于临时拍照计数
  276. is_af = True if self.af_times > 0 else False
  277. if self.smart_shooter != None:
  278. # 拍照
  279. print("smart shooter 拍照")
  280. record_id = self.record_id
  281. goods_art_no = self.goods_art_no
  282. if record_id == -1:
  283. goods_art_no = ""
  284. print("smart shooter CameraShooter", record_id, goods_art_no)
  285. await self.smart_shooter.CameraShooter(
  286. msg_type="run_mcu",
  287. goods_art_no=goods_art_no,
  288. id=record_id,
  289. is_af=is_af,
  290. )
  291. print("smart shooter CameraShooter end")
  292. else:
  293. # 指定自动对焦
  294. self.capture_one.photograph(is_af=is_af)
  295. self.last_photograph_time = time.time() # 记录最近一次拍照时间
  296. # print("{} 拍照时间:{}".format(self.action_name, time.time() - start_time))
  297. print("{}-{}已完成".format(self.mode_type, self.action_name))
  298. if self.after_delay_time:
  299. print("拍照后延时:{}".format(self.after_delay_time))
  300. await asyncio.sleep(self.after_delay_time)
  301. return True
  302. async def RunSmartShooter(self, goods_art_no,record_id):
  303. await asyncio.gather(
  304. self.smart_shooter.CameraShooter(
  305. msg_type="run_mcu",
  306. goods_art_no=goods_art_no,
  307. id=record_id,
  308. ),
  309. )
  310. def digicam_take_picture(self):
  311. self.mcu.to_deal_device(device_name="buzzer", times=1)
  312. # 用于临时拍照计数
  313. is_af = True
  314. self.capture_one.photograph(is_af=is_af)
  315. self.last_photograph_time = time.time() # 记录最近一次拍照时间
  316. print("仅拍照执行完成")
  317. def rephotograph_one_pic(self, *args):
  318. """
  319. 1、获取最近一张照片
  320. 2、判断拍照时间距离当前节点的最近拍照时间是否小于3秒
  321. 3、删除该照片
  322. 4、重新触发进行拍照,并更新最近拍照时间
  323. """
  324. if (
  325. settings.getSysConfigs("other_configs", "running_mode", "普通模式")
  326. == "普通模式"
  327. ):
  328. return
  329. print("-----100-2--self", self)
  330. # 需要删除最近一张照片
  331. # if self.last_photograph_time is not None:
  332. # record = self.windows.image_process_data.photo_todo_list
  333. # if record:
  334. # last_record = record[-1]
  335. # if self.goods_art_no == last_record["goods_art_no"]:
  336. # if last_record["is_photo"]:
  337. # photo_create_time_formate = last_record[
  338. # "photo_create_time_formate"
  339. # ]
  340. # image_path = last_record["image_path"]
  341. # print(
  342. # "photo_create_time_formate,self.last_photograph_time",
  343. # photo_create_time_formate,
  344. # self.last_photograph_time,
  345. # )
  346. # # 如果照片的时间大于触发时间,且小于3秒,则认定是当前触发
  347. # if (
  348. # self.last_photograph_time
  349. # < photo_create_time_formate
  350. # < self.last_photograph_time
  351. # + settings.PHOTO_TRANSFER_TIME_INTERVAL
  352. # ):
  353. # if os.path.exists(image_path):
  354. # os.remove(image_path)
  355. # self.windows.add_goods_images_count(
  356. # self.goods_art_no, flag=False
  357. # )
  358. # if (
  359. # self.windows.image_process_data.goods_art_no_times_record
  360. # ):
  361. # goods_art_no = self.windows.image_process_data.goods_art_no_times_record[
  362. # 0
  363. # ][
  364. # "goods_art_no"
  365. # ]
  366. # if goods_art_no == self.goods_art_no:
  367. # self.windows.image_process_data.goods_art_no_times_record[
  368. # 0
  369. # ][
  370. # "number"
  371. # ] -= 1
  372. # self.program_refresh_photo_list_sign.emit()
  373. # if self.is_photograph:
  374. # # 重新拍照
  375. # self.last_photograph_time = time.time() # 记录最近一次拍照时间
  376. # print("last_photograph_time:", self.last_photograph_time)
  377. # print("photograph==================")
  378. # self.mcu.to_deal_device(device_name="buzzer", times=1)
  379. # # 用于临时拍照计数
  380. # self.windows.add_goods_images_count(self.goods_art_no)
  381. # if self.af_times > 0:
  382. # self.windows.capture_one.photograph(is_af=True)
  383. # else:
  384. # self.windows.capture_one.photograph(is_af=False)
  385. def do_retry(self, *args, **kwargs):
  386. """
  387. 重试操作,注意事项:
  388. 1、需要根据当前系统的角度位置,重新进行返回并拍摄
  389. :param args:
  390. :param kwargs:
  391. :return:
  392. """
  393. # self.program_sign.emit({})
  394. # print("do_retry")
  395. self.reset()
  396. self.mcu.action_state = 1
  397. self.run()
  398. self.mcu.action_state = 2
  399. def get_photo_node_name_and_sound(self, photo_take_time):
  400. data = {
  401. "action_name": self.action_name,
  402. "flag": False,
  403. "is_sound_play": False,
  404. }
  405. if self.state == 0:
  406. return data
  407. if self.last_photograph_time is None:
  408. return data
  409. # if (
  410. # self.last_photograph_time
  411. # < photo_take_time
  412. # < self.last_photograph_time + settings.PHOTO_TRANSFER_TIME_INTERVAL
  413. # ):
  414. # # 认定为当节点拍摄
  415. # # 如果为待用户确认则播放声音
  416. # data["flag"] = True
  417. # if settings.RUNNING_MODE == "待用户确认模式":
  418. # _f = False
  419. # if settings.RUNNING_MODE_DETAIL == "所有节点待确认":
  420. # _f = True
  421. # else:
  422. # print("self.is_need_confirm", self.is_need_confirm)
  423. # if self.is_need_confirm:
  424. # _f = True
  425. # else:
  426. # _f = False
  427. # if _f:
  428. # data["is_sound_play"] = True
  429. # print("========is_sound_play===========")
  430. # self.windows.playsound.tips_type = "photo_confirm"
  431. # self.windows.playsound.start()
  432. # return data
  433. return data