Skip to content

Commit d28d762

Browse files
authored
Remove extreme fluctuation check, because of accuracy decline
1 parent c97d817 commit d28d762

File tree

1 file changed

+33
-58
lines changed

1 file changed

+33
-58
lines changed

apps/smartbatt/module.js

Lines changed: 33 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
{
22
var dataFile = "smartbattdata.json";
33
var interval;
4-
var storage=require("Storage");
5-
6-
4+
var storage = require("Storage");
5+
6+
77
var logFile = "smartbattlog.json";
8-
9-
function getSettings(){
8+
9+
function getSettings() {
1010
return Object.assign({
1111
//Record Interval stored in ms
12-
doLogging:false
12+
doLogging: false
1313
}, require('Storage').readJSON("smartbatt.settings.json", true) || {});
1414
}
15-
15+
1616
function logBatterySample(entry) {
1717
let log = storage.readJSON(logFile, 1) || [];
1818
//get human-readable time
1919
let d = new Date();
2020
entry.time = d.getFullYear() + "-" +
21-
("0"+(d.getMonth()+1)).slice(-2) + "-" +
22-
("0"+d.getDate()).slice(-2) + " " +
23-
("0"+d.getHours()).slice(-2) + ":" +
24-
("0"+d.getMinutes()).slice(-2) + ":" +
25-
("0"+d.getSeconds()).slice(-2);
21+
("0" + (d.getMonth() + 1)).slice(-2) + "-" +
22+
("0" + d.getDate()).slice(-2) + " " +
23+
("0" + d.getHours()).slice(-2) + ":" +
24+
("0" + d.getMinutes()).slice(-2) + ":" +
25+
("0" + d.getSeconds()).slice(-2);
2626

2727
log.push(entry);
2828
if (log.length > 100) log = log.slice(-100);
@@ -44,89 +44,64 @@
4444

4545
if (battChange <= 0) {
4646
reason = "Skipped: battery fluctuated or no change";
47-
if(Math.abs(battChange)<5){
47+
if (Math.abs(battChange) < 5) {
4848
//less than 6% difference, average percents
49-
var newBatt=(batt+data.battLastRecorded)/2;
49+
var newBatt = (batt + data.battLastRecorded) / 2;
5050
data.battLastRecorded = newBatt;
51-
}else{
51+
} else {
5252
//probably charged, ignore average
5353
data.battLastRecorded = batt;
5454
}
55-
55+
5656
storage.writeJSON(dataFile, data);
5757
} else if (deltaHours <= 0 || !isFinite(deltaHours)) {
5858
reason = "Skipped: invalid time delta";
5959
data.timeLastRecorded = now;
6060
data.battLastRecorded = batt;
6161
storage.writeJSON(dataFile, data);
6262
} else {
63-
63+
let weightCoefficient = 1;
6464
let currentDrainage = battChange / deltaHours;
65-
let drainageChange=data.avgBattDrainage-currentDrainage;
66-
//check if drainage rate has fluctuated quite a bit
67-
//If fluctuation event is 0, first time fluctuating like this, cycles > 10 so as not to interfere with initial data collection.
68-
if(Math.abs(drainageChange)>currentDrainage*0.7&&data.fluctuationEvent==0&&data.totalCycles>=10){
69-
//has fluctuated, first time doing so
70-
reason="Skipped: Extreme fluctuation";
71-
//set fluctuationevent so it knows what was the first time.
72-
data.fluctuationEvent=data.totalCycles+1;
73-
data.battLastRecorded=batt;
74-
storage.writeJSON(dataFile, data);
75-
76-
if(getSettings().doLogging){
77-
// Always log the sample
78-
logBatterySample({
79-
battNow: batt,
80-
battLast: data.battLastRecorded,
81-
battChange: battChange,
82-
deltaHours: deltaHours,
83-
avgDrainage: data.avgBattDrainage,
84-
reason: reason
85-
});
86-
return;
87-
}else{
88-
data.fluctuationEvent=0;
89-
}
90-
91-
}
92-
93-
let newAvg = weightedAverage(data.avgBattDrainage, data.totalHours, currentDrainage, deltaHours);
94-
data.avgBattDrainage=newAvg;
65+
let drainageChange = data.avgBattDrainage - currentDrainage;
66+
67+
68+
let newAvg = weightedAverage(data.avgBattDrainage, data.totalHours, currentDrainage, deltaHours * weightCoefficient);
69+
data.avgBattDrainage = newAvg;
9570
data.timeLastRecorded = now;
9671
data.totalCycles += 1;
97-
data.totalHours+=deltaHours;
72+
data.totalHours += deltaHours;
9873
data.battLastRecorded = batt;
9974
storage.writeJSON(dataFile, data);
10075

10176
reason = "Drainage recorded: " + currentDrainage.toFixed(3) + "%/hr";
10277
}
103-
if(getSettings().doLogging){
78+
if (getSettings().doLogging) {
10479
// Always log the sample
10580
logBatterySample({
10681
battNow: batt,
10782
battLast: data.battLastRecorded,
10883
battChange: battChange,
10984
deltaHours: deltaHours,
85+
timeLastRecorded: data.timeLastRecorded,
11086
avgDrainage: data.avgBattDrainage,
11187
reason: reason
11288
});
11389
}
11490
}
115-
91+
11692
function weightedAverage(oldValue, oldWeight, newValue, newWeight) {
11793
return (oldValue * oldWeight + newValue * newWeight) / (oldWeight + newWeight);
11894
}
11995

120-
96+
12197

12298
function getData() {
12399
return storage.readJSON(dataFile, 1) || {
124100
avgBattDrainage: 0,
125101
battLastRecorded: E.getBattery(),
126102
timeLastRecorded: Date.now(),
127103
totalCycles: 0,
128-
totalHours:0,
129-
fluctuationEvent:0
104+
totalHours: 0,
130105
};
131106
}
132107

@@ -142,20 +117,20 @@
142117
hrsLeft: hrsLeft,
143118
};
144119
}
145-
146-
function deleteData(){
120+
121+
function deleteData() {
147122
storage.erase(dataFile);
148123
storage.erase(logFile);
149124
}
150125
// Expose public API
151126
exports.record = recordBattery;
152127
exports.deleteData = deleteData;
153128
exports.get = estimateBatteryLife;
154-
exports.changeInterval = function(newInterval) {
129+
exports.changeInterval = function (newInterval) {
155130
clearInterval(interval);
156-
interval=setInterval(recordBattery, newInterval);
131+
interval = setInterval(recordBattery, newInterval);
157132
};
158133
// Start recording every 5 minutes
159-
interval=setInterval(recordBattery, 600000);
134+
interval = setInterval(recordBattery, 600000);
160135
recordBattery(); // Log immediately
161136
}

0 commit comments

Comments
 (0)