BangleApps/apps/qalarm/qalarmcheck.js

42 lines
963 B
JavaScript
Raw Normal View History

2021-10-27 16:54:06 +00:00
/**
* This file checks for upcoming alarms and schedules qalarm.js to deal with them and itself to continue doing these checks.
*/
print("Checking for alarms...");
clearInterval();
function getCurrentTime() {
let time = new Date();
return (
time.getHours() * 3600000 +
time.getMinutes() * 60000 +
time.getSeconds() * 1000
);
}
let time = new Date();
let t = getCurrentTime();
let nextAlarms = (require("Storage").readJSON("qalarm.json", 1) || [])
.filter(
(alarm) =>
alarm.on &&
alarm.t > t &&
alarm.last != time.getDate() &&
(alarm.timer || alarm.daysOfWeek[time.getDay()])
)
.sort((a, b) => a.t - b.t);
if (nextAlarms[0]) {
setTimeout(() => {
eval(require("Storage").read("qalarmcheck.js"));
2021-10-28 14:00:43 +00:00
load("qalarm.js");
}, nextAlarms[0].t - t);
2021-10-27 16:54:06 +00:00
} else {
2021-10-28 14:00:43 +00:00
// No alarms found: will re-check at midnight
2021-10-27 16:54:06 +00:00
setTimeout(() => {
eval(require("Storage").read("qalarmcheck.js"));
}, 86400000 - t);
}