ProgramItem.py 17 KB

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