-
Notifications
You must be signed in to change notification settings - Fork 0
/
battery_info.go
50 lines (45 loc) · 911 Bytes
/
battery_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"io/ioutil"
"os"
"strconv"
"strings"
)
const (
BatteryStatusPath = "/sys/class/power_supply/BAT0/status"
BatteryCapacityPath = "/sys/class/power_supply/BAT0/capacity"
AcOnlinePath = "/sys/class/power_supply/AC/online"
)
type BatteryInfo struct {
onAC bool
status string
level int
}
func NewBatteryInfo() *BatteryInfo {
s := &BatteryInfo{
onAC: false,
status: "Unknown",
level: 0,
}
s.Update()
return s
}
func (b *BatteryInfo) Update() {
bytesArray, err := ioutil.ReadFile(BatteryStatusPath)
if err != nil {
panic(err)
}
b.status = string(bytesArray)
if _, err := os.Stat(AcOnlinePath); !os.IsNotExist(err) {
b.onAC = true
}
bytesArray, err = ioutil.ReadFile(BatteryCapacityPath)
if err != nil {
panic(err)
}
level, err := strconv.Atoi(strings.TrimSuffix(string(bytesArray), "\n"))
if err != nil {
panic(err)
}
b.level = level
}