TopicListener.py 13 KB

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