mirror of
https://github.com/henrygd/beszel.git
synced 2025-12-17 10:46:16 +01:00
fix battery nil pointer error (#1353)
This commit is contained in:
@@ -6,6 +6,7 @@ package battery
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"math"
|
||||||
|
|
||||||
"github.com/distatus/battery"
|
"github.com/distatus/battery"
|
||||||
)
|
)
|
||||||
@@ -51,6 +52,8 @@ func GetBatteryStats() (batteryPercent uint8, batteryState uint8, err error) {
|
|||||||
totalCharge := float64(0)
|
totalCharge := float64(0)
|
||||||
errs, partialErrs := err.(battery.Errors)
|
errs, partialErrs := err.(battery.Errors)
|
||||||
|
|
||||||
|
batteryState = math.MaxUint8
|
||||||
|
|
||||||
for i, bat := range batteries {
|
for i, bat := range batteries {
|
||||||
if partialErrs && errs[i] != nil {
|
if partialErrs && errs[i] != nil {
|
||||||
// if there were some errors, like missing data, skip it
|
// if there were some errors, like missing data, skip it
|
||||||
@@ -63,9 +66,12 @@ func GetBatteryStats() (batteryPercent uint8, batteryState uint8, err error) {
|
|||||||
}
|
}
|
||||||
totalCapacity += bat.Full
|
totalCapacity += bat.Full
|
||||||
totalCharge += bat.Current
|
totalCharge += bat.Current
|
||||||
|
if bat.State.Raw >= 0 {
|
||||||
|
batteryState = uint8(bat.State.Raw)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if totalCapacity == 0 {
|
if totalCapacity == 0 || batteryState == math.MaxUint8 {
|
||||||
// for macs there's sometimes a ghost battery with 0 capacity
|
// for macs there's sometimes a ghost battery with 0 capacity
|
||||||
// https://github.com/distatus/battery/issues/34
|
// https://github.com/distatus/battery/issues/34
|
||||||
// Instead of skipping over those batteries, we'll check for total 0 capacity
|
// Instead of skipping over those batteries, we'll check for total 0 capacity
|
||||||
@@ -74,6 +80,5 @@ func GetBatteryStats() (batteryPercent uint8, batteryState uint8, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
batteryPercent = uint8(totalCharge / totalCapacity * 100)
|
batteryPercent = uint8(totalCharge / totalCapacity * 100)
|
||||||
batteryState = uint8(batteries[0].State.Raw)
|
|
||||||
return batteryPercent, batteryState, nil
|
return batteryPercent, batteryState, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user