Cross-platform, normalized battery information library.
Gives access to a system independent, typed battery state and capacity/charge values recalculated as necessary to be returned in mW(h) units.
Currently supported systems:
- Linux 2.6.39+
- OS X 10.10+
- Windows XP+
- FreeBSD
- DragonFlyBSD
- NetBSD
$ go get -u github.com/distatus/battery
import (
"fmt"
"github.com/distatus/battery"
)
func main() {
batteries, err := battery.GetAll()
if err != nil {
fmt.Println("Could not get battery info!")
return
}
for i, battery := range batteries {
fmt.Printf("Bat%d: ", i)
fmt.Printf("state: %f, ", battery.State)
fmt.Printf("current capacity: %f mWh, ", battery.Current)
fmt.Printf("last full capacity: %f mWh, ", battery.Full)
fmt.Printf("design capacity: %f mWh, ", battery.Design)
fmt.Printf("charge rate: %f mW\n", battery.ChargeRate)
}
}
There is also a little utility which - more or less - mimicks the GNU/Linux acpi -b
command.
Installation
$ go get -u github.com/distatus/battery/cmd/battery
Usage
$ battery
BAT0: Full, 95.61%