Skip to content
Merged
1 change: 1 addition & 0 deletions apps/smartbatt/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
v0.01: New app!
v0.02: Add dynamic intervals, depending on total cycles recorded
v0.03: Return more data inside of .get();
v0.04: Return to fixed update intervals, added setting to change update interval, limit cycles to 60 for greater precision over longer period of time
6 changes: 6 additions & 0 deletions apps/smartbatt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ The module provides two clockInfos:
## Settings
### Clear Data - Clears all learned data.
Use this when you switch to a new clock or change the battery drainage in a fundamental way. The app averages drainage over time, and so you might just want to restart the learned data to be more accurate for the new configurations you have implemented.
### Update Interval - The time that the module should record battery drainage
This changes the interval when you record battery drainage. Generally, a longer interval means more precise estimates, at the cost of quick updates.

Min: 30 minutes

Max: 48 hours
### Logging - Enables logging for stats that this module uses.
To view the log file, go to the [Web IDE](https://www.espruino.com/ide/#), click on the storage icon (4 discs), and scroll to the file named `smartbattlog.json`. From there, you can view the file, copy to editor, or save it to your computer.
Logs:
Expand Down
2 changes: 1 addition & 1 deletion apps/smartbatt/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "smartbatt",
"name": "Smart Battery Module",
"shortName": "Smart Battery",
"version": "0.03",
"version": "0.04",
"description": "Provides a `smartbatt` module that returns the battery in days, and learns from daily usage over time for accurate predictions.",
"icon": "app.png",
"type": "module",
Expand Down
40 changes: 15 additions & 25 deletions apps/smartbatt/module.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
var dataFile = "smartbattdata.json";
var interval;
var storage = require("Storage");


Expand All @@ -9,7 +8,8 @@
function getSettings() {
return Object.assign({
//Record Interval stored in ms
doLogging: false
doLogging: false,
updateInterval:18000000 //default to 5 hours
}, require('Storage').readJSON("smartbatt.settings.json", true) || {});
}

Expand Down Expand Up @@ -65,8 +65,16 @@
let newAvg = weightedAverage(data.avgBattDrainage, data.totalHours, currentDrainage, deltaHours * weightCoefficient);
data.avgBattDrainage = newAvg;
data.timeLastRecorded = now;
data.totalCycles += 1;
data.totalHours += deltaHours;
if(data.totalCycles<60){
data.totalCycles += 1;
data.totalHours += deltaHours;

}else{
data.totalCycles=60;
data.totalHours=100;
}


data.battLastRecorded = batt;
storage.writeJSON(dataFile, data);

Expand All @@ -84,20 +92,7 @@
reason: reason
});
}
clearInterval(interval)
if(data.totalCycles<=200){
//5m intervals
interval=setInterval(recordBattery, 600000);
}else if(data.totalCycles<=300){
//30m intervals
interval=setInterval(recordBattery, 1800000);
}else if(data.totalCycles<=500){
//1h intervals
interval=setInterval(recordBattery, 3600000);
}else {
//3h intervals
interval=setInterval(recordBattery, 10800000);
}
setTimeout(recordBattery,getSettings().updateInterval);
}

function weightedAverage(oldValue, oldWeight, newValue, newWeight) {
Expand Down Expand Up @@ -139,14 +134,9 @@
storage.erase(logFile);
}
// Expose public API
exports.record = recordBattery;
exports.deleteData = deleteData;
exports.get = getExportData;
exports.changeInterval = function (newInterval) {
clearInterval(interval);
interval = setInterval(recordBattery, newInterval);
};
// Start recording every 5 minutes
interval = setInterval(recordBattery, 600000);

// Start recording every 8 hours for accurate long tracking
recordBattery(); // Log immediately
}
39 changes: 27 additions & 12 deletions apps/smartbatt/settings.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@

(function(back) {
var FILE = "smartbatt.settings.json";
// Load settings
var settings = Object.assign({
//Record Interval stored in ms
doLogging:false
doLogging:false,
updateInterval:18000000
}, require('Storage').readJSON(FILE, true) || {});

function writeSettings() {
require('Storage').writeJSON(FILE, settings);
}

// Show the menu
E.showMenu({
"" : { "title" : "Smart Day Battery" },
"" : { "title" : "Smart Battery" },
"< Back" : () => back(),

'Clear Data': function () {
Expand All @@ -28,15 +25,33 @@
}
});
},
'Log Battery': {
value: !!settings.doLogging, // !! converts undefined to false
'Update Interval': {
value: 0|settings.updateInterval,
min:1800000,
max:172800000,
step:1800000,
format: v=>{
var totalMinutes = Math.floor(v / 60000);
var h = Math.floor(totalMinutes / 60);
var m = totalMinutes % 60;

let result = '';
if (h > 0) result += h+"h";
if (m > 0) result += m+"m";

return result || '0m';
},
onchange: v => {
settings.updateInterval = v;
writeSettings();
}
},
'Log Battery': {
value: !!settings.doLogging,
onchange: v => {
settings.doLogging = v;
writeSettings();
}
// format: ... may be specified as a function which converts the value to a string
// if the value is a boolean, showMenu() will convert this automatically, which
// keeps settings menus consistent
},
}
});
})