VehicleStateMessageListener.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from hub import methods, Global
  2. import threading
  3. import time
  4. import json
  5. class VehicleStateMessageListener(object):
  6. @staticmethod
  7. def decorate_method(client, userdata, message):
  8. """消息处理方法"""
  9. try:
  10. # --- check ---
  11. message = json.loads(message.payload)
  12. if 'direction' not in message:
  13. methods.debug_log(f"VehicleStateMessageListener.18", f"#message: {message}")
  14. return
  15. # --- define ---
  16. check_vehicle_direction = False
  17. current_vehicle_direction = 0
  18. direction = message.get('direction')
  19. offset = 15 # 允许偏移角度(单位:度)
  20. if 0 - offset < direction < 0 + offset:
  21. check_vehicle_direction = True
  22. current_vehicle_direction = 3
  23. elif 90 - offset < direction < 90 + offset:
  24. check_vehicle_direction = True
  25. current_vehicle_direction = 12
  26. elif 180 - offset < direction < 180 + offset:
  27. check_vehicle_direction = True
  28. current_vehicle_direction = 9
  29. elif 270 - offset < direction < 270 + offset:
  30. check_vehicle_direction = True
  31. current_vehicle_direction = 6
  32. # --- update ---
  33. """
  34. VehicleInfo: 渣包车信息表
  35. VehicleInfo.uuid: 标识
  36. VehicleInfo.name: 车辆名称
  37. VehicleInfo.address: 车辆ip
  38. VehicleInfo.state: 车辆状态 1 离线(默认值) 2 在线空闲 3 人工驾驶中 4 远程驾驶中 5 自动驾驶中
  39. VehicleInfo.check_vehicle_direction: 当前车头方向是否符合自动驾驶条件 True 符合 False 不符合(默认值)
  40. VehicleInfo.current_vehicle_direction: 当前车头方向类型 3 围栏方向 9 渣场方向 12 维修间方向 6 维修间正对方向 0 未知(默认值)
  41. VehicleInfo.check_vehicle_coordinate: 当前车辆坐标是否符合自动驾驶条件 True 符合 False 不符合(默认值)
  42. VehicleInfo.current_vehicle_coordinate_x: 当前所在位置坐标
  43. VehicleInfo.current_vehicle_coordinate_y: 当前所在位置坐标
  44. VehicleInfo.current_vehicle_weight: 当前车辆负载重量
  45. """
  46. unique_dict = {'address': message.get('address')}
  47. update_dict = {
  48. 'state': message.get('state'),
  49. 'check_vehicle_direction': check_vehicle_direction,
  50. 'current_vehicle_direction': current_vehicle_direction,
  51. 'coordinate_x': message.get('coordinate_x'),
  52. 'coordinate_y': message.get('coordinate_y'),
  53. 'current_vehicle_weight': message.get('weight'),
  54. }
  55. Global.mdb.update_one('VehicleInfo', unique_dict, update_dict)
  56. methods.debug_log(f"VehicleStateMessageListener.38", f"#message: {message}")
  57. except Exception as exception:
  58. methods.debug_log('VehicleStateMessageListener.40', f"#exception: {exception}")
  59. methods.debug_log('VehicleStateMessageListener.40', f"#traceback: {methods.trace_log()}")
  60. @classmethod
  61. def start_check_loop(cls):
  62. """启动循环"""
  63. """
  64. topic: hs/vehicle/state
  65. message.address: 车辆ip
  66. message.state: 车辆状态 1 离线 2 在线空闲 3 人工驾驶中 4 远程驾驶中 5 自动驾驶中
  67. message.direction: 车头方向(场地坐标偏转角度)
  68. message.coordinate_x: 当前车辆坐标
  69. message.coordinate_y: 当前车辆坐标
  70. message.weight: 负载重量
  71. """
  72. Global.mq01.start_subscribe_loop(
  73. decorate_method=VehicleStateMessageListener.decorate_method,
  74. subscribe_topic='hs/vehicle/state'
  75. )
  76. @classmethod
  77. def run_background(cls, background_is=True):
  78. """启动现成"""
  79. p1 = threading.Thread(target=cls.start_check_loop)
  80. p1.start()
  81. if __name__ == '__main__':
  82. # --- test ---
  83. MessageListener.run_background()