From 75032540333dbbbd0ac75b926963bb9e2f9eb3d5 Mon Sep 17 00:00:00 2001 From: Erik Andresen Date: Mon, 18 Mar 2024 19:35:07 +0100 Subject: [PATCH] sleepphasealarm: Fix json dates Fix handle dates saved as object with milliseconds --- apps/sleepphasealarm/ChangeLog | 1 + apps/sleepphasealarm/app.js | 2 +- apps/sleepphasealarm/interface.html | 6 +++--- apps/sleepphasealarm/metadata.json | 2 +- apps/sleepphasealarm/settings.js | 16 ++++++++++------ 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/apps/sleepphasealarm/ChangeLog b/apps/sleepphasealarm/ChangeLog index d6e3cb30b..a836cac23 100644 --- a/apps/sleepphasealarm/ChangeLog +++ b/apps/sleepphasealarm/ChangeLog @@ -19,3 +19,4 @@ Add plot logged data to settings 0.15: Convert Yes/No On/Off in settings to checkboxes 0.16: Fix Keep alarm enabled inverted settings +0.17: Fix handle dates saved as object with milliseconds diff --git a/apps/sleepphasealarm/app.js b/apps/sleepphasealarm/app.js index 25ca7e781..9560d9c8d 100644 --- a/apps/sleepphasealarm/app.js +++ b/apps/sleepphasealarm/app.js @@ -136,7 +136,7 @@ function buzz() { } function addLog(time, type) { - logs.push({time: time, type: type}); + logs.push({time: time.toISOString(), type: type}); if (logs.length > 1) { // Do not write if there is only one state require("Storage").writeJSON(CONFIGFILE, config); } diff --git a/apps/sleepphasealarm/interface.html b/apps/sleepphasealarm/interface.html index 9b2004904..da73ed9c4 100644 --- a/apps/sleepphasealarm/interface.html +++ b/apps/sleepphasealarm/interface.html @@ -35,7 +35,7 @@ function getData() { logs.forEach((log, i) => { const timeStr = log.filter(entry => entry.type === "alarm")[0]?.time; if (timeStr) { - const date = new Date(timeStr); + const date = new Date(typeof timeStr === 'string' ? timeStr : timeStr.ms); let option = document.createElement("option"); option.text = date.toLocaleDateString(); option.value = i; @@ -89,10 +89,10 @@ function getData() { select.onchange = () => { const log = logs[select.value]; - chart.data.labels = log.map(entry => new Date(entry.time)); + chart.data.labels = log.map(entry => new Date(typeof entry.time === 'string' ? entry.time : entry.time.ms)); chart.data.datasets[0].data = log.map(entry => yTicks.indexOf(entry.type)); const timeStr = log.filter(entry => entry.type === "alarm")[0]?.time; - chart.data.datasets[0].label = new Date(timeStr).toLocaleDateString(); + chart.data.datasets[0].label = new Date(typeof timeStr === 'string' ? timeStr : timeStr.ms).toLocaleDateString(); chart.update(); } }); diff --git a/apps/sleepphasealarm/metadata.json b/apps/sleepphasealarm/metadata.json index aa69694ab..667c1259b 100644 --- a/apps/sleepphasealarm/metadata.json +++ b/apps/sleepphasealarm/metadata.json @@ -2,7 +2,7 @@ "id": "sleepphasealarm", "name": "SleepPhaseAlarm", "shortName": "SleepPhaseAlarm", - "version": "0.16", + "version": "0.17", "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": "tool,alarm", diff --git a/apps/sleepphasealarm/settings.js b/apps/sleepphasealarm/settings.js index e72d95e7f..f0661fc78 100644 --- a/apps/sleepphasealarm/settings.js +++ b/apps/sleepphasealarm/settings.js @@ -16,8 +16,8 @@ function draw(log) { const step = 10*60*1000; // resolution 10min const yTicks = ["sleep", "awake", "alarm"]; - const starttime = new Date(log[0].time); - const endtime = new Date(log[log.length-1].time); + const starttime = dateFromJson(log[0].time); + const endtime = dateFromJson(log[log.length-1].time); let logidx = 0; let curtime = starttime; @@ -29,7 +29,7 @@ if (logtime === undefined || curtime > logtime) { curval = yTicks.indexOf(log[logidx].type); logidx++; - logtime = new Date(log[logidx].time); + logtime = dateFromJson(log[logidx].time); } data[i++] = curval; @@ -70,8 +70,8 @@ const logs = config.logs.filter(log => log != null && log.filter(entry => entry.type === "alarm").length > 0); logs.sort(function(a, b) { // sort by alarm date desc - const adate = new Date(a.filter(entry => entry.type === "alarm")[0].time); - const bdate = new Date(b.filter(entry => entry.type === "alarm")[0].time); + const adate = dateFromJson(a.filter(entry => entry.type === "alarm")[0].time); + const bdate = dateFromJson(b.filter(entry => entry.type === "alarm")[0].time); return bdate - adate; }); @@ -79,12 +79,16 @@ menu[""] = { title: /*LANG*/"Select day" }; menu["< Back"] = () => settingsmenu(); logs.forEach((log, i) => { - const date = new Date(log.filter(entry => entry.type === "alarm")[0].time); + const date = dateFromJson(log.filter(entry => entry.type === "alarm")[0].time); menu[require("locale").date(date, 1)] = () => { E.showMenu(); draw(log); }; }); E.showMenu(menu); } + function dateFromJson(o) { + return new Date(typeof o === 'string' ? o : o.ms); + } + function settingsmenu() { // Show the menu E.showMenu({