新增舰R脚本

This commit is contained in:
Yutousama 2022-07-25 12:35:50 +08:00
parent c36044e6e4
commit 30fda14264
4 changed files with 389 additions and 0 deletions

View File

@ -0,0 +1,115 @@
package com.yutou.qqbot.Controllers;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.qqbot.data.JianRScriptData;
import com.yutou.qqbot.utlis.JianRTaskManager;
import com.yutou.qqbot.utlis.RedisTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Set;
@Controller
public class JianRScriptController {
@RequestMapping("/jianr/run.do")
@ResponseBody
public JSONObject runScript(String task) {
JSONObject json = new JSONObject();
String data = RedisTools.get("jianrScript_Model_" + task);
if (data == null) {
json.put("code", -1);
json.put("msg", "没有找到该方案,请配置");
} else {
JianRScriptData script = JSON.parseObject(data, JianRScriptData.class);
JianRTaskManager manager = JianRTaskManager.getInstance();
if (manager.isRunning()) {
manager.stop();
}
manager.setTask(script);
manager.start();
manager.start();
json.put("code", 0);
json.put("msg", "任务创建成功");
}
return json;
}
@RequestMapping("/jianr/stop.do")
@ResponseBody
public JSONObject stop(){
JianRTaskManager.getInstance().stop();
JSONObject json = new JSONObject();
json.put("code", 0);
json.put("msg", "任务已停止");
return json;
}
@RequestMapping("/jianr/status.do")
@ResponseBody
public JSONObject status() {
JSONObject json = new JSONObject();
JSONObject data = new JSONObject();
JianRTaskManager manager = JianRTaskManager.getInstance();
data.put("status", manager.isRunning());
data.put("runIndex", manager.getRunIndex());
data.put("taskName", manager.getTaskName());
data.put("log", manager.getLog());
json.put("code", 0);
json.put("data", data);
return json;
}
@RequestMapping("/jianr/task/list.do")
@ResponseBody
public JSONObject tasks() {
JSONObject json = new JSONObject();
JSONArray array = new JSONArray();
Set<String> jianrScript_model = RedisTools.list_get("jianrScript_Model");
array.addAll(jianrScript_model);
json.put("code", 0);
json.put("data", array);
return json;
}
@RequestMapping("/jianr/task/add.do")
@ResponseBody
public JSONObject addTask(String task) {
JSONObject json = new JSONObject();
JSONObject data = JSON.parseObject(task);
if (data == null) {
json.put("code", -1);
json.put("msg", "JSON格式错误");
} else {
String taskName = data.getString("name");
if (RedisTools.list_isExist("jianrScript_Model", taskName)) {
json.put("code", -1);
json.put("msg", "该任务名称已存在");
} else {
RedisTools.list_add("jianrScript_Model", taskName);
RedisTools.set("jianrScript_Model_"+taskName,task);
json.put("code", 0);
json.put("msg", "任务创建成功");
}
}
return json;
}
@RequestMapping("/jianr/task/get.do")
@ResponseBody
public JSONObject getTask (String task){
JSONObject json = new JSONObject();
json.put("code", 0);
json.put("data", RedisTools.get("jianrScript_Model_" + task));
return json;
}
@RequestMapping("/jianr/task/remove.do")
@ResponseBody
public JSONObject removeTask (String task){
RedisTools.remove("jianrScript_Model_" + task);
RedisTools.list_remove("jianrScript_Model", task);
JSONObject json = new JSONObject();
json.put("code", 0);
json.put("msg", "任务删除成功");
return json;
}
}

View File

@ -0,0 +1,38 @@
package com.yutou.qqbot.data;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
public class JianRScriptData {
private String name;
private String device;
private List<Script> run;
public void addRun(Script script) {
if(run==null){
run=new ArrayList<>();
}
run.add(script);
}
@Data
public static class Script {
private String name;
private int nextWaitTime;
private int randomNextWaitTime;
private String model;
private int x;
private int y;
private int rx;
private int ry;
public static class ScriptModel {
public static final String CLICK = "click";
public static final String WAIT = "wait";
}
}
}

View File

@ -0,0 +1,113 @@
package com.yutou.qqbot.utlis;
import com.yutou.qqbot.data.JianRScriptData;
import java.util.Random;
public class JianRTaskManager {
private static JianRTaskManager instance;
private JianRScriptData task;
private boolean running = false;
private int runIndex = 0;
private String log;
public static JianRTaskManager getInstance() {
if (instance == null) {
instance = new JianRTaskManager();
}
return instance;
}
private JianRTaskManager() {
}
public void setTask(JianRScriptData data) {
task = data;
String s = RedisTools.get("jianrScript_RunIndex_" + data.getName());
if (s != null) {
runIndex = Integer.parseInt(s);
}
}
public void start() {
running = true;
new Thread(new Runnable() {
@Override
public void run() {
while (running) {
try {
log("已运行: " + runIndex + "");
for (JianRScriptData.Script script : task.getRun()) {
log(script.getName());
if (script.getModel().equals(JianRScriptData.Script.ScriptModel.CLICK)) {
adbTap(task.getDevice(), script);
}
try {
Thread.sleep((long) getRandom(0, script.getRandomNextWaitTime(), script.getNextWaitTime()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
runIndex++;
RedisTools.set("jianrScript_RunIndex_" + task.getName(), runIndex + "");
log("循环一次任务");
} catch (Exception e) {
running = false;
}
}
log("终止任务");
}
}).start();
}
public void stop() {
task = null;
running = false;
}
public int getRunIndex() {
return runIndex;
}
public boolean isRunning() {
return running;
}
private void adbTap(String deviceId, JianRScriptData.Script script) {
String exec = String.format("adb -s %s shell input tap %f %f",
deviceId,
getRandom(script.getRx(), script.getX()),
getRandom(script.getRy(), script.getY()));
AppTools.exec(exec
, null, false, false);
}
private void log(String log) {
this.log = String.format("[%s]%s",
AppTools.getToDayNowTimeToString(),
log);
}
private float getRandom(int origin, int randomNum, int x) {
Random random = new Random();
return x + random.nextFloat(origin, randomNum);
}
private float getRandom(int randomNum, int x) {
return getRandom(-randomNum, randomNum, x);
}
public String getTaskName() {
if (task != null) {
return task.getName();
}
return "没有任务在运行";
}
public String getLog() {
return log;
}
}

123
web/jianrTask.html Normal file
View File

@ -0,0 +1,123 @@
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>开门</title>
<link rel="stylesheet" href="layui/css/layui.css">
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
</head>
<body>
<div class="myDiy">
<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>
<button class="layui-btn layui-btn-lg layui-btn-normal" onclick="createTask()">创建任务</button>
<br/><br/><br/>
<div class="layui-bg-gray layui-row layui-col-space15" id="card" style="padding: 30px;">
</div>
</div>
</body>
<script src="layui/layui.js"></script>
<script src="layui/jquery-3.2.1.js"></script>
<script>
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" 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>';
$('#card').append(html)
}
function createTask() {
layer.prompt({
formType: 2,
value: '{\n' +
' "name": "",\n' +
' "device": "192.168.31.46:6666",\n' +
' "run": [\n' +
' {\n' +
' "name": "",\n' +
' "nextWaitTime": 0,\n' +
' "randomNextWaitTime": 0,\n' +
' "model": "click",\n' +
' "x": 0,\n' +
' "y": 0,\n' +
' "rx": 0,\n' +
' "ry": 0\n' +
' }\n' +
' ]\n' +
'}',
title: '创建任务',
maxlength: 99999,
area: ['800px', '350px'] //自定义文本域宽高
}, function (value, index, elem) {
$.post("http://127.0.0.1:8002/jianr/task/add.do", {task: value}, function (json) {
layer.msg(json.msg)
})
layer.close(index);
});
}
function runTask(name) {
$.post("http://127.0.0.1:8002/jianr/run.do", {task: name}, function (json) {
layer.msg(json.msg)
})
}
function stopTask() {
$.post("http://127.0.0.1:8002/jianr/stop.do", function (json) {
layer.msg(json.msg)
})
}
function removeTask(name) {
$.post("http://127.0.0.1:8002/jianr/task/remove.do", {task: name}, function (json) {
layer.msg(json.msg)
})
}
function getStatus() {
$.post("http://127.0.0.1:8002/jianr/status.do", function (json) {
if(json.data.status){
$('#taskStatus')[0].innerHTML="运行"
$('#taskStatus').css('color','#5C962C')
$('#taskName')[0].innerHTML='执行次数: '+json.data.runIndex;
$('#runIndex')[0].innerHTML='当前正在执行: '+json.data.taskName;
$('#log')[0].innerHTML=json.data.log
}else{
$('#taskStatus')[0].innerHTML="停止"
$('#taskStatus').css('color','#FF5722')
}
getStatus()
})
}
window.onload = function init() {
$.get("http://127.0.0.1:8002/jianr/task/list.do", function (json) {
json.data.forEach(function (item) {
createCard(item)
}
)
})
getStatus()
}
</script>
<style>
.myDiy {
width: 40%;
height: 300px;
margin-top: 10px;
margin-left: 25%;
}
.button {
width: 100px;
height: 100px;
font-size: 1em;
}
</style>
</html>