mirror of
https://github.com/fankes/beszel.git
synced 2025-10-21 19:10:38 +08:00
update battery charge to include all batteries
This commit is contained in:
@@ -4,15 +4,21 @@ import "github.com/distatus/battery"
|
|||||||
|
|
||||||
// getBatteryStats returns the current battery percent and charge state
|
// getBatteryStats returns the current battery percent and charge state
|
||||||
func getBatteryStats() (batteryPercent uint8, batteryState uint8, err error) {
|
func getBatteryStats() (batteryPercent uint8, batteryState uint8, err error) {
|
||||||
bat, err := battery.Get(0)
|
batteries, err := battery.GetAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return batteryPercent, batteryState, err
|
return batteryPercent, batteryState, err
|
||||||
}
|
}
|
||||||
full := bat.Design
|
totalCapacity := float64(0)
|
||||||
if full == 0 {
|
totalCharge := float64(0)
|
||||||
full = bat.Full
|
for _, bat := range batteries {
|
||||||
|
if bat.Design != 0 {
|
||||||
|
totalCapacity += bat.Design
|
||||||
|
} else {
|
||||||
|
totalCapacity += bat.Full
|
||||||
|
}
|
||||||
|
totalCharge += bat.Current
|
||||||
}
|
}
|
||||||
batteryPercent = uint8(bat.Current / full * 100)
|
batteryPercent = uint8(totalCharge / totalCapacity * 100)
|
||||||
batteryState = uint8(bat.State.Raw)
|
batteryState = uint8(batteries[0].State.Raw)
|
||||||
return batteryPercent, batteryState, nil
|
return batteryPercent, batteryState, nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user