check_command_ZKXY.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import datetime
  2. import hashlib
  3. import sys
  4. import time
  5. import requests
  6. sys.path.append("..")
  7. import uuid
  8. import message_pb2 as message
  9. import google.protobuf.text_format as tf
  10. import http_client
  11. import mytool.db_helper.db_operation as spmng
  12. MeasureStatu = {"ok": message.MeasureStatu.Value('Measure_OK'),
  13. "无数据": message.MeasureStatu.Value('Measure_Empty_Clouds'),
  14. "噪声": message.MeasureStatu.Value('Measure_Failture'),
  15. "PLC超界": message.MeasureStatu.Value('Measure_Border'),
  16. "终端超界": message.MeasureStatu.Value('Measure_Terminal_Border'),
  17. "失去连接": message.MeasureStatu.Value('Lidar_Disconnect')
  18. }
  19. Range_status = {"未超界": message.Range_status.Value('Range_correct'),
  20. "前超界": message.Range_status.Value('Range_front'),
  21. "后超界": message.Range_status.Value('Range_back'),
  22. "左超界": message.Range_status.Value('Range_left'),
  23. "右超界": message.Range_status.Value('Range_right'),
  24. "底盘超界": message.Range_status.Value('Range_bottom'),
  25. "车顶超界": message.Range_status.Value('Range_top'),
  26. "车宽超界": message.Range_status.Value('Range_car_width'),
  27. "轴距超界": message.Range_status.Value('Range_car_wheelbase'),
  28. "左旋转超界": message.Range_status.Value('Range_angle_anti_clock'),
  29. "右旋转超界": message.Range_status.Value('Range_angle_clock'),
  30. "方向盘未回正": message.Range_status.Value('Range_steering_wheel_nozero'),
  31. "车辆移动": message.Range_status.Value('Range_car_moving')
  32. }
  33. class CommandChecker:
  34. def __init__(self, db_config):
  35. self.db = spmng.DBOperation(db_config['db_ip'], db_config['db_port'], db_config['db_name'],
  36. db_config['db_user'], db_config['db_password'])
  37. self._dispatch_statu = {}
  38. self._measure_statu = {}
  39. def receive_dispatch_statu(self, statu, ex, key):
  40. self._dispatch_statu[key] = statu
  41. def receive_measure_info(self, statu, ex, key):
  42. self._measure_statu[key] = statu
  43. # 检查存车指令
  44. def entrance_isOK(self, park_body):
  45. # 停车表单
  46. park_table = message.park_table()
  47. try:
  48. tf.Parse(park_body, park_table)
  49. except Exception as e:
  50. park_table.statu.execute_statu = message.eError
  51. park_table.statu.statu_description = "设备故障,请联系管理员!" + "(停车表单错误)" + str(e.args)
  52. return park_table
  53. # 检查调度状态
  54. dispatch_key = "dispatch_%d_statu_port" % park_table.unit_id
  55. if (dispatch_key in self._dispatch_statu) is False or self._dispatch_statu[dispatch_key].timeout():
  56. park_table.statu.execute_statu = message.eError
  57. park_table.statu.statu_description = "设备故障,请联系管理员!" + "(调度)"
  58. return park_table
  59. # 获取车高信息
  60. dispatch_node_statu = message.dispatch_node_statu()
  61. try:
  62. tf.Parse(self._dispatch_statu[dispatch_key].statu, dispatch_node_statu)
  63. except Exception:
  64. park_table.statu.execute_statu = message.eError
  65. park_table.statu.statu_description = "设备故障,请联系管理员!" + "(调度状态消息错误)"
  66. return park_table
  67. # 判断搬运器状态
  68. if dispatch_node_statu.plc_carrier_status == 0:
  69. park_table.statu.execute_statu = message.eError
  70. park_table.statu.statu_description = "设备故障,请联系管理员!" + "(搬运器)"
  71. return park_table
  72. dispatch_plc_passway_status = dispatch_node_statu.dispatch_plc_passway_status_vector[park_table.import_id - 1]
  73. # 检查入口状态
  74. if dispatch_node_statu.plc_carrier_status == 4 or dispatch_plc_passway_status.plc_passway_enable == 2:
  75. park_table.statu.execute_statu = message.eWarning
  76. park_table.statu.statu_description = "该单元设备维护中!请前往其他入口!"
  77. return park_table
  78. if dispatch_plc_passway_status.car_height == 0:
  79. park_table.statu.execute_statu = message.eWarning
  80. park_table.statu.statu_description = "未检测到车辆!"
  81. return park_table
  82. elif dispatch_plc_passway_status.car_height == 4:
  83. park_table.statu.execute_statu = message.eWarning
  84. park_table.statu.statu_description = "车辆超高,请退出车库!"
  85. return park_table
  86. sensor_1 = dispatch_plc_passway_status.sensor_1
  87. # if (sensor_1 >> 2) & 0x01 != 1:
  88. # park_table.statu.execute_statu = message.eWarning
  89. # park_table.statu.statu_description = "车辆超限,请联系现场工作人员!"
  90. # return park_table
  91. if (sensor_1 >> 3) & 0x01 != 1:
  92. park_table.statu.execute_statu = message.eWarning
  93. park_table.statu.statu_description = "车辆后超界,请往前!"
  94. return park_table
  95. if (sensor_1 >> 4) & 0x01 != 1:
  96. park_table.statu.execute_statu = message.eWarning
  97. park_table.statu.statu_description = "车辆前超界,请往后!"
  98. return park_table
  99. #
  100. # if (sensor_1 >> 5) & 0x01 != 1:
  101. # park_table.statu.execute_statu = message.eWarning
  102. # park_table.statu.statu_description = "车辆左超界,请往右!"
  103. # return park_table
  104. #
  105. # if (sensor_1 >> 6) & 0x01 != 1:
  106. # park_table.statu.execute_statu = message.eWarning
  107. # park_table.statu.statu_description = "车辆右超界,请往左!"
  108. # return park_table
  109. measure_buffer = message.measure_buffer()
  110. measure_info_to_terminal = message.measure_info()
  111. measure_info_to_plc_reverse = message.measure_info()
  112. measure_info_to_plc_forward = message.measure_info()
  113. measure_key = "measure_%d_statu_port" % park_table.terminal_id
  114. if (measure_key in self._measure_statu) is False or self._measure_statu[measure_key].timeout():
  115. park_table.statu.execute_statu = message.eError
  116. park_table.statu.statu_description = "设备故障,请联系管理员!" + "(雷达)"
  117. return park_table
  118. # 获取车高信息
  119. try:
  120. tf.Parse(self._measure_statu[measure_key].statu, measure_buffer)
  121. measure_info_to_terminal.CopyFrom(measure_buffer.measure_info_to_terminal)
  122. measure_info_to_plc_reverse.CopyFrom(measure_buffer.measure_info_to_plc_reverse)
  123. measure_info_to_plc_forward.CopyFrom(measure_buffer.measure_info_to_plc_forward)
  124. except Exception:
  125. park_table.statu.execute_statu = message.eError
  126. park_table.statu.statu_description = "设备故障,请联系管理员!" + "(雷达状态消息错误)"
  127. return park_table
  128. # 获取雷达信息
  129. tm = time.time()
  130. while time.time() - tm < 2:
  131. park_table.entrance_measure_info.CopyFrom(measure_buffer)
  132. # 没有超界。车为静止状态。
  133. # if measure_info.border_statu == MeasureStatu["ok"] and measure_info.ground_status == 0 and measure_info.motion_statu == 1:
  134. if measure_info_to_terminal.border_statu == Range_status["未超界"] and \
  135. measure_info_to_terminal.ground_status == MeasureStatu["ok"] and \
  136. measure_info_to_plc_reverse.border_statu == Range_status["未超界"] and \
  137. measure_info_to_plc_reverse.ground_status == MeasureStatu["ok"] and \
  138. measure_info_to_plc_forward.border_statu == Range_status["未超界"] and \
  139. measure_info_to_plc_forward.ground_status == MeasureStatu["ok"]:
  140. park_table.statu.execute_statu = message.eNormal
  141. break
  142. else:
  143. park_table.statu.execute_statu = message.eError
  144. if self._measure_statu[measure_key].statu is not None and self._measure_statu[
  145. measure_key].timeout() is False:
  146. try:
  147. measure_buffer = message.measure_buffer()
  148. measure_info_to_terminal = message.measure_info()
  149. tf.Parse(self._measure_statu[measure_key].statu, measure_buffer)
  150. measure_info_to_terminal.CopyFrom(measure_buffer.measure_info_to_terminal)
  151. measure_info_to_plc_reverse.CopyFrom(measure_buffer.measure_info_to_plc_reverse)
  152. measure_info_to_plc_forward.CopyFrom(measure_buffer.measure_info_to_plc_forward)
  153. # print("check measure info again:", measure_info)
  154. except:
  155. print("parse exception:\n" + self._measure_statu[measure_key].statu)
  156. time.sleep(0.1)
  157. if park_table.statu.execute_statu == message.eError:
  158. if measure_info_to_terminal.ground_status == MeasureStatu["ok"] or \
  159. measure_info_to_plc_reverse.ground_status == MeasureStatu["ok"] or \
  160. measure_info_to_plc_forward.ground_status == MeasureStatu["ok"]:
  161. park_table.statu.statu_description = "雷达状态异常!"
  162. elif measure_info_to_terminal.ground_status == MeasureStatu["终端超界"] or \
  163. measure_info_to_plc_reverse.ground_status == MeasureStatu["PLC超界"] or \
  164. measure_info_to_plc_forward.ground_status == MeasureStatu["PLC超界"]:
  165. park_table.statu.statu_description = "请按提示调整!"
  166. elif measure_info_to_terminal.ground_status == MeasureStatu["失去连接"] or \
  167. measure_info_to_plc_reverse.ground_status == MeasureStatu["失去连接"] or \
  168. measure_info_to_plc_forward.ground_status == MeasureStatu["失去连接"]:
  169. park_table.statu.statu_description = "设备故障,请联系管理员! (雷达)"
  170. else:
  171. park_table.statu.statu_description = "请检查库内是否有异物!"
  172. print(" MeasureInfo:\n " + tf.MessageToString(measure_info_to_terminal, as_utf8=True))
  173. return park_table
  174. # 填写车高信息
  175. height = dispatch_plc_passway_status.car_height
  176. # 赋值唯一码
  177. uid = str(uuid.uuid1())
  178. park_table.primary_key = uid
  179. # 查询黑名单
  180. print("--------------查询黑名单---------------" + str(datetime.datetime.now()))
  181. blacklist_res = self.db.query_blacklist_in_car_number(park_table.car_number)
  182. blacklist_count = len(blacklist_res)
  183. if blacklist_count > 0:
  184. park_table.statu.execute_statu = message.eWarning
  185. park_table.statu.statu_description = "车辆 :%s 已在黑名单,不允许存车!" % park_table.car_number
  186. return park_table
  187. # 查询车位表和指令表是否有该车
  188. print("--------------查询车位表---------------" + str(datetime.datetime.now()))
  189. space_res = self.db.query_space_in_car_number(park_table.car_number)
  190. space_count = len(space_res)
  191. if space_count > 0:
  192. park_table.statu.execute_statu = message.eWarning
  193. park_table.statu.statu_description = "车辆 :%s 已在库内!" % park_table.car_number
  194. return park_table
  195. print("--------------查询指令表---------------" + str(datetime.datetime.now()))
  196. command_res = self.db.query_command_in_car_number(park_table.car_number)
  197. command_count = len(command_res)
  198. if command_count > 0:
  199. park_table.statu.execute_statu = message.eWarning
  200. park_table.statu.statu_description = "指令正在执行中!请稍等!"
  201. return park_table
  202. # 车辆不在库内 查询是否有对应大小空车位
  203. print("--------------查询空车位---------------" + str(datetime.datetime.now()))
  204. space_empty_res = self.db.query_space_in_height_level_wheel_base_unit_and_empty(height,
  205. measure_info_to_terminal.wheelbase,
  206. park_table.unit_id)
  207. space_empty_count = len(space_empty_res)
  208. if space_empty_count <= 0:
  209. park_table.statu.execute_statu = message.eWarning
  210. park_table.statu.statu_description = "没有空余的合适车位,车牌号:%s 高度等级:%d 轴距:%.3fm!" % (
  211. park_table.car_number, height, measure_info_to_terminal.wheelbase)
  212. return park_table
  213. # 检查取车指令
  214. def exit_isOK(self, pick_body):
  215. pick_table = message.pick_table()
  216. try:
  217. tf.Parse(pick_body, pick_table)
  218. except Exception as e:
  219. pick_table.statu.execute_statu = message.eError
  220. pick_table.statu.statu_description = "设备故障,请联系管理员!" + "(取车表单错误)" + str(e.args)
  221. return pick_table
  222. if pick_table.primary_key is None or pick_table.primary_key == '':
  223. pick_table.statu.execute_statu = message.eWarning
  224. pick_table.statu.statu_description = " 唯一码不能为空!"
  225. else:
  226. # 查询车位表和指令表是否有该车
  227. print("**************查询车位表**************" + str(datetime.datetime.now()))
  228. space_res = self.db.query_space_in_primary_key(pick_table.primary_key)
  229. space_count = len(space_res)
  230. print("**************查询停车指令表**************" + str(datetime.datetime.now()))
  231. command_res_park = self.db.query_park_command_in_primary_key(pick_table.primary_key)
  232. command_count_park = len(command_res_park)
  233. if space_count <= 0 and command_count_park <= 0:
  234. pick_table.statu.execute_statu = message.eWarning
  235. pick_table.statu.statu_description = "车辆信息不存在!"
  236. return pick_table
  237. print("**************查询取车指令表**************" + str(datetime.datetime.now()))
  238. command_res = self.db.query_pick_command_in_primary_key(pick_table.primary_key)
  239. command_count = len(command_res)
  240. if command_count > 0:
  241. pick_table.unit_id = space_res[0]['unit']
  242. t_unit_str = self.get_unit_str(pick_table.unit_id)
  243. # 重复取车,提示成功,流程失败!
  244. pick_table.statu.execute_statu = message.eWarning
  245. pick_table.statu.statu_description = "取车成功!请前往 << %s >> 单元出口等待车辆,观看大屏幕提示取车!" % t_unit_str
  246. return pick_table
  247. unit_str = self.get_unit_str(pick_table.unit_id)
  248. pick_table.statu.execute_statu = message.eNormal
  249. pick_table.statu.statu_description = "取车成功,请前往 << %s >> 单元出口等待车辆,观看大屏幕提示取车!" % unit_str
  250. # 完善表单信息
  251. try:
  252. try:
  253. pick_table.car_number = space_res[0]['car_number']
  254. pick_table.unit_id = space_res[0]['unit']
  255. except Exception as e:
  256. pick_table.car_number = command_res_park[0]['car_number']
  257. pick_table.unit_id = command_res_park[0]['unit']
  258. print("车位表未找到,使用指令表结果")
  259. pick_table.car_number_info.plate_color = space_res[0]['plate_color']
  260. pick_table.car_number_info.plate_type = space_res[0]['plate_type']
  261. pick_table.car_number_info.plate_confidence = space_res[0]['plate_confidence']
  262. pick_table.car_number_info.recognition_time = space_res[0]['recognition_time']
  263. pick_table.car_number_info.plate_full_image = ''
  264. pick_table.car_number_info.plate_clip_image = ''
  265. except Exception as e:
  266. print("数据库信息不全,缺失号牌颜色等信息!")
  267. return pick_table
  268. def get_unit_str(self, unit):
  269. if unit == 11:
  270. return 'A1'
  271. elif unit == 12:
  272. return 'A2'
  273. elif unit == 13:
  274. return 'A3'
  275. elif unit == 14:
  276. return 'A4'
  277. elif unit == 21:
  278. return 'B1'
  279. elif unit == 22:
  280. return 'B2'
  281. elif unit == 23:
  282. return 'B3'
  283. elif unit == 24:
  284. return 'B4'
  285. elif unit == 25:
  286. return 'B5'
  287. elif unit == 31:
  288. return 'C1'
  289. elif unit == 32:
  290. return 'C2'
  291. elif unit == 1:
  292. return 'A'
  293. elif unit == 2:
  294. return 'B'
  295. elif unit == 3:
  296. return 'C'
  297. return ''