TopicListener.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. class TopicListener:
  9. """
  10. """
  11. # last_log_key = str()
  12. def __init__(self, subscribe_topic, method_name):
  13. self.subscribe_topic = subscribe_topic
  14. self.method_name = method_name
  15. def subscribe(self):
  16. last_log_key = str()
  17. def method_c001(_client, _userdata, message):
  18. """消息处理方法"""
  19. """
  20. {
  21. 'basControl': {
  22. 'directSwitch': 1,
  23. 'eStop': 0,
  24. 'gearControl': 0,
  25. 'hazardLight': 0,
  26. 'keyStatus': 0,
  27. 'parkControl': 0,
  28. 'silencedAlarm': 0,
  29. 'travelLight': 2,
  30. 'travelMode': 0,
  31. 'vehicleHorn': 0
  32. },
  33. 'driverControl': {
  34. 'accPedal': '0 AND 0 || 0 AND -1',
  35. 'brakePedal': '2 AND 2',
  36. 'steeringWheel': '1 AND 0',
  37. 'turnMode': 0,
  38. 'turnSignal': 0
  39. },
  40. 'errCode': {
  41. 'errAccPedal': 0,
  42. 'errBasOperation': '',
  43. 'errBrakePedal': 0,
  44. 'errEndTool': 0,
  45. 'errHandle': 0,
  46. 'errOther': '',
  47. 'errSteeringWheel': 0
  48. },
  49. 'taskControl': {
  50. 'baseLegControl': '103 AND 24',
  51. 'baseLegSwitch': 0,
  52. 'bypassSwitch': 0,
  53. 'cabLift': 0,
  54. 'coopSignal': 3,
  55. 'enableHydraulic': 1,
  56. 'endJoint': '1 AND 0',
  57. 'esCabLift': 0,
  58. 'suckerSelect': 0,
  59. 'taskJoint_1': '5 AND 0',
  60. 'taskJoint_2': '0 AND 0',
  61. 'taskJoint_3': '0 AND 0',
  62. 'toolControl': '0 AND 0',
  63. 'workLight': 0
  64. },
  65. 'timeStamp': 576301601,
  66. 'userID': 'Ego',
  67. 'cockpitID': 'Cop001',
  68. 'vehicleID': '0538'
  69. }
  70. """
  71. # --- check ---
  72. # methods.debug_log(f"method_c001|75", f"#message.payload: {message.payload}")
  73. try:
  74. log_dict = json.loads(message.payload)
  75. except Exception as exception:
  76. methods.debug_log("method_c001|79", f"#message.payload: {message.payload}")
  77. methods.debug_log("method_c001|79", f"#exception: {exception.__class__.__name__}")
  78. methods.debug_log("method_c001|79", f"#traceback: {traceback.format_exc()}")
  79. return
  80. # --- check ---
  81. # log_dict = json.loads(message.payload)
  82. log_time = int(log_dict.get('timeStamp')) / 1000
  83. user_uuid = log_dict.get('userID')
  84. vehicle_id = log_dict.get('vehicleID')
  85. cockpit_id = log_dict.get('cockpitID')
  86. log_uuid = UserWorkRecord.get_log_uuid(user_uuid, log_time, vehicle_id, cockpit_id)
  87. if not log_uuid:
  88. methods.debug_log(f"MessageListenerC001|error92", f"user_uuid: {user_uuid}, log_time: {log_time}")
  89. return
  90. # --- fill log_list ---
  91. log_list = [
  92. f"timeStamp: {methods.ts_to_string(log_time)}", # 时间
  93. f"topic: Cockpit/CanBus/CanId001/Cop001", # 话题
  94. # f"userID: {log_dict.get('userID')}", # 用户id
  95. # f"cockpitID: {log_dict.get('cockpitID')}", # 舱id
  96. # f"vehicleID: {log_dict.get('vehicleID')}", # 车id
  97. f"directSwitch: {log_dict.get('basControl').get('directSwitch')}",
  98. f"eStop: {log_dict.get('basControl').get('eStop')}",
  99. f"gearControl: {log_dict.get('basControl').get('gearControl')}",
  100. f"hazardLight: {log_dict.get('basControl').get('hazardLight')}",
  101. f"keyStatus: {log_dict.get('basControl').get('keyStatus')}",
  102. f"parkControl: {log_dict.get('basControl').get('parkControl')}",
  103. f"silencedAlarm: {log_dict.get('basControl').get('silencedAlarm')}",
  104. f"travelLight: {log_dict.get('basControl').get('travelLight')}",
  105. f"travelMode: {log_dict.get('basControl').get('travelMode')}",
  106. f"vehicleHorn: {log_dict.get('basControl').get('vehicleHorn')}",
  107. f"accPedal: {log_dict.get('driverControl').get('accPedal').replace('||', 'OR')}",
  108. f"brakePedal: {log_dict.get('driverControl').get('brakePedal')}",
  109. f"steeringWheel: {log_dict.get('driverControl').get('steeringWheel')}",
  110. f"turnMode: {log_dict.get('driverControl').get('turnMode')}",
  111. f"turnSignal: {log_dict.get('driverControl').get('turnSignal')}",
  112. f"errAccPedal: {log_dict.get('errCode').get('errAccPedal')}",
  113. f"errBasOperation: {log_dict.get('errCode').get('errBasOperation')}",
  114. f"errBrakePedal: {log_dict.get('errCode').get('errBrakePedal')}",
  115. f"errEndTool: {log_dict.get('errCode').get('errEndTool')}",
  116. f"errHandle: {log_dict.get('errCode').get('errHandle')}",
  117. f"errOther: {log_dict.get('errCode').get('errOther')}",
  118. f"errSteeringWheel: {log_dict.get('errCode').get('errSteeringWheel')}",
  119. f"baseLegControl: {log_dict.get('taskControl').get('baseLegControl')}",
  120. f"baseLegSwitch: {log_dict.get('taskControl').get('baseLegSwitch')}",
  121. f"bypassSwitch: {log_dict.get('taskControl').get('bypassSwitch')}",
  122. f"cabLift: {log_dict.get('taskControl').get('cabLift')}",
  123. f"coopSignal: {log_dict.get('taskControl').get('coopSignal')}",
  124. f"enableHydraulic: {log_dict.get('taskControl').get('enableHydraulic')}",
  125. f"endJoint: {log_dict.get('taskControl').get('endJoint')}",
  126. f"esCabLift: {log_dict.get('taskControl').get('esCabLift')}",
  127. f"suckerSelect: {log_dict.get('taskControl').get('suckerSelect')}",
  128. f"taskJoint_1: {log_dict.get('taskControl').get('taskJoint_1')}",
  129. f"taskJoint_2: {log_dict.get('taskControl').get('taskJoint_2')}",
  130. f"taskJoint_3: {log_dict.get('taskControl').get('taskJoint_3')}",
  131. f"toolControl: {log_dict.get('taskControl').get('toolControl')}",
  132. f"workLight: {log_dict.get('taskControl').get('workLight')}",
  133. ]
  134. # --- check ---
  135. log_key = '-'.join(log_list[1:])
  136. if last_log_key and last_log_key == log_key:
  137. # methods.debug_log(f"MessageListener24", f"#86: {last_log_key == log_key}")
  138. return
  139. # --- update --
  140. last_log_key = log_key
  141. # --- save ---
  142. # methods.debug_log(f"MessageListenerC001|158", f"#log_list: {log_list}")
  143. log_file_path = os.path.join(Global.save_dir, f'{log_uuid}.log')
  144. methods.write_text(log_file_path, ' | '.join(log_list) + '\n', 'a')
  145. def method_v001(client, userdata, message):
  146. """消息处理方法"""
  147. # --- log ---
  148. # methods.debug_log(f"method_v001|26", f"#message.payload: {message.payload}")
  149. # return
  150. # --- check ---
  151. # if not methods.is_dir(Global.save_dir):
  152. # out = methods.run_command(f'mkdir -p {Global.save_dir}', callback=True)
  153. # methods.debug_log('MessageListener46', f"#out: {out}")
  154. # --- check ---
  155. try:
  156. log_dict = json.loads(message.payload)
  157. log_time = int(log_dict.get('timestamp')) / 1000
  158. user_uuid = log_dict.get('userID')
  159. vehicle_id = log_dict.get('VehicleID')
  160. cockpit_id = log_dict.get('cockpitID')
  161. except Exception as exception:
  162. methods.debug_log("method_v001|41", f"#message.payload: {message.payload}")
  163. methods.debug_log("method_v001|41", f"#exception: {exception.__class__.__name__}")
  164. methods.debug_log("method_v001|41", f"#traceback: {traceback.format_exc()}")
  165. return
  166. # --- check ---
  167. log_uuid = UserWorkRecord.get_log_uuid(user_uuid, log_time, vehicle_id, cockpit_id)
  168. if not log_uuid:
  169. methods.debug_log(f"MessageListenerV001|error50", f"user_uuid: {user_uuid}, log_time: {log_time}")
  170. return
  171. # --- fill log_list ---
  172. log_list = [
  173. f"timestamp: {methods.ts_to_string(log_time)}", # 时间
  174. f"topic: Vehicle/ControlVehicle/Veh001", # 话题
  175. # f"userID: {log_dict.get('userID')}", # 用户id
  176. # f"VehicleID: {log_dict.get('VehicleID')}", # 车id
  177. # f"cockpitID: {log_dict.get('cockpitID')}", # 舱id
  178. f"directSwitch: {log_dict.get('baseControl')[0].get('directSwitch')}", # 前后切换
  179. f"eStop: {log_dict.get('baseControl')[0].get('eStop')}", # 急停开关
  180. f"gearCaontrol: {log_dict.get('baseControl')[0].get('gearCaontrol')}", # 车辆档位控制
  181. f"hazardLight: {log_dict.get('baseControl')[0].get('hazardLight')}", # 双闪灯
  182. f"keyStatus: {log_dict.get('baseControl')[0].get('keyStatus')}", # 钥匙状态
  183. f"parkControl: {log_dict.get('baseControl')[0].get('parkControl')}", # 驻车控制
  184. f"silencedAlarm: {log_dict.get('baseControl')[0].get('silencedAlarm')}", # 消报警音按钮
  185. f"travelLight: {log_dict.get('baseControl')[0].get('travelLight')}", # 行驶灯光
  186. f"travelMode: {log_dict.get('baseControl')[0].get('travelMode')}", # 驾驶模式
  187. f"vehicleHorn: {log_dict.get('baseControl')[0].get('vehicleHorn')}", # 喇叭
  188. f"accPedalF: {log_dict.get('driveControl')[0].get('accPedal')[0].get('accPedalF')}", # 油门踏板.手油门
  189. f"accPedalH: {log_dict.get('driveControl')[0].get('accPedal')[0].get('accPedalH')}", # 油门踏板.脚油门
  190. f"accPedalH: {log_dict.get('driveControl')[0].get('brakePedal')}", # 刹车踏板
  191. f"steeringWheel: {log_dict.get('driveControl')[0].get('steeringWheel')}", # 方向盘转速
  192. f"turnMode: {log_dict.get('driveControl')[0].get('turnMode')}", # 转向模式
  193. f"turnSignal: {log_dict.get('driveControl')[0].get('turnSignal')}", # 转向灯
  194. f"errAccPedal: {log_dict.get('errCode')[0].get('errAccPedal')}", # 油门信号故障
  195. f"errBasOperation: {log_dict.get('errCode')[0].get('errBasOperation')}", # 基本操作故障
  196. f"errBrakePedal: {log_dict.get('errCode')[0].get('errBrakePedal')}", # 刹车信号故障
  197. f"errEndTool: {log_dict.get('errCode')[0].get('errEndTool')}", # 末端工具控制故障
  198. f"errHandle: {log_dict.get('errCode')[0].get('errHandle')}", # 手柄信号故障
  199. f"errOther: {log_dict.get('errCode')[0].get('errOther')}", # 其他故障
  200. f"errSteeringWheel: {log_dict.get('errCode')[0].get('errSteeringWheel')}", # 转向故障
  201. f"baseLegControl: {log_dict.get('taskControl')[0].get('baseLegControl')}", # 支腿动作控制
  202. f"baseLegSwitch: {log_dict.get('taskControl')[0].get('baseLegSwitch')}", # 支腿选择开关
  203. f"bypassSwitch: {log_dict.get('taskControl')[0].get('bypassSwitch')}", # 旁通开关
  204. f"cabLift: {log_dict.get('taskControl')[0].get('cabLift')}", # 驾驶室升降机构
  205. f"coopSignal: {log_dict.get('taskControl')[0].get('coopSignal')}", # 协同作业信号
  206. f"enableHydraulic: {log_dict.get('taskControl')[0].get('enableHydraulic')}", # 液压使能开关
  207. f"endJoint: {log_dict.get('taskControl')[0].get('endJoint')}", # 末端关节控制
  208. f"esCabLift: {log_dict.get('taskControl')[0].get('esCabLift')}", # 驾驶室应急下降开关
  209. f"suckerSelect: {log_dict.get('taskControl')[0].get('suckerSelect')}", # 吸盘选择开关
  210. f"taskJoint_1: {log_dict.get('taskControl')[0].get('taskJoint_1')}", # 第一个作业关节控制
  211. f"taskJoint_2: {log_dict.get('taskControl')[0].get('taskJoint_2')}", # 第二个作业关节控制
  212. f"taskJoint_3: {log_dict.get('taskControl')[0].get('taskJoint_3')}", # 第三个作业关节控制
  213. f"toolControl: {log_dict.get('taskControl')[0].get('toolControl')}", # 末端工具控制
  214. f"workLight: {log_dict.get('taskControl')[0].get('workLight')}", # 工作灯
  215. ]
  216. # --- check ---
  217. log_key = '-'.join(log_list[1:])
  218. if last_log_key and last_log_key == log_key:
  219. # methods.debug_log(f"MessageListener24", f"#86: {last_log_key == log_key}")
  220. return
  221. # --- update --
  222. last_log_key = log_key
  223. # --- save ---
  224. # methods.debug_log(f"MessageListenerV001|113", f"#log_list: {log_list}")
  225. log_file_path = os.path.join(Global.save_dir, f'{log_uuid}.log')
  226. methods.write_text(log_file_path, ' | '.join(log_list) + '\n', 'a')
  227. client = Global.emqx_factory.Client(host=Global.emqx_host, port=Global.emqx_port)
  228. client.start_subscribe_loop(
  229. decorate_method=getattr(locals(), self.method_name, None),
  230. subscribe_topic=self.subscribe_topic
  231. )