ProgramItem.py 16 KB

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