1
0
Fork 0
BangleApps/apps/sleeplog/interface.html

144 lines
4.3 KiB
HTML
Raw Normal View History

2022-05-24 08:14:10 +00:00
<html>
<head>
<link rel="stylesheet" href="../../css/spectre.min.css">
</head>
<body>
<div id="table"></div>
<script src="../../core/lib/interface.js"></script>
<script>
var domTable = document.getElementById("table");
2022-05-24 10:28:17 +00:00
function saveFile(logData, title) {
var fileFormat = domTable.getElementById("fileFormat").selectedIndex;
2022-05-24 10:37:43 +00:00
2022-05-24 10:28:17 +00:00
if (fileFormat === 1) {
Util.saveCSV(
2022-05-24 10:52:08 +00:00
title + ".txt",
2022-05-24 10:28:17 +00:00
logData.map(entry => {
return new Date(entry[0] *= 6E5).toLocaleString(undefined) + " > " +
"unknown ,non consec.,consecutive".split(",")[entry[2]] + " " +
"unknown,not worn,awake,light sleep,deep sleep".split(",")[entry[1]];
}).join("\n")
);
} else {
var timeFormat = domTable.getElementById("timeFormat").selectedIndex;
Util.saveCSV(
title + ".csv",
"time,sleep,consecutive\n" +
logData.forEach(entry => {
entry[0] *= 6E5;
if (timeFormat === 1) entry[0] /= 1E3;
if (timeFormat === 2) entry[0] = entry[0] / 864E5 + 25569;
return entry.join(",");
2022-05-24 10:37:43 +00:00
}).join("\n")
);
2022-05-24 10:28:17 +00:00
}
2022-05-24 08:14:10 +00:00
}
function readLog(date, callback) {
Util.showModal("Downloading logged data...");
Puck.eval(`require("sleeplog").readLog(date, date + 12096E5)`, logData => {
Util.hideModal();
callback(logData);
});
}
function getFnList() {
Util.showModal("Loading...");
domTable.innerHTML = "";
Puck.eval(`require("Storage").list(/^sleeplog_\\d\\d\\d\\d\\.log$/)`, files => {
2022-05-24 08:14:10 +00:00
// add this fortnight
files.push("" + Math.floor(Date.now() / 12096E5 - 0.25));
files = files.map(file => {
2022-05-24 08:34:46 +00:00
var ret = {
2022-05-24 10:28:17 +00:00
bName: file,
2022-05-24 11:01:54 +00:00
date: (parseInt(file.substr(9, 4)) + 0.25) * 12096E5,
2022-05-24 10:52:08 +00:00
}
2022-05-24 11:16:03 +00:00
console.log(ret.date);
var thisDates = [new Date(ret.date), new Date(ret.date + 12096E5)];
2022-05-24 11:18:48 +00:00
ret.dName = thisDates[0].toISOString().substr(0, 10) + "_" + thisDates[1].toISOString().substr(5, 5);
ret.dateA = thisDates[0].toLocaleDateString(undefined);
ret.dateB = thisDates[1].toLocaleDateString(undefined);
2022-05-24 08:14:10 +00:00
return ret;
2022-05-24 08:34:46 +00:00
});
2022-05-24 10:28:17 +00:00
files = files.sort((a, b) => a.fortnigt - b.fortnigt);
2022-05-24 08:34:46 +00:00
var html = `
2022-05-24 10:28:17 +00:00
<table class="table table-striped table-hover">
<thead>
<tr>
2022-05-24 10:52:08 +00:00
<th>File</th>
<th>Date from</th>
<th>to</th>
<th>Actions</th>
2022-05-24 10:28:17 +00:00
</tr>
</thead>
<tbody>`;
2022-05-24 08:14:10 +00:00
files.forEach(file => {
html += `
2022-05-24 10:28:17 +00:00
<tr>
2022-05-24 10:52:08 +00:00
<td>${file.bName.endsWith(".log") ? file.bName : "active log"}</td>
<td>${file.dateA}</td>
<td>${file.dateB}</td>
2022-05-24 10:28:17 +00:00
<td>
<button class="btn btn-primary" task="download" date="${file.date}" dName="${file.dName}">Download</button>`;
if (file.bName.endsWith(".log")) html += `
<button class="btn btn-default" task="delete" bName="${file.bName}">Delete</button>`;
2022-05-24 08:14:10 +00:00
html += `
2022-05-24 10:28:17 +00:00
</td>
</tr>`;
2022-05-24 08:14:10 +00:00
});
html += `
2022-05-24 10:28:17 +00:00
</tbody>
</table>
<table>
<tr>
<td>file format:</td>
2022-05-24 10:52:08 +00:00
<td><select id="fileFormat">
2022-05-24 10:28:17 +00:00
<option>csv</option>
<option>text</option>
</select></td>
</tr>
<tr>
<td>csv time format:</td>
2022-05-24 10:52:08 +00:00
<td><select id="timeFormat">
2022-05-24 10:28:17 +00:00
<option>JavaScript (msec since 1970)</option>
<option>UNIX (sec since 1970)</option>
<option>Office (days since 1900)</option>
</select></td>
</tr>
</table>`;
2022-05-24 08:14:10 +00:00
domTable.innerHTML = html;
Util.hideModal();
var buttons = domTable.querySelectorAll("button");
2022-05-24 08:34:46 +00:00
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", event => {
2022-05-24 08:14:10 +00:00
var button = event.currentTarget;
var task = button.getAttribute("task");
if (!task) return;
2022-05-24 10:28:17 +00:00
if (task=="download") {
2022-05-24 08:14:10 +00:00
var date = button.getAttribute("date");
2022-05-24 10:28:17 +00:00
var dName = button.getAttribute("dName");
if (!date || !dName) return;
readLog(date, logData => saveFile(logData, dName));
2022-05-24 08:14:10 +00:00
} else if (task=="delete") {
2022-05-24 10:28:17 +00:00
var bName = button.getAttribute("bName");
if (!bName) return;
2022-05-24 08:14:10 +00:00
Util.showModal("Deleting...");
2022-05-24 10:28:17 +00:00
Util.eraseStorage(bName, () => {
2022-05-24 08:14:10 +00:00
Util.hideModal();
getTrackList();
});
}
});
}
2022-05-24 08:34:46 +00:00
});
2022-05-24 08:14:10 +00:00
}
function onInit() {
getFnList();
}
</script>
</body>
</html>