[sleeplog] Improving triggers and web interface

to 0.13:
- Prevent to stay in consecutive sleep if not worn.
- Correct calling triggers where last time is earlier then first time.
- Add the trigger object itself as argument to the fn function to be able to store data or functions inside the trigger object.

to 0.14:
improvements initiated by #2677
- Add a option to delete all logfiles before a specific date from the watch
- Correct regex file search from `\d\d\d\d` / `\d{4}` to `\d+`
pull/2679/head
Scharf 2023-04-05 16:47:09 +02:00
parent a33a6df463
commit 00a9db1bf0
6 changed files with 107 additions and 57 deletions

View File

@ -8,3 +8,5 @@
0.10beta: Add interface.html to view saved log data, add "View log" function for debugging log, send data for gadgetbridge, change caching for global getStats
0.11: Prevent module not found error
0.12: Improve README, option to add functions triggered by status changes or time periods, remove old log (<0.10) conversion
0.13: Prevent to stay in consecutive sleep if not worn, correct trigger calling, add trigger object itself as argument to the fn function
0.14: Add "Delete all logfiles before" to interface.html, display all logfiles in the interface

View File

@ -19,7 +19,7 @@ Logfiles are not removed on un-/reinstall to prevent data loss.
| Filename (* _example_) | Content | Removeable in |
|------------------------------|-----------------|-------------------|
| `sleeplog.log (StorageFile)` | recent logfile | App Web Interface |
| `sleeplog_1234.log`* | old logfiles | App Web Interface |
| `sleeplog_1234.log`* | past logfiles | App Web Interface |
| `sleeplog_123456.csv`* | debugging files | Web IDE |
@ -100,13 +100,20 @@ Logfiles are not removed on un-/reinstall to prevent data loss.
Available through the App Loader when your watch is connected.
- __view data__
Display the data to each timestamp in a table.
- __save csv-file__
Download a csv-file with the data to each timestamp.
The time format is chooseable beneath the file list.
- __delete file__
Deletes the logfile from the watch. __Please backup your data first!__
- A list of all found logfiles with following options for each file:
- __view data__
Display the data to each timestamp in a table.
- __save csv-file__
Download a csv-file with the data to each timestamp.
The time format is chooseable beneath the file list.
- __delete file__
Deletes the logfile from the watch. __Please backup your data first!__
- __csv time format__
__JavaScript (milliseconds since 1970)__ /
_UNIX (seconds since 1970)_ /
_Office (days since 1900)_
- __delete all logfiles before__
Deletes all logfile before the given date from the watch. __Please backup your data first!__
---
### Timestamps and Files
@ -184,11 +191,12 @@ if (typeof (global.sleeplog || {}).trigger === "object") {
from: 0, // 0 as default, in ms, first time fn will be called
to: 24*60*60*1000, // 24h as default, in ms, last time fn will be called
// reference time to from & to is rounded to full minutes
fn: function(data) { print(data); } // function to be executed
fn: function(data, thisTriggerEntry) { print(data); } // function to be executed
};
}
```
The passed data object has the following properties:
The passed __data__ object has the following properties:
- timestamp: of the status change as date object,
(should be around 10min. before "now", the actual call of the function)
- status: value of the new status (0-4),
@ -199,6 +207,9 @@ The passed data object has the following properties:
- prevConsecutive: if changed the value of the previous status (0-2) else undefined
If you want to use other variables or functions from the trigger object inside the trigger fn function, you will find them inside the __thisTriggerEntry__ object, as the this keyword is not working in this scenario. The function itself (the fn property) is not passed inside the thisTriggerEntry object.
---
### Worth Mentioning
---

View File

@ -235,6 +235,8 @@ if (sleeplog.conf.enabled) {
// reset consecutive status
data.consecutive = 0;
}
// reset consecutive sleep if not worn
if (data.status === 1) this.consecutive = 1;
// check if consecutive unknown
if (!this.consecutive) {
// check if long enough asleep or too long awake
@ -265,10 +267,13 @@ if (sleeplog.conf.enabled) {
// go through all triggers
triggers.forEach(key => {
// read entry to key
var entry = this.trigger[key];
let entry = this.trigger[key];
// set from and to values to default if unset
let from = entry.from || 0;
let to = entry.to || 24 * 60 * 60 * 1000;
// check if the event matches the entries requirements
if (typeof entry.fn === "function" && (changed || !entry.onChange) &&
(entry.from || 0) <= time && (entry.to || 24 * 60 * 60 * 1000) >= time)
(from <= to ? from <= time && time <= to : time <= to || from <= time))
// and call afterwards with status data
setTimeout(entry.fn, 100, {
timestamp: new Date(data.timestamp),
@ -276,7 +281,7 @@ if (sleeplog.conf.enabled) {
consecutive: data.consecutive,
prevStatus: data.status === this.status ? undefined : this.status,
prevConsecutive: data.consecutive === this.consecutive ? undefined : this.consecutive
});
}, (e => {delete e.fn; return e;})(entry.clone()));
});
}

View File

@ -84,7 +84,7 @@ function readLog(date, callback) {
function deleteFile(filename, callback) {
if (window.confirm("Do you really want to remove " + filename)) {
Util.showModal("Deleting...");
Util.showModal("Deleting " + filename + " ...");
if (filename.endsWith(" (StorageFile)")) {
Util.eraseStorageFile(filename, () => {
Util.hideModal();
@ -99,15 +99,29 @@ function deleteFile(filename, callback) {
}
}
function deleteBefore(dateString) {
date = new Date(dateString);
if (window.confirm("Do you really want to remove all data before" + date.toLocaleDateString(undefined))) {
Util.showModal("Deleting all data before" + date.toLocaleDateString(undefined) + " ...");
Puck.eval(`require("Storage").list(/^sleeplog_\\d+.log$/).forEach(file => {
date = (parseInt(file.match(/\\d+/)[0]) + 0.25) * 12096E5
print(date < ` + date + `)
})`, () => {
Util.hideModal();
callback();
});
}
}
function viewFiles() {
Util.showModal("Loading...");
domTable.innerHTML = "";
Puck.eval(`require("Storage").list(/^sleeplog_\\d\\d\\d\\d\\.log$/)`, files => {
Puck.eval(`require("Storage").list(/^sleeplog_\\d+.log$/)`, files => {
// add active log
files.push("" + Math.floor(Date.now() / 12096E5 - 0.25));
files = files.map(file => { return {
filename: file.length === 4 ? "sleeplog.log (StorageFile)" : file,
date: (parseInt(file.match(/\d{4}/)[0]) + 0.25) * 12096E5
date: (parseInt(file.match(/\d+/)[0]) + 0.25) * 12096E5
}});
files = files.sort((a, b) => a.date - b.date);
var html = `
@ -146,10 +160,10 @@ function viewFiles() {
<div class="container">
<form class="form-horizontal">
<div class="form-group">
<div class="col-3 col-sm-12">
<div class="col-sm-12">
<label class="form-label"><b>csv time format</b></label>
</div>
<div class="col-9 col-sm-12">
<div class="col-sm-12">
<select class="form-select" id="csvTime">
<option>JavaScript (milliseconds since 1970)</option>
<option>UNIX (seconds since 1970)</option>
@ -158,6 +172,23 @@ function viewFiles() {
</div>
</div>
</form>
</div>
<div class="container">
<form class="form-horizontal">
<div class="form-group">
<div class="col-sm-12">
<label class="form-label"><b>Delete all logfiles before</b></label>
</div>
<div class="col-sm-10">
<input class="form-input" id="delBeforeDate" type="date" value="2022-01-01">
</div>
<div class="col-mx-auto">
<button class="btn tooltip btn-error" data-tooltip="delete old files" task="delBefore" filename="" date="">
<i class="icon icon-delete"></i>
</button>
</div>
</div>
</form>
</div>`;
domTable.innerHTML = html;
Util.hideModal();
@ -172,6 +203,7 @@ function viewFiles() {
if (task === "view") readLog(date, logData => viewLog(logData, filename));
else if (task === "csv") readLog(date, logData => saveCSV(logData, date, date + 12096E5));
else if (task === "del") deleteFile(filename, () => viewFiles());
else if (task === "delBefore") deleteBefore(document.getElementById("delBeforeDate").value);
});
}
});

View File

@ -2,7 +2,7 @@
"id":"sleeplog",
"name":"Sleep Log",
"shortName": "SleepLog",
"version": "0.12",
"version": "0.14",
"description": "Log and view your sleeping habits. This app is using the built in movement calculation.",
"icon": "app.png",
"type": "app",