ProgramItem.py 21 KB

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