update:重构脚本执行方式

This commit is contained in:
Yutousama 2022-07-26 22:46:00 +08:00
parent af54377bb2
commit 21dd8033d1
7 changed files with 607 additions and 72 deletions

View File

@ -3,47 +3,58 @@ package com.yutou.qqbot.Controllers;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.yutou.qqbot.data.JianRScriptData; import com.yutou.qqbot.data.jianr.AndroidDevice;
import com.yutou.qqbot.data.jianr.JianRScriptV2Data;
import com.yutou.qqbot.utlis.JianRTaskManager; import com.yutou.qqbot.utlis.JianRTaskManager;
import com.yutou.qqbot.utlis.RedisTools; import com.yutou.qqbot.utlis.RedisTools;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Set; import java.util.Map;
@Controller @Controller
public class JianRScriptController { public class JianRScriptController {
@RequestMapping("/jianr/run.do") @RequestMapping("/jianr/run.do")
@ResponseBody @ResponseBody
public JSONObject runScript(String task) { public JSONObject runScript(String task, String device, String modelName) {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
String data = RedisTools.get("jianrScript_Model_" + task); String data = RedisTools.getHashMap(JianRTaskManager.Redis_Script, task);
if (data == null) { String deviceJsonString = RedisTools.getHashMap(JianRTaskManager.Redis_Device, device);
if (data == null || deviceJsonString == null) {
json.put("code", -1); json.put("code", -1);
json.put("msg", "没有找到该方案,请配置"); json.put("msg", "没有找到该方案或设备错误,请配置");
} else { } else {
JianRScriptData script = JSON.parseObject(data, JianRScriptData.class); JianRScriptV2Data script = JSON.parseObject(data, JianRScriptV2Data.class);
AndroidDevice androidDevice = JSON.parseObject(deviceJsonString, AndroidDevice.class);
JianRTaskManager manager = JianRTaskManager.getInstance(); JianRTaskManager manager = JianRTaskManager.getInstance();
if (manager.isRunning()) { if (manager.isRunning()) {
manager.stop(); manager.stop();
} }
manager.setTask(script); for (int i = 0; i < androidDevice.getDeviceDisplay().size(); i++) {
if (androidDevice.getDeviceDisplay().get(i).getTitle().equals(modelName)) {
manager.setModelId(i);
break;
}
}
manager.setTask(script, androidDevice);
manager.start(); manager.start();
json.put("code", 0); json.put("code", 0);
json.put("msg", "任务创建成功"); json.put("msg", "任务创建成功");
} }
return json; return json;
} }
@RequestMapping("/jianr/stop.do") @RequestMapping("/jianr/stop.do")
@ResponseBody @ResponseBody
public JSONObject stop(){ public JSONObject stop() {
JianRTaskManager.getInstance().stop(); JianRTaskManager.getInstance().stop();
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("code", 0); json.put("code", 0);
json.put("msg", "任务已停止"); json.put("msg", "任务已停止");
return json; return json;
} }
@RequestMapping("/jianr/status.do") @RequestMapping("/jianr/status.do")
@ResponseBody @ResponseBody
public JSONObject status() { public JSONObject status() {
@ -64,8 +75,8 @@ public class JianRScriptController {
public JSONObject tasks() { public JSONObject tasks() {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
JSONArray array = new JSONArray(); JSONArray array = new JSONArray();
Set<String> jianrScript_model = RedisTools.list_get("jianrScript_Model"); Map<String, String> map = RedisTools.getHashMap(JianRTaskManager.Redis_Script);
array.addAll(jianrScript_model); array.addAll(map.keySet());
json.put("code", 0); json.put("code", 0);
json.put("data", array); json.put("data", array);
return json; return json;
@ -80,35 +91,150 @@ public class JianRScriptController {
json.put("code", -1); json.put("code", -1);
json.put("msg", "JSON格式错误"); json.put("msg", "JSON格式错误");
} else { } else {
String taskName = data.getString("name"); String taskName = data.getString("title");
if (RedisTools.list_isExist("jianrScript_Model", taskName)) { if (RedisTools.getHashMap(JianRTaskManager.Redis_Script).containsKey(taskName)) {
json.put("code", -1); json.put("code", -1);
json.put("msg", "该任务名称已存在"); json.put("msg", "该任务名称已存在");
} else { } else {
RedisTools.list_add("jianrScript_Model", taskName); RedisTools.setHashMap(JianRTaskManager.Redis_Script, taskName, task);
RedisTools.set("jianrScript_Model_"+taskName,task);
json.put("code", 0); json.put("code", 0);
json.put("msg", "任务创建成功"); json.put("msg", "任务创建成功");
} }
} }
return json; return json;
} }
@RequestMapping("/jianr/task/get.do") @RequestMapping("/jianr/task/get.do")
@ResponseBody @ResponseBody
public JSONObject getTask (String task){ public JSONObject getTask(String task) {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("code", 0); json.put("code", 0);
json.put("data", RedisTools.get("jianrScript_Model_" + task)); json.put("data", RedisTools.getHashMap(JianRTaskManager.Redis_Script, task));
return json; return json;
} }
@RequestMapping("/jianr/task/remove.do") @RequestMapping("/jianr/task/remove.do")
@ResponseBody @ResponseBody
public JSONObject removeTask (String task){ public JSONObject removeTask(String task) {
RedisTools.remove("jianrScript_Model_" + task); RedisTools.removeHashMap(JianRTaskManager.Redis_Script, task);
RedisTools.list_remove("jianrScript_Model", task);
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("code", 0); json.put("code", 0);
json.put("msg", "任务删除成功"); json.put("msg", "任务删除成功");
return json; return json;
} }
@RequestMapping("/jianr/device/connect.do")
@ResponseBody
public JSONObject connectDevice(String device) {
JSONObject json = new JSONObject();
JianRTaskManager.getInstance().connect(device);
json.put("code", 0);
json.put("msg", "成功");
return json;
} }
@RequestMapping("/jianr/device/add.do")
@ResponseBody
public JSONObject addDevice(String device) {
Map<String, String> hashMap = RedisTools.getHashMap(JianRTaskManager.Redis_Device);
JSONObject deviceJson = JSON.parseObject(device);
JSONObject json = new JSONObject();
json.put("code", 0);
if (deviceJson == null) {
json.put("msg", "JSON格式错误");
} else {
if (hashMap.containsKey(device)) {
json.put("msg", "设备已存在");
} else {
json.put("msg", RedisTools.setHashMap(JianRTaskManager.Redis_Device,
deviceJson.getString("title"), device) ?
"设备添加成功" : "设备添加失败");
}
}
return json;
}
@RequestMapping("/jianr/device/edit.do")
@ResponseBody
public JSONObject editDevice(String device, String oldDevice) {
JSONObject deviceJson = JSON.parseObject(device);
JSONObject json = new JSONObject();
json.put("code", 0);
if (deviceJson == null) {
json.put("msg", "JSON格式错误");
} else {
RedisTools.removeHashMap(JianRTaskManager.Redis_Device, oldDevice);
json.put("msg", RedisTools.setHashMap(JianRTaskManager.Redis_Device,
deviceJson.getString("title"), device) ?
"设备修改成功" : "设备修改失败");
}
return json;
}
@RequestMapping("/jianr/device/del.do")
@ResponseBody
public JSONObject delDevice(String device) {
JSONObject deviceJson = JSON.parseObject(device);
JSONObject json = new JSONObject();
json.put("code", 0);
if (deviceJson == null) {
json.put("msg", "JSON格式错误");
} else {
json.put("msg", RedisTools.removeHashMap(JianRTaskManager.Redis_Device) ?
"设备修改成功" : "设备修改失败");
}
return json;
}
@RequestMapping("/jianr/device/list.do")
@ResponseBody
public JSONObject getDeviceList() {
Map<String, String> hashMap = RedisTools.getHashMap(JianRTaskManager.Redis_Device);
JSONObject json = new JSONObject();
JSONArray array = new JSONArray();
json.put("code", 0);
hashMap.keySet().forEach(key -> {
JSONObject device = JSONObject.parseObject(hashMap.get(key));
JSONArray child = new JSONArray();
JSONObject item = new JSONObject();
JSONObject del = new JSONObject();
JSONObject edit = new JSONObject();
JSONObject select = new JSONObject();
JSONObject connect = new JSONObject();
item.put("title", key);
del.put("title", "删除");
del.put("data", "del");
del.put("device", device);
edit.put("title", "编辑");
edit.put("data", "edit");
edit.put("device", device);
select.put("title", "选择");
select.put("data", "select");
select.put("device", device);
connect.put("title", "连接设备");
connect.put("data", "connect");
connect.put("device", device);
child.add(connect);
child.add(select);
child.add(del);
child.add(edit);
item.put("child", child);
array.add(item);
});
json.put("data", array);
return json;
}
public static void main(String[] args) {
Map<String, String> hashMap = RedisTools.getHashMap(JianRTaskManager.Redis_Device);
JSONObject json = new JSONObject();
JSONArray array = new JSONArray();
json.put("code", 0);
hashMap.keySet().forEach(key -> {
JSONObject item = JSONObject.parseObject(hashMap.get(key));
array.add(item);
});
json.put("data", array);
System.out.println(json);
}
}

View File

@ -0,0 +1,35 @@
package com.yutou.qqbot.data.jianr;
import lombok.Data;
import java.util.List;
@Data
public class AndroidDevice {
String title;
String deviceId;
DeviceDisplay androidDevice;
List<GameDisplay> deviceDisplay;
@Data
public static class DeviceDisplay {
int width, height;
}
@Data
public static class GameDisplay {
String title;
double width, height;
Vector2D start, end;
public double getWidth() {
return end.getX() - start.getX();
}
public double getHeight() {
return end.getY() - start.getY();
}
}
}

View File

@ -1,4 +1,4 @@
package com.yutou.qqbot.data; package com.yutou.qqbot.data.jianr;
import lombok.Data; import lombok.Data;

View File

@ -0,0 +1,26 @@
package com.yutou.qqbot.data.jianr;
import lombok.Data;
import java.util.List;
@Data
public class JianRScriptV2Data {
private String title;
private List<Script> script;
@Data
public static class Script {
private String title;
private String activity;
private int nextWaitTime;
private int randomNextWaitTime;
}
public static class ScriptModel {
public static final String MAP = "map";
public static final String attack = "attack";
public static final String formationType = "formationType";
public static final String dialog_go = "dialog_go";
public static final String dialog_back = "dialog_back";
}
}

View File

@ -0,0 +1,38 @@
package com.yutou.qqbot.data.jianr;
import lombok.Data;
import java.util.Random;
@Data
public class Vector2D {
double x, y;
boolean enableRandomX, enableRandomY;
boolean absRandomX, absRandomY;
int randomNumX, randomNumY;
public double getX() {
if (isEnableRandomX()) {
double tmp = new Random().nextDouble(-randomNumX, randomNumX);
if (isAbsRandomX()) {
return Math.abs(tmp) + x;
} else {
return tmp + x;
}
}
return x;
}
public double getY() {
if(isEnableRandomY()){
double tmp = new Random().nextDouble(-randomNumY, randomNumY);
if (isAbsRandomY()) {
return Math.abs(tmp) + y;
} else {
return tmp + y;
}
}
return y;
}
}

View File

@ -1,15 +1,26 @@
package com.yutou.qqbot.utlis; package com.yutou.qqbot.utlis;
import com.yutou.qqbot.data.JianRScriptData; import com.alibaba.fastjson2.JSON;
import com.yutou.qqbot.data.jianr.AndroidDevice;
import com.yutou.qqbot.data.jianr.JianRScriptData;
import com.yutou.qqbot.data.jianr.JianRScriptV2Data;
import com.yutou.qqbot.data.jianr.Vector2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Random; import java.util.Random;
public class JianRTaskManager { public class JianRTaskManager {
public static final String Redis_Script ="jianrScript_Script";
public static final String Redis_Device="jianrScript_Devices";
public static final String Redis_RunIndex="jianrScript_RunIndex";
private static JianRTaskManager instance; private static JianRTaskManager instance;
private JianRScriptData task; private JianRScriptV2Data task;
private AndroidDevice device;
private boolean running = false; private boolean running = false;
private int runIndex = 0; private int runIndex = 0;
private String log; private String log;
private int modelId;
public static JianRTaskManager getInstance() { public static JianRTaskManager getInstance() {
if (instance == null) { if (instance == null) {
@ -22,17 +33,22 @@ public class JianRTaskManager {
} }
public void setTask(JianRScriptData data) { public void setModelId(int modelId) {
this.modelId = modelId;
}
public void setTask(JianRScriptV2Data data, AndroidDevice device) {
task = data; task = data;
runIndex=0; this.device = device;
String s = RedisTools.get("jianrScript_RunIndex_" + data.getName()); runIndex = 0;
String s = RedisTools.getHashMap(Redis_RunIndex,data.getTitle());
if (s != null) { if (s != null) {
runIndex = Integer.parseInt(s); runIndex = Integer.parseInt(s);
} }
} }
public void start() { public void start() {
if(running){ if (running) {
return; return;
} }
running = true; running = true;
@ -43,19 +59,21 @@ public class JianRTaskManager {
while (running) { while (running) {
try { try {
log("已运行: " + runIndex + ""); log("已运行: " + runIndex + "");
for (JianRScriptData.Script script : task.getRun()) { for (JianRScriptV2Data.Script script : task.getScript()) {
log(script.getName()); if(!isRunning()){
if (script.getModel().equals(JianRScriptData.Script.ScriptModel.CLICK)) { log("任务已停止");
adbTap(task.getDevice(), script); break;
} }
log(script.getTitle());
JianRTaskManager.this.run(device, modelId, script);
try { try {
Thread.sleep((long) getRandom(0, script.getRandomNextWaitTime(), script.getNextWaitTime())); Thread.sleep((long) getRandom(0, script.getRandomNextWaitTime()*1000, script.getNextWaitTime()*1000));
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
runIndex++; runIndex++;
RedisTools.set("jianrScript_RunIndex_" + task.getName(), runIndex + ""); RedisTools.setHashMap(Redis_RunIndex,task.getTitle(),runIndex+"");
log("循环一次任务"); log("循环一次任务");
} catch (Exception e) { } catch (Exception e) {
running = false; running = false;
@ -67,6 +85,32 @@ public class JianRTaskManager {
}).start(); }).start();
} }
private void run(AndroidDevice device, int modelId, JianRScriptV2Data.Script script) {
AndroidDevice.GameDisplay gameDisplay = device.getDeviceDisplay().get(modelId);
Vector2D vector2D = null;
switch (script.getActivity()) {
case JianRScriptV2Data.ScriptModel.MAP:
vector2D = getMapCoords(device, gameDisplay);
break;
case JianRScriptV2Data.ScriptModel.attack:
vector2D = getAttackCoords(device, gameDisplay);
break;
case JianRScriptV2Data.ScriptModel.dialog_go:
vector2D = getDialogCoords(device, gameDisplay, false);
break;
case JianRScriptV2Data.ScriptModel.dialog_back:
vector2D = getDialogCoords(device, gameDisplay, true);
break;
default:
if (script.getActivity().startsWith(JianRScriptV2Data.ScriptModel.formationType)) {
vector2D = getNextFormationCoords(device, gameDisplay, Integer.parseInt(script.getActivity().split("#")[1]));
}
}
if (vector2D != null) {
adb(device, vector2D);
}
}
public void stop() { public void stop() {
task = null; task = null;
running = false; running = false;
@ -80,6 +124,10 @@ public class JianRTaskManager {
return running; return running;
} }
/**
* 老版本脚本执行命令
*/
@Deprecated
private void adbTap(String deviceId, JianRScriptData.Script script) { private void adbTap(String deviceId, JianRScriptData.Script script) {
String exec = String.format("adb -s %s shell input tap %f %f", String exec = String.format("adb -s %s shell input tap %f %f",
deviceId, deviceId,
@ -93,6 +141,7 @@ public class JianRTaskManager {
this.log = String.format("[%s]%s", this.log = String.format("[%s]%s",
AppTools.getToDayNowTimeToString(), AppTools.getToDayNowTimeToString(),
log); log);
System.out.println(this.log);
} }
private float getRandom(int origin, int randomNum, int x) { private float getRandom(int origin, int randomNum, int x) {
@ -106,7 +155,7 @@ public class JianRTaskManager {
public String getTaskName() { public String getTaskName() {
if (task != null) { if (task != null) {
return task.getName(); return task.getTitle();
} }
return "没有任务在运行"; return "没有任务在运行";
} }
@ -114,4 +163,121 @@ public class JianRTaskManager {
public String getLog() { public String getLog() {
return log; return log;
} }
public void connect(String deviceId) {
String exec = String.format("adb connect %s",
deviceId);
System.out.println(exec);
AppTools.exec(exec
, null, false, false);
}
/**
* 获取出击坐标
*
* @param device 设备信息
* @return 坐标
*/
private Vector2D getAttackCoords(AndroidDevice device, AndroidDevice.GameDisplay gameDisplay) {
Vector2D v2d = new Vector2D();
v2d.setX(gameDisplay.getWidth() * 0.8725 + gameDisplay.getStart().getX());
v2d.setY(gameDisplay.getHeight() * 0.9161 + gameDisplay.getStart().getY());
return v2d;
}
/**
* 获取地图点击坐标
*
* @param device 设备信息
* @return 坐标
*/
private Vector2D getMapCoords(AndroidDevice device, AndroidDevice.GameDisplay gameDisplay) {
Vector2D v2d = new Vector2D();
v2d.setX(gameDisplay.getWidth() * 0.5 + gameDisplay.getStart().getX());
v2d.setY(gameDisplay.getHeight() * 0.5 + gameDisplay.getStart().getY());
return v2d;
}
/**
* 获取阵型点击坐标
*
* @param device 设备信息
* @param type 阵型0~4
* @return 坐标
*/
private Vector2D getNextFormationCoords(AndroidDevice device, AndroidDevice.GameDisplay gameDisplay, int type) {
Vector2D v2d = new Vector2D();
v2d.setX(gameDisplay.getWidth() * 0.75 + gameDisplay.getStart().getX());
v2d.setY(gameDisplay.getHeight() * 0.24835 + gameDisplay.getStart().getY());
for (int i = 0; i < type; i++) {
v2d.setY(v2d.getY() + (gameDisplay.getHeight() * 0.1461));
}
return v2d;
}
/**
* 获取出击坐标
*
* @param device 设备信息
* @return 坐标
*/
private Vector2D getDialogCoords(AndroidDevice device, AndroidDevice.GameDisplay gameDisplay, boolean isBack) {
Vector2D v2d = new Vector2D();
v2d.setY(gameDisplay.getHeight() * 0.61722 + gameDisplay.getStart().getY());
if (isBack) {
v2d.setX(gameDisplay.getWidth() * 0.6575 + gameDisplay.getStart().getX());
} else {
v2d.setX(gameDisplay.getWidth() * 0.344642857143 + gameDisplay.getStart().getX());
}
return v2d;
}
public static void main(String[] args) {
AndroidDevice device = new AndroidDevice();
AndroidDevice.DeviceDisplay deviceDisplay = new AndroidDevice.DeviceDisplay();
AndroidDevice.GameDisplay gameDisplay = new AndroidDevice.GameDisplay();
deviceDisplay.setHeight(1800);
deviceDisplay.setWidth(2800);
Vector2D start = new Vector2D();
Vector2D end = new Vector2D();
start.setX(2000);
start.setY(57);
end.setX(2800);
end.setY(581);
gameDisplay.setStart(start);
gameDisplay.setEnd(end);
List<AndroidDevice.GameDisplay> displays = new ArrayList<>();
displays.add(gameDisplay);
device.setDeviceDisplay(displays);
device.setAndroidDevice(deviceDisplay);
device.setDeviceId("192.168.31.142:6666");
device.setTitle("SAMSUNG-TAB-S7+");
JianRTaskManager manager = new JianRTaskManager();
Vector2D mapCoords;
// mapCoords = manager.getMapCoords(device,device.getGameDisplay().get(0));
// mapCoords = manager.getAttackCoords(device,device.getGameDisplay().get(0));
// mapCoords = manager.getNextFormationCoords(device,device.getGameDisplay().get(0), 0);
mapCoords = manager.getDialogCoords(device, device.getDeviceDisplay().get(0), true);
// adb(device, mapCoords);
System.out.println("device = " + device);
String jsonString = "{\"name\":\"1-1Test\",\"script\":[{\"name\":\"点击地图\",\"activity\":\"map\",\"nextWaitTime\":2,\"randomNextWaitTime\":3},{\"name\":\"出击\",\"activity\":\"attack\",\"nextWaitTime\":12,\"randomNextWaitTime\":2},{\"name\":\"战斗\",\"activity\":\"attack\",\"nextWaitTime\":2,\"randomNextWaitTime\":3},{\"name\":\"选择单纵\",\"activity\":\"formationType#0\",\"nextWaitTime\":20,\"randomNextWaitTime\":5},{\"name\":\"结算1\",\"activity\":\"map\",\"nextWaitTime\":2,\"randomNextWaitTime\":3},{\"name\":\"结算2\",\"activity\":\"map\",\"nextWaitTime\":2,\"randomNextWaitTime\":3},{\"name\":\"获取舰娘\",\"activity\":\"map\",\"nextWaitTime\":2,\"randomNextWaitTime\":3},{\"name\":\"前进\",\"activity\":\"dialog_go\",\"nextWaitTime\":12,\"randomNextWaitTime\":3},{\"name\":\"战斗\",\"activity\":\"attack\",\"nextWaitTime\":2,\"randomNextWaitTime\":3},{\"name\":\"选择梯形\",\"activity\":\"formationType#3\",\"nextWaitTime\":20,\"randomNextWaitTime\":5},{\"name\":\"结算1\",\"activity\":\"map\",\"nextWaitTime\":2,\"randomNextWaitTime\":3},{\"name\":\"结算2\",\"activity\":\"map\",\"nextWaitTime\":2,\"randomNextWaitTime\":3},{\"name\":\"获取舰娘\",\"activity\":\"map\",\"nextWaitTime\":2,\"randomNextWaitTime\":3}]}";
JianRScriptV2Data script = JSON.parseObject(jsonString, JianRScriptV2Data.class);
manager.setTask(script,device);
manager.setModelId(0);
manager.start();
}
private void adb(AndroidDevice device, Vector2D v2d) {
String exec = String.format("adb -s %s shell input tap %f %f",
device.getDeviceId(),
v2d.getX(),
v2d.getY());
System.out.println(exec);
AppTools.exec(exec
, null, false, false);
}
} }

View File

@ -16,6 +16,12 @@
<blockquote class="layui-elem-quote">舰R脚本管理器<span id="taskStatus"></span><br/><br/><span id="taskName">当前无任务</span><br/><br/><span id="runIndex"></span><br/><br/><span id="log"></span> <blockquote class="layui-elem-quote">舰R脚本管理器<span id="taskStatus"></span><br/><br/><span id="taskName">当前无任务</span><br/><br/><span id="runIndex"></span><br/><br/><span id="log"></span>
</blockquote> </blockquote>
<button class="layui-btn layui-btn-lg layui-btn-normal" onclick="createTask()">创建任务</button> <button class="layui-btn layui-btn-lg layui-btn-normal" onclick="createTask()">创建任务</button>
<button class="layui-btn layui-btn-lg layui-btn-normal" id="createDevice">选择设备
<i class="layui-icon layui-icon-down layui-font-12"></i>
</button>
<blockquote class="layui-elem-quote">
调用设备:<span id="deviceName"></span><br>
</blockquote>
<br/><br/><br/> <br/><br/><br/>
<div class="layui-bg-gray layui-row layui-col-space15" id="card" style="padding: 30px;"> <div class="layui-bg-gray layui-row layui-col-space15" id="card" style="padding: 30px;">
@ -26,27 +32,25 @@
<script src="layui/layui.js"></script> <script src="layui/layui.js"></script>
<script src="layui/jquery-3.2.1.js"></script> <script src="layui/jquery-3.2.1.js"></script>
<script> <script>
function createCard(name) { let homeUrl="http://127.0.0.1:8002";
let html = '<div class="layui-card layui-col-md3 layui-col-lg-offset1"><div class="layui-card-header">' + name + '</div><div class="layui-card-body"><button class="layui-btn layui-btn-radius layui-btn-normal" onclick="runTask(\'' + name + '\')"><i class="layui-icon">&#xe652;</i>运行</button><br/><br/><button class="layui-btn layui-btn-radius layui-btn-warm" onclick="stopTask()"><i class="layui-icon">&#xe651;</i>停止</button><br/><br/><button class="layui-btn layui-btn-radius layui-btn-danger" onclick="removeTask(\'' + name + '\')"><i class="layui-icon">&#xe64d;</i>删除</button></div></div>'; let device;
$('#card').append(html) let deviceModel=[];
}
function createTask() { function createTask() {
if(device===undefined){
layer.msg('请选择设备')
return
}
layer.prompt({ layer.prompt({
formType: 2, formType: 2,
value: '{\n' + value: '{\n' +
' "name": "",\n' + ' "title":"",\n' +
' "device": "192.168.31.46:6666",\n' + ' "script":[\n' +
' "run": [\n' +
' {\n' + ' {\n' +
' "name": "",\n' + ' "title":"",\n' +
' "activity":"map|attack|formationType#{0-4}|dialog_go|dialog_back",\n' +
' "nextWaitTime": 0,\n' + ' "nextWaitTime": 0,\n' +
' "randomNextWaitTime": 0,\n' + ' "randomNextWaitTime": 0\n' +
' "model": "click",\n' +
' "x": 0,\n' +
' "y": 0,\n' +
' "rx": 0,\n' +
' "ry": 0\n' +
' }\n' + ' }\n' +
' ]\n' + ' ]\n' +
'}', '}',
@ -54,32 +58,61 @@
maxlength: 99999, maxlength: 99999,
area: ['800px', '350px'] //自定义文本域宽高 area: ['800px', '350px'] //自定义文本域宽高
}, function (value, index, elem) { }, function (value, index, elem) {
$.post("/jianr/task/add.do", {task: value}, function (json) { $.post(homeUrl+"/jianr/task/add.do", {task: value}, function (json) {
layer.msg(json.msg) layer.msg(json.msg)
}) })
layer.close(index); layer.close(index);
}); });
} }
function runTask(name) { function runTask(name,title) {
$.post("/jianr/run.do", {task: name}, function (json) { if(device===undefined){
layer.msg('请选择设备')
return
}
$.post(homeUrl+"/jianr/run.do", {task: name,device:device.title,modelName:title}, function (json) {
layer.msg(json.msg) layer.msg(json.msg)
}) })
} }
function stopTask() { function stopTask() {
$.post("/jianr/stop.do", function (json) { $.post(homeUrl+"/jianr/stop.do", function (json) {
layer.msg(json.msg) layer.msg(json.msg)
}) })
} }
function removeTask(name) { function removeTask(name) {
$.post("/jianr/task/remove.do", {task: name}, function (json) { $.post(homeUrl+"/jianr/task/remove.do", {task: name}, function (json) {
layer.msg(json.msg) layer.msg(json.msg)
}) })
} }
function showDevice() {
if(isSelectDevice()){
layer.msg("当前未选择设备")
}else{
layer.prompt({
formType: 2,
value: JSON.stringify(device),
title: '查看设备',
maxlength: 99999,
area: ['800px', '350px'] //自定义文本域宽高
}, function (value, index, elem) {
$.post(homeUrl+"/jianr/device/edit.do", {device: value,oldDevice:device.title}, function (json) {
layer.msg(json.msg)
})
layer.close(index);
});
}
}
function delDevice() {
}
function isSelectDevice() {
return device===undefined;
}
function getStatus() { function getStatus() {
$.post("/jianr/status.do", function (json) { $.post(homeUrl+"/jianr/status.do", function (json) {
if(json.data.status){ if(json.data.status){
$('#taskStatus')[0].innerHTML="运行" $('#taskStatus')[0].innerHTML="运行"
$('#taskStatus').css('color','#5C962C') $('#taskStatus').css('color','#5C962C')
@ -90,17 +123,128 @@
$('#taskStatus')[0].innerHTML="停止" $('#taskStatus')[0].innerHTML="停止"
$('#taskStatus').css('color','#FF5722') $('#taskStatus').css('color','#FF5722')
} }
setTimeout(function () {
getStatus() getStatus()
},1000)
}) })
} }
window.onload = function init() { layui.use('dropdown', function(){
$.get("/jianr/task/list.do", function (json) { let dropdown = layui.dropdown
function createCard(name) {
let html = '<div class="layui-card layui-col-md3 layui-col-lg-offset1"><div class="layui-card-header">' + name
+ '</div><div class="layui-card-body">' +
'<button class="layui-btn layui-btn-radius layui-btn-normal" id="runTask_'+name+'" ><i class="layui-icon">&#xe652;</i>运行 <i class="layui-icon layui-icon-down layui-font-12"></i></button><br/><br/><button class="layui-btn layui-btn-radius layui-btn-warm" onclick="stopTask()"><i class="layui-icon">&#xe651;</i>停止</button><br/><br/><button class="layui-btn layui-btn-radius layui-btn-danger" onclick="removeTask(\'' + name + '\')"><i class="layui-icon">&#xe64d;</i>删除</button></div></div>';
$('#card').append(html)
dropdown.render({
elem: '#runTask_'+name //可绑定在任意元素中,此处以上述按钮为例
,data: deviceModel
,id: 'runTask_'+name
//菜单被点击的事件
,click: function(obj){
console.log(obj);
runTask(name,obj.title)
}
});
}
$.get(homeUrl+"/jianr/task/list.do", function (json) {
json.data.forEach(function (item) { json.data.forEach(function (item) {
createCard(item) createCard(item)
} }
) )
}) })
$.get(homeUrl+"/jianr/device/list.do",function (json) {
let devices=json.data;
devices.push({type:'-'})
devices.push({
title: '添加设备',
name:"addDevice"
,href: '#'
})
dropdown.render({
elem: '#createDevice' //可绑定在任意元素中,此处以上述按钮为例
,data: devices
,id: 'createDevice'
//菜单被点击的事件
,click: function(obj){
if(obj.name==='addDevice'){
layer.prompt({
formType: 2,
value: '{\n' +
' "deviceId":"",\n' +
' "title":"",\n' +
' "androidDevice":{\n' +
' "height":0,\n' +
' "width":0\n' +
' },\n' +
' "deviceDisplay":[\n' +
' {\n' +
' "title":"",\n' +
' "start":{\n' +
' "x":0,\n' +
' "y":0,\n' +
' "enableRandomX":false,\n' +
' "enableRandomY":false,\n' +
' "absRandomX":false,\n' +
' "absRandomY":false,\n' +
' "randomNumX":0,\n' +
' "randomNumY":0\n' +
' },\n' +
' "end":{\n' +
' "x": 0,\n' +
' "y": 0,\n' +
' "enableRandomX": false,\n' +
' "enableRandomY": false,\n' +
' "absRandomX": false,\n' +
' "absRandomY": false,\n' +
' "randomNumX": 0,\n' +
' "randomNumY": 0\n' +
' }\n' +
' }\n' +
' ]\n' +
'}',
title: '添加设备',
maxlength: 99999,
area: ['800px', '350px'] //自定义文本域宽高
}, function (value, index, elem) {
$.post(homeUrl+"/jianr/device/add.do", {device: value}, function (json) {
layer.msg(json.msg)
})
layer.close(index);
});
}else{
console.log(obj)
if(obj.data==='select'){
device=obj.device;
device.deviceDisplay.forEach(function (item){
deviceModel.push({
title:item.title
})
})
$('#deviceName')[0].innerHTML=obj.device.title;
}else if (obj.data==='edit'){
showDevice()
}else if (obj.data==='del'){
delDevice()
}else if (obj.data==='connect'){
$.post(homeUrl+"/jianr/device/connect.do",{device:obj.device.deviceId},function (json) {
layer.msg(json.msg)
})
}
}
}
});
})
});
window.onload = function init() {
getStatus() getStatus()
} }
</script> </script>