diff --git a/apps/sleepphasealarm/ChangeLog b/apps/sleepphasealarm/ChangeLog index 875b3c1da..94ad5852e 100644 --- a/apps/sleepphasealarm/ChangeLog +++ b/apps/sleepphasealarm/ChangeLog @@ -3,3 +3,4 @@ 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.05: Refactor decodeTime() to scheduling library +0.06: Add logging diff --git a/apps/sleepphasealarm/app.js b/apps/sleepphasealarm/app.js index 236b71c0b..0a8e83a08 100644 --- a/apps/sleepphasealarm/app.js +++ b/apps/sleepphasealarm/app.js @@ -1,6 +1,8 @@ const BANGLEJS2 = process.env.HWVERSION == 2; //# check for bangle 2 -const alarms = require("Storage").readJSON("sched.json",1)||[]; +const alarms = require("Storage").readJSON("sched.json",1) || []; +const config = require("Storage").readJSON("sleepphasealarm.json",1) || {logs: []}; const active = alarms.filter(a=>a.on); +let logs = []; // Sleep/Wake detection with Estimation of Stationary Sleep-segments (ESS): // Marko Borazio, Eugen Berlin, Nagihan Kücükyildiz, Philipp M. Scholl and Kristof Van Laerhoven, "Towards a Benchmark for Wearable Sleep Analysis with Inertial Wrist-worn Sensing Units", ICHI 2014, Verona, Italy, IEEE Press, 2014. @@ -108,12 +110,20 @@ function buzz() { }); } +function addLog(time, type) { + logs.push({time: time, type: type}); + require("Storage").writeJSON("sleepphasealarm.json", config); +} + // run var minAlarm = new Date(); var measure = true; if (nextAlarm !== undefined) { - Bangle.loadWidgets(); //# correct widget load draw order + config.logs[nextAlarm.getDate()] = []; // overwrite log on each day of month + logs = config.logs[nextAlarm.getDate()]; + Bangle.loadWidgets(); Bangle.drawWidgets(); + let swest_last; // minimum alert 30 minutes early minAlarm.setTime(nextAlarm.getTime() - (30*60*1000)); @@ -124,7 +134,16 @@ if (nextAlarm !== undefined) { if (swest !== undefined) { if (Bangle.isLCDOn()) { - drawString(swest ? "Sleep" : "Awake", BANGLEJS2 ? 150 : 180); //# remove x, adjust height + drawString(swest ? "Sleep" : "Awake", BANGLEJS2 ? 150 : 180); + } + // log + if (swest_last != swest) { + if (swest) { + addLog(new Date(Date.now() - sleepthresh*13/12.5*1000), "sleep"); // calculate begin of no motion phase, 13 values/second at 12.5Hz + } else { + addLog(new Date(), "awake"); + } + swest_last = swest; } } @@ -132,6 +151,7 @@ if (nextAlarm !== undefined) { // The alarm widget should handle this one setTimeout(load, 1000); } else if (measure && now >= minAlarm && swest === false) { + addLog(new Date(), "alarm"); buzz(); measure = false; } diff --git a/apps/sleepphasealarm/interface.html b/apps/sleepphasealarm/interface.html new file mode 100644 index 000000000..d9e5a2504 --- /dev/null +++ b/apps/sleepphasealarm/interface.html @@ -0,0 +1,103 @@ + + + + + + +

Please select a wakeup day:

+
+ +
+
+ +
+ + + + + + + diff --git a/apps/sleepphasealarm/metadata.json b/apps/sleepphasealarm/metadata.json index aecfa36e4..aba240e37 100644 --- a/apps/sleepphasealarm/metadata.json +++ b/apps/sleepphasealarm/metadata.json @@ -2,7 +2,7 @@ "id": "sleepphasealarm", "name": "SleepPhaseAlarm", "shortName": "SleepPhaseAlarm", - "version": "0.05", + "version": "0.06", "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", "tags": "alarm", @@ -11,5 +11,7 @@ "storage": [ {"name":"sleepphasealarm.app.js","url":"app.js"}, {"name":"sleepphasealarm.img","url":"app-icon.js","evaluate":true} - ] + ], + "data": [{"name":"sleepphasealarm.json","storageFile":true}], + "interface": "interface.html" }