diff --git a/apps/twenties/ChangeLog b/apps/twenties/ChangeLog index 3381ef6a9f..fc8759718c 100644 --- a/apps/twenties/ChangeLog +++ b/apps/twenties/ChangeLog @@ -4,3 +4,4 @@ 0.04: Convert to boot code 0.05: Improve energy efficiency 0.06: Align with whole thirds of the hour. +0.07: Refactor to use the scheduling library. diff --git a/apps/twenties/boot.js b/apps/twenties/boot.js index 1b55a577fe..295bf42a8d 100644 --- a/apps/twenties/boot.js +++ b/apps/twenties/boot.js @@ -1,31 +1,6 @@ -(() => { - const LOOP_INTERVAL = 1.2e6; // 20 minutes - const BUZZ_INTERVAL = 2e4; // 20 seconds - - const isWorkTime = (d) => - d.getDay() % 6 && d.getHours() >= 8 && d.getHours() < 18; - - const scheduleNext = () => { - const now = new Date(); - if (isWorkTime(now)) { - Bangle.buzz().then(() => setTimeout(Bangle.buzz, BUZZ_INTERVAL)); - setTimeout(scheduleNext, LOOP_INTERVAL); - } else { - const next = new Date(now); - next.setHours(8, 0, 0, 0); - while (!isWorkTime(next)) next.setDate(next.getDate() + 1); - setTimeout(scheduleNext, next - now); - } - }; - - // Align so we fire at whole hour, 20 min past and 40 min past - not at arbitrary times. - const TIME_AT_BOOT = new Date(); - const TIME_SINCE_WHOLE_THIRD_OF_HOUR = (TIME_AT_BOOT.getMinutes() % 20) * 6e4 + TIME_AT_BOOT.getSeconds() * 1e3; - setTimeout(scheduleNext, - LOOP_INTERVAL - TIME_SINCE_WHOLE_THIRD_OF_HOUR); - // Make sure we don't miss the 2nd buzz after 20 seconds if we rebooted during that interval. - if (TIME_SINCE_WHOLE_THIRD_OF_HOUR <= BUZZ_INTERVAL) { - setTimeout(Bangle.buzz, - BUZZ_INTERVAL - TIME_SINCE_WHOLE_THIRD_OF_HOUR); - } -})(); +{ // This boot script is deleted once the companion alarm is set up. + // The app will continue working by having the alarm rearm itself until + // the app is uninstalled. + require("twenties").setup(); + setTimeout(require("Storage").erase, 1000, "twenties.boot.js"); +} diff --git a/apps/twenties/lib.js b/apps/twenties/lib.js new file mode 100644 index 0000000000..e33ce464a2 --- /dev/null +++ b/apps/twenties/lib.js @@ -0,0 +1,53 @@ +{ + exports.getTimeAtNextBuzz = () => { + const isWorkTime = (d) => + d.getDay() % 6 && d.getHours() >= 8 && d.getHours() < 18; + const isLookAwayTime = (d) => + d.getMinutes() % 20 === 0 && d.getSeconds() < 20; + const NOW = new Date(); + let t = 8 * 3600000; + if (isWorkTime(NOW)) { + if (isLookAwayTime(NOW)) { + t = NOW.getHours() * 3600000 + + NOW.getMinutes() * 60000 + 20 * 1000; + } else { + t = NOW.getHours() * 3600000 + + (NOW.getMinutes() + (20 - NOW.getMinutes() % 20)) * 60000; + } + } + print(t); + return t; + }; + + exports.buzzAndSetup = function () { + setTimeout(() => { // Timeout to try and not interfere with `edgeclk` redrawing the time. + Bangle.buzz(); + exports.setup(); + }, 10) + }; + + const JS_DELETE_ALARM_THEN_BUZZ_AND_SETUP = `require("sched").setAlarm("twenties", undefined); require('twenties').buzzAndSetup();`; + + const S = require("sched"); + + exports.setup = function () { + const TIME_AT_NEXT_BUZZ = exports.getTimeAtNextBuzz(); + const TIME_AT_SETUP = new Date(); + let alarm = { + on: true, + t: TIME_AT_NEXT_BUZZ, + dow: 0b0111110, + hidden: true, + group: "Hidden", + js: JS_DELETE_ALARM_THEN_BUZZ_AND_SETUP + }; + if (TIME_AT_NEXT_BUZZ < + TIME_AT_SETUP.getHours() * 3600000 + + TIME_AT_SETUP.getMinutes() * 60000 + + TIME_AT_SETUP.getSeconds() * 1000) { // FIXME: this is done to work around a behavior in sched library. I think that is maybe a :BUG: that we should fix there. But unsure. It's around https://github.com/espruino/BangleApps/blob/master/apps/sched/boot.js#L21-L21 + alarm.last = Date().getDate(); + } + S.setAlarm("twenties", alarm); + S.reload(); + }; +} diff --git a/apps/twenties/metadata.json b/apps/twenties/metadata.json index 62b12d1401..12c20a7a9e 100644 --- a/apps/twenties/metadata.json +++ b/apps/twenties/metadata.json @@ -2,7 +2,7 @@ "id": "twenties", "name": "Twenties", "shortName": "Twenties", - "version": "0.06", + "version": "0.07", "description": "Buzzes every 20m to stand / sit and look 20ft away for 20s.", "icon": "app.png", "type": "bootloader", @@ -11,5 +11,8 @@ "supports": ["BANGLEJS", "BANGLEJS2"], "allow_emulator": true, "readme": "README.md", - "storage": [{ "name": "twenties.boot.js", "url": "boot.js" }] + "storage": [ + { "name": "twenties.boot.js", "url": "boot.js" }, + { "name": "twenties", "url": "lib.js" } + ] }