add charging checks, made logging optional

master^2^2
RKBoss6 2025-07-23 16:20:37 -04:00 committed by GitHub
parent d4d4a7eaaa
commit 276bf0c7cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 24 additions and 15 deletions

View File

@ -4,7 +4,7 @@
var storage=require("Storage");
var logFile = "smartbattlog.json";
var doLogging=true;
function logBatterySample(entry) {
let log = storage.readJSON(logFile, 1) || [];
@ -30,8 +30,16 @@
if (battChange <= 0) {
reason = "Skipped: battery rose or no change";
reason = "Skipped: battery fluctuated or no change";
if(Math.abs(battChange)<5){
//less than 6% difference, average percents
var newBatt=(batt+data.battLastRecorded)/2;
data.battLastRecorded = newBatt;
}else{
//probably charged, ignore average
data.battLastRecorded = batt;
}
storage.writeJSON(filename, data);
} else if (deltaHours <= 0 || !isFinite(deltaHours)) {
reason = "Skipped: invalid time delta";
@ -56,7 +64,7 @@
reason = "Drainage recorded: " + currentDrainage.toFixed(3) + "%/hr";
}
if(doLogging){
// Always log the sample
logBatterySample({
time: now,
@ -68,6 +76,7 @@
reason: reason
});
}
}
@ -96,7 +105,7 @@
function deleteData(){
storage.erase(filename);
}
};
// Expose public API
exports.record = recordBattery;
exports.deleteData = deleteData;