From ea017d69e6646455d08b9dec26bd3a2fe59afc10 Mon Sep 17 00:00:00 2001 From: "yineng.huang" Date: Tue, 9 Jul 2024 11:50:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Bandroid=E6=88=AA=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../driver/agent/command/EchoCommand.java | 55 +++++++++++++ .../command/protocol/ProtocolStreamUtil.java | 82 +++++++++++++++++++ .../service/AndroidDebuggerServiceImpl.java | 70 +++++++++++----- 3 files changed, 185 insertions(+), 22 deletions(-) create mode 100644 cctp-atu/atu-upper-computer/src/main/java/net/northking/cctp/upperComputer/driver/agent/command/EchoCommand.java create mode 100644 cctp-atu/atu-upper-computer/src/main/java/net/northking/cctp/upperComputer/driver/agent/command/protocol/ProtocolStreamUtil.java diff --git a/cctp-atu/atu-upper-computer/src/main/java/net/northking/cctp/upperComputer/driver/agent/command/EchoCommand.java b/cctp-atu/atu-upper-computer/src/main/java/net/northking/cctp/upperComputer/driver/agent/command/EchoCommand.java new file mode 100644 index 0000000..e2b4b0f --- /dev/null +++ b/cctp-atu/atu-upper-computer/src/main/java/net/northking/cctp/upperComputer/driver/agent/command/EchoCommand.java @@ -0,0 +1,55 @@ +package net.northking.cctp.upperComputer.driver.agent.command; + + +import net.northking.cctp.upperComputer.driver.agent.command.protocol.AbstractProtocolCommand; +import net.northking.cctp.upperComputer.driver.agent.command.protocol.ProtocolStreamUtil; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; + +public class EchoCommand extends AbstractProtocolCommand { + + private static EchoCommand instance = null; + + public static EchoCommand getInstance() { + if (instance == null) { + instance = new EchoCommand(); + } + return instance; + } + + private String text = ""; + + private EchoCommand() { + } + + public EchoCommand(String text) { + if (text == null) throw new IllegalArgumentException("文本不能为null"); + this.text = text; + } + + public String getText() { + return text; + } + + public void setText(String text) { + if (text == null) throw new IllegalArgumentException("文本不能为null"); + this.text = text; + } + + @Override + public int getCommandId() { + return 2; + } + + @Override + public void doSend(DataOutput output) throws IOException { + ProtocolStreamUtil.writeText(output, text); + } + + @Override + public String buildReceive(DataInput input) throws IOException { + return ProtocolStreamUtil.readText(input); + } +} diff --git a/cctp-atu/atu-upper-computer/src/main/java/net/northking/cctp/upperComputer/driver/agent/command/protocol/ProtocolStreamUtil.java b/cctp-atu/atu-upper-computer/src/main/java/net/northking/cctp/upperComputer/driver/agent/command/protocol/ProtocolStreamUtil.java new file mode 100644 index 0000000..49ac95f --- /dev/null +++ b/cctp-atu/atu-upper-computer/src/main/java/net/northking/cctp/upperComputer/driver/agent/command/protocol/ProtocolStreamUtil.java @@ -0,0 +1,82 @@ +package net.northking.cctp.upperComputer.driver.agent.command.protocol; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.util.ArrayList; + +public class ProtocolStreamUtil { + + /** + * 发送文本
+ * 作为DataOutput方法的补充
+ * 文本字节长度+文本字节 + * + * @param output 发送操作对象 + * @param text 要发送的文本内容 + * @throws IOException 发送过程中发生输入输出错误 + */ + public static void writeText(DataOutput output, String text) throws IOException { + byte[] textByteArray = text.getBytes(); + output.writeInt(textByteArray.length); + output.write(textByteArray); + } + + /** + * 读取文本
+ * 作为DataInput方法的补充
+ * 会读取一个整型作为接下来要读取的文本字节长度 + * + * @param input 读取操作对象 + * @return 读取到的文本 + * @throws IOException 读取过程中发生输入输出错误 + */ + public static String readText(DataInput input) throws IOException { + int textLength = input.readInt(); + byte[] textByteArray = new byte[textLength]; + input.readFully(textByteArray); + return new String(textByteArray); + } + + public static int[] readIntArray(DataInput input) throws IOException { + int length = input.readInt(); + int[] array = new int[length]; + for (int i = 0; i < length; i++) { + array[i] = input.readInt(); + } + return array; + } + + public static void writeNullable(DataOutput output, K data, WriteNullableData writer) throws IOException { + if (data == null) { + output.writeBoolean(false); + } else { + output.writeBoolean(true); + writer.write(output, data); + } + } + + public static K readNullable(DataInput input, ReadNullableData reader) throws IOException { + if (input.readBoolean()) { + return reader.read(input); + } + return null; + } + + public static ArrayList readStringArrayList(DataInput input) throws IOException { + int length = input.readInt(); + ArrayList list = new ArrayList(length); + for (int i = 0; i < length; i++) { + list.add(readText(input)); + } + return list; + } + + public interface ReadNullableData { + K read(DataInput input) throws IOException; + } + + public interface WriteNullableData { + void write(DataOutput output, K data) throws IOException; + } +} diff --git a/cctp-atu/atu-upper-computer/src/main/java/net/northking/cctp/upperComputer/service/AndroidDebuggerServiceImpl.java b/cctp-atu/atu-upper-computer/src/main/java/net/northking/cctp/upperComputer/service/AndroidDebuggerServiceImpl.java index a469fdc..6ae8f6d 100644 --- a/cctp-atu/atu-upper-computer/src/main/java/net/northking/cctp/upperComputer/service/AndroidDebuggerServiceImpl.java +++ b/cctp-atu/atu-upper-computer/src/main/java/net/northking/cctp/upperComputer/service/AndroidDebuggerServiceImpl.java @@ -11,10 +11,7 @@ import net.northking.cctp.upperComputer.driver.adb.Adb; import net.northking.cctp.upperComputer.driver.adb.AdbDevice; import net.northking.cctp.upperComputer.driver.adb.AdbTransport; import net.northking.cctp.upperComputer.driver.agent.AndroidAgentSession; -import net.northking.cctp.upperComputer.driver.agent.command.AppiumSourceXmlCommand; -import net.northking.cctp.upperComputer.driver.agent.command.CloseSessionCommand; -import net.northking.cctp.upperComputer.driver.agent.command.GetSmsListCommand; -import net.northking.cctp.upperComputer.driver.agent.command.TakeScreenshotCommand; +import net.northking.cctp.upperComputer.driver.agent.command.*; import net.northking.cctp.upperComputer.enums.FileBusinessTypeEnum; import net.northking.cctp.upperComputer.entity.Attachment; import net.northking.cctp.upperComputer.entity.DebuggerDeviceInfo; @@ -62,6 +59,8 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService { private ConcurrentHashMap queryDeviceInfoMap = new ConcurrentHashMap<>(); + private ConcurrentHashMap screenShotAgentSessionMap = new ConcurrentHashMap<>(); + private Adb adb; @Value("${appium.server.host:127.0.0.1}") @@ -643,6 +642,10 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService { @Override public String shotAllScreen(DebuggerDeviceInfo info) { File file = getScreenShotInCommand(info.getDeviceId()); + if (null == file || !file.exists()) { + logger.error("设备【{}】截出来的图不存在",info.getDeviceId()); + throw new ExecuteException("截取图片失败"); + } // File file = ScreenShotUtils.getMobileScreenShot(info.getDeviceId(), info.getResolution(), false); try { return Base64.encode(file); @@ -663,6 +666,10 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService { @Override public String uploadShotAllScreen(DebuggerDeviceInfo info) { File file = getScreenShotInCommand(info.getDeviceId()); + if (null == file || !file.exists()) { + logger.error("设备【{}】截出来的图不存在",info.getDeviceId()); + throw new ExecuteException("截取图片失败"); + } String path = null; try { //租户id @@ -676,18 +683,18 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService { path = upload.getUrlPath(); } } catch (ExecuteException e) { - logger.error("截图失败", e); + logger.error("上传截图失败", e); throw e; } catch (Exception e) { - logger.error("截图失败", e); - throw new ExecuteException("截取图片失败"); + logger.error("上传截图失败", e); + throw new ExecuteException("上传截图失败"); } finally { - if (file.exists()) { - boolean delete = file.delete(); - if (!delete) { - logger.warn("临时文件【{}】删除失败", file.getAbsolutePath()); - } - } +// if (file.exists()) { +// boolean delete = file.delete(); +// if (!delete) { +// logger.warn("临时文件【{}】删除失败", file.getAbsolutePath()); +// } +// } } return path; } @@ -860,6 +867,10 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService { Double realityX = info.getX().doubleValue() / screenWidth * realityWidth; Double realityY = info.getY().doubleValue() / screenHeight * realityHeight; snapShotFile = getScreenShotInCommand(info.getDeviceId()); + if (null == snapShotFile || !snapShotFile.exists()) { + logger.error("设备【{}】截出来的图不存在",info.getDeviceId()); + throw new ExecuteException("截取图片失败"); + } ImgUtil.cut(snapShotFile, cutFile, new java.awt.Rectangle(realityX.intValue(), realityY.intValue(), realityCutWidth.intValue(), realityCutHeight.intValue())); return cutFile; } catch (ExecuteException e) { @@ -879,13 +890,30 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService { File tmpPicFile = null; AndroidAgentSession agentSession = null; try { - AdbDevice currentDevice = AndroidDeviceManager.getInstance().getCurrentDevice(deviceId); - if (null == currentDevice) { - logger.warn("设备【{}】不在线,请稍后再试",deviceId); - throw new ExecuteException("当前设备已掉线无法截图"); + agentSession = screenShotAgentSessionMap.get(deviceId); + if (null == agentSession || !agentSession.isState()) { + AdbDevice currentDevice = AndroidDeviceManager.getInstance().getCurrentDevice(deviceId); + if (null == currentDevice) { + throw new ExecuteException("当前设备不在线"); + } + agentSession = new AndroidAgentSession(currentDevice, false); + agentSession.start(); + screenShotAgentSessionMap.put(deviceId, agentSession); + } else { + String send = agentSession.send(new EchoCommand("hello")); //检查 + if (!"hello".equals(send)) { + logger.warn("设备【{}】当前使用的截图的session失效了,重新创建一个。。。。。",deviceId); + AdbDevice currentDevice = AndroidDeviceManager.getInstance().getCurrentDevice(deviceId); + if (null == currentDevice) { + throw new ExecuteException("当前设备不在线"); + } + agentSession = new AndroidAgentSession(currentDevice, false); + agentSession.start(); + screenShotAgentSessionMap.put(deviceId, agentSession); + } else { + logger.debug("设备【{}】当前使用的截图的session还能用,继续使用。。。。。",deviceId); + } } - agentSession = new AndroidAgentSession(currentDevice,false); - agentSession.start(); byte[] screenShotData = agentSession.send(TakeScreenshotCommand.getInstance()); if (null != screenShotData && screenShotData.length > 0) { //截图 logger.debug("收到手机【{}】截图,大小:{}", deviceId, screenShotData.length); @@ -904,9 +932,7 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService { } catch (Exception e) { logger.error("设备【"+deviceId+"】截图异常", e); }finally { - if (null != agentSession) { - agentSession.closeSilence(); - } + logger.debug("设备【{}】写到本地的图片大小:{}"); } return tmpPicFile; }