more refactoring to try and reduce code duplication

pull/1661/head
Gordon Williams 2022-04-04 16:58:17 +01:00
parent 5c84ec9e2c
commit 25aafe1ccf
6 changed files with 34 additions and 46 deletions

View File

@ -1,8 +1,8 @@
Bangle.loadWidgets(); Bangle.loadWidgets();
Bangle.drawWidgets(); Bangle.drawWidgets();
var alarms = require("Storage").readJSON("sched.json",1)||[]; var alarms = require("sched").getAlarms();
// An array of alarm objects (see README.md) // An array of alarm objects (see sched/README.md)
// time in ms -> { hrs, mins } // time in ms -> { hrs, mins }
function decodeTime(t) { function decodeTime(t) {
@ -31,7 +31,7 @@ function getCurrentTime() {
} }
function saveAndReload() { function saveAndReload() {
require("Storage").write("sched.json",JSON.stringify(alarms)); require("sched").setAlarms(alarms);
require("sched").reload(); require("sched").reload();
} }

View File

@ -3,7 +3,7 @@
"name": "Alarm & Timer", "name": "Alarm & Timer",
"shortName": "Alarms", "shortName": "Alarms",
"version": "0.17", "version": "0.17",
"description": "Set and respond to alarms and timers", "description": "Set alarms and timers on your Bangle",
"icon": "app.png", "icon": "app.png",
"tags": "tool,alarm,widget", "tags": "tool,alarm,widget",
"supports": ["BANGLEJS","BANGLEJS2"], "supports": ["BANGLEJS","BANGLEJS2"],

View File

@ -4,7 +4,7 @@
"shortName": "Q Alarm", "shortName": "Q Alarm",
"icon": "app.png", "icon": "app.png",
"version": "0.04", "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", "tags": "tool,alarm,widget",
"supports": ["BANGLEJS", "BANGLEJS2"], "supports": ["BANGLEJS", "BANGLEJS2"],
"storage": [ "storage": [

View File

@ -1,8 +1,8 @@
// check for alarms // check for alarms
(function() { (function() { // run in closure to ensure allocated vars get removed
if (Bangle.ALARM) { if (Bangle.SCHED) {
clearTimeout(Bangle.ALARM); clearTimeout(Bangle.SCHED);
delete Bangle.ALARM; delete Bangle.SCHED;
} }
var alarms = require('Storage').readJSON('sched.json',1)||[]; var alarms = require('Storage').readJSON('sched.json',1)||[];
var time = new Date(); var time = new Date();
@ -18,10 +18,8 @@
will then clearInterval() to get rid of this call so it can proceed will then clearInterval() to get rid of this call so it can proceed
normally. normally.
If active[0].js is defined, just run that code as-is and not alarm.js */ 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) } else { // check for new alarms at midnight (so day of week works)
Bangle.ALARM = setTimeout(() => { Bangle.SCHED = setTimeout('eval(require("Storage").read("sched.boot.js"))', 86400000 - (Date.now()%86400000));
eval(require("Storage").read("sched.boot.js"));
}, 86400000 - (Date.now()%86400000));
} }
})(); })();

View File

@ -2,15 +2,24 @@
exports.getAlarms = function() { exports.getAlarms = function() {
return require("Storage").readJSON("sched.json",1)||[]; 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 // Return an alarm object based on ID
exports.getAlarm = function(id) { exports.getAlarm = function(id) {
var alarms = require("Storage").readJSON("sched.json",1)||[]; return exports.getAlarms().find(a=>a.id==id);
return alarms.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.t<currentTime)&&(a.last!=time.getDate()) && (!a.date || a.date==time.toISOString().substr(0,10))).sort((a,b)=>a.t-b.t);
}
// Set an alarm object based on ID. Leave 'alarm' undefined to remove it // Set an alarm object based on ID. Leave 'alarm' undefined to remove it
exports.setAlarm = function(id, alarm) { exports.setAlarm = function(id, alarm) {
var alarms = require("Storage").readJSON("sched.json",1)||[]; var alarms = exports.getAlarms().filter(a=>a.id!=id);
alarms = alarms.filter(a=>a.id!=id);
if (alarm !== undefined) { if (alarm !== undefined) {
alarm.id = id; alarm.id = id;
if (alarm.dow===undefined) alarm.dow = 0b1111111; if (alarm.dow===undefined) alarm.dow = 0b1111111;
@ -22,7 +31,7 @@ exports.setAlarm = function(id, alarm) {
} }
} }
alarms.push(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 /// 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) { exports.getTimeToAlarm = function(alarm, time) {

View File

@ -1,8 +1,8 @@
// Chances are boot0.js got run already and scheduled *another* // Chances are boot0.js got run already and scheduled *another*
// 'load(sched.js)' - so let's remove it first! // 'load(sched.js)' - so let's remove it first!
if (Bangle.ALARM) { if (Bangle.SCHED) {
clearInterval(Bangle.ALARM); clearInterval(Bangle.SCHED);
delete Bangle.ALARM; delete Bangle.SCHED;
} }
// time in ms -> { hrs, mins } // time in ms -> { hrs, mins }
@ -12,25 +12,11 @@ function decodeTime(t) {
return { hrs : hrs, mins : Math.round((t-hrs*3600000)/60000) }; 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) { function formatTime(t) {
var o = decodeTime(t); var o = decodeTime(t);
return o.hrs+":"+("0"+o.mins).substr(-2); 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) { function showAlarm(alarm) {
var msg = alarm.timer ? formatTime(alarm.timer) : formatTime(alarm.t); var msg = alarm.timer ? formatTime(alarm.timer) : formatTime(alarm.t);
var buzzCount = 10; var buzzCount = 10;
@ -54,7 +40,8 @@ function showAlarm(alarm) {
} }
if (!alarm.rp) alarm.on = false; 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(); load();
}); });
function buzz() { function buzz() {
@ -72,15 +59,9 @@ function showAlarm(alarm) {
} }
// Check for alarms // Check for alarms
var day = (new Date()).getDate(); var alarms = require("sched").getAlarms();
var currentTime = getCurrentTime()+10000; // get current time - 10s in future to ensure we alarm if we've started the app a tad early var active = require("sched").getActiveAlarms(alarms);
var alarms = require("Storage").readJSON("sched.json",1)||[]; if (active.length) // if there's an alarm, show it
var active = alarms.filter(a=>a.on&&(a.t<currentTime)&&(a.last!=day) && (!a.date || a.date==time.toISOString().substr(0,10)));
if (active.length) {
// if there's an alarm, show it
active = active.sort((a,b)=>a.t-b.t);
showAlarm(active[0]); showAlarm(active[0]);
} else { else // otherwise just go back to default app
// otherwise just go back to default app
setTimeout(load, 100); setTimeout(load, 100);
}