TopicListener.py 13 KB

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