@@ -32,9 +32,6 @@ exports.onAlarm = function(index, clock) {
3232 const Sched = require ( "sched" ) ;
3333 const alarm = Sched . getAlarm ( `${ APP_ID } .${ index } ` ) ;
3434 alarm . last = date . getDate ( ) ; // prevent second run on the same day
35- // -- Don't think alarm.date is required, but just in case
36- //date.setDate(alarm.last + 1);
37- //alarm.date = date.toLocalISOString().slice(0,10);
3835 Sched . setAlarm ( alarm . id , alarm ) ;
3936 setClock ( clock ) ;
4037} ;
@@ -56,24 +53,28 @@ exports.syncAlarms = function() {
5653 // If the app is disabled, we're done.
5754 if ( ! settings . enabled ) return ;
5855
59- const time = new Date ( ) ;
60- const currentTime = ( time . getHours ( ) * 3600000 ) + ( time . getMinutes ( ) * 60000 ) + ( time . getSeconds ( ) * 1000 ) ;
61-
56+ // Alarms need "last" set to let sched know they've already ran for the day
57+ // So if an alarm is for before "now", set last to yesterday so it still triggers today
58+ // else set last to today.
59+ const currentDate = new Date ( ) ;
60+ const currentTime = ( currentDate . getHours ( ) * 3600000 ) + ( currentDate . getMinutes ( ) * 60000 ) + ( currentDate . getSeconds ( ) * 1000 ) ;
61+ const dayOfMonthToday = currentDate . getDate ( ) ;
62+ const dayOfMonthYesterday = dayOfMonthToday - 1 ;
63+
6264 // Add a new alarm for each setting item
6365 settings . sched . forEach ( ( item , index ) => {
6466
65- if ( item . hour === undefined ) return ;
67+ // Skip invalid records
68+ if ( item . hour === undefined || item . minute === undefined ) return ;
6669
67- const schedTime = ( item . hour * 3600000 ) + ( item . minute * 60000 ) ;
68- const today = time . getDate ( ) ;
69- const yesterday = today - 1 ;
70+ const scheduledTime = ( item . hour * 3600000 ) + ( item . minute * 60000 ) ;
7071
7172 // Create the new alarm object and save it using a unique ID.
7273 Sched . setAlarm ( `${ APP_ID } .${ index } ` , {
73- t : schedTime , // time in milliseconds since midnight
74+ t : scheduledTime , // time in milliseconds since midnight
7475 on : true ,
7576 rp : true ,
76- last : ( schedTime > currentTime ) ? yesterday : today ,
77+ last : ( scheduledTime > currentTime ) ? dayOfMonthYesterday : dayOfMonthToday ,
7778 dow : item . dow ,
7879 hidden : true ,
7980 appid : APP_ID ,
0 commit comments