mirror of
https://github.com/fankes/beszel.git
synced 2025-10-19 09:49:28 +08:00
freebsd: fix battery-related bug (#1081)
This commit is contained in:
@@ -36,7 +36,6 @@ type Agent struct {
|
|||||||
server *ssh.Server // SSH server
|
server *ssh.Server // SSH server
|
||||||
dataDir string // Directory for persisting data
|
dataDir string // Directory for persisting data
|
||||||
keys []gossh.PublicKey // SSH public keys
|
keys []gossh.PublicKey // SSH public keys
|
||||||
hasBattery bool // true if agent has access to battery stats
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewAgent creates a new agent with the given data directory for persisting data.
|
// NewAgent creates a new agent with the given data directory for persisting data.
|
||||||
|
@@ -1,24 +0,0 @@
|
|||||||
package agent
|
|
||||||
|
|
||||||
import "github.com/distatus/battery"
|
|
||||||
|
|
||||||
// getBatteryStats returns the current battery percent and charge state
|
|
||||||
func getBatteryStats() (batteryPercent uint8, batteryState uint8, err error) {
|
|
||||||
batteries, err := battery.GetAll()
|
|
||||||
if err != nil || len(batteries) == 0 {
|
|
||||||
return batteryPercent, batteryState, err
|
|
||||||
}
|
|
||||||
totalCapacity := float64(0)
|
|
||||||
totalCharge := float64(0)
|
|
||||||
for _, bat := range batteries {
|
|
||||||
if bat.Design != 0 {
|
|
||||||
totalCapacity += bat.Design
|
|
||||||
} else {
|
|
||||||
totalCapacity += bat.Full
|
|
||||||
}
|
|
||||||
totalCharge += bat.Current
|
|
||||||
}
|
|
||||||
batteryPercent = uint8(totalCharge / totalCapacity * 100)
|
|
||||||
batteryState = uint8(batteries[0].State.Raw)
|
|
||||||
return batteryPercent, batteryState, nil
|
|
||||||
}
|
|
53
beszel/internal/agent/battery/battery.go
Normal file
53
beszel/internal/agent/battery/battery.go
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
//go:build !freebsd
|
||||||
|
|
||||||
|
// Package battery provides functions to check if the system has a battery and to get the battery stats.
|
||||||
|
package battery
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"log/slog"
|
||||||
|
|
||||||
|
"github.com/distatus/battery"
|
||||||
|
)
|
||||||
|
|
||||||
|
var systemHasBattery = false
|
||||||
|
var haveCheckedBattery = false
|
||||||
|
|
||||||
|
// HasReadableBattery checks if the system has a battery and returns true if it does.
|
||||||
|
func HasReadableBattery() bool {
|
||||||
|
if haveCheckedBattery {
|
||||||
|
return systemHasBattery
|
||||||
|
}
|
||||||
|
haveCheckedBattery = true
|
||||||
|
bat, err := battery.Get(0)
|
||||||
|
if err == nil && bat != nil {
|
||||||
|
systemHasBattery = true
|
||||||
|
} else {
|
||||||
|
slog.Debug("No battery found", "err", err)
|
||||||
|
}
|
||||||
|
return systemHasBattery
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBatteryStats returns the current battery percent and charge state
|
||||||
|
func GetBatteryStats() (batteryPercent uint8, batteryState uint8, err error) {
|
||||||
|
if !systemHasBattery {
|
||||||
|
return batteryPercent, batteryState, errors.ErrUnsupported
|
||||||
|
}
|
||||||
|
batteries, err := battery.GetAll()
|
||||||
|
if err != nil || len(batteries) == 0 {
|
||||||
|
return batteryPercent, batteryState, err
|
||||||
|
}
|
||||||
|
totalCapacity := float64(0)
|
||||||
|
totalCharge := float64(0)
|
||||||
|
for _, bat := range batteries {
|
||||||
|
if bat.Design != 0 {
|
||||||
|
totalCapacity += bat.Design
|
||||||
|
} else {
|
||||||
|
totalCapacity += bat.Full
|
||||||
|
}
|
||||||
|
totalCharge += bat.Current
|
||||||
|
}
|
||||||
|
batteryPercent = uint8(totalCharge / totalCapacity * 100)
|
||||||
|
batteryState = uint8(batteries[0].State.Raw)
|
||||||
|
return batteryPercent, batteryState, nil
|
||||||
|
}
|
13
beszel/internal/agent/battery/battery_freebsd.go
Normal file
13
beszel/internal/agent/battery/battery_freebsd.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
//go:build freebsd
|
||||||
|
|
||||||
|
package battery
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
func HasReadableBattery() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetBatteryStats() (uint8, uint8, error) {
|
||||||
|
return 0, 0, errors.ErrUnsupported
|
||||||
|
}
|
@@ -2,6 +2,7 @@ package agent
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"beszel"
|
"beszel"
|
||||||
|
"beszel/internal/agent/battery"
|
||||||
"beszel/internal/entities/system"
|
"beszel/internal/entities/system"
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -64,13 +65,6 @@ func (a *Agent) initializeSystemInfo() {
|
|||||||
} else {
|
} else {
|
||||||
a.zfs = true
|
a.zfs = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// battery
|
|
||||||
if _, _, err := getBatteryStats(); err != nil {
|
|
||||||
slog.Debug("No battery detected", "err", err)
|
|
||||||
} else {
|
|
||||||
a.hasBattery = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns current info, stats about the host system
|
// Returns current info, stats about the host system
|
||||||
@@ -78,8 +72,8 @@ func (a *Agent) getSystemStats() system.Stats {
|
|||||||
systemStats := system.Stats{}
|
systemStats := system.Stats{}
|
||||||
|
|
||||||
// battery
|
// battery
|
||||||
if a.hasBattery {
|
if battery.HasReadableBattery() {
|
||||||
systemStats.Battery[0], systemStats.Battery[1], _ = getBatteryStats()
|
systemStats.Battery[0], systemStats.Battery[1], _ = battery.GetBatteryStats()
|
||||||
}
|
}
|
||||||
|
|
||||||
// cpu percent
|
// cpu percent
|
||||||
|
Reference in New Issue
Block a user