diff --git a/main.js b/main.js
index ab771de..6b7dded 100644
--- a/main.js
+++ b/main.js
@@ -2,19 +2,48 @@
storage = storages.create("mower-ng helper");
-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");
-port = storage.get("port", "58000");
+const config_items = {
+ wx: 10,
+ wy: 10,
+ http_proto: "http",
+ host: "127.0.0.1",
+ port: "58000",
+ auto_close: false,
+};
+
+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() {
- http_proto = ui.proto.getText();
- host = ui.host.getText();
- port = ui.port.getText();
- storage.put("http_proto", http_proto);
- storage.put("host", host);
- storage.put("port", port);
+ 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;
@@ -25,7 +54,7 @@ ui.layout(
-
+
@@ -40,16 +69,20 @@ ui.layout(
-
-
-
+
+
+
+
+
+
);
-ui.proto.setText(http_proto);
-ui.host.setText(host);
-ui.port.setText(port);
+ui.proto.setText(config.http_proto);
+ui.host.setText(config.host);
+ui.port.setText(config.port);
+ui.auto_close.setChecked(config.auto_close);
function toggle_ui(running) {
group_waiting = [ui.proto, ui.host, ui.port, ui.start];
@@ -72,7 +105,6 @@ function toggle_ui(running) {
toggle_ui(false);
ws = null;
-ws_proto = http_proto == "https" ? "wss" : "ws";
reg = /^[0-9].*/;
log = [];
@@ -109,9 +141,7 @@ function time_diff(diff) {
function update_tasks() {
if (w) {
- request = new Request.Builder()
- .url(`${http_proto}://${host}:${port}/scheduler`)
- .build();
+ request = new Request.Builder().url(config.scheduler_url).build();
response = client.newCall(request).execute();
scheduler = JSON.parse(response.body().string());
setTimeout(update_tasks, 5000);
@@ -130,6 +160,7 @@ function stop_floaty() {
w.close();
w = null;
}
+ scheduler = { idle: false, next_time: null };
stop_ws();
clearInterval(w_timer);
toggle_ui(false);
@@ -190,7 +221,7 @@ ui.start.click(() => {
);
w.setSize(-2, -2);
- w.setPosition(wx, wy);
+ w.setPosition(config.wx, config.wy);
w.container.setOnTouchListener((view, event) => {
switch (event.getAction()) {
@@ -201,13 +232,11 @@ ui.start.click(() => {
case event.ACTION_MOVE:
dx = event.getRawX() - X;
dy = event.getRawY() - Y;
- w.setPosition(wx + dx, wy + dy);
+ w.setPosition(config.wx + dx, config.wy + dy);
return true;
case event.ACTION_UP:
- wx += dx;
- wy += dy;
- storage.put("wx", wx);
- storage.put("wy", wy);
+ config.wx += dx;
+ config.wy += dy;
return true;
}
});
@@ -220,9 +249,7 @@ ui.start.click(() => {
stop_ws();
return;
}
- request = new Request.Builder()
- .url(`${ws_proto}://${host}:${port}/ws`)
- .build();
+ request = new Request.Builder().url(config.ws_url).build();
ws = client.newWebSocket(
request,
new okhttp3.WebSocketListener({
@@ -253,13 +280,17 @@ ui.start.click(() => {
w_timer = setInterval(() => {
if (scheduler.idle && scheduler.next_time) {
- task_time = new Date(scheduler.next_time.substring(0, 19));
+ 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}`);
@@ -267,7 +298,14 @@ ui.start.click(() => {
}, 1000);
threads.start(() => {
- update_tasks();
+ try {
+ update_tasks();
+ } catch {
+ ui.run(() => {
+ stop_floaty();
+ alert("网络连接错误", "请检查设置填写是否正确!");
+ });
+ }
});
});
diff --git a/project.json b/project.json
index b841139..49ba9b2 100644
--- a/project.json
+++ b/project.json
@@ -16,5 +16,5 @@
"packageName": "vip.zhaozuohong.mowernghelper",
"scripts": {},
"versionCode": 2,
- "versionName": "1.1.0"
+ "versionName": "1.3.0"
}