MessageListener.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from hub import methods, Global
  2. import threading
  3. import time
  4. import json
  5. import os
  6. # --- for linux
  7. # save_dir = f"/home/server/logs"
  8. # --- for windows
  9. # save_dir = r'C:\SRI-DINO.Server-py\logs' # sri内网测试环境
  10. save_dir = r'C:\logs' # sri内网测试环境
  11. class MessageListener(object):
  12. """
  13. """
  14. log_key = str()
  15. @staticmethod
  16. def v001(client, userdata, message):
  17. """消息处理方法"""
  18. # --- log ---
  19. # print(f'MessageListener24: #message.payload: {message.payload}', flush=True)
  20. # methods.debug_log(f"MessageListener24", f"#message.payload: {message.payload}")
  21. # --- save log ---
  22. file_name = methods.now_string('v001-%Y-%m-%d-%H.log')
  23. log_file_path = os.path.join(save_dir, file_name)
  24. log_dict = json.loads(message.payload)
  25. log_list = [
  26. f"timestamp: {methods.ts_to_string(int(log_dict.get('timestamp')) / 1000)}", # 时间
  27. f"userID: {log_dict.get('userID')}", # 用户id
  28. f"VehicleID: {log_dict.get('VehicleID')}", # 车id
  29. f"directSwitch: {log_dict.get('baseControl')[0].get('directSwitch')}", # 前后切换
  30. f"eStop: {log_dict.get('baseControl')[0].get('eStop')}", # 急停开关
  31. f"gearCaontrol: {log_dict.get('baseControl')[0].get('gearCaontrol')}", # 车辆档位控制
  32. f"hazardLight: {log_dict.get('baseControl')[0].get('hazardLight')}", # 双闪灯
  33. f"keyStatus: {log_dict.get('baseControl')[0].get('keyStatus')}", # 钥匙状态
  34. f"parkControl: {log_dict.get('baseControl')[0].get('parkControl')}", # 驻车控制
  35. f"silencedAlarm: {log_dict.get('baseControl')[0].get('silencedAlarm')}", # 消报警音按钮
  36. f"travelLight: {log_dict.get('baseControl')[0].get('travelLight')}", # 行驶灯光
  37. f"travelMode: {log_dict.get('baseControl')[0].get('travelMode')}", # 驾驶模式
  38. f"vehicleHorn: {log_dict.get('baseControl')[0].get('vehicleHorn')}", # 喇叭
  39. f"accPedalF: {log_dict.get('driveControl')[0].get('accPedal')[0].get('accPedalF')}", # 油门踏板.手油门
  40. f"accPedalH: {log_dict.get('driveControl')[0].get('accPedal')[0].get('accPedalH')}", # 油门踏板.脚油门
  41. f"accPedalH: {log_dict.get('driveControl')[0].get('brakePedal')}", # 刹车踏板
  42. f"steeringWheel: {log_dict.get('driveControl')[0].get('steeringWheel')}", # 方向盘转速
  43. f"turnMode: {log_dict.get('driveControl')[0].get('turnMode')}", # 转向模式
  44. f"turnSignal: {log_dict.get('driveControl')[0].get('turnSignal')}", # 转向灯
  45. f"errAccPedal: {log_dict.get('errCode')[0].get('errAccPedal')}", # 油门信号故障
  46. f"errBasOperation: {log_dict.get('errCode')[0].get('errBasOperation')}", # 基本操作故障
  47. f"errBrakePedal: {log_dict.get('errCode')[0].get('errBrakePedal')}", # 刹车信号故障
  48. f"errEndTool: {log_dict.get('errCode')[0].get('errEndTool')}", # 末端工具控制故障
  49. f"errHandle: {log_dict.get('errCode')[0].get('errHandle')}", # 手柄信号故障
  50. f"errOther: {log_dict.get('errCode')[0].get('errOther')}", # 其他故障
  51. f"errSteeringWheel: {log_dict.get('errCode')[0].get('errSteeringWheel')}", # 转向故障
  52. f"baseLegControl: {log_dict.get('taskControl')[0].get('baseLegControl')}", # 支腿动作控制
  53. f"baseLegSwitch: {log_dict.get('taskControl')[0].get('baseLegSwitch')}", # 支腿选择开关
  54. f"bypassSwitch: {log_dict.get('taskControl')[0].get('bypassSwitch')}", # 旁通开关
  55. f"cabLift: {log_dict.get('taskControl')[0].get('cabLift')}", # 驾驶室升降机构
  56. f"coopSignal: {log_dict.get('taskControl')[0].get('coopSignal')}", # 协同作业信号
  57. f"enableHydraulic: {log_dict.get('taskControl')[0].get('enableHydraulic')}", # 液压使能开关
  58. f"endJoint: {log_dict.get('taskControl')[0].get('endJoint')}", # 末端关节控制
  59. f"esCabLift: {log_dict.get('taskControl')[0].get('esCabLift')}", # 驾驶室应急下降开关
  60. f"suckerSelect: {log_dict.get('taskControl')[0].get('suckerSelect')}", # 吸盘选择开关
  61. f"taskJoint_1: {log_dict.get('taskControl')[0].get('taskJoint_1')}", # 第一个作业关节控制
  62. f"taskJoint_2: {log_dict.get('taskControl')[0].get('taskJoint_2')}", # 第二个作业关节控制
  63. f"taskJoint_3: {log_dict.get('taskControl')[0].get('taskJoint_3')}", # 第三个作业关节控制
  64. f"toolControl: {log_dict.get('taskControl')[0].get('toolControl')}", # 末端工具控制
  65. f"workLight: {log_dict.get('taskControl')[0].get('workLight')}", # 工作灯
  66. ]
  67. # --- check ---
  68. if MessageListener.log_key and MessageListener.log_key == '-'.join(log_list[1:]):
  69. return
  70. # --- check --
  71. if not MessageListener.log_key:
  72. MessageListener.log_key = '-'.join(log_list[1:])
  73. print(f'MessageListener24: #log_list: {log_list}', flush=True)
  74. methods.write_text(log_file_path, ' | '.join(log_list) + '\n', 'a')
  75. @classmethod
  76. def subscribe001(cls):
  77. """"""
  78. # --- check ---
  79. # if not methods.is_dir(save_dir):
  80. # out = methods.run_command(f'mkdir -p {save_dir}', callback=True)
  81. # methods.debug_log('MessageListener46', f"#out: {out}")
  82. Global.emqx.start_subscribe_loop(
  83. decorate_method=MessageListener.v001,
  84. # subscribe_topic='bg/log'
  85. subscribe_topic='Vehicle/ControlVehicle/Veh001'
  86. )
  87. # @classmethod
  88. # def run_background(cls, background_is=True):
  89. # """"""
  90. # p1 = threading.Thread(target=cls.subscribe001)
  91. # p1.start()
  92. @classmethod
  93. def run(cls, background_is=True):
  94. thread_list = [
  95. threading.Thread(target=cls.subscribe001),
  96. ]
  97. for thread in thread_list:
  98. thread.setDaemon(True)
  99. thread.start()
  100. if background_is:
  101. return
  102. for thread in thread_list:
  103. thread.join()
  104. if __name__ == '__main__':
  105. # --- test ---
  106. MessageListener.run()