Skip to content

Commit 3efb92f

Browse files
committed
improve estimated time
1 parent 040a7e9 commit 3efb92f

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

nodejs-assets/nodejs-project/index.html

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,17 @@
772772
sections.innerHTML += element;
773773
}
774774

775+
776+
export function convertSecondsToHHMM(totalSeconds) {
777+
const hours = Math.floor(totalSeconds / 3600);
778+
const minutes = Math.floor((totalSeconds % 3600) / 60);
779+
780+
// You can customize the format here
781+
return `${hours}h ${minutes}m`;
782+
}
783+
784+
785+
775786
monitorStarted = false;
776787
function fetchSchedule() {
777788
fetchBatteryInfo().then(info => {
@@ -820,24 +831,24 @@
820831
voltage: info.totalVolts,
821832
});
822833

823-
// use current capacity, current and nominal capacity to calculate remaining
824-
let secondsRemaining =
825-
(info.remainingCapacityAh / Math.abs(info.current)) * 3600;
834+
const charging = current > 0;
835+
const discharging = current < 0;
836+
837+
let secondsRemaining;
826838

827-
const charging = info.current > 0;
828-
const discharging = info.current < 0;
829839
if (charging) {
830840
secondsRemaining =
831-
((info.nominalCapacityAh - info.remainingCapacityAh) /
832-
Math.abs(info.current)) *
833-
3600;
841+
((nominalCapacityAh - remainingCapacityAh) / Math.abs(current)) * 3600;
842+
} else if (discharging) {
843+
secondsRemaining = (remainingCapacityAh / Math.abs(current)) * 3600;
834844
}
845+
const HHMMRemaining = convertSecondsToHHMM(secondsRemaining);
835846

836847
let estTitle = '';
837848
if (charging || discharging) {
838-
estTitle = `Estimated ${
849+
estTitle = `Est ${
839850
charging ? 'Charging' : 'Discharging'
840-
}: ${new Date(secondsRemaining * 1000).toISOString().slice(11, 19)}`;
851+
}: ${HHMMRemaining}`;
841852
}
842853
const estTime = document.getElementById('estimateTime');
843854
estTime.innerText = !charging && !discharging ? '' : estTitle;

0 commit comments

Comments
 (0)