Skip to content

Commit e46c386

Browse files
authored
Merge pull request #4068 from RKBoss6/smartbattUpdates
[Smart Battery Module] Update for greater accuracy
2 parents 51dff1b + bca53e8 commit e46c386

File tree

5 files changed

+50
-38
lines changed

5 files changed

+50
-38
lines changed

apps/smartbatt/ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
v0.01: New app!
22
v0.02: Add dynamic intervals, depending on total cycles recorded
33
v0.03: Return more data inside of .get();
4+
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

apps/smartbatt/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ The module provides two clockInfos:
1616
## Settings
1717
### Clear Data - Clears all learned data.
1818
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.
19+
### Update Interval - The time that the module should record battery drainage
20+
This changes the interval when you record battery drainage. Generally, a longer interval means more precise estimates, at the cost of quick updates.
21+
22+
Min: 30 minutes
23+
24+
Max: 48 hours
1925
### Logging - Enables logging for stats that this module uses.
2026
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.
2127
Logs:

apps/smartbatt/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "smartbatt",
33
"name": "Smart Battery Module",
44
"shortName": "Smart Battery",
5-
"version": "0.03",
5+
"version": "0.04",
66
"description": "Provides a `smartbatt` module that returns the battery in days, and learns from daily usage over time for accurate predictions.",
77
"icon": "app.png",
88
"type": "module",

apps/smartbatt/module.js

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
var dataFile = "smartbattdata.json";
3-
var interval;
43
var storage = require("Storage");
54

65

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

@@ -65,8 +65,16 @@
6565
let newAvg = weightedAverage(data.avgBattDrainage, data.totalHours, currentDrainage, deltaHours * weightCoefficient);
6666
data.avgBattDrainage = newAvg;
6767
data.timeLastRecorded = now;
68-
data.totalCycles += 1;
69-
data.totalHours += deltaHours;
68+
if(data.totalCycles<60){
69+
data.totalCycles += 1;
70+
data.totalHours += deltaHours;
71+
72+
}else{
73+
data.totalCycles=60;
74+
data.totalHours=100;
75+
}
76+
77+
7078
data.battLastRecorded = batt;
7179
storage.writeJSON(dataFile, data);
7280

@@ -84,20 +92,7 @@
8492
reason: reason
8593
});
8694
}
87-
clearInterval(interval)
88-
if(data.totalCycles<=200){
89-
//5m intervals
90-
interval=setInterval(recordBattery, 600000);
91-
}else if(data.totalCycles<=300){
92-
//30m intervals
93-
interval=setInterval(recordBattery, 1800000);
94-
}else if(data.totalCycles<=500){
95-
//1h intervals
96-
interval=setInterval(recordBattery, 3600000);
97-
}else {
98-
//3h intervals
99-
interval=setInterval(recordBattery, 10800000);
100-
}
95+
setTimeout(recordBattery,getSettings().updateInterval);
10196
}
10297

10398
function weightedAverage(oldValue, oldWeight, newValue, newWeight) {
@@ -139,14 +134,9 @@
139134
storage.erase(logFile);
140135
}
141136
// Expose public API
142-
exports.record = recordBattery;
143137
exports.deleteData = deleteData;
144138
exports.get = getExportData;
145-
exports.changeInterval = function (newInterval) {
146-
clearInterval(interval);
147-
interval = setInterval(recordBattery, newInterval);
148-
};
149-
// Start recording every 5 minutes
150-
interval = setInterval(recordBattery, 600000);
139+
140+
// Start recording every 8 hours for accurate long tracking
151141
recordBattery(); // Log immediately
152142
}

apps/smartbatt/settings.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
21
(function(back) {
32
var FILE = "smartbatt.settings.json";
4-
// Load settings
53
var settings = Object.assign({
6-
//Record Interval stored in ms
7-
doLogging:false
4+
doLogging:false,
5+
updateInterval:18000000
86
}, require('Storage').readJSON(FILE, true) || {});
97

108
function writeSettings() {
119
require('Storage').writeJSON(FILE, settings);
1210
}
1311

14-
// Show the menu
1512
E.showMenu({
16-
"" : { "title" : "Smart Day Battery" },
13+
"" : { "title" : "Smart Battery" },
1714
"< Back" : () => back(),
1815

1916
'Clear Data': function () {
@@ -28,15 +25,33 @@
2825
}
2926
});
3027
},
31-
'Log Battery': {
32-
value: !!settings.doLogging, // !! converts undefined to false
28+
'Update Interval': {
29+
value: 0|settings.updateInterval,
30+
min:1800000,
31+
max:172800000,
32+
step:1800000,
33+
format: v=>{
34+
var totalMinutes = Math.floor(v / 60000);
35+
var h = Math.floor(totalMinutes / 60);
36+
var m = totalMinutes % 60;
37+
38+
let result = '';
39+
if (h > 0) result += h+"h";
40+
if (m > 0) result += m+"m";
41+
42+
return result || '0m';
43+
},
44+
onchange: v => {
45+
settings.updateInterval = v;
46+
writeSettings();
47+
}
48+
},
49+
'Log Battery': {
50+
value: !!settings.doLogging,
3351
onchange: v => {
3452
settings.doLogging = v;
3553
writeSettings();
3654
}
37-
// format: ... may be specified as a function which converts the value to a string
38-
// if the value is a boolean, showMenu() will convert this automatically, which
39-
// keeps settings menus consistent
40-
},
55+
}
4156
});
4257
})

0 commit comments

Comments
 (0)