This repository was archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathAndroidStfSocketServer.java
95 lines (81 loc) · 3.23 KB
/
AndroidStfSocketServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.daxiang.websocket;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.daxiang.App;
import com.daxiang.core.mobile.android.AndroidDevice;
import io.appium.java_client.android.AndroidDriver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
/**
* Created by jiangyitao.
*/
@Slf4j
@Component
@ServerEndpoint(value = "/stf/android/{mobileId}/user/{username}/project/{projectId}")
public class AndroidStfSocketServer extends DeviceSocketServer {
private AndroidDevice androidDevice;
@OnOpen
public void onOpen(@PathParam("mobileId") String mobileId, @PathParam("username") String username,
@PathParam("projectId") Integer projectId, Session session) throws Exception {
onWebsocketOpenStart(mobileId, username, session);
androidDevice = (AndroidDevice) device;
int width = androidDevice.getMobile().getScreenWidth();
int height = androidDevice.getMobile().getScreenHeight();
String realResolution = width + "x" + height;
String virtualResolution = width / 2 + "x" + height / 2;
// android5以下的Mobile不多,暂不处理横竖屏切换
sender.sendText("启动minicap...");
androidDevice.getMinicap().start(Integer.parseInt(App.getProperty("minicap-quality")),
realResolution,
virtualResolution,
0,
minicapImgData -> {
try {
sender.sendBinary(minicapImgData);
} catch (IOException e) {
log.error("[{}]发送minicap数据异常", mobileId, e);
}
});
sender.sendText("启动minitouch...");
androidDevice.getMinitouch().start();
freshDriver(projectId);
onWebsocketOpenFinish();
}
@OnClose
public void onClose() {
if (androidDevice != null) {
androidDevice.getMinitouch().stop();
androidDevice.getMinicap().stop();
onWebSocketClose();
}
}
@OnError
public void onError(Throwable t) {
log.error("[{}]onError", deviceId, t);
}
@OnMessage
public void onMessage(String msg) {
JSONObject message = JSON.parseObject(msg);
String operation = message.getString("operation");
switch (operation) {
case "m":
androidDevice.getMinitouch()
.moveTo(message.getIntValue("x"), message.getIntValue("y"), message.getIntValue("width"), message.getIntValue("height"));
break;
case "d":
androidDevice.getMinitouch()
.touchDown(message.getIntValue("x"), message.getIntValue("y"), message.getIntValue("width"), message.getIntValue("height"));
break;
case "u":
androidDevice.getMinitouch().touchUp();
break;
case "k":
((AndroidDriver) androidDevice.getDriver()).pressKeyCode(message.getIntValue("keycode"));
break;
}
}
}