alarm fixes (fix #1667)

pull/1669/head
Gordon Williams 2022-04-07 15:47:01 +01:00
parent 2c36071a37
commit 1dd232ffce
6 changed files with 30 additions and 9 deletions

View File

@ -17,3 +17,4 @@
0.16: Adding alarm library
0.17: Moving alarm internals to 'sched' library
0.18: Cope with >1 identical alarm at once (#1667)
0.19: Ensure rescheduled alarms that already fired have 'last' reset

View File

@ -138,8 +138,7 @@ function editAlarm(alarmIndex, alarm) {
};
menu[/*LANG*/"Save"] = function() {
a.t = encodeTime(t);
if (a.t < getCurrentTime())
a.day = (new Date()).getDate();
a.last = (a.t < getCurrentTime()) ? (new Date()).getDate() : 0;
if (newAlarm) alarms.push(a);
else alarms[alarmIndex] = a;
saveAndReload();
@ -191,6 +190,7 @@ function editTimer(alarmIndex, alarm) {
menu[/*LANG*/"Save"] = function() {
a.timer = encodeTime(t);
a.t = getCurrentTime() + a.timer;
a.last = 0;
if (newAlarm) alarms.push(a);
else alarms[alarmIndex] = a;
saveAndReload();

View File

@ -2,7 +2,7 @@
"id": "alarm",
"name": "Alarm & Timer",
"shortName": "Alarms",
"version": "0.18",
"version": "0.19",
"description": "Set alarms and timers on your Bangle",
"icon": "app.png",
"tags": "tool,alarm,widget",

View File

@ -1 +1,2 @@
0.01: New App!
0.02: Fix scheduling of other alarms if there is a pending alarm from the past (fix #1667)

View File

@ -6,13 +6,19 @@
}
var alarms = require('Storage').readJSON('sched.json',1)||[];
var time = new Date();
var active = alarms.filter(a=>a.on && (a.dow>>time.getDay())&1 && (!a.date || a.date==time.toISOString().substr(0,10)));
var currentTime = (time.getHours()*3600000)+(time.getMinutes()*60000)+(time.getSeconds()*1000);
var d = time.getDate();
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
);
if (active.length) {
active = active.sort((a,b)=>(a.t-b.t)+(a.last-b.last)*86400000);
var currentTime = (time.getHours()*3600000)+(time.getMinutes()*60000)+(time.getSeconds()*1000);
active = active.sort((a,b)=>a.t-b.t); // sort by time
var t = active[0].t-currentTime;
if (active[0].last == time.getDate() || t < -60000) t += 86400000;
if (t<1000) t=1000; // start alarm min 1 sec from now
if (t<1000) t=1000; // start alarm minimum 1 sec from now
/* 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
@ -23,3 +29,16 @@
Bangle.SCHED = setTimeout('eval(require("Storage").read("sched.boot.js"))', 86400000 - (Date.now()%86400000));
}
})();
/* 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)
*/

View File

@ -1,7 +1,7 @@
{
"id": "sched",
"name": "Scheduler",
"version": "0.01",
"version": "0.02",
"description": "Scheduling library for alarms and timers",
"icon": "app.png",
"type": "scheduler",