def chassis_state_change_echo(container): result = Chassis.Exec(container).rostopic_echo( topicName='/chassis/state_change') state = re.search('.+(\d+)', str(result)).group(1) #state_str=re.search('data:.+(\d+)', str(result)).group()#add_by_xxq #state = re.search('.+(\d+)', str(state_str)).group(1)#add_by_xxq return int(state)
def chassis_alarm_echo(container): ''' 返回chassis_alarm的一次结果,类型为字典 header: seq: 23703 stamp: secs: 2066 nsecs: 514321000 frame_id: '' system_alarm: 32768 motor_alarm: 0 motor_driver_alarm: 0 external_sensor_alarm: 0 internal_sensor_alarm: 0 power_alarm: 0 ''' time.sleep(2) result = str(Chassis.Exec(container).rostopic_echo(topicName='/chassis_alarm')) output = {} List = result.split("\\n") for Line in List: line = Line.strip() if ": " in line: output[line.split(': ')[0]] = line.split(': ')[1] return output
def test_025(self): ip = '192.168.0.179' self.TX2 = TX2.TX2(ip) containers = self.TX2.Containers() self.container = self.TX2.Containers()['chassis_serial'] self.TX2.Images() if len(containers) != 13: raise Exception('docker未全部启动') if not Chassis.Exec(self.container).roscore_check(): raise Exception('roscore未自动启动') # data_value = "'set_env cpu_usage 25'" # data = '"data: %s"' % data_value # Chassis.Exec(self.container).rostopic_pub(topic='/chassis/cmd_req ', msg_type='std_msgs/String ', args=data) # # result = Chassis.Exec(self.container).cmd_res_vf(isSET=True) # if result != '25': # raise Exception('参数设置日志有误') # # result = Chassis.Exec(self.container).rostopic_echo(topicName='/chassis_state') # cpu_usage = re.search('cpu_usage:\s(\d+)', result).group(1) # if cpu_usage != '25': # raise Exception('预期值为25,实际值为' + cpu_usage) # # data_value = "'del_env cpu_usage'" # data = '"data: %s"' % data_value # Chassis.Exec(self.container).rostopic_pub(topic='/chassis/cmd_req ', msg_type='std_msgs/String ', args=data) # # if not Chassis.Exec(self.container).cmd_res_vf(isSET=False): # raise Exception('操作日志有误') print 'over'
def collect_log(container, logpath): data = r'''"data: '{\"message_id\":123441,\"from\":\"communication\",\"to\":\"control\",\"log_collect\":{ \"collect_target\": \"vehicle\",\"jwt\": \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE4NzYyODI5NzIsImp0aSI6ImJkNWNkMGI1MDhlYTA4ZWUiLCJpYXQiOjE1NjA5MjI5NzJ9.1HSPUz2fE58fxPRBaxx4AhPdmTgZWtAvMyd6SvLcQeY\", \"upload_status\": \"22\",\"access_key\": \"AKIAO2DNISXSC7YGDAQA--\",\"secret_key\": \"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\",\"token\": \"XXX\",\"reply_url\": \"https://dev-logcull.goldenridge.cn/log-collect/v1/tx2UploadReply--\", \"start_time\": \"2019-5-1\",\"end_time\": \"2019-8-1\",\"vehicle_log_type\": \"log\",\"software_attribute\": \"all\", \"collect_num\": \"2\",\"module\": [\"chassis\"]}}'"''' File = os.path.join('/opt/routineCar/log/chassis', time.strftime('%Y-%m-%d', time.localtime()) + '.txt') os.system('rm -rf %s' % File) stop_ftp_task(container) Chassis.Exec(container).rostopic_pub(topic='/communication/control', msg_type='std_msgs/String ', args=data) for i in range(10): if os.path.isfile(File): break else: time.sleep(3) if i == 9: raise Exception('采集日志失败') if not os.path.isfile(logpath): raise Exception('初始日志不存在') os.system('grep -xvFf %s %s | tee %s' % (logpath, File, logpath)) time.sleep(2) return logpath
def chassis_state_echo(container): ''' 返回chassis_state的一次结果,类型为字典 seq: 1251 secs: 10290 nsecs: 199577000 frame_id: '' stack_sensor: 137 stack_control: 137 stack_comm: 345 stack_rec: [165, 697, 0, 0, 0] cpu_usage: 25 wdg_state: 0 bms: 0 motor: 0 motor_driver: 0 joystick: 0 imu: 0 ultrasonic: 8152 weighing: 0 flash: 0 other_res: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ''' result = str( Chassis.Exec(container).rostopic_echo(topicName='/chassis_state')) output = {} List = result.split("\\n") for Line in List: line = Line.strip() if ": " in line: output[line.split(': ')[0]] = line.split(': ')[1] return output
def tp_main_get(container,eTP_num): ''' 通过/chassis/cmd_req查询TP点状态,例如: rostopic pub -1 /chassis/cmd_req std_msgs/String "data: 'tp_main get_tp 5'" ''' data_value = "tp_main get_tp %s" %eTP_num data = '"data: %s"' % data_value Chassis.Exec(container).rostopic_pub(topic='/chassis/cmd_req',msg_type='std_msgs/String ',args=data)
def tp_main_set(container,eTP_num,switch): ''' 通过/chassis/cmd_req设置TP点,例如: rostopic pub -1 /chassis/cmd_req std_msgs/String "data: 'tp_main set_tp 5 1'" ''' data_value = "tp_main set_tp %s %s" %(eTP_num,switch) data = '"data: %s"' % data_value Chassis.Exec(container).rostopic_pub(topic='/chassis/cmd_req',msg_type='std_msgs/String ',args=data)
def alarm_mask(container, id=0): ''' rostopic pub -1 /alarm_mask std_msgs/UInt32 "data: 0" 按ID屏蔽告警,默认0屏蔽所有告警 ''' Chassis.Exec(container).rostopic_pub(topic='alarm_mask', msg_type='std_msgs/UInt32', args='"data: %d"' % id)
def stop_ftp_task(container): data = r'''"data: '{\"message_id\":123441,\"from\":\"communication\",\"to\":\"control\",\"log_collect\":{ \"collect_target\": \"vehicle\",\"jwt\": \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE4NzYyODI5NzIsImp0aSI6ImJkNWNkMGI1MDhlYTA4ZWUiLCJpYXQiOjE1NjA5MjI5NzJ9.1HSPUz2fE58fxPRBaxx4AhPdmTgZWtAvMyd6SvLcQeY\", \"upload_status\": \"stop\",\"access_key\": \"AKIAO2DNISXSC7YGDAQA--\",\"secret_key\": \"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\",\"token\": \"XXX\",\"reply_url\": \"https://dev-logcull.goldenridge.cn/log-collect/v1/tx2UploadReply--\", \"start_time\": \"2019-5-1\",\"end_time\": \"2019-8-1\",\"vehicle_log_type\": \"log\",\"software_attribute\": \"all\", \"collect_num\": \"2\",\"module\": [\"chassis\"]}}'"''' Chassis.Exec(container).rostopic_pub(topic='/communication/control', msg_type='std_msgs/String ', args=data)
def self_check_service(container): ''' rosservice rosservice call /self_check "req: '{\"check_mode\"}'" 对影响状态机的自检结果解析,返回告警列表 ''' service = '/self_check' args = r'''"req: '{\"check_mode\":1}'"''' result = Chassis.Exec(container).rosservice_call(service, args).decode() idList = re.findall('\d{7,}', result) return idList
def log_sd_clean(container): ''' 通过/chassis/cmd_req对日志进行[<clean>]操作,例如: rostopic pub -1 /chassis/cmd_req std_msgs/String "data: 'log_sd clean'" ''' data_value = "log_sd clean" data = '"data: %s"' % data_value Chassis.Exec(container).rostopic_pub(topic='/chassis/cmd_req', msg_type='std_msgs/String ', args=data)
def recover_chassis_remap(container): ''' 恢复remap下位机传感器的/imu,/odom,/ultrasound,/chassis/control_info话题 ''' file = '/opt/routineCar/src/platform_vehicle/models/chassis_serial/launch/node.launch' content = '"<launch> \ <node pkg=\\"chassis_serial\\" type=\\"chassis_serial_node\\" name=\\"chassis_serial_node\\" respawn =\\"true\\"> \ <param name=\\"port\\" value=\\"/dev/tty_chassis\\"/> \ </node> \ </launch>"' Chassis.Exec(container).popen("bash -c 'echo %s > %s'" % (content, file))
def communication_alarm_echo(container): ''' 查看上报给pad的最新一次告警.返回字典,包含模块,告警id,type和告警内容信息 type为0表示注入,1表示恢复 ''' result = Chassis.Exec(container).rostopic_echo( topicName='/communication/alarm').decode().replace('\\', '') info = {} info['id'] = re.search('id\":(\d+)', result).group(1) info['type'] = re.search('type\":(\d+)', result).group(1) info['content'] = re.search('content\":\"(.+?)\"', result).group(1) info['module'] = re.search('module\":\"(\w+)', result).group(1) return info
def alarm_command(container, id=0, mask_state='true', check_mask='true'): ''' rosservice call /alarm_command "req: '{\"alarm_command\":{\"id\":33649089,\"mask_state\":true,\"check_mask\":true}}'" check_mask:为True时查询已屏蔽的告警列表。为False时才识别告警ID和屏蔽恢复动作 id:告警ID mask_state:为'true'时屏蔽,'false'为恢复 ''' service = '/alarm_command' args = r'''"req: '{\"alarm_command\":{\"id\":%d,\"mask_state\":%s,\"check_mask\":%s}}'"''' % ( id, mask_state, check_mask) result = Chassis.Exec(container).rosservice_call(service, args).decode() if check_mask == True: idList = re.findall('\d{7,}', result) return idList
def default_state(): logging.info('前置步骤:所有docker均已启动。roscore已运行') tx2 = TX2.TX2(ip) containers = tx2.Containers() container = tx2.Containers()['chassis_serial'] tx2.Images() if len(containers) != 11: raise Exception('docker未全部启动') if not Chassis.Exec(container).roscore_check(): raise Exception('roscore未自动启动') logging.info('1、查看当前状态/chassis_state' '//各项均为默认值(无故障切换状态)') result = chassis_state_echo(container=container) if int(result['cpu_usage']) >= 50: raise Exception('cpu占用超过80%') if result['wdg_state'] != '0': raise Exception('看门狗状态异常') if result['bms'] != '0': raise Exception('电源板异常') if result['motor'] != '0': raise Exception('电机异常') if result['motor_driver'] != '0': raise Exception('电驱异常') if result['joystick'] != '0': raise Exception('摇杆异常') if result['imu'] != '0': raise Exception('imu异常') if result['ultrasonic'] != '0': raise Exception('超声波异常') if result['ultrasonic'] != '0': raise Exception('超声波异常') if result['weighing'] != '0': raise Exception('重力系统异常') if result['flash'] != '0': raise Exception('flash异常') other_res = re.match('\[(\d+),\s(\d+),\s(\d+),\s(\d+),\s0', result['other_res']).groups() if other_res[0] != '0': raise Exception('网络通信异常') if other_res[1] != '0': raise Exception('led通信异常') if int(other_res[2]) >= 80: raise Exception('cpu温度过高') if int(other_res[3]) >= 80: raise Exception('imu温度过高') logging.info('检查完成,下位机各项指标均正常') print(result)
def chassis_control_state_pub(container, operation): ''' 操作operation对应的命令 0 上锁..............................................lock 1 解锁..............................................unlock 2 停车..............................................stop 3 手动驾驶..........................................manual 4 自动驾驶..........................................auto 5 故障..............................................fault 6 故障恢复(故障->锁车)............................fault_rec ''' OperationList = [ 'lock', 'unlock', 'stop', 'manual', 'auto', 'fault', 'fault_rec' ] data = OperationList.index(operation) msg = '"data: %s"' % str(data) Chassis.Exec(container).rostopic_pub(topic='/chassis/control_state ', msg_type='std_msgs/UInt8 ', args=msg)
TestSuite = os.path.basename(__file__) logpath = path + TestSuite.split('.')[0] + time.strftime( "_%Y-%m-%d_%H-%M-%S.log", time.localtime()) logging.basicConfig( level=logging.INFO, format= '%(levelname)s: %(message)s- %(filename)s[line:%(lineno)d]-%(asctime)s') logging.info('前置步骤:所有docker均已启动。roscore已运行') tx2 = TX2.TX2(ip) containers = tx2.Containers() container = tx2.Containers()['chassis_serial'] if len(containers) != 11: raise Exception('docker未全部启动') if not Chassis.Exec(container).roscore_check(): raise Exception('roscore未自动启动') def setup_module(): # 清除日志 init_collect_log(container, logpath) # 检查是否无告警 result = chassis_alarm_echo(container) for key in result.keys(): if 'alarm' in key and result[key] != '0': raise Exception('存在异常告警') def teardown_module(): # 恢复,停车落锁
# coding:utf-8 import TX2 from Command.Container import Chassis import re ip = '192.168.0.179' TX2 = TX2.TX2(ip) containers = TX2.Containers() container = TX2.Containers()['chassis_serial'] TX2.Images() if len(containers) != 13: raise Exception('docker未全部启动') if not Chassis.Exec(container).roscore_check(): raise Exception('roscore未自动启动') data_value = "'set_env motor_state 1'" data = '"data: %s"' % data_value Chassis.Exec(container).rostopic_pub(topic='/chassis/cmd_req ', msg_type='std_msgs/String ', args=data) result = Chassis.Exec(container).cmd_res_vf(isSET=True) if result != '1': raise Exception('参数设置日志有误') result = Chassis.Exec(container).rostopic_echo(topicName='/chassis_state') motor = re.search('motor:\s(\d+)', result).group(1) if motor != '1': raise Exception('预期值为1,实际值为' + motor) data_value = "'del_env motor_state'"
def test_answer(self): tx2 = TX2.TX2(ip) container = tx2.Containers()['chassis_serial'] if not Chassis.Exec(container).roscore_check(): raise Exception('roscore未自动启动')