MessageListenerV001.py 6.1 KB

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