|
1 | 1 | { |
2 | 2 | var dataFile = "smartbattdata.json"; |
3 | 3 | var interval; |
4 | | - var storage=require("Storage"); |
5 | | - |
6 | | - |
| 4 | + var storage = require("Storage"); |
| 5 | + |
| 6 | + |
7 | 7 | var logFile = "smartbattlog.json"; |
8 | | - |
9 | | - function getSettings(){ |
| 8 | + |
| 9 | + function getSettings() { |
10 | 10 | return Object.assign({ |
11 | 11 | //Record Interval stored in ms |
12 | | - doLogging:false |
| 12 | + doLogging: false |
13 | 13 | }, require('Storage').readJSON("smartbatt.settings.json", true) || {}); |
14 | 14 | } |
15 | | - |
| 15 | + |
16 | 16 | function logBatterySample(entry) { |
17 | 17 | let log = storage.readJSON(logFile, 1) || []; |
18 | 18 | //get human-readable time |
19 | 19 | let d = new Date(); |
20 | 20 | 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); |
26 | 26 |
|
27 | 27 | log.push(entry); |
28 | 28 | if (log.length > 100) log = log.slice(-100); |
|
44 | 44 |
|
45 | 45 | if (battChange <= 0) { |
46 | 46 | reason = "Skipped: battery fluctuated or no change"; |
47 | | - if(Math.abs(battChange)<5){ |
| 47 | + if (Math.abs(battChange) < 5) { |
48 | 48 | //less than 6% difference, average percents |
49 | | - var newBatt=(batt+data.battLastRecorded)/2; |
| 49 | + var newBatt = (batt + data.battLastRecorded) / 2; |
50 | 50 | data.battLastRecorded = newBatt; |
51 | | - }else{ |
| 51 | + } else { |
52 | 52 | //probably charged, ignore average |
53 | 53 | data.battLastRecorded = batt; |
54 | 54 | } |
55 | | - |
| 55 | + |
56 | 56 | storage.writeJSON(dataFile, data); |
57 | 57 | } else if (deltaHours <= 0 || !isFinite(deltaHours)) { |
58 | 58 | reason = "Skipped: invalid time delta"; |
59 | 59 | data.timeLastRecorded = now; |
60 | 60 | data.battLastRecorded = batt; |
61 | 61 | storage.writeJSON(dataFile, data); |
62 | 62 | } else { |
63 | | - |
| 63 | + let weightCoefficient = 1; |
64 | 64 | 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; |
95 | 70 | data.timeLastRecorded = now; |
96 | 71 | data.totalCycles += 1; |
97 | | - data.totalHours+=deltaHours; |
| 72 | + data.totalHours += deltaHours; |
98 | 73 | data.battLastRecorded = batt; |
99 | 74 | storage.writeJSON(dataFile, data); |
100 | 75 |
|
101 | 76 | reason = "Drainage recorded: " + currentDrainage.toFixed(3) + "%/hr"; |
102 | 77 | } |
103 | | - if(getSettings().doLogging){ |
| 78 | + if (getSettings().doLogging) { |
104 | 79 | // Always log the sample |
105 | 80 | logBatterySample({ |
106 | 81 | battNow: batt, |
107 | 82 | battLast: data.battLastRecorded, |
108 | 83 | battChange: battChange, |
109 | 84 | deltaHours: deltaHours, |
| 85 | + timeLastRecorded: data.timeLastRecorded, |
110 | 86 | avgDrainage: data.avgBattDrainage, |
111 | 87 | reason: reason |
112 | 88 | }); |
113 | 89 | } |
114 | 90 | } |
115 | | - |
| 91 | + |
116 | 92 | function weightedAverage(oldValue, oldWeight, newValue, newWeight) { |
117 | 93 | return (oldValue * oldWeight + newValue * newWeight) / (oldWeight + newWeight); |
118 | 94 | } |
119 | 95 |
|
120 | | - |
| 96 | + |
121 | 97 |
|
122 | 98 | function getData() { |
123 | 99 | return storage.readJSON(dataFile, 1) || { |
124 | 100 | avgBattDrainage: 0, |
125 | 101 | battLastRecorded: E.getBattery(), |
126 | 102 | timeLastRecorded: Date.now(), |
127 | 103 | totalCycles: 0, |
128 | | - totalHours:0, |
129 | | - fluctuationEvent:0 |
| 104 | + totalHours: 0, |
130 | 105 | }; |
131 | 106 | } |
132 | 107 |
|
|
142 | 117 | hrsLeft: hrsLeft, |
143 | 118 | }; |
144 | 119 | } |
145 | | - |
146 | | - function deleteData(){ |
| 120 | + |
| 121 | + function deleteData() { |
147 | 122 | storage.erase(dataFile); |
148 | 123 | storage.erase(logFile); |
149 | 124 | } |
150 | 125 | // Expose public API |
151 | 126 | exports.record = recordBattery; |
152 | 127 | exports.deleteData = deleteData; |
153 | 128 | exports.get = estimateBatteryLife; |
154 | | - exports.changeInterval = function(newInterval) { |
| 129 | + exports.changeInterval = function (newInterval) { |
155 | 130 | clearInterval(interval); |
156 | | - interval=setInterval(recordBattery, newInterval); |
| 131 | + interval = setInterval(recordBattery, newInterval); |
157 | 132 | }; |
158 | 133 | // Start recording every 5 minutes |
159 | | - interval=setInterval(recordBattery, 600000); |
| 134 | + interval = setInterval(recordBattery, 600000); |
160 | 135 | recordBattery(); // Log immediately |
161 | 136 | } |
0 commit comments