2022-04-04 14:49:45 +00:00
|
|
|
// check for alarms
|
2022-04-04 15:58:17 +00:00
|
|
|
(function() { // run in closure to ensure allocated vars get removed
|
|
|
|
if (Bangle.SCHED) {
|
|
|
|
clearTimeout(Bangle.SCHED);
|
|
|
|
delete Bangle.SCHED;
|
2022-04-04 14:49:45 +00:00
|
|
|
}
|
|
|
|
var alarms = require('Storage').readJSON('sched.json',1)||[];
|
|
|
|
var time = new Date();
|
2022-04-07 14:47:01 +00:00
|
|
|
var currentTime = (time.getHours()*3600000)+(time.getMinutes()*60000)+(time.getSeconds()*1000);
|
|
|
|
var d = time.getDate();
|
2022-05-20 16:17:48 +00:00
|
|
|
var active = alarms.filter(a =>
|
|
|
|
a.on // enabled
|
|
|
|
&& (a.last != d) // not already fired today
|
|
|
|
&& (a.t + 60000 > currentTime) // is not in the past by >1 minute
|
|
|
|
&& (a.dow >> time.getDay() & 1) // is allowed on this day of the week
|
|
|
|
&& (!a.date || a.date == time.toISOString().substr(0, 10)) // is allowed on this date
|
2022-04-07 14:47:01 +00:00
|
|
|
);
|
2022-04-04 14:49:45 +00:00
|
|
|
if (active.length) {
|
2022-04-07 14:47:01 +00:00
|
|
|
active = active.sort((a,b)=>a.t-b.t); // sort by time
|
2022-04-04 14:49:45 +00:00
|
|
|
var t = active[0].t-currentTime;
|
2022-04-07 14:47:01 +00:00
|
|
|
if (t<1000) t=1000; // start alarm minimum 1 sec from now
|
2022-04-04 14:49:45 +00:00
|
|
|
/* execute alarm at the correct time. We avoid execing immediately
|
|
|
|
since this code will get called AGAIN when alarm.js is loaded. alarm.js
|
|
|
|
will then clearInterval() to get rid of this call so it can proceed
|
|
|
|
normally.
|
|
|
|
If active[0].js is defined, just run that code as-is and not alarm.js */
|
2022-05-23 09:34:18 +00:00
|
|
|
Bangle.SCHED = setTimeout(active[0].js||'load("sched.js")',t);
|
2022-04-04 14:49:45 +00:00
|
|
|
} else { // check for new alarms at midnight (so day of week works)
|
2022-04-04 15:58:17 +00:00
|
|
|
Bangle.SCHED = setTimeout('eval(require("Storage").read("sched.boot.js"))', 86400000 - (Date.now()%86400000));
|
2022-04-04 14:49:45 +00:00
|
|
|
}
|
|
|
|
})();
|
2022-04-07 14:47:01 +00:00
|
|
|
/* DEBUGGING
|
|
|
|
===============
|
|
|
|
|
|
|
|
// show the current timer for the next event
|
|
|
|
global["\xff"].timers[Bangle.SCHED]
|
|
|
|
|
|
|
|
// time in hours of scheduled timer event
|
|
|
|
global["\xff"].timers[Bangle.SCHED].time / (1024*1024*60*60)
|
|
|
|
|
|
|
|
// set time 1 hour in the past
|
|
|
|
setTime(getTime() - 60*60)
|
|
|
|
|
|
|
|
*/
|