import threading
import time
from const.const import Const
from util.dos_cmd import DosCmd
from util.port import Port
from util.handle_devices_cfg import HandleDevicesCfg
import yaml
class Server:
def init(self, start_port=4723):
self.dos = DosCmd()
self.devices_list = self.get_devices()
self.port = Port()
self.start_port = start_port
self.handle_cfg = HandleDevicesCfg()
# 通过IP连接设备(STB)
# def connect_device(self):
# result = self.dos.exe_return_result('adb connect '+Const.DEVICE_IP)
# print(result)
# if ‘connected to’ in result[0] or ‘already’ in result[0]:
# print(‘connect successful’)
# return True
# else:
# print(‘connect failed’)
# return False
#获取设备信息
def get_devices(self):
result_list = []
result = self.dos.exe_return_result('adb devices')
print(result)
if len(result) > 1:
for i in result:
if 'List' in i:
continue
if 'device' in i:
device = i.split('\t')[0]
result_list.append(device.strip('\n'))
return result_list
else:
return None
# 生成启动appium命令列表
def create_command(self,i):
# command_list = []
port_list = self.port.create_port_list(self.start_port, self.devices_list)
# 默认bootstrap起始端口比port大100
bp_list = self.port.create_port_list(self.start_port+100, self.devices_list)
# appium -p 4723 -bp 4823 -U 127.0.0.1:62001 --no-reset --session-override --log D:/teamwork/appium_log/test01.log
command = "appium -p " + str(port_list[i]) + " -bp " + str(bp_list[i]) + " -U " + self.devices_list[i] + " --no-reset --session-override --log D:/teamwork/appium_log/device" + str(i)+".log"
# command_list.append(command)
# return command_list
self.handle_cfg.write_cfg("device_info"+str(i), port_list[i], self.devices_list[i], bp_list[i])
return command
#启动appium服务
def start_server(self,i):
start_list = self.create_command(i)
self.dos.exe_return_result(start_list)
def kill_server(self):
server_list = self.dos.exe_return_result('tasklist | find "node.exe"')
if len(server_list) > 0:
self.dos.exe_no_result('taskkill -F -PID node.exe')
#多线程启动appium服务
def start_threads(self):
thread_list = []
self.kill_server()
self.handle_cfg.clear_cfg()
for i in range(len(self.devices_list)):
start_device = threading.Thread(target=self.start_server, args=(i,))
thread_list.append(start_device)
for j in thread_list:
j.start()
time.sleep(30)
if name == ‘main’:
server = Server()
server.start_threads()
这是运行结果:
这是保存的日志: