ProgramItem.py 20 KB

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