MessageListenerC001.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_c001(client, userdata, message):
  16. """消息处理方法"""
  17. """
  18. {
  19. 'basControl': {
  20. 'directSwitch': 1,
  21. 'eStop': 0,
  22. 'gearControl': 0,
  23. 'hazardLight': 0,
  24. 'keyStatus': 0,
  25. 'parkControl': 0,
  26. 'silencedAlarm': 0,
  27. 'travelLight': 2,
  28. 'travelMode': 0,
  29. 'vehicleHorn': 0
  30. },
  31. 'driverControl': {
  32. 'accPedal': '0 AND 0 || 0 AND -1',
  33. 'brakePedal': '2 AND 2',
  34. 'steeringWheel': '1 AND 0',
  35. 'turnMode': 0,
  36. 'turnSignal': 0
  37. },
  38. 'errCode': {
  39. 'errAccPedal': 0,
  40. 'errBasOperation': '',
  41. 'errBrakePedal': 0,
  42. 'errEndTool': 0,
  43. 'errHandle': 0,
  44. 'errOther': '',
  45. 'errSteeringWheel': 0
  46. },
  47. 'taskControl': {
  48. 'baseLegControl': '103 AND 24',
  49. 'baseLegSwitch': 0,
  50. 'bypassSwitch': 0,
  51. 'cabLift': 0,
  52. 'coopSignal': 3,
  53. 'enableHydraulic': 1,
  54. 'endJoint': '1 AND 0',
  55. 'esCabLift': 0,
  56. 'suckerSelect': 0,
  57. 'taskJoint_1': '5 AND 0',
  58. 'taskJoint_2': '0 AND 0',
  59. 'taskJoint_3': '0 AND 0',
  60. 'toolControl': '0 AND 0',
  61. 'workLight': 0
  62. },
  63. 'timeStamp': 576301601,
  64. 'userID': 'Ego',
  65. 'cockpitID': 'Cop001',
  66. 'vehicleID': '0538'
  67. }
  68. """
  69. # --- print ---
  70. # methods.debug_log(f"method_c001|75", f"#message.payload: {message.payload}")
  71. try:
  72. log_dict = json.loads(message.payload)
  73. except Exception as exception:
  74. methods.debug_log("method_c001|79", f"#message.payload: {message.payload}")
  75. methods.debug_log("method_c001|79", f"#exception: {exception.__class__.__name__}")
  76. methods.debug_log("method_c001|79", f"#traceback: {traceback.format_exc()}")
  77. return
  78. # --- check ---
  79. # log_dict = json.loads(message.payload)
  80. log_time = int(log_dict.get('timeStamp')) / 1000
  81. user_uuid = log_dict.get('userID')
  82. vehicle_id = log_dict.get('vehicleID')
  83. cockpit_id = log_dict.get('cockpitID')
  84. log_uuid = UserWorkRecord.get_log_uuid(user_uuid, log_time, vehicle_id, cockpit_id)
  85. methods.debug_log(f"method_c001|92", f"user_uuid: {user_uuid}, log_time: {log_time}")
  86. if not log_uuid:
  87. return
  88. # --- fill log_list ---
  89. log_list = [
  90. f"timeStamp: {methods.ts_to_string(log_time)}", # 时间
  91. f"topic: Cockpit/CanBus/CanId001/Cop001", # 话题
  92. # f"userID: {log_dict.get('userID')}", # 用户id
  93. # f"cockpitID: {log_dict.get('cockpitID')}", # 舱id
  94. # f"vehicleID: {log_dict.get('vehicleID')}", # 车id
  95. f"directSwitch: {log_dict.get('basControl').get('directSwitch')}",
  96. f"eStop: {log_dict.get('basControl').get('eStop')}",
  97. f"gearControl: {log_dict.get('basControl').get('gearControl')}",
  98. f"hazardLight: {log_dict.get('basControl').get('hazardLight')}",
  99. f"keyStatus: {log_dict.get('basControl').get('keyStatus')}",
  100. f"parkControl: {log_dict.get('basControl').get('parkControl')}",
  101. f"silencedAlarm: {log_dict.get('basControl').get('silencedAlarm')}",
  102. f"travelLight: {log_dict.get('basControl').get('travelLight')}",
  103. f"travelMode: {log_dict.get('basControl').get('travelMode')}",
  104. f"vehicleHorn: {log_dict.get('basControl').get('vehicleHorn')}",
  105. f"accPedal: {log_dict.get('driverControl').get('accPedal').replace('||', 'OR')}",
  106. f"brakePedal: {log_dict.get('driverControl').get('brakePedal')}",
  107. f"steeringWheel: {log_dict.get('driverControl').get('steeringWheel')}",
  108. f"turnMode: {log_dict.get('driverControl').get('turnMode')}",
  109. f"turnSignal: {log_dict.get('driverControl').get('turnSignal')}",
  110. f"errAccPedal: {log_dict.get('errCode').get('errAccPedal')}",
  111. f"errBasOperation: {log_dict.get('errCode').get('errBasOperation')}",
  112. f"errBrakePedal: {log_dict.get('errCode').get('errBrakePedal')}",
  113. f"errEndTool: {log_dict.get('errCode').get('errEndTool')}",
  114. f"errHandle: {log_dict.get('errCode').get('errHandle')}",
  115. f"errOther: {log_dict.get('errCode').get('errOther')}",
  116. f"errSteeringWheel: {log_dict.get('errCode').get('errSteeringWheel')}",
  117. f"baseLegControl: {log_dict.get('taskControl').get('baseLegControl')}",
  118. f"baseLegSwitch: {log_dict.get('taskControl').get('baseLegSwitch')}",
  119. f"bypassSwitch: {log_dict.get('taskControl').get('bypassSwitch')}",
  120. f"cabLift: {log_dict.get('taskControl').get('cabLift')}",
  121. f"coopSignal: {log_dict.get('taskControl').get('coopSignal')}",
  122. f"enableHydraulic: {log_dict.get('taskControl').get('enableHydraulic')}",
  123. f"endJoint: {log_dict.get('taskControl').get('endJoint')}",
  124. f"esCabLift: {log_dict.get('taskControl').get('esCabLift')}",
  125. f"suckerSelect: {log_dict.get('taskControl').get('suckerSelect')}",
  126. f"taskJoint_1: {log_dict.get('taskControl').get('taskJoint_1')}",
  127. f"taskJoint_2: {log_dict.get('taskControl').get('taskJoint_2')}",
  128. f"taskJoint_3: {log_dict.get('taskControl').get('taskJoint_3')}",
  129. f"toolControl: {log_dict.get('taskControl').get('toolControl')}",
  130. f"workLight: {log_dict.get('taskControl').get('workLight')}",
  131. ]
  132. # --- check ---
  133. log_key = '-'.join(log_list[1:])
  134. if LocalClass.last_log_key and LocalClass.last_log_key == log_key:
  135. # methods.debug_log(f"MessageListener24", f"#86: {LocalClass.last_log_key == log_key}")
  136. return
  137. # --- update --
  138. LocalClass.last_log_key = log_key
  139. # --- save ---
  140. log_file_path = os.path.join(save_dir, f'{log_uuid}.log')
  141. methods.debug_log(f"MessageListener|149", f"#log_list: {log_list}")
  142. methods.write_text(log_file_path, ' | '.join(log_list) + '\n', 'a')
  143. def subscribe_c001():
  144. """"""
  145. Global.emqx.start_subscribe_loop(
  146. decorate_method=method_c001,
  147. subscribe_topic='Cockpit/CanBus/CanId001/Cop001'
  148. )