Handle exit and make interval configurable

This commit is contained in:
Daniel Bulant 2020-07-09 10:33:43 +02:00
parent 64028cd91c
commit 493ddc123c

View file

@ -1,21 +1,24 @@
import AutoPilot from 'https://deno.land/x/autopilot/mod.ts'; import AutoPilot from 'https://deno.land/x/autopilot/mod.ts';
import { parse } from "https://deno.land/std/flags/mod.ts"; import { parse } from "https://deno.land/std/flags/mod.ts";
import { signal } from "https://deno.land/std/signal/mod.ts";
const args = parse(Deno.args, { const args = parse(Deno.args, {
alias: { alias: {
inactivity: "a",
interval: "i", interval: "i",
start: "s", start: "s",
stop: "p" stop: "p"
}, },
default: { default: {
interval: 300000 interval: 10000,
start: 0,
stop: 24,
inactivity: 300000
} }
}); });
const pilot = new AutoPilot(); const pilot = new AutoPilot();
pilot.notify("Do not sleep", "Do not sleep is now running!");
var last = { var last = {
x: 0, x: 0,
y: 0 y: 0
@ -30,14 +33,41 @@ setInterval(() =>{
wasActive = false; wasActive = false;
return; return;
} }
if(!wasActive) {
pilot.notify("Do not sleep", "Do not sleep is now active");
}
wasActive = true; wasActive = true;
var pos = pilot.mousePosition(); var pos = pilot.mousePosition();
if(pos.x !== last.x && pos.y !== last.y) { if(pos.x !== last.x && pos.y !== last.y) {
lastUpdate = Date.now(); lastUpdate = Date.now();
} else { } else {
if(Date.now() - lastUpdate > args.interval) { // 5minutes if(Date.now() - lastUpdate > args.inactivity) { // 5minutes
pilot.moveMouse(pos.x + 1, pos.y + 1); pilot.moveMouse(pos.x + 1, pos.y + 1);
pilot.notify("Do not sleep", "Your mouse was moved just now."); pilot.notify("Do not sleep", "Your mouse was moved just now.");
} }
} }
}, 10000); }, args.interval);
console.log("Ready");
const exit = () => {
console.log('Goodbye!');
pilot.notify("Do not sleep", "Do not sleep stopped");
};
const sig = signal(
Deno.Signal.SIGINT
);
window.addEventListener("unload", exit);
function never() {
return new Promise(async () => {
for await (const _ of sig) {
exit();
Deno.exit();
}
});
}
await never();