Make the alarm scheduling more reliable

Fixes https://github.com/espruino/BangleApps/issues/433
master
Sabin Iacob 2020-05-27 08:14:48 +03:00
parent 5530b80ca8
commit b13c5d13f4
1 changed files with 5 additions and 4 deletions

View File

@ -2,15 +2,16 @@
(function() {
var alarms = require('Storage').readJSON('alarm.json',1)||[];
var time = new Date();
var active = alarms.filter(a=>a.on&&(a.last!=time.getDate()));
var active = alarms.filter(a=>a.on);
if (active.length) {
active = active.sort((a,b)=>a.hr-b.hr);
active = active.sort((a,b)=>(a.hr-b.hr)+(a.last-b.last)*24);
var hr = time.getHours()+(time.getMinutes()/60)+(time.getSeconds()/3600);
if (!require('Storage').read("alarm.js")) {
console.log("No alarm app!");
require('Storage').write('alarm.json',"[]")
require('Storage').write('alarm.json',"[]");
} else {
var t = 3600000*(active[0].hr-hr);
if (active[0].last == time.getDate()) t += 86400000;
if (t<1000) t=1000;
/* execute alarm at the correct time. We avoid execing immediately
since this code will get called AGAIN when alarm.js is loaded. alarm.js
@ -21,4 +22,4 @@
},t);
}
}
})()
})();