helper/main.js

193 lines
4.3 KiB
JavaScript
Raw Normal View History

2025-01-04 00:02:21 +08:00
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:58000");
rawInput(
"连接地址(要写http://或https://,不写token)",
`${http_proto}://${host}`,
(url) => {
[http_proto, host] = url.split("://");
storage.put("http_proto", http_proto);
storage.put("host", host);
}
);
w = floaty.rawWindow(
<vertical id="container" bg="#000000" padding="8 6 8 6" alpha="0.6">
<horizontal gravity="center_vertical">
<img id="logo" w="36" h="36" src="file://./logo.png" />
<vertical margin="8 0 8 0">
<text
textColor="#FFFFFF"
id="next"
typeface="monospace"
text="下次任务:--:--:--"
/>
<text
textColor="#FFFFFF"
id="remain"
typeface="monospace"
text="剩余时间:--:--:--"
/>
</vertical>
<img
w="24"
h="24"
id="exit"
marginLeft="4"
src="file://./stop.png"
tint="#FFFFFF"
/>
</horizontal>
<text
id="log"
gravity="bottom"
h="120"
textColor="#FFFFFF"
typeface="monospace"
ems="10"
marginTop="6"
textSize="12"
visibility="gone"
/>
</vertical>
);
w.setSize(-2, -2);
w.exitOnClose();
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;
}
});
w.exit.click(() => {
exit();
});
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.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;
}
hh = Math.floor(diff / (3600 * 1000));
diff %= 3600 * 1000;
mm = Math.floor(diff / (60 * 1000));
diff %= 60 * 1000;
ss = Math.floor(diff / 1000);
hh = pad_num(hh);
mm = pad_num(mm);
ss = pad_num(ss);
return `${hh}:${mm}:${ss}`;
}
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);
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);
}
}
}
log = log.slice(-5);
ui.run(() => {
w.log.setText(log.join("\n"));
});
},
})
);