@@ -5,7 +5,7 @@ const APP_ID = "schedclock";
55 * Called directly by an alarm to load a specific clock face
66 * @param {string } faceSrc - Source file of the clock face to load (e.g. "myclock.js")
77 **/
8- exports . loadFace = function ( faceSrc ) {
8+ const setClock = function ( faceSrc ) {
99 const settings = require ( "Storage" ) . readJSON ( "setting.json" , 1 ) || { } ;
1010 // Only change the clock if it's different
1111 if ( faceSrc && settings . clock !== faceSrc ) {
@@ -22,6 +22,23 @@ exports.loadFace = function(faceSrc) {
2222 }
2323} ;
2424
25+ /**
26+ * Handle alarms and resetting them
27+ * @param {number } index Index of the alarm that went off
28+ * @param {string } clock Clockface
29+ */
30+ exports . onAlarm = function ( index , clock ) {
31+ const date = new Date ( ) ;
32+ const Sched = require ( "sched" ) ;
33+ const alarm = Sched . getAlarm ( `${ APP_ID } .${ index } ` ) ;
34+ 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);
38+ Sched . setAlarm ( alarm . id , alarm ) ;
39+ setClock ( clock ) ;
40+ } ;
41+
2542/**
2643 * Function to sync all alarms in the scheduler with the settings file.
2744 * Called every time settings are changed; maybe a bit excessive, but keeps things simple.
@@ -39,21 +56,28 @@ exports.syncAlarms = function() {
3956 // If the app is disabled, we're done.
4057 if ( ! settings . enabled ) return ;
4158
59+ const time = new Date ( ) ;
60+ const currentTime = ( time . getHours ( ) * 3600000 ) + ( time . getMinutes ( ) * 60000 ) + ( time . getSeconds ( ) * 1000 ) ;
61+
4262 // Add a new alarm for each setting item
4363 settings . sched . forEach ( ( item , index ) => {
4464
4565 if ( item . hour === undefined ) return ;
4666
67+ const schedTime = ( item . hour * 3600000 ) + ( item . minute * 60000 ) ;
68+ const today = time . getDate ( ) ;
69+ const tomorrow = today + 1 ;
70+
4771 // Create the new alarm object and save it using a unique ID.
4872 Sched . setAlarm ( `${ APP_ID } .${ index } ` , {
73+ t : schedTime , // time in milliseconds since midnight
4974 on : true ,
50- appid : APP_ID ,
51- t : ( item . hour * 3600000 ) + ( item . minute * 60000 ) , // time in milliseconds since midnight
75+ rp : true ,
76+ last : ( schedTime > currentTime ) ? today : tomorrow ,
5277 dow : item . dow ,
5378 hidden : true ,
54- rp : { interval : "week" } ,
55- js : `require('${ APP_ID } .lib.js').loadFace( '${ item . face } ')` ,
79+ appid : APP_ID ,
80+ js : `require('${ APP_ID } .lib.js').onAlarm( ${ index } , '${ item . face } ')` ,
5681 } ) ;
5782 } ) ;
5883} ;
59-
0 commit comments