1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2021 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# limitations under the License.
17#
18
19import os
20
21from distributed.common.devices import DeviceShell
22
23
24##############################################################################
25##############################################################################
26
27class DeviceManager:
28    def __init__(self, result_path):
29        self.is_hdc = True
30        self.phone_device_list = []
31        self.ivi_device_list = []
32        self.tv_device_list = []
33        self.watch_device_list = []
34        self.make_device_list(result_path)
35
36    @staticmethod
37    def get_device_info_list(result):
38        device_info_list = []
39        tmp_path = os.path.join(result, "temp")
40        device_info_file_path = os.path.join(tmp_path,
41                                             "device_info_file.txt")
42
43        if os.path.exists(device_info_file_path):
44            with open(device_info_file_path, "r") as file_handle:
45                lines = file_handle.readlines()
46                for line in lines:
47                    line = line.replace("\n", "")
48                    line = line.strip()
49                    temp = line.split(",")
50                    device_info_list.append(temp)
51        return device_info_list
52
53    def make_device_adapter(self, device_info_list, device_name):
54        device = DeviceShell(self.is_hdc, device_sn=device_info_list[0],
55                             remote_ip=device_info_list[2],
56                             repote_port=device_info_list[3],
57                             name=device_name)
58        return device
59
60    def make_device_list(self, result_path):
61        device_info_list = self.get_device_info_list(result_path)
62        for item in device_info_list:
63            if len(item) != 4:
64                continue
65            if item[1] == "phone":
66                index = len(self.phone_device_list) + 1
67                device_name = "PHONE%s" % index
68                device = self.make_device_adapter(item, device_name)
69                self.phone_device_list.append(device)
70                setattr(self, device.name, device)
71            elif item[1] == "ivi":
72                index = len(self.ivi_device_list) + 1
73                device_name = "IVI%s" % index
74                device = self.make_device_adapter(item, device_name)
75                self.ivi_device_list.append(device)
76                setattr(self, device.name, device)
77            elif item[1] == "tv":
78                index = len(self.tv_device_list) + 1
79                device_name = "TV%s" % index
80                device = self.make_device_adapter(item, device_name)
81                self.tv_device_list.append(device)
82                setattr(self, device.name, device)
83            elif item[1] == "watch":
84                index = len(self.watch_device_list) + 1
85                device_name = "WATCH%s" % index
86                device = self.make_device_adapter(item, device_name)
87                self.watch_device_list.append(device)
88                setattr(self, device.name, device)
89        return
90
91##############################################################################
92##############################################################################
93