1 Commits

Author SHA1 Message Date
eleksir
757fb5e57e Add sysfs method to get battery charge for linux 2025-04-24 20:42:32 +03:00
2 changed files with 23 additions and 13 deletions

View File

@@ -16,14 +16,10 @@ import (
// UpdateBatteryInfo updates info about battery charge. // UpdateBatteryInfo updates info about battery charge.
func (c *MyConfig) UpdateBatteryInfo() { func (c *MyConfig) UpdateBatteryInfo() {
var ( var (
ch int
status string
Batts string
InitialDelay = 100 * time.Millisecond InitialDelay = 100 * time.Millisecond
LoopIterationDelay = 5 * time.Second LoopIterationDelay = 5 * time.Second
Delay = InitialDelay Delay = InitialDelay
ticker = time.NewTicker(Delay) ticker = time.NewTicker(Delay)
batteries []*battery.Battery
) )
for range ticker.C { for range ticker.C {
@@ -32,8 +28,17 @@ func (c *MyConfig) UpdateBatteryInfo() {
ticker.Reset(Delay) ticker.Reset(Delay)
} }
var (
batteries []*battery.Battery = []*battery.Battery{}
Batts string
ch int
status string
)
if c.Battery.UseSysfs { if c.Battery.UseSysfs {
for battNum, file := range c.Battery.SysfsFiles { for _, file := range c.Battery.SysfsFiles {
var batt battery.Battery
_, err := os.Stat(file) _, err := os.Stat(file)
// If file exists. // If file exists.
@@ -52,7 +57,7 @@ func (c *MyConfig) UpdateBatteryInfo() {
myCharge = strings.Trim(string(b), "\n") myCharge = strings.Trim(string(b), "\n")
statusFile := filepath.Dir(file) + "status" statusFile := filepath.Dir(file) + "/status"
// If file exists. // If file exists.
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
@@ -71,15 +76,15 @@ func (c *MyConfig) UpdateBatteryInfo() {
// Make stub battery class :) . // Make stub battery class :) .
switch myStatus { switch myStatus {
case "Charging": case "Charging":
batteries[battNum].State.Raw = battery.Charging batt.State.Raw = battery.Charging
case "Discharging": case "Discharging":
batteries[battNum].State.Raw = battery.Discharging batt.State.Raw = battery.Discharging
case "Empty": case "Empty":
batteries[battNum].State.Raw = battery.Empty batt.State.Raw = battery.Empty
case "Full": case "Full":
batteries[battNum].State.Raw = battery.Full batt.State.Raw = battery.Full
default: default:
batteries[battNum].State.Raw = battery.Unknown batt.State.Raw = battery.Unknown
} }
ch, err := strconv.Atoi(myCharge) ch, err := strconv.Atoi(myCharge)
@@ -89,7 +94,9 @@ func (c *MyConfig) UpdateBatteryInfo() {
continue continue
} }
batteries[battNum].Current = float64(ch) batt.Current = float64(ch)
batteries = append(batteries, &batt)
} }
} }
} else { } else {

View File

@@ -783,7 +783,10 @@ func ReadConf(defaultConfig []byte) (*MyConfig, error) {
// sampleConfig.Battery.UseSysfs will be false if not set in config // sampleConfig.Battery.UseSysfs will be false if not set in config
if len(sampleConfig.Battery.SysfsFiles) == 0 { if len(sampleConfig.Battery.SysfsFiles) == 0 {
sampleConfig.Battery.SysfsFiles[0] = "/sys/class/power_supply/BAT0/capacity" sampleConfig.Battery.SysfsFiles = append(
sampleConfig.Battery.SysfsFiles,
"/sys/class/power_supply/BAT0/capacity",
)
} }
if sampleConfig.Battery.Color == "" { if sampleConfig.Battery.Color == "" {