MessageListenerC001.py 6.1 KB

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