From 58af5410409733f1598342841c671fd4a98e92ae Mon Sep 17 00:00:00 2001 From: Ishidres <17363426+Ishidres@users.noreply.github.com> Date: Wed, 20 Mar 2024 14:53:15 +0100 Subject: [PATCH] Refactor --- apps/sleeplog/boot.js | 36 +++++++++++++++++++----------------- apps/sleeplog/settings.js | 7 +++++++ 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/apps/sleeplog/boot.js b/apps/sleeplog/boot.js index 9a90ef3e6..44b7c08c9 100644 --- a/apps/sleeplog/boot.js +++ b/apps/sleeplog/boot.js @@ -13,6 +13,7 @@ global.sleeplog = { minConsec: 18E5, // [ms] minimal time to count for consecutive sleep deepTh: 100, // threshold for deep sleep lightTh: 200, // threshold for light sleep + tempWearCheck: false, // use temperature to detect if worn wearTemp: 29, // temperature threshold to count as worn }, require("Storage").readJSON("sleeplog.json", true) || {}) }; @@ -166,22 +167,23 @@ if (sleeplog.conf.enabled) { // check if changing to deep sleep from non sleeping if (data.status === 4 && sleeplog.status <= 2) { - // check wearing status - // if not worn set status to not worn - if (sleeplog.isNotWorn()) { - data.status = 1; - } - - sleeplog.setStatus(data); - - /* - sleeplog.checkIsWearing((isWearing, data) => { - // correct status - if (!isWearing) data.status = 1; - // set status + // check wearing status either based on HRM or temperature as set in settings + if (this.conf.tempWearCheck) { + // if not worn set status to not worn + if (!sleeplog.isWornByTemp()) { + data.status = 1; + } + sleeplog.setStatus(data); - }, data); - */ + } else { + // if not worn set status to not worn + sleeplog.checkIsWearing((isWearing, data) => { + // correct status + if (!isWearing) data.status = 1; + // set status + sleeplog.setStatus(data); + }, data); + } } else { // set status sleeplog.setStatus(data); @@ -221,8 +223,8 @@ if (sleeplog.conf.enabled) { // Determine if Bangle.JS is worn based on temperature (same strategy as in activityreminder) // https://github.com/espruino/BangleApps/blob/master/apps/activityreminder/boot.js#L37 - isNotWorn: function() { - return (Bangle.isCharging() || this.conf.wearTemp > E.getTemperature()); + isWornByTemp: function() { + return (!Bangle.isCharging() && E.getTemperature() >= this.conf.wearTemp); }, // define function to set the status diff --git a/apps/sleeplog/settings.js b/apps/sleeplog/settings.js index a2c428c29..d23a6369a 100644 --- a/apps/sleeplog/settings.js +++ b/apps/sleeplog/settings.js @@ -13,6 +13,7 @@ minConsec: 18E5, // [ms] minimal time to count for consecutive sleep deepTh: 100, // threshold for deep sleep lightTh: 200, // threshold for light sleep + tempWearCheck: false, // use temperature to detect if worn wearTemp: 29, // temperature threshold to count as worn // app settings breakToD: 12, // [h] time of day when to start/end graphs @@ -435,6 +436,12 @@ require("sleeplog").setEnabled(v); } }, + /*LANG*/"Wear detection using temperature": { + value: settings.tempWearCheck, + onchange: v => { + settings.tempWearCheck = v; + } + }, /*LANG*/"Debugging": { value: debugImg, onchange: () => setTimeout(showDebug, 10)