1#!/usr/bin/env python3 2# coding=utf-8 3 4# 5# Copyright (c) 2020-2022 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# 17 18from xdevice import UserConfigManager 19from xdevice import platform_logger 20from xdevice import get_local_ip 21 22__all__ = ["OHOSUserConfigManager"] 23LOG = platform_logger("OHOSConfigManager") 24 25 26class OHOSUserConfigManager(UserConfigManager): 27 28 def __init__(self, config_file="", env=""): 29 super().__init__(config_file, env) 30 31 def get_com_device(self, target_name): 32 devices = [] 33 34 for node in self.config_content.findall(target_name): 35 if node.attrib["type"] != "com": 36 continue 37 38 device = [node.attrib] 39 40 # get remote device 41 data_dic = {} 42 for sub in node: 43 if sub.text is not None and sub.tag != "serial": 44 data_dic[sub.tag] = sub.text 45 if data_dic: 46 if data_dic.get("ip", "") == get_local_ip(): 47 data_dic["ip"] = "127.0.0.1" 48 device.append(data_dic) 49 devices.append(device) 50 continue 51 52 # get local device 53 for serial in node.findall("serial"): 54 data_dic = {} 55 for sub in serial: 56 if sub.text is None: 57 data_dic[sub.tag] = "" 58 else: 59 data_dic[sub.tag] = sub.text 60 device.append(data_dic) 61 devices.append(device) 62 return devices 63 64 def get_devices(self, target_name): 65 devices = [env for env in self.environment if env.get("label") == "ohos"] 66 if devices: 67 return devices 68 69 device_list = {} 70 for node in self.config_content.findall(target_name): 71 data_dic = {} 72 if node.attrib["type"] != "usb-hdc": 73 continue 74 data_dic["usb_type"] = node.attrib["type"] 75 for sub in node: 76 if sub.text is None: 77 data_dic[sub.tag] = "" 78 else: 79 data_dic[sub.tag] = sub.text 80 if not data_dic.get("ip", "") or data_dic.get("ip", "") == get_local_ip(): 81 data_dic["ip"] = "127.0.0.1" 82 label = node.get("label", None) 83 if label and label != "ohos": 84 continue 85 if data_dic["ip"] not in device_list: 86 device_list[data_dic["ip"]] = data_dic 87 return list(device_list.values()) 88