ProgramItem.py 19 KB

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