From 689e6e74702dc1018daa22f7b242888ad3aece95 Mon Sep 17 00:00:00 2001 From: Stiralbios Date: Thu, 12 May 2022 10:30:11 +0200 Subject: [PATCH] Fix dates loading from file --- apps/activityreminder/boot.js | 24 +++++++++++------------- apps/activityreminder/lib.js | 24 ++++++++++++++++++------ 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/apps/activityreminder/boot.js b/apps/activityreminder/boot.js index 1c08f6c9c..bc07ebc28 100644 --- a/apps/activityreminder/boot.js +++ b/apps/activityreminder/boot.js @@ -2,25 +2,21 @@ function run() { if (isNotWorn()) return; var now = new Date(); var h = now.getHours(); - var doAlert = false; - var doSave = false; var health = Bangle.getHealthStatus("day"); if (h >= activityreminder_settings.startHour && h < activityreminder_settings.endHour) { - if (health.steps - activityreminder_data.stepsOnDate >= activityreminder_settings.minSteps) { + if (health.steps - activityreminder_data.stepsOnDate >= activityreminder_settings.minSteps // more steps made than needed + || health.steps < activityreminder_data.stepsOnDate) { // new day or reboot of the watch activityreminder_data.stepsOnDate = health.steps; activityreminder_data.stepsDate = now; - doSave = true; + activityreminder.saveData(activityreminder_data); + } + + if(activityreminder.mustAlert(activityreminder_data, activityreminder_settings)){ + load('activityreminder.app.js'); } - doAlert = activityreminder.mustAlert(activityreminder_data, activityreminder_settings); } - if (doSave) { - activityreminder.saveData(activityreminder_data); - } - if (doAlert) { - load('activityreminder.app.js'); - } } function isNotWorn() { @@ -32,7 +28,9 @@ const activityreminder = require("activityreminder"); activityreminder_settings = activityreminder.loadSettings(); if (activityreminder_settings.enabled) { activityreminder_data = activityreminder.loadData(); - activityreminder.saveData(activityreminder_data); + if(activityreminder_data.firstLoad){ + activityreminder_data.firstLoad =false; + activityreminder.saveData(activityreminder_data); + } setInterval(run, 60000); } - diff --git a/apps/activityreminder/lib.js b/apps/activityreminder/lib.js index 329087649..141579cae 100644 --- a/apps/activityreminder/lib.js +++ b/apps/activityreminder/lib.js @@ -22,20 +22,32 @@ exports.saveData = function (data) { exports.loadData = function () { var health = Bangle.getHealthStatus("day"); - return Object.assign({ + var data = Object.assign({ + firstLoad: true, stepsDate: new Date(), stepsOnDate: health.steps, - okDate: new Date(1970, 1, 1), - dismissDate: new Date(1970, 1, 1), - pauseDate: new Date(1970, 1, 1), + okDate: new Date(1970), + dismissDate: new Date(1970), + pauseDate: new Date(1970), }, - storage.readJSON("activityreminder.data.json") || {}); + storage.readJSON("activityreminder.data.json") || {}); + + if(typeof(data.stepsDate) == "string") + data.stepsDate = new Date(data.stepsDate); + if(typeof(data.okDate) == "string") + data.okDate = new Date(data.okDate); + if(typeof(data.dismissDate) == "string") + data.dismissDate = new Date(data.dismissDate); + if(typeof(data.stepsDate) == "string") + data.pauseDate = new Date(data.pauseDate); + + return data; }; exports.mustAlert = function(activityreminder_data, activityreminder_settings) { var now = new Date(); if ((now - activityreminder_data.stepsDate) / 60000 > activityreminder_settings.maxInnactivityMin) { // inactivity detected - if ((now - activityreminder_settings.okDate) / 60000 > 3 && // last alert was more than 3 min ago + if ((now - activityreminder_settings.okDate) / 60000 > 3 && // last alert anwsered with ok was more than 3 min ago (now - activityreminder_settings.dismissDate) / 60000 > activityreminder_settings.dismissDelayMin && // last alert was more than dismissDelayMin ago (now - activityreminder_settings.pauseDate) / 60000 > activityreminder_settings.pauseDelayMin) { // last alert was more than pauseDelayMin ago return true;