Refactor some methods to scheduling library

pull/1722/head
Erik Andresen 2022-04-20 19:32:23 +02:00
parent 7f657a31d0
commit 8c048f321c
11 changed files with 34 additions and 50 deletions

View File

@ -19,3 +19,4 @@
0.18: Cope with >1 identical alarm at once (#1667) 0.18: Cope with >1 identical alarm at once (#1667)
0.19: Ensure rescheduled alarms that already fired have 'last' reset 0.19: Ensure rescheduled alarms that already fired have 'last' reset
0.20: Use the new 'sched' factories to initialize new alarms/timers 0.20: Use the new 'sched' factories to initialize new alarms/timers
0.21: Refactor some methods to scheduling library

View File

@ -4,23 +4,6 @@ Bangle.drawWidgets();
// An array of alarm objects (see sched/README.md) // An array of alarm objects (see sched/README.md)
let alarms = require("sched").getAlarms(); let alarms = require("sched").getAlarms();
// time in ms -> { hrs, mins }
function decodeTime(t) {
t = 0 | t; // sanitise
let hrs = 0 | (t / 3600000);
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) {
let o = decodeTime(t);
return o.hrs + ":" + ("0" + o.mins).substr(-2);
}
function getCurrentTime() { function getCurrentTime() {
let time = new Date(); let time = new Date();
return ( return (
@ -48,10 +31,10 @@ function showMainMenu() {
var type,txt; // a leading space is currently required (JS error in Espruino 2v12) var type,txt; // a leading space is currently required (JS error in Espruino 2v12)
if (alarm.timer) { if (alarm.timer) {
type = /*LANG*/"Timer"; type = /*LANG*/"Timer";
txt = " "+formatTime(alarm.timer); txt = " "+require("sched").formatTime(alarm.timer);
} else { } else {
type = /*LANG*/"Alarm"; type = /*LANG*/"Alarm";
txt = " "+formatTime(alarm.t); txt = " "+require("sched").formatTime(alarm.t);
} }
if (alarm.rp) txt += "\0"+atob("FBaBAAABgAAcAAHn//////wAHsABzAAYwAAMAADAAAAAAwAAMAADGAAzgAN4AD//////54AAOAABgAA="); if (alarm.rp) txt += "\0"+atob("FBaBAAABgAAcAAHn//////wAHsABzAAYwAAMAADAAAAAAwAAMAADGAAzgAN4AD//////54AAOAABgAA=");
// rename duplicate alarms // rename duplicate alarms
@ -94,7 +77,7 @@ function editAlarm(alarmIndex, alarm) {
let a = require("sched").newDefaultAlarm(); let a = require("sched").newDefaultAlarm();
if (!newAlarm) Object.assign(a, alarms[alarmIndex]); if (!newAlarm) Object.assign(a, alarms[alarmIndex]);
if (alarm) Object.assign(a,alarm); if (alarm) Object.assign(a,alarm);
let t = decodeTime(a.t); let t = require("sched").decodeTime(a.t);
const menu = { const menu = {
'': { 'title': /*LANG*/'Alarm' }, '': { 'title': /*LANG*/'Alarm' },
@ -129,7 +112,7 @@ function editAlarm(alarmIndex, alarm) {
} }
}; };
menu[/*LANG*/"Save"] = function() { menu[/*LANG*/"Save"] = function() {
a.t = encodeTime(t); a.t = require("sched").encodeTime(t);
a.last = (a.t < getCurrentTime()) ? (new Date()).getDate() : 0; a.last = (a.t < getCurrentTime()) ? (new Date()).getDate() : 0;
if (newAlarm) alarms.push(a); if (newAlarm) alarms.push(a);
else alarms[alarmIndex] = a; else alarms[alarmIndex] = a;
@ -151,7 +134,7 @@ function editTimer(alarmIndex, alarm) {
let a = require("sched").newDefaultTimer(); let a = require("sched").newDefaultTimer();
if (!newAlarm) Object.assign(a, alarms[alarmIndex]); if (!newAlarm) Object.assign(a, alarms[alarmIndex]);
if (alarm) Object.assign(a,alarm); if (alarm) Object.assign(a,alarm);
let t = decodeTime(a.timer); let t = require("sched").decodeTime(a.timer);
const menu = { const menu = {
'': { 'title': /*LANG*/'Timer' }, '': { 'title': /*LANG*/'Timer' },
@ -172,7 +155,7 @@ function editTimer(alarmIndex, alarm) {
/*LANG*/'Vibrate': require("buzz_menu").pattern(a.vibrate, v => a.vibrate=v ), /*LANG*/'Vibrate': require("buzz_menu").pattern(a.vibrate, v => a.vibrate=v ),
}; };
menu[/*LANG*/"Save"] = function() { menu[/*LANG*/"Save"] = function() {
a.timer = encodeTime(t); a.timer = require("sched").encodeTime(t);
a.t = getCurrentTime() + a.timer; a.t = getCurrentTime() + a.timer;
a.last = 0; a.last = 0;
if (newAlarm) alarms.push(a); if (newAlarm) alarms.push(a);

View File

@ -2,7 +2,7 @@
"id": "alarm", "id": "alarm",
"name": "Alarms & Timers", "name": "Alarms & Timers",
"shortName": "Alarms", "shortName": "Alarms",
"version": "0.20", "version": "0.21",
"description": "Set alarms and timers on your Bangle", "description": "Set alarms and timers on your Bangle",
"icon": "app.png", "icon": "app.png",
"tags": "tool,alarm,widget", "tags": "tool,alarm,widget",

View File

@ -3,3 +3,4 @@
0.03: Fix `getTimeToAlarm` for a timer already used at same day, don't set `last` for timers. 0.03: Fix `getTimeToAlarm` for a timer already used at same day, don't set `last` for timers.
0.04: Fix `getTimeToAlarm` to check for next dow if alarm.t lower currentTime. 0.04: Fix `getTimeToAlarm` to check for next dow if alarm.t lower currentTime.
0.05: Export new functions (`newDefaultAlarm/Timer`), add Settings page 0.05: Export new functions (`newDefaultAlarm/Timer`), add Settings page
0.06: Refactor some methods to library

View File

@ -106,4 +106,21 @@ exports.getSettings = function () {
// Write the updated settings back to storage // Write the updated settings back to storage
exports.setSettings = function(settings) { exports.setSettings = function(settings) {
require("Storage").writeJSON("sched.settings.json", settings); require("Storage").writeJSON("sched.settings.json", settings);
}; };
// time in ms -> { hrs, mins }
exports.decodeTime = function(t) {
t = 0 | t; // sanitise
let hrs = 0 | (t / 3600000);
return { hrs: hrs, mins: Math.round((t - hrs * 3600000) / 60000) };
}
// time in { hrs, mins } -> ms
exports.encodeTime = function(o) {
return o.hrs * 3600000 + o.mins * 60000;
}
exports.formatTime = function(t) {
let o = decodeTime(t);
return o.hrs + ":" + ("0" + o.mins).substr(-2);
}

View File

@ -1,7 +1,7 @@
{ {
"id": "sched", "id": "sched",
"name": "Scheduler", "name": "Scheduler",
"version": "0.05", "version": "0.06",
"description": "Scheduling library for alarms and timers", "description": "Scheduling library for alarms and timers",
"icon": "app.png", "icon": "app.png",
"type": "scheduler", "type": "scheduler",

View File

@ -5,23 +5,11 @@ if (Bangle.SCHED) {
delete Bangle.SCHED; delete Bangle.SCHED;
} }
// time in ms -> { hrs, mins }
function decodeTime(t) {
t = 0|t; // sanitise
var hrs = 0|(t/3600000);
return { hrs : hrs, mins : Math.round((t-hrs*3600000)/60000) };
}
function formatTime(t) {
var o = decodeTime(t);
return o.hrs+":"+("0"+o.mins).substr(-2);
}
function showAlarm(alarm) { function showAlarm(alarm) {
const settings = require("sched").getSettings(); const settings = require("sched").getSettings();
let msg = ""; let msg = "";
msg += alarm.timer ? formatTime(alarm.timer) : formatTime(alarm.t); msg += alarm.timer ? require("sched").formatTime(alarm.timer) : require("sched").formatTime(alarm.t);
if (alarm.msg) { if (alarm.msg) {
msg += "\n"+alarm.msg; msg += "\n"+alarm.msg;
} else { } else {

View File

@ -2,3 +2,4 @@
0.02: Respect Quiet Mode 0.02: Respect Quiet Mode
0.03: Add compatibility for Bangle.js 2 and new firmware, added "Alarm at " for the alarm time 0.03: Add compatibility for Bangle.js 2 and new firmware, added "Alarm at " for the alarm time
0.04: Read alarms from new scheduling library, account for higher acceleration sensor noise on Bangle.js 2 0.04: Read alarms from new scheduling library, account for higher acceleration sensor noise on Bangle.js 2
0.05: Refactor decodeTime() to scheduling library

View File

@ -38,19 +38,11 @@ function calc_ess(acc_magn) {
} }
} }
// copied from alarm app
// time in ms -> { hrs, mins }
function decodeTime(t) {
t = 0|t; // sanitise
var hrs = 0|(t/3600000);
return { hrs : hrs, mins : Math.round((t-hrs*3600000)/60000) };
}
// locate next alarm // locate next alarm
var nextAlarm; var nextAlarm;
active.forEach(alarm => { active.forEach(alarm => {
const now = new Date(); const now = new Date();
const t = decodeTime(alarm.t); const t = require("sched").decodeTime(alarm.t);
var dateAlarm = new Date(now.getFullYear(), now.getMonth(), now.getDate(), t.hrs, t.mins); var dateAlarm = new Date(now.getFullYear(), now.getMonth(), now.getDate(), t.hrs, t.mins);
if (dateAlarm < now) { // dateAlarm in the past, add 24h if (dateAlarm < now) { // dateAlarm in the past, add 24h
dateAlarm.setTime(dateAlarm.getTime() + (24*60*60*1000)); dateAlarm.setTime(dateAlarm.getTime() + (24*60*60*1000));

View File

@ -2,11 +2,12 @@
"id": "sleepphasealarm", "id": "sleepphasealarm",
"name": "SleepPhaseAlarm", "name": "SleepPhaseAlarm",
"shortName": "SleepPhaseAlarm", "shortName": "SleepPhaseAlarm",
"version": "0.04", "version": "0.05",
"description": "Uses the accelerometer to estimate sleep and wake states with the principle of Estimation of Stationary Sleep-segments (ESS, see https://ubicomp.eti.uni-siegen.de/home/datasets/ichi14/index.html.en). This app will read the next alarm from the alarm application and will wake you up to 30 minutes early at the best guessed time when you are almost already awake.", "description": "Uses the accelerometer to estimate sleep and wake states with the principle of Estimation of Stationary Sleep-segments (ESS, see https://ubicomp.eti.uni-siegen.de/home/datasets/ichi14/index.html.en). This app will read the next alarm from the alarm application and will wake you up to 30 minutes early at the best guessed time when you are almost already awake.",
"icon": "app.png", "icon": "app.png",
"tags": "alarm", "tags": "alarm",
"supports": ["BANGLEJS","BANGLEJS2"], "supports": ["BANGLEJS","BANGLEJS2"],
"dependencies": {"scheduler":"type"},
"storage": [ "storage": [
{"name":"sleepphasealarm.app.js","url":"app.js"}, {"name":"sleepphasealarm.app.js","url":"app.js"},
{"name":"sleepphasealarm.img","url":"app-icon.js","evaluate":true} {"name":"sleepphasealarm.img","url":"app-icon.js","evaluate":true}

View File

@ -81,7 +81,7 @@ a.btn.btn-link.dropdown-toggle {
min-height: 8em; min-height: 8em;
} }
.tile-content { position: relative; } .tile-content { position: relative; word-break: break-word; }
.link-github { .link-github {
position:absolute; position:absolute;
top: 36px; top: 36px;