check_command_GyBhhy.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import sys
  2. sys.path.append("..")
  3. import threading
  4. import time
  5. import async_communication as CM
  6. import uuid
  7. import message_pb2 as message
  8. import google.protobuf.text_format as tf
  9. MeasureStatu={"ok":0,"无数据":1,"噪声":2,"超界":3}
  10. ArrowType={0:"正确图片",0x01:"向后调整",0x02:"向前调整",0x04:"向右调整",0x08:"向左调整"
  11. ,0x10:"左前调整",0x06:"右前调整",0x09:"右后调整",0x05:"左后调整"}
  12. class CommandChecker(threading.Thread):
  13. def __init__(self,db):
  14. threading.Thread.__init__(self)
  15. self._db = db
  16. self._dispatch_statu = CM.TimeStatu(timeout=0.1)
  17. self._measure_statu = CM.TimeStatu(timeout=0.1)
  18. self._plate_color = {'蓝色':'1','黑色':'2','黄色':'3','白色':'4','绿色':'5','其他':'6'}
  19. self.is_close = False
  20. def receive_dispatch_statu(self, statu, ex, key):
  21. self._dispatch_statu = statu
  22. def receive_measureInfo(self,statu, ex, key):
  23. self._measure_statu = statu
  24. def stop(self):
  25. self.is_close = True
  26. def exit_isOK(self, pick_body):
  27. pick_table = message.pick_table()
  28. try:
  29. tf.Parse(pick_body, pick_table)
  30. except Exception as e:
  31. pick_table.statu.execute_statu = message.eError
  32. pick_table.statu.statu_description = "设备故障,请联系管理员!" + "(取车表单错误)" + str(e.args)
  33. return pick_table
  34. if pick_table.primary_key is None or pick_table.primary_key == '':
  35. pick_table.statu.execute_statu = message.eWarning
  36. pick_table.statu.statu_description = " 唯一码不能为空!"
  37. else:
  38. # 查询车位表和指令表是否有该车
  39. space_res = self._db.query_space_in_primary_key(pick_table.primary_key)
  40. command_res = self._db.query_command_in_primary_key(pick_table.primary_key)
  41. if len(command_res) > 0:
  42. pick_table.statu.execute_statu = message.eWarning
  43. pick_table.statu.statu_description = "指令正在执行中!请稍等!"
  44. elif len(space_res) <= 0:
  45. pick_table.statu.execute_statu = message.eWarning
  46. pick_table.statu.statu_description = "车辆信息不存在!"
  47. return pick_table
  48. def entrance_isOK(self, park_body):
  49. # 停车表单
  50. park_table = message.park_table()
  51. try:
  52. tf.Parse(park_body, park_table)
  53. except Exception as e:
  54. park_table.statu.execute_statu = message.eError
  55. park_table.statu.statu_description = "设备故障,请联系管理员!" + "(停车表单错误)" + str(e.args)
  56. return park_table
  57. # 检查调度状态
  58. key = "dispatch_%d_statu_port" % park_table.unit_id
  59. if (key in self._dispatch_statu) is False or self._dispatch_statu[key].timeout():
  60. park_table.statu.execute_statu = message.eError
  61. park_table.statu.statu_description = "设备故障,请联系管理员!" + "(调度)"
  62. return park_table
  63. key = "measure_%d_statu_port" % park_table.unit_id
  64. if (key in self._measure_statu) is False or self._measure_statu[key].timeout():
  65. park_table.statu.execute_statu = message.eError
  66. park_table.statu.statu_description = "设备故障,请联系管理员!" + "(雷达)"
  67. return park_table
  68. # 获取车高信息
  69. dispatch_node_statu = message.dispatch_node_statu()
  70. try:
  71. tf.Parse(self._dispatch_statu[key].statu, dispatch_node_statu)
  72. except Exception:
  73. park_table.statu.execute_statu = message.eError
  74. park_table.statu.statu_description = "设备故障,请联系管理员!" + "(调度状态消息错误)"
  75. return park_table
  76. if dispatch_node_statu.plc_carrier_status == 0:
  77. park_table.statu.execute_statu = message.eError
  78. park_table.statu.statu_description = "设备故障,请联系管理员!" + "(搬运器)"
  79. return park_table
  80. elif dispatch_node_statu.plc_carrier_status == 4:
  81. park_table.statu.execute_statu = message.eWarning
  82. park_table.statu.statu_description = "该单元设备维护中!请前往其他入口!"
  83. return park_table
  84. car_height = dispatch_node_statu.dispatch_plc_passway_status_vector[park_table.import_id - 1].car_height
  85. if car_height == 0:
  86. park_table.statu.execute_statu = message.eWarning
  87. park_table.statu.statu_description = "未检测到车辆!"
  88. return park_table
  89. elif car_height == 4:
  90. park_table.statu.execute_statu = message.eWarning
  91. park_table.statu.statu_description = "车辆超高,请退出车库!"
  92. return park_table
  93. # 填写车高信息
  94. measure_info = message.measure_info()
  95. measure_info.height = car_height
  96. park_table.entrance_measure_info.CopyFrom(measure_info)
  97. # 赋值唯一码
  98. uid = str(uuid.uuid1())
  99. park_table.primary_key = uid
  100. # 查询车位表和指令表是否有该车
  101. space_res = self._db.query_space_in_car_number(park_table.car_number)
  102. command_res = self._db.query_command_in_car_number(park_table.car_number)
  103. if len(command_res) > 0:
  104. park_table.statu.execute_statu = message.eWarning
  105. park_table.statu.statu_description = "指令正在执行中!请稍等!"
  106. elif len(space_res) > 0:
  107. park_table.statu.execute_statu = message.eWarning
  108. park_table.statu.statu_description = "车辆 :%s 已在库内!" % park_table.car_number
  109. else:
  110. # 车辆不在库内 查询是否有对应大小空车位
  111. space_res = self._db.query_space_in_height_unit_and_empty(measure_info.height, park_table.unit_id)
  112. command_res = self._db.query_command_in_height_unit_and_statu(measure_info.height, park_table.unit_id)
  113. if len(space_res) - len(command_res) <= 0:
  114. park_table.statu.execute_statu = message.eWarning
  115. park_table.statu.statu_description = "没有空余车位,车牌号:%s 高度:%.3fm!" % (park_table.car_number, measure_info.height)
  116. return park_table
  117. def run(self):
  118. while self.is_close is False:
  119. # print(self.error_str)
  120. time.sleep(0.05)
  121. # 先判断连接状态
  122. if self._dispatch_statu.timeout():
  123. self.error_str = "服务器超时,请联系管理员!"
  124. continue
  125. if self.measure_statu.timeout():
  126. self.error_str = '服务器超时,请联系管理员!'
  127. continue
  128. # 有车才显示
  129. icpu_statu = message.in_mcpu_statu()
  130. try:
  131. tf.Parse(self.icpu_statu.statu, icpu_statu)
  132. except:
  133. print(self.icpu_statu.statu)
  134. continue
  135. if icpu_statu.heighth == 1:
  136. self.error_str = '未知车高!'
  137. continue
  138. if icpu_statu.heighth == 5:
  139. self.error_str = '车辆超高!'
  140. continue
  141. # 以下是有车的处理
  142. # 1,显示测量数据(实时和静态)
  143. measure_info = message.measure_info()
  144. tf.Parse(self.measure_statu.statu, measure_info)
  145. # 2,获取车辆运动状态(静止or动态)
  146. border_statu = measure_info.border_statu
  147. is_moving = ((border_statu >> 11) & 0x01) == 1
  148. lidar_statu = measure_info.ground_status # 测量状态(正常,无数据、噪声、超界)
  149. if is_moving:
  150. self.last_moving_statu = None
  151. else:
  152. # 当前静止
  153. if lidar_statu == MeasureStatu["ok"]:
  154. self.last_moving_statu = measure_info
  155. elif lidar_statu == MeasureStatu["超界"]:
  156. if not self.last_moving_statu == None:
  157. # 上一刻静止且数据正确,当前静止,不可能出现超界,将超界清除
  158. new_border = border_statu
  159. new_border = (new_border & (~(0x01 << 0)))
  160. new_border = (new_border & (~(0x01 << 1)))
  161. new_border = (new_border & (~(0x01 << 2)))
  162. new_border = (new_border & (~(0x01 << 3)))
  163. new_border = (new_border & (~(0x01 << 6)))
  164. new_border = (new_border & (~(0x01 << 7)))
  165. new_border = (new_border & (~(0x01 << 8)))
  166. new_border = (new_border & (~(0x01 << 9)))
  167. border_statu = new_border
  168. elif lidar_statu == MeasureStatu["无数据"]:
  169. # 当前静止无车,清除上一时刻数据
  170. self.last_moving_statu = None
  171. elif lidar_statu == MeasureStatu["噪声"]:
  172. if not self.last_moving_statu == None:
  173. # 上一时刻静止且正确,当前噪声,不显示当前数据,显示上一正确数据
  174. measure_info.CopyFrom(self.last_moving_statu)
  175. # 先判断光电
  176. if is_moving:
  177. if icpu_statu.back_io == 1:
  178. self.error_str = '请按提示调整!'
  179. self.last_show = "调整"
  180. continue
  181. # 光电正常
  182. if lidar_statu == MeasureStatu["无数据"] and icpu_statu.is_occupy != 2:
  183. self.error_str = '无车!'
  184. self.last_show = "空闲"
  185. continue
  186. elif lidar_statu == MeasureStatu["噪声"]:
  187. if self.last_show == "超时":
  188. self.error_str = '雷达噪声'
  189. self.last_show = "空闲"
  190. continue
  191. elif lidar_statu == MeasureStatu["ok"]:
  192. self.error_str = 'OK'
  193. continue
  194. elif lidar_statu == MeasureStatu["超界"]:
  195. if (border_statu >> 7) & 0x01 == 1:
  196. self.error_str = '轴距不满足规格,请驶出!'
  197. self.last_show = "轴距超差"
  198. continue
  199. if (border_statu >> 9) & 0x01 == 1:
  200. self.error_str = '请按提示调整!'
  201. self.last_show = "请调整"
  202. continue
  203. if (border_statu >> 8) & 0x01 == 1:
  204. self.error_str = '请按提示调整!'
  205. self.last_show = "请调整"
  206. continue
  207. if (border_statu >> 10) & 0x01 == 1:
  208. self.error_str = '请按提示调整!'
  209. self.last_show = "请调整"
  210. continue
  211. if (border_statu >> 6) & 0x01 == 1:
  212. self.error_str = '车宽不满足规格,请驶出!'
  213. self.last_show = "超宽"
  214. continue
  215. border = border_statu & 0x0f
  216. if (border in ArrowType.keys()) is False:
  217. self.error_str = '服务器超时,请联系管理员!'
  218. self.last_show = "超时"
  219. else:
  220. if ArrowType[border] == "正确图片":
  221. self.error_str = "OK"
  222. self.last_show = "正确"
  223. else:
  224. self.error_str = "请按提示调整"
  225. self.last_show = "请调整"