helper/main.js

277 lines
6.2 KiB
JavaScript

"ui";
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");
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);
}
w = null;
ui.layout(
<vertical gravity="center">
<horizontal gravity="center" margin="0 0 0 6">
<img id="logo" w="64" h="64" src="file://logo.png" />
<vertical marginLeft="8">
<text text="mower-ng helper" textColor="black" textSize="22sp" />
<text text="v1.1.0" />
</vertical>
</horizontal>
<horizontal gravity="center">
<text text="proto" w="40" />
<input id="proto" text="http" w="200" />
</horizontal>
<horizontal gravity="center">
<text text="host" w="40" />
<input id="host" text="127.0.0.1" w="200" />
</horizontal>
<horizontal gravity="center">
<text text="port" w="40" />
<input id="port" text="58000" w="200" />
</horizontal>
<horizontal gravity="center" margin="0 6 0 0">
<button w="130" id="start" text="开启悬浮窗" />
<button w="130" id="stop" text="关闭悬浮窗" />
</horizontal>
</vertical>
);
ui.proto.setText(http_proto);
ui.host.setText(host);
ui.port.setText(port);
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;
}
for (i of to_enable) {
i.setEnabled(true);
}
for (i of to_disable) {
i.setEnabled(false);
}
}
toggle_ui(false);
ws = null;
ws_proto = http_proto == "https" ? "wss" : "ws";
reg = /^[0-9].*/;
log = [];
no_time = "--:--:--";
scheduler = { idle: false, next_time: null };
client = new OkHttpClient.Builder().build();
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());
return `${hh}:${mm}:${ss}`;
}
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}`;
}
function update_tasks() {
if (w) {
request = new Request.Builder()
.url(`${http_proto}://${host}:${port}/scheduler`)
.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;
}
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(
<vertical id="container" bg="#000000" padding="8 6 8 6" alpha="0.8">
<horizontal gravity="center_vertical">
<img id="logo" w="36" h="36" src="file://logo.png" />
<vertical margin="6 0 6 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"
margin="0 0 2 0"
src="file://stop.png"
tint="#FFFFFF"
/>
</horizontal>
<text
id="log"
gravity="bottom"
line="5"
maxLines="8"
ems="10"
textColor="#FFFFFF"
typeface="monospace"
marginTop="6"
textSize="12"
visibility="gone"
/>
</vertical>
);
w.setSize(-2, -2);
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.logo.click(() => {
ui.run(() => {
visibility = ws ? 8 : 0;
w.log.visibility = visibility;
if (ws) {
stop_ws();
return;
}
request = new Request.Builder()
.url(`${ws_proto}://${host}:${port}/ws`)
.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.substring(0, 19));
next_time = format_time(task_time);
remain_time = time_diff(task_time.getTime() - Date.now());
} else {
next_time = no_time;
remain_time = no_time;
}
ui.run(() => {
w.next.setText(`下次任务:${next_time}`);
w.remain.setText(`剩余时间:${remain_time}`);
});
}, 1000);
threads.start(() => {
update_tasks();
});
});
events.on("exit", () => {
save_config();
stop_floaty();
});