2022-03-31 14:32:26 +00:00
// Return an array of all alarms
exports . getAlarms = function ( ) {
2022-04-04 14:49:45 +00:00
return require ( "Storage" ) . readJSON ( "sched.json" , 1 ) || [ ] ;
2022-03-31 14:32:26 +00:00
} ;
2022-04-04 15:58:17 +00:00
// Write a list of alarms back to storage
exports . setAlarms = function ( alarms ) {
return require ( "Storage" ) . writeJSON ( "sched.json" , alarms ) ;
} ;
2022-03-31 14:32:26 +00:00
// Return an alarm object based on ID
exports . getAlarm = function ( id ) {
2022-04-04 15:58:17 +00:00
return exports . getAlarms ( ) . find ( a => a . id == id ) ;
2022-03-31 14:32:26 +00:00
} ;
2022-04-04 15:58:17 +00:00
// Given a list of alarms from getAlarms, return a list of active alarms for the given time (or current time if time not specified)
2022-05-20 16:17:48 +00:00
exports . getActiveAlarms = function ( alarms , time ) {
2022-04-04 15:58:17 +00:00
if ( ! time ) time = new Date ( ) ;
2022-05-20 16:17:48 +00:00
// get current time 10s in future to ensure we alarm if we've started the app a tad early
var currentTime = ( time . getHours ( ) * 3600000 ) + ( time . getMinutes ( ) * 60000 ) + ( time . getSeconds ( ) * 1000 ) + 10000 ;
return alarms
. filter ( a =>
a . on // enabled
&& ( a . last != time . getDate ( ) ) // not already fired today
&& ( a . t < currentTime )
&& ( a . dow >> time . getDay ( ) & 1 ) // is allowed on this day of the week
lib.js support timezone - toLocalISOString
Test scenario:
E.setTimeZone(-8);
date = new Date("2022-12-05T23:00");
date.toLocalISOString(); // -> "2022-12-05T23:00:00.000-0800"
getActiveAlarms([{on: true, t: 0, dow: 0b1111111, date: "2022-12-05"}], date); //should be marked as active, but isn't
getActiveAlarms([{on: true, t: 0, dow: 0b1111111, date: "2022-12-06"}], date); //should not be marked as active, but it is because it is already "tomorrow" in UTC
2022-12-08 22:48:56 +00:00
&& ( ! a . date || a . date == time . toLocalISOString ( ) . substr ( 0 , 10 ) ) // is allowed on this date
2022-05-20 16:17:48 +00:00
)
. sort ( ( a , b ) => a . t - b . t ) ;
2022-04-04 15:58:17 +00:00
}
2022-03-31 14:32:26 +00:00
// Set an alarm object based on ID. Leave 'alarm' undefined to remove it
exports . setAlarm = function ( id , alarm ) {
2022-04-04 15:58:17 +00:00
var alarms = exports . getAlarms ( ) . filter ( a => a . id != id ) ;
2022-03-31 14:32:26 +00:00
if ( alarm !== undefined ) {
alarm . id = id ;
if ( alarm . dow === undefined ) alarm . dow = 0b1111111 ;
if ( alarm . on !== false ) alarm . on = true ;
if ( alarm . timer ) { // if it's a timer, set the start time as a time from *now*
var time = new Date ( ) ;
var currentTime = ( time . getHours ( ) * 3600000 ) + ( time . getMinutes ( ) * 60000 ) + ( time . getSeconds ( ) * 1000 ) ;
alarm . t = currentTime + alarm . timer ;
}
2022-04-05 09:59:13 +00:00
alarms . push ( alarm ) ;
2022-03-31 14:32:26 +00:00
}
2022-04-04 15:58:17 +00:00
exports . setAlarms ( alarms ) ;
2022-03-31 14:32:26 +00:00
} ;
2022-04-07 17:47:45 +00:00
/// Get time until the given alarm (object). Return undefined if alarm not enabled, or if 86400000 or more, alarm could be *more* than a day in the future
2022-03-31 14:32:26 +00:00
exports . getTimeToAlarm = function ( alarm , time ) {
if ( ! alarm ) return undefined ;
if ( ! time ) time = new Date ( ) ;
var currentTime = ( time . getHours ( ) * 3600000 ) + ( time . getMinutes ( ) * 60000 ) + ( time . getSeconds ( ) * 1000 ) ;
lib.js support timezone - toLocalISOString
Test scenario:
E.setTimeZone(-8);
date = new Date("2022-12-05T23:00");
date.toLocalISOString(); // -> "2022-12-05T23:00:00.000-0800"
getActiveAlarms([{on: true, t: 0, dow: 0b1111111, date: "2022-12-05"}], date); //should be marked as active, but isn't
getActiveAlarms([{on: true, t: 0, dow: 0b1111111, date: "2022-12-06"}], date); //should not be marked as active, but it is because it is already "tomorrow" in UTC
2022-12-08 22:48:56 +00:00
var active = alarm . on && ( alarm . dow >> ( ( time . getDay ( ) + ( alarm . t < currentTime ) ) % 7 ) ) & 1 && ( ! alarm . date || alarm . date == time . toLocalISOString ( ) . substr ( 0 , 10 ) ) ;
2022-04-08 19:24:24 +00:00
if ( ! active ) return undefined ;
2022-03-31 14:32:26 +00:00
var t = alarm . t - currentTime ;
if ( alarm . last == time . getDate ( ) || t < - 60000 ) t += 86400000 ;
return t ;
} ;
/// Force a reload of the current alarms and widget
exports . reload = function ( ) {
2022-04-04 14:49:45 +00:00
eval ( require ( "Storage" ) . read ( "sched.boot.js" ) ) ;
2022-04-25 13:02:12 +00:00
if ( global . WIDGETS && WIDGETS [ "alarm" ] ) {
2022-03-31 14:32:26 +00:00
WIDGETS [ "alarm" ] . reload ( ) ;
Bangle . drawWidgets ( ) ;
}
} ;
2022-04-20 12:12:46 +00:00
// Factory that creates a new alarm with default values
exports . newDefaultAlarm = function ( ) {
const settings = exports . getSettings ( ) ;
2022-05-12 20:59:03 +00:00
var alarm = {
2022-04-20 12:12:46 +00:00
t : 12 * 3600000 , // Default to 12:00
2022-06-11 13:36:48 +00:00
del : false , // Never delete an alarm when it expires
2022-04-20 12:12:46 +00:00
on : true ,
2022-06-11 13:36:48 +00:00
rp : false ,
2022-04-24 14:25:36 +00:00
as : settings . defaultAutoSnooze ,
2022-05-20 16:19:34 +00:00
dow : 0b1111111 ,
2022-04-20 12:12:46 +00:00
last : 0 ,
vibrate : settings . defaultAlarmPattern ,
} ;
delete settings ;
return alarm ;
}
// Factory that creates a new timer with default values
exports . newDefaultTimer = function ( ) {
const settings = exports . getSettings ( ) ;
2022-05-12 20:59:03 +00:00
var timer = {
2022-04-20 12:12:46 +00:00
timer : 5 * 60 * 1000 , // 5 minutes
2022-06-11 13:36:48 +00:00
del : settings . defaultDeleteExpiredTimers ,
2022-04-20 12:12:46 +00:00
on : true ,
rp : false ,
as : false ,
dow : 0b1111111 ,
last : 0 ,
vibrate : settings . defaultTimerPattern
}
delete settings ;
return timer ;
} ;
// Return the scheduler settings
exports . getSettings = function ( ) {
return Object . assign (
{
unlockAtBuzz : false ,
defaultSnoozeMillis : 600000 , // 10 minutes
defaultAutoSnooze : false ,
2022-06-11 13:36:48 +00:00
defaultDeleteExpiredTimers : true , // Always
2022-04-20 12:12:46 +00:00
buzzCount : 10 ,
buzzIntervalMillis : 3000 , // 3 seconds
2022-06-03 15:32:09 +00:00
defaultAlarmPattern : "::" ,
defaultTimerPattern : "::"
2022-04-20 12:12:46 +00:00
} ,
require ( "Storage" ) . readJSON ( "sched.settings.json" , true ) || { }
) ;
}
// Write the updated settings back to storage
exports . setSettings = function ( settings ) {
require ( "Storage" ) . writeJSON ( "sched.settings.json" , settings ) ;
2022-04-20 17:32:23 +00:00
} ;