Skip to content

Commit 294690a

Browse files
authored
Merge pull request #64 from atjn/storage-info
Add device storage info
2 parents 1cdcb34 + e496c04 commit 294690a

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

js/.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"parserOptions": {
3-
"ecmaVersion": 6,
3+
"ecmaVersion": 2020,
44
"sourceType": "script"
55
},
66
"rules": {

js/comms.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ const Comms = {
196196
});
197197
});
198198
},
199-
// Get Device ID and version, plus a JSON list of installed apps
199+
// Get Device ID, version, storage stats, and a JSON list of installed apps
200200
getDeviceInfo : (noReset) => {
201-
Progress.show({title:`Getting app list...`,sticky:true});
201+
Progress.show({title:`Getting device info...`,sticky:true});
202202
return new Promise((resolve,reject) => {
203203
Puck.write("\x03",(result) => {
204204
if (result===null) {
@@ -240,7 +240,7 @@ const Comms = {
240240
return;
241241
}
242242

243-
let cmd, finalJS = `E.toJS([process.env.BOARD,process.env.VERSION,process.env.EXPTR,0|getTime(),E.CRC32(getSerial()+NRF.getAddress())]).substr(1)`;
243+
let cmd, finalJS = `JSON.stringify(require("Storage").getStats?require("Storage").getStats():{})+","+E.toJS([process.env.BOARD,process.env.VERSION,process.env.EXPTR,0|getTime(),E.CRC32(getSerial()+NRF.getAddress())]).substr(1)`;
244244
if (Const.SINGLE_APP_ONLY) // only one app on device, info file is in app.info
245245
cmd = `\x10Bluetooth.println("["+(require("Storage").read("app.info")||"null")+","+${finalJS})\n`;
246246
else
@@ -259,12 +259,13 @@ const Comms = {
259259
let appList;
260260
try {
261261
appList = JSON.parse(appListJSON);
262-
// unpack the last 4 elements which are board info (See finalJS above)
262+
// unpack the last 6 elements which are board info (See finalJS above)
263263
info.uid = appList.pop(); // unique ID for watch (hash of internal serial number and MAC)
264264
info.currentTime = appList.pop()*1000; // time in ms
265265
info.exptr = appList.pop(); // used for compilation
266266
info.version = appList.pop();
267267
info.id = appList.pop();
268+
info.storageStats = appList.pop(); // how much storage has been used
268269
// if we just have 'null' then it means we have no apps
269270
if (appList.length==1 && appList[0]==null)
270271
appList = [];

js/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,7 @@ function getInstalledApps(refresh) {
995995
device.id = info.id;
996996
device.version = info.version;
997997
device.exptr = info.exptr;
998+
device.storageStats = info.storageStats;
998999
device.appsInstalled = info.apps;
9991000
haveInstalledApps = true;
10001001
if ("function"==typeof onFoundDeviceInfo)
@@ -1016,11 +1017,39 @@ function getInstalledApps(refresh) {
10161017
const deviceInfoElem = document.getElementById("more-deviceinfo");
10171018
if (deviceInfoElem) {
10181019
deviceInfoElem.style.display = "inherit";
1020+
let storageRow = "";
1021+
if (device.storageStats?.totalBytes) {
1022+
const stats = device.storageStats;
1023+
const totalKB = (stats.totalBytes / 1000).toFixed(2);
1024+
const usedKB = (stats.fileBytes / 1000).toFixed(2);
1025+
const trashKB = (stats.trashBytes / 1000).toFixed(2);
1026+
const freeKB = (stats.freeBytes / 1000).toFixed(2);
1027+
const bytePrc = 100 / stats.totalBytes;
1028+
const usedPrc = bytePrc * stats.fileBytes;
1029+
const trashPrc = bytePrc * stats.trashBytes;
1030+
const freePrc = bytePrc * stats.freeBytes;
1031+
if (isNaN(usedPrc) || isNaN(trashPrc) || isNaN(freePrc)) {
1032+
console.error("Unexpected error: Could not calculate storage statistics");
1033+
} else {
1034+
storageRow = `
1035+
<tr><td><b>Storage</b></td><td>
1036+
<p style="margin-bottom:.4rem;">${totalKB} KiB in total, ${stats.fileCount} files used, ${stats.trashCount} files trashed.</p>
1037+
<div class="bar" style="margin-bottom:.3rem;">
1038+
<!-- These styles prevent overflow of text if the bar item is too small to fit all the text -->
1039+
<style>.bar-item{white-space:nowrap;padding-left:.1rem;padding-right:.1rem;}</style>
1040+
<div class="bar-item tooltip bg-error" data-tooltip="${usedKB} KiB, ${usedPrc.toFixed(2)}% used" style="width:${usedPrc}%; color:hsl(218 16% 2%)">${usedPrc.toFixed(0)}% used</div>
1041+
<div class="bar-item tooltip bg-warning" data-tooltip="${trashKB} KiB, ${trashPrc.toFixed(2)}% trash" style="width:${trashPrc}%;color:hsl(218 16% 7%)">${trashPrc.toFixed(0)}% trash</div>
1042+
<div class="bar-item tooltip bg-success" data-tooltip="${freeKB} KiB, ${freePrc.toFixed(2)}% free" style="width:${freePrc}%; color:hsl(218 16% 7%)">${freePrc.toFixed(0)}% free</div>
1043+
</div>
1044+
</td></tr>`;
1045+
}
1046+
}
10191047
const deviceInfoContentElem = document.getElementById("more-deviceinfo-content");
10201048
deviceInfoContentElem.innerHTML = `
10211049
<table class="table"><tbody>
10221050
<tr><td><b>Device Type</b></td><td>${device.id}</td></tr>
10231051
<tr><td><b>Firmware Version</b></td><td>${device.version}</td></tr>
1052+
${storageRow}
10241053
<tr><td><b>Apps Installed</b></td><td>${(device.appsInstalled || []).map(a => `${a.id} (${a.version})`).join(", ")}</td></tr>
10251054
</tbody></table>`;
10261055
}

0 commit comments

Comments
 (0)