diff --git a/apps/alarm/app.js b/apps/alarm/app.js index d5379f469..45edd83f5 100644 --- a/apps/alarm/app.js +++ b/apps/alarm/app.js @@ -1,8 +1,8 @@ Bangle.loadWidgets(); Bangle.drawWidgets(); -var alarms = require("Storage").readJSON("sched.json",1)||[]; -// An array of alarm objects (see README.md) +var alarms = require("sched").getAlarms(); +// An array of alarm objects (see sched/README.md) // time in ms -> { hrs, mins } function decodeTime(t) { @@ -31,7 +31,7 @@ function getCurrentTime() { } function saveAndReload() { - require("Storage").write("sched.json",JSON.stringify(alarms)); + require("sched").setAlarms(alarms); require("sched").reload(); } diff --git a/apps/alarm/metadata.json b/apps/alarm/metadata.json index 726e2461c..fe82e04c9 100644 --- a/apps/alarm/metadata.json +++ b/apps/alarm/metadata.json @@ -3,7 +3,7 @@ "name": "Alarm & Timer", "shortName": "Alarms", "version": "0.17", - "description": "Set and respond to alarms and timers", + "description": "Set alarms and timers on your Bangle", "icon": "app.png", "tags": "tool,alarm,widget", "supports": ["BANGLEJS","BANGLEJS2"], diff --git a/apps/qalarm/metadata.json b/apps/qalarm/metadata.json index 326ba33a7..2039af4bf 100644 --- a/apps/qalarm/metadata.json +++ b/apps/qalarm/metadata.json @@ -4,7 +4,7 @@ "shortName": "Q Alarm", "icon": "app.png", "version": "0.04", - "description": "Alarm and timer app with days of week and 'hard' option.", + "description": "[Not recommended - use 'Alarm & Timer' app] Alarm and timer app with days of week and 'hard' option.", "tags": "tool,alarm,widget", "supports": ["BANGLEJS", "BANGLEJS2"], "storage": [ diff --git a/apps/sched/boot.js b/apps/sched/boot.js index 15c332ad7..c772a135e 100644 --- a/apps/sched/boot.js +++ b/apps/sched/boot.js @@ -1,8 +1,8 @@ // check for alarms -(function() { - if (Bangle.ALARM) { - clearTimeout(Bangle.ALARM); - delete Bangle.ALARM; +(function() { // run in closure to ensure allocated vars get removed + if (Bangle.SCHED) { + clearTimeout(Bangle.SCHED); + delete Bangle.SCHED; } var alarms = require('Storage').readJSON('sched.json',1)||[]; var time = new Date(); @@ -18,10 +18,8 @@ will then clearInterval() to get rid of this call so it can proceed normally. If active[0].js is defined, just run that code as-is and not alarm.js */ - Bangle.ALARM = setTimeout(active[0].js||'load("sched.js")',t); + Bangle.SCHED = setTimeout(active[0].js||'load("sched.js")',t); } else { // check for new alarms at midnight (so day of week works) - Bangle.ALARM = setTimeout(() => { - eval(require("Storage").read("sched.boot.js")); - }, 86400000 - (Date.now()%86400000)); + Bangle.SCHED = setTimeout('eval(require("Storage").read("sched.boot.js"))', 86400000 - (Date.now()%86400000)); } })(); diff --git a/apps/sched/lib.js b/apps/sched/lib.js index 9d9744b1f..4b645aede 100644 --- a/apps/sched/lib.js +++ b/apps/sched/lib.js @@ -2,15 +2,24 @@ exports.getAlarms = function() { return require("Storage").readJSON("sched.json",1)||[]; }; +// Write a list of alarms back to storage +exports.setAlarms = function(alarms) { + return require("Storage").writeJSON("sched.json",alarms); +}; // Return an alarm object based on ID exports.getAlarm = function(id) { - var alarms = require("Storage").readJSON("sched.json",1)||[]; - return alarms.find(a=>a.id==id); + return exports.getAlarms().find(a=>a.id==id); }; +// Given a list of alarms from getAlarms, return a list of active alarms for the given time (or current time if time not specified) +exports.getActiveAlarms = function(alarms, time) { + if (!time) time = new Date(); + var currentTime = (time.getHours()*3600000)+(time.getMinutes()*60000)+(time.getSeconds()*1000) + +10000;// get current time - 10s in future to ensure we alarm if we've started the app a tad early + return alarms.filter(a=>a.on&&(a.ta.t-b.t); +} // Set an alarm object based on ID. Leave 'alarm' undefined to remove it exports.setAlarm = function(id, alarm) { - var alarms = require("Storage").readJSON("sched.json",1)||[]; - alarms = alarms.filter(a=>a.id!=id); + var alarms = exports.getAlarms().filter(a=>a.id!=id); if (alarm !== undefined) { alarm.id = id; if (alarm.dow===undefined) alarm.dow = 0b1111111; @@ -22,7 +31,7 @@ exports.setAlarm = function(id, alarm) { } } alarms.push(alarm); - require("Storage").writeJSON("sched.json", alarms); + exports.setAlarms(alarms); }; /// Get time until the given alarm (object). Return undefined if alarm not enabled, or if 86400000 or more, alarm could me *more* than a day in the future exports.getTimeToAlarm = function(alarm, time) { diff --git a/apps/sched/sched.js b/apps/sched/sched.js index f42a15fc3..a19c85717 100644 --- a/apps/sched/sched.js +++ b/apps/sched/sched.js @@ -1,8 +1,8 @@ // Chances are boot0.js got run already and scheduled *another* // 'load(sched.js)' - so let's remove it first! -if (Bangle.ALARM) { - clearInterval(Bangle.ALARM); - delete Bangle.ALARM; +if (Bangle.SCHED) { + clearInterval(Bangle.SCHED); + delete Bangle.SCHED; } // time in ms -> { hrs, mins } @@ -12,25 +12,11 @@ function decodeTime(t) { return { hrs : hrs, mins : Math.round((t-hrs*3600000)/60000) }; } -// time in { hrs, mins } -> ms -function encodeTime(o) { - return o.hrs*3600000 + o.mins*60000; -} - function formatTime(t) { var o = decodeTime(t); return o.hrs+":"+("0"+o.mins).substr(-2); } -function getCurrentTime() { - var time = new Date(); - return ( - time.getHours() * 3600000 + - time.getMinutes() * 60000 + - time.getSeconds() * 1000 - ); -} - function showAlarm(alarm) { var msg = alarm.timer ? formatTime(alarm.timer) : formatTime(alarm.t); var buzzCount = 10; @@ -54,7 +40,8 @@ function showAlarm(alarm) { } if (!alarm.rp) alarm.on = false; } - require("Storage").write("sched.json",JSON.stringify(alarms)); + // alarm is still a member of 'alarms', so writing to array writes changes back directly + require("sched").setAlarms(alarms); load(); }); function buzz() { @@ -72,15 +59,9 @@ function showAlarm(alarm) { } // Check for alarms -var day = (new Date()).getDate(); -var currentTime = getCurrentTime()+10000; // get current time - 10s in future to ensure we alarm if we've started the app a tad early -var alarms = require("Storage").readJSON("sched.json",1)||[]; -var active = alarms.filter(a=>a.on&&(a.ta.t-b.t); +var alarms = require("sched").getAlarms(); +var active = require("sched").getActiveAlarms(alarms); +if (active.length) // if there's an alarm, show it showAlarm(active[0]); -} else { - // otherwise just go back to default app +else // otherwise just go back to default app setTimeout(load, 100); -}