Skip to content

Commit

Permalink
Merge pull request #71 from ZhouYixun/scrcpy_dev
Browse files Browse the repository at this point in the history
Scrcpy dev
  • Loading branch information
ZhouYixun authored Dec 9, 2021
2 parents cb81f1a + 9568dbd commit 247c14e
Show file tree
Hide file tree
Showing 26 changed files with 1,804 additions and 632 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ sonic:
# 替换为前端新增Agent生成的key
key: 29002272-4659-4808-a804-08ce3388b136
server:
# 如果跨网段,这个host改成后端的port
# 如果跨网段,这个host改成后端的host
host: localhost
# 如果跨网段,这个port改成后端文件中心的port(一般不变)
folder-port: 8094
Expand All @@ -18,18 +18,20 @@ modules:
# 安卓模块配置
android:
enable: true
# 是否开启远程adb调试功能
use-adbkit: false
# iOS模块配置
ios:
enable: true
#替换为wda的bundleId
wda-bundle-id: com.sonic.wda
#替换为wda的ipa文件名,并放到plugins文件夹中
wda-ipa-name: sonic-wda.ipa
#替换为wda的bundleId,正常要添加.xctrunner后缀
wda-bundle-id: com.sonic.wda.xctrunner
appium:
enable: true
# 在线webView调试模块配置
webview:
enable: true
# 谷歌调试端口,一般不需要修改
chrome-driver-debug-port: 7778
# 谷歌调试端口,一般不需要修改(默认0使用随机端口,如果需要开启防火墙给外部使用,请设置固定端口如7778)
chrome-driver-debug-port: 0
# Agent机器上的chrome浏览器的driver路径,可以去http://npm.taobao.org/mirrors/chromedriver/下载
chrome-driver-path: C:\Program Files\Google\Chrome\Application\chromedriver.exe

Expand Down
8 changes: 1 addition & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.sonic</groupId>
<artifactId>sonic-agent</artifactId>
<version>v1.2.0-beta3</version>
<version>v1.2.0-rc</version>
<packaging>jar</packaging>

<properties>
Expand All @@ -23,12 +23,6 @@
</parent>

<dependencies>
<!-- 重定向 -->
<dependency>
<groupId>org.mitre.dsmiley.httpproxy</groupId>
<artifactId>smiley-http-proxy-servlet</artifactId>
<version>1.12</version>
</dependency>
<!-- websocket client -->
<dependency>
<groupId>org.java-websocket</groupId>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/sonic/agent/automation/AppiumServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public static void start() {
service.start();
}

public static void close(){
if (service.isRunning()) {
public static void close() {
if (service != null && service.isRunning()) {
service.stop();
}
}
Expand Down
22 changes: 13 additions & 9 deletions src/main/java/com/sonic/agent/automation/RemoteDebugDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.lang.NonNull;

/**
* @author ZhouYiXun
Expand All @@ -20,30 +23,30 @@
*/
@ConditionalOnProperty(value = "modules.webview.enable", havingValue = "true")
@Configuration
public class RemoteDebugDriver {
public class RemoteDebugDriver implements ApplicationListener<ContextRefreshedEvent> {
private static final Logger logger = LoggerFactory.getLogger(RemoteDebugDriver.class);
private static String chromePath;
private static String debugHost;
private static int chromePort;
public static int chromePort;
public static WebDriver webDriver;
@Value("${modules.webview.chrome-driver-path}")
private String path;
@Value("${modules.webview.chrome-driver-debug-port}")
private int port;
@Value("${sonic.agent.host}")
private String agentHost;

@Bean
public void setChromePath() {
chromePath = path;
chromePort = port;
debugHost = agentHost;
}

@Bean
@DependsOn(value = "setChromePath")
@Override
public void onApplicationEvent(@NonNull ContextRefreshedEvent event) {
startChromeDriver();
}

public static void startChromeDriver() {
logger.info("开启webview相关功能");
System.setProperty("webdriver.chrome.silentOutput", "true");
try {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
ChromeOptions chromeOptions = new ChromeOptions();
Expand All @@ -55,13 +58,14 @@ public static void startChromeDriver() {
} else {
chromeOptions.addArguments("--remote-debugging-port=" + chromePort);
}
chromeOptions.addArguments("--remote-debugging-address=" + debugHost);
chromeOptions.addArguments("--remote-debugging-address=0.0.0.0");
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--disable-dev-shm-usage");
desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
webDriver = new ChromeDriver(desiredCapabilities);
logger.info("chromeDriver启动完毕!");
} catch (Exception e) {
logger.info("chromeDriver启动失败!");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.sonic.agent.bridge.android;

import com.alibaba.fastjson.JSONObject;
import com.android.ddmlib.*;
import com.sonic.agent.tools.DownImageTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import javax.websocket.Session;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
Expand All @@ -22,12 +24,16 @@
@ConditionalOnProperty(value = "modules.android.enable", havingValue = "true")
@DependsOn({"androidThreadPoolInit", "nettyMsgInit"})
@Component
public class AndroidDeviceBridgeTool {
public class AndroidDeviceBridgeTool implements ApplicationListener<ContextRefreshedEvent> {
private static final Logger logger = LoggerFactory.getLogger(AndroidDeviceBridgeTool.class);
private static AndroidDebugBridge androidDebugBridge = null;
private static AndroidDeviceStatusListener androidDeviceStatusListener = new AndroidDeviceStatusListener();

public AndroidDeviceBridgeTool() {
@Autowired
private AndroidDeviceStatusListener androidDeviceStatusListener;


@Override
public void onApplicationEvent(@NonNull ContextRefreshedEvent event) {
logger.info("开启安卓相关功能");
init();
}
Expand Down Expand Up @@ -55,7 +61,7 @@ private static String getADBPathFromSystemEnv() {
* @des 定义方法
* @date 2021/8/16 19:36
*/
public static void init() {
public void init() {
//获取系统SDK路径
String systemADBPath = getADBPathFromSystemEnv();
//添加设备上下线监听
Expand Down Expand Up @@ -141,10 +147,19 @@ public static IDevice getIDeviceByUdId(String udId) {
public static String getScreenSize(IDevice iDevice) {
String size = "";
try {
//不同机型获取的结果有偏差,需要去掉空格、/r、/n和异常情况
size = executeCommand(iDevice, "wm size").split(":")[1].trim()
.replace("\r", "").replace("\n", "")
.replace("Override size", "");
size = executeCommand(iDevice, "wm size");
if (size.contains("Override size")) {
size = size.substring(size.indexOf("Override size"));
} else {
size = size.split(":")[1];
}
//注意顺序问题
size = size.trim()
.replace(":", "")
.replace("Override size", "")
.replace("\r", "")
.replace("\n", "")
.replace(" ", "");
if (size.length() > 20) {
size = "未知";
}
Expand Down Expand Up @@ -347,7 +362,7 @@ public static void pushToCamera(IDevice iDevice, String url) {
}
}

public static void searchDevice(IDevice iDevice){
public static void searchDevice(IDevice iDevice) {
executeCommand(iDevice, "am start -n com.sonic.plugins.assist/com.sonic.plugins.assist.SearchActivity");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import com.sonic.agent.netty.NettyThreadPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
* @author ZhouYiXun
* @des adb上下线监听,发送对应给server
* @date 2021/08/16 19:26
*/
@Component
public class AndroidDeviceStatusListener implements AndroidDebugBridge.IDeviceChangeListener {
private final Logger logger = LoggerFactory.getLogger(AndroidDeviceStatusListener.class);

Expand Down
65 changes: 38 additions & 27 deletions src/main/java/com/sonic/agent/bridge/ios/TIDeviceTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
Expand All @@ -24,26 +27,24 @@
@ConditionalOnProperty(value = "modules.ios.enable", havingValue = "true")
@DependsOn({"iOSThreadPoolInit", "nettyMsgInit"})
@Component
public class TIDeviceTool {
public class TIDeviceTool implements ApplicationListener<ContextRefreshedEvent> {
private static final Logger logger = LoggerFactory.getLogger(TIDeviceTool.class);
@Value("${modules.ios.wda-bundle-id}")
private String getBundleId;
private static String bundleId;
@Value("${modules.ios.wda-ipa-name}")
private String getIpa;
private static String ipa;

@Bean
public void setEnv() {
bundleId = getBundleId;
ipa = getIpa;
}

public TIDeviceTool() {
@Override
public void onApplicationEvent(@NonNull ContextRefreshedEvent event) {
logger.info("开启iOS相关功能");
init();
}


public static void init() {
IOSDeviceThreadPool.cachedThreadPool.execute(() -> {
List<String> aDevice = ProcessCommandTool.getProcessLocalCommand("tidevice list");
Expand Down Expand Up @@ -146,34 +147,38 @@ public static String getName(String udId) {

public static int startWda(String udId) throws IOException, InterruptedException {
synchronized (TIDeviceTool.class) {
Runtime.getRuntime().exec("tidevice -u " + udId + " uninstall " + bundleId);
Thread.sleep(500);
Runtime.getRuntime().exec("tidevice -u " + udId + " install plugins/" + ipa);
Thread.sleep(1000);
int port = PortTool.getPort();
Process wdaProcess = Runtime.getRuntime().exec("tidevice -u " + udId +
" wdaproxy" + " -B " + bundleId +
" --port " + port);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(wdaProcess.getInputStream()));
String s;
while ((s = stdInput.readLine()) != null) {
if (s.contains("WebDriverAgent start successfully")) {
logger.info(udId + " wda启动完毕!");
break;
} else {
Thread.sleep(500);
}
}
List<Process> processList;
if (IOSProcessMap.getMap().get(udId) != null) {
processList = IOSProcessMap.getMap().get(udId);
for (Process p : processList) {
if (p != null) {
p.children().forEach(ProcessHandle::destroy);
p.destroy();
}
}
}
int port = PortTool.getPort();
Process wdaProcess;
String commandLine = "tidevice -u " + udId +
" wdaproxy" + " -B " + bundleId +
" --port " + port;
if (System.getProperty("os.name").contains("Mac")) {
wdaProcess = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", commandLine});
} else {
wdaProcess = Runtime.getRuntime().exec(new String[]{"cmd", "/C", commandLine});
}
// BufferedReader stdInput = new BufferedReader(new
// InputStreamReader(wdaProcess.getInputStream()));
// String s;
// while ((s = stdInput.readLine()) != null) {
// if (s.contains("WebDriverAgent start successfully")) {
// logger.info(udId + " wda启动完毕!");
// break;
// } else {
// Thread.sleep(500);
// }
// }
Thread.sleep(3000);
processList = new ArrayList<>();
processList.add(wdaProcess);
IOSProcessMap.getMap().put(udId, processList);
Expand All @@ -183,8 +188,14 @@ public static int startWda(String udId) throws IOException, InterruptedException

public static int relayImg(String udId) throws IOException {
int port = PortTool.getPort();
Process relayProcess = Runtime.getRuntime().exec("tidevice -u " + udId +
" relay " + port + " " + 9100);
Process relayProcess;
String commandLine = "tidevice -u " + udId +
" relay " + port + " " + 9100;
if (System.getProperty("os.name").contains("Mac")) {
relayProcess = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", commandLine});
} else {
relayProcess = Runtime.getRuntime().exec(new String[]{"cmd", "/C", commandLine});
}
List<Process> processList;
if (IOSProcessMap.getMap().get(udId) != null) {
processList = IOSProcessMap.getMap().get(udId);
Expand Down
25 changes: 0 additions & 25 deletions src/main/java/com/sonic/agent/config/ProxyControl.java

This file was deleted.

12 changes: 12 additions & 0 deletions src/main/java/com/sonic/agent/maps/GlobalProcessMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sonic.agent.maps;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GlobalProcessMap {
private static Map<String, Process> processMap = new ConcurrentHashMap<>();

public static Map<String, Process> getMap() {
return processMap;
}
}
Loading

0 comments on commit 247c14e

Please sign in to comment.