ProgramItem.py 16 KB

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