123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- from hub import methods, Global
- import threading
- import time
- import json
- import os
- from lib.UserWorkRecord import UserWorkRecord
- # --- for linux
- # save_dir = f"/home/server/logs"
- # --- for windows
- # save_dir = r'C:\SRI-DINO.Server-py\logs' # sri内网测试环境
- save_dir = r'C:\logs' # sri内网测试环境
- class LocalClass(object):
- last_log_key = str()
- def method_c001(client, userdata, message):
- """消息处理方法"""
- """
- {
- 'basControl': {
- 'directSwitch': 1,
- 'eStop': 0,
- 'gearControl': 0,
- 'hazardLight': 0,
- 'keyStatus': 0,
- 'parkControl': 0,
- 'silencedAlarm': 0,
- 'travelLight': 2,
- 'travelMode': 0,
- 'vehicleHorn': 0
- },
- 'driverControl': {
- 'accPedal': '0 AND 0 || 0 AND -1',
- 'brakePedal': '2 AND 2',
- 'steeringWheel': '1 AND 0',
- 'turnMode': 0,
- 'turnSignal': 0
- },
- 'errCode': {
- 'errAccPedal': 0,
- 'errBasOperation': '',
- 'errBrakePedal': 0,
- 'errEndTool': 0,
- 'errHandle': 0,
- 'errOther': '',
- 'errSteeringWheel': 0
- },
- 'taskControl': {
- 'baseLegControl': '103 AND 24',
- 'baseLegSwitch': 0,
- 'bypassSwitch': 0,
- 'cabLift': 0,
- 'coopSignal': 3,
- 'enableHydraulic': 1,
- 'endJoint': '1 AND 0',
- 'esCabLift': 0,
- 'suckerSelect': 0,
- 'taskJoint_1': '5 AND 0',
- 'taskJoint_2': '0 AND 0',
- 'taskJoint_3': '0 AND 0',
- 'toolControl': '0 AND 0',
- 'workLight': 0
- },
- 'timeStamp': 576301601,
- 'userID': 'Ego',
- 'cockpitID': 'Cop001',
- 'vehicleID': '0538'
- }
- """
- # --- print ---
- # methods.debug_log(f"method_c001|75", f"#message.payload: {message.payload}")
- # log_dict = json.loads(message.payload)
- # try:
- # log_dict = json.loads(message.payload)
- # methods.debug_log(f"MessageListenerC001|19", f"#message.payload: {repr(log_dict)}")
- # except Exception as e:
- # pass
- # --- fill log_list ---
- log_dict = json.loads(message.payload)
- log_time = int(log_dict.get('timeStamp')) / 1000
- user_uuid = log_dict.get('userID')
- vehicle_id = log_dict.get('vehicleID')
- cockpit_id = log_dict.get('cockpitID')
- log_uuid = UserWorkRecord.get_log_uuid(user_uuid, log_time, vehicle_id, cockpit_id)
- log_list = [
- f"timeStamp: {methods.ts_to_string(log_time)}", # 时间
- f"topic: Cockpit/CanBus/CanId001/Cop001", # 话题
- # f"userID: {log_dict.get('userID')}", # 用户id
- # f"cockpitID: {log_dict.get('cockpitID')}", # 舱id
- # f"vehicleID: {log_dict.get('vehicleID')}", # 车id
- f"directSwitch: {log_dict.get('basControl').get('directSwitch')}",
- f"eStop: {log_dict.get('basControl').get('eStop')}",
- f"gearControl: {log_dict.get('basControl').get('gearControl')}",
- f"hazardLight: {log_dict.get('basControl').get('hazardLight')}",
- f"keyStatus: {log_dict.get('basControl').get('keyStatus')}",
- f"parkControl: {log_dict.get('basControl').get('parkControl')}",
- f"silencedAlarm: {log_dict.get('basControl').get('silencedAlarm')}",
- f"travelLight: {log_dict.get('basControl').get('travelLight')}",
- f"travelMode: {log_dict.get('basControl').get('travelMode')}",
- f"vehicleHorn: {log_dict.get('basControl').get('vehicleHorn')}",
- f"accPedal: {log_dict.get('driverControl').get('accPedal').replace('||', 'OR')}",
- f"brakePedal: {log_dict.get('driverControl').get('brakePedal')}",
- f"steeringWheel: {log_dict.get('driverControl').get('steeringWheel')}",
- f"turnMode: {log_dict.get('driverControl').get('turnMode')}",
- f"turnSignal: {log_dict.get('driverControl').get('turnSignal')}",
- f"errAccPedal: {log_dict.get('errCode').get('errAccPedal')}",
- f"errBasOperation: {log_dict.get('errCode').get('errBasOperation')}",
- f"errBrakePedal: {log_dict.get('errCode').get('errBrakePedal')}",
- f"errEndTool: {log_dict.get('errCode').get('errEndTool')}",
- f"errHandle: {log_dict.get('errCode').get('errHandle')}",
- f"errOther: {log_dict.get('errCode').get('errOther')}",
- f"errSteeringWheel: {log_dict.get('errCode').get('errSteeringWheel')}",
- f"baseLegControl: {log_dict.get('taskControl').get('baseLegControl')}",
- f"baseLegSwitch: {log_dict.get('taskControl').get('baseLegSwitch')}",
- f"bypassSwitch: {log_dict.get('taskControl').get('bypassSwitch')}",
- f"cabLift: {log_dict.get('taskControl').get('cabLift')}",
- f"coopSignal: {log_dict.get('taskControl').get('coopSignal')}",
- f"enableHydraulic: {log_dict.get('taskControl').get('enableHydraulic')}",
- f"endJoint: {log_dict.get('taskControl').get('endJoint')}",
- f"esCabLift: {log_dict.get('taskControl').get('esCabLift')}",
- f"suckerSelect: {log_dict.get('taskControl').get('suckerSelect')}",
- f"taskJoint_1: {log_dict.get('taskControl').get('taskJoint_1')}",
- f"taskJoint_2: {log_dict.get('taskControl').get('taskJoint_2')}",
- f"taskJoint_3: {log_dict.get('taskControl').get('taskJoint_3')}",
- f"toolControl: {log_dict.get('taskControl').get('toolControl')}",
- f"workLight: {log_dict.get('taskControl').get('workLight')}",
- ]
- # --- check ---
- log_key = '-'.join(log_list[1:])
- if LocalClass.last_log_key and LocalClass.last_log_key == log_key:
- # methods.debug_log(f"MessageListener24", f"#86: {LocalClass.last_log_key == log_key}")
- return
- # --- update --
- LocalClass.last_log_key = log_key
- # --- save ---
- log_file_path = os.path.join(save_dir, f'{log_uuid}.log')
- methods.debug_log(f"MessageListener|149", f"#log_list: {log_list}")
- methods.write_text(log_file_path, ' | '.join(log_list) + '\n', 'a')
- def subscribe_c001():
- """"""
- Global.emqx.start_subscribe_loop(
- decorate_method=method_c001,
- subscribe_topic='Cockpit/CanBus/CanId001/Cop001'
- )
|