diff --git a/main.js b/main.js
index 6b7dded..1358051 100644
--- a/main.js
+++ b/main.js
@@ -1,129 +1,136 @@
-"ui";
-
storage = storages.create("mower-ng helper");
-const config_items = {
- wx: 10,
- wy: 10,
- http_proto: "http",
- host: "127.0.0.1",
- port: "58000",
- auto_close: false,
-};
+wx = storage.get("wx", 10);
+wy = storage.get("wy", 10);
+http_proto = storage.get("http_proto", "http");
+host = storage.get("host", "127.0.0.1:58000");
-config = {};
-
-Object.entries(config_items).forEach(([key, value]) => {
- config[key + "_"] = storage.get(key, value);
- Object.defineProperty(config, key, {
- get() {
- return this[key + "_"];
- },
- set(new_value) {
- this[key + "_"] = new_value;
- storage.put(key, new_value);
- },
- });
-});
-Object.defineProperties(config, {
- scheduler_url: {
- get() {
- return `${this.http_proto}://${this.host}:${this.port}/scheduler`;
- },
- },
- ws_url: {
- get() {
- ws_proto = this.http_proto == "https" ? "wss" : "ws";
- return `${ws_proto}://${this.host}:${this.port}/ws`;
- },
- },
-});
-
-function save_config() {
- config.http_proto = ui.proto.getText();
- config.host = ui.host.getText();
- config.port = ui.port.getText();
- config.auto_close = ui.auto_close.isChecked();
-}
-
-w = null;
-
-ui.layout(
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+rawInput(
+ "连接地址(要写http://或https://,不写token)",
+ `${http_proto}://${host}`,
+ (url) => {
+ [http_proto, host] = url.split("://");
+ storage.put("http_proto", http_proto);
+ storage.put("host", host);
+ }
);
-ui.proto.setText(config.http_proto);
-ui.host.setText(config.host);
-ui.port.setText(config.port);
-ui.auto_close.setChecked(config.auto_close);
+w = floaty.rawWindow(
+
+
+
+
+
+
+
+
+
+
+
+);
+w.setSize(-2, -2);
+w.exitOnClose();
-function toggle_ui(running) {
- group_waiting = [ui.proto, ui.host, ui.port, ui.start];
- group_running = [ui.stop];
- if (running) {
- to_enable = group_running;
- to_disable = group_waiting;
- } else {
- to_enable = group_waiting;
- to_disable = group_running;
+w.setPosition(wx, wy);
+
+w.container.setOnTouchListener((view, event) => {
+ switch (event.getAction()) {
+ case event.ACTION_DOWN:
+ X = event.getRawX();
+ Y = event.getRawY();
+ return true;
+ case event.ACTION_MOVE:
+ dx = event.getRawX() - X;
+ dy = event.getRawY() - Y;
+ w.setPosition(wx + dx, wy + dy);
+ return true;
+ case event.ACTION_UP:
+ wx += dx;
+ wy += dy;
+ storage.put("wx", wx);
+ storage.put("wy", wy);
+ return true;
}
- for (i of to_enable) {
- i.setEnabled(true);
- }
- for (i of to_disable) {
- i.setEnabled(false);
- }
-}
+});
-toggle_ui(false);
+w.exit.click(() => {
+ exit();
+});
-ws = null;
-reg = /^[0-9].*/;
-log = [];
-
-no_time = "--:--:--";
-scheduler = { idle: false, next_time: null };
-
-client = new OkHttpClient.Builder().build();
+w.logo.click(() => {
+ ui.run(() => {
+ visibility = w.log.visibility == 8 ? 0 : 8;
+ w.log.visibility = visibility;
+ w.container.alpha = w.log.visibility == 8 ? 0.6 : 0.8;
+ });
+});
function pad_num(n) {
return String(n).padStart(2, "0");
}
function format_time(d) {
- hh = pad_num(d.getHours());
- mm = pad_num(d.getMinutes());
- ss = pad_num(d.getSeconds());
+ hh = pad_num(d.getUTCHours());
+ mm = pad_num(d.getUTCMinutes());
+ ss = pad_num(d.getUTCSeconds());
return `${hh}:${mm}:${ss}`;
}
+axios = require("axios");
+
+no_time = "--:--:--";
+calculate_remain = false;
+
+function update_tasks() {
+ Promise.all([
+ axios.get(`${http_proto}://${host}/idle`),
+ axios.get(`${http_proto}://${host}/scheduler`),
+ ]).then(([resp1, resp2]) => {
+ idle = resp1.data;
+ task_list = resp2.data;
+ if (idle == "True" && task_list.length > 0) {
+ task_time = new Date(task_list[0].time.substring(0, 19));
+ display_time = format_time(task_time);
+ calculate_remain = true;
+ } else {
+ display_time = no_time;
+ calculate_remain = false;
+ }
+ ui.run(() => {
+ w.next.setText(`下次任务:${display_time}`);
+ });
+ });
+ setTimeout(update_tasks, 5000);
+}
+update_tasks();
+
function time_diff(diff) {
if (diff < 0) {
return no_time;
@@ -139,177 +146,47 @@ function time_diff(diff) {
return `${hh}:${mm}:${ss}`;
}
-function update_tasks() {
- if (w) {
- request = new Request.Builder().url(config.scheduler_url).build();
- response = client.newCall(request).execute();
- scheduler = JSON.parse(response.body().string());
- setTimeout(update_tasks, 5000);
- }
-}
-
-function stop_ws() {
- if (ws) {
- ws.cancel();
- ws = null;
- }
-}
-
-function stop_floaty() {
- if (w) {
- w.close();
- w = null;
- }
- scheduler = { idle: false, next_time: null };
- stop_ws();
- clearInterval(w_timer);
- toggle_ui(false);
- return;
-}
-
-ui.stop.click(() => {
- if (w) {
- stop_floaty();
- }
-});
-
-ui.start.click(() => {
- toggle_ui(true);
-
- save_config();
-
- w = floaty.rawWindow(
-
-
-
-
-
-
-
-
-
-
-
- );
-
- w.setSize(-2, -2);
- w.setPosition(config.wx, config.wy);
-
- w.container.setOnTouchListener((view, event) => {
- switch (event.getAction()) {
- case event.ACTION_DOWN:
- X = event.getRawX();
- Y = event.getRawY();
- return true;
- case event.ACTION_MOVE:
- dx = event.getRawX() - X;
- dy = event.getRawY() - Y;
- w.setPosition(config.wx + dx, config.wy + dy);
- return true;
- case event.ACTION_UP:
- config.wx += dx;
- config.wy += dy;
- return true;
- }
+setInterval(() => {
+ ui.run(() => {
+ display_time = calculate_remain
+ ? time_diff(
+ task_time.getTime() +
+ task_time.getTimezoneOffset() * 60000 -
+ Date.now()
+ )
+ : no_time;
+ w.remain.setText(`剩余时间:${display_time}`);
});
+}, 1000);
- w.logo.click(() => {
- ui.run(() => {
- visibility = ws ? 8 : 0;
- w.log.visibility = visibility;
- if (ws) {
- stop_ws();
- return;
+ws_proto = http_proto == "https" ? "wss" : "ws";
+log = [];
+
+importPackage(Packages["okhttp3"]);
+client = new OkHttpClient.Builder().retryOnConnectionFailure(true).build();
+request = new Request.Builder().url(`${ws_proto}://${host}/ws`).build();
+client.dispatcher().cancelAll();
+
+reg = /^[0-9].*/;
+
+client.newWebSocket(
+ request,
+ new WebSocketListener({
+ onMessage: function (ws, data) {
+ data = JSON.parse(data);
+ if (data.type == "log") {
+ for (line of data.data.split("\n")) {
+ if (line.match(reg)) {
+ log.push(line.substring(6));
+ } else {
+ log.push(line);
+ }
+ }
}
- request = new Request.Builder().url(config.ws_url).build();
- ws = client.newWebSocket(
- request,
- new okhttp3.WebSocketListener({
- onMessage: function (ws, data) {
- data = JSON.parse(data);
- if (data.type == "log") {
- for (line of data.data.split("\n")) {
- if (line.match(reg)) {
- log.push(line.substring(6));
- } else {
- log.push(line);
- }
- }
- }
- log = log.slice(-5);
- ui.run(() => {
- w.log.setText(log.join("\n"));
- });
- },
- })
- );
- });
- });
-
- w.exit.click(() => {
- stop_floaty();
- });
-
- w_timer = setInterval(() => {
- if (scheduler.idle && scheduler.next_time) {
- task_time = new Date(scheduler.next_time);
- next_time = format_time(task_time);
- remain_time = time_diff(task_time.getTime() - Date.now());
- } else {
- next_time = no_time;
- remain_time = no_time;
- }
- if (config.auto_close && remain_time == no_time) {
- stop_floaty();
- return;
- }
- ui.run(() => {
- w.next.setText(`下次任务:${next_time}`);
- w.remain.setText(`剩余时间:${remain_time}`);
- });
- }, 1000);
-
- threads.start(() => {
- try {
- update_tasks();
- } catch {
+ log = log.slice(-5);
ui.run(() => {
- stop_floaty();
- alert("网络连接错误", "请检查设置填写是否正确!");
+ w.log.setText(log.join("\n"));
});
- }
- });
-});
-
-events.on("exit", () => {
- save_config();
- stop_floaty();
-});
+ },
+ })
+);
diff --git a/project.json b/project.json
index 49ba9b2..6250a08 100644
--- a/project.json
+++ b/project.json
@@ -1,20 +1,44 @@
{
- "abis": [],
- "assets": [],
+ "abis": ["arm64-v8a", "armeabi-v7a", "x86", "x86_64"],
+ "assets": [
+ {
+ "form": "/storage/emulated/0/脚本/helper_script",
+ "to": "/project"
+ }
+ ],
+ "buildDir": "build",
"build": {
+ "build_id": null,
"build_number": 0,
"build_time": 0
},
"useFeatures": [],
"icon": "logo.png",
+ "ignoredDirs": ["build"],
"launchConfig": {
- "hideLogs": false
+ "displaySplash": false,
+ "hideAccessibilityServices": false,
+ "hideLauncher": false,
+ "hideLogs": true,
+ "stableMode": false,
+ "volumeUpcontrol": false,
+ "permissions": ["draw_overlay"],
+ "serviceDesc": "使脚本自动操作(点击、长按、滑动等)所需,若关闭则只能执行不涉及自动操作的脚本。",
+ "splashIcon": "logo.png",
+ "splashText": "mower-ng helper"
},
- "libs": [],
+ "libs": ["libjackpal-androidterm5.so", "libjackpal-termexec2.so"],
"main": "main.js",
"name": "mower-ng helper",
+ "outputPath": "/storage/emulated/0/脚本/helper_script/build",
"packageName": "vip.zhaozuohong.mowernghelper",
+ "projectDirectory": "/storage/emulated/0/脚本/helper_script",
"scripts": {},
- "versionCode": 2,
- "versionName": "1.3.0"
+ "signingConfig": {
+ "alias": "autoxjs6",
+ "keystore": "/storage/emulated/0/脚本/.keyStore/AutoX.keystore"
+ },
+ "sourcePath": "/storage/emulated/0/脚本/helper_script/main.js",
+ "versionCode": 1,
+ "versionName": "1.0.0"
}