-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupower_checker.go
84 lines (72 loc) · 1.69 KB
/
upower_checker.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"github.com/godbus/dbus"
"math"
)
type UPowerChecker struct {
conn *dbus.Conn
}
var _ CheckerBackend = &UPowerChecker{}
func (c *UPowerChecker) Init(string) error {
var err error
c.conn, err = dbus.SystemBus()
if err != nil {
return err
}
return nil
}
const (
upStateUnknown = iota
upStateCharging
upStateDischarging
upStateEmpty
upStateFull
upStatePendingCharge
upStatePendingDischarge
upState
)
func (c *UPowerChecker) Check() (*PowerStatus, error) {
upower := c.conn.Object("org.freedesktop.UPower",
"/org/freedesktop/UPower/devices/DisplayDevice")
props := map[string]dbus.Variant{}
err := upower.Call("org.freedesktop.DBus.Properties.GetAll",
0, "org.freedesktop.UPower.Device").Store(&props)
if err != nil {
return nil, err
}
energyFull := props["EnergyFull"].Value().(float64)
energy := props["Energy"].Value().(float64)
var timeRemaining float32
var charging bool
switch int(props["State"].Value().(uint32)) {
case upStateCharging:
timeRemaining = float32(props["TimeToFull"].Value().(int64))
charging = true
case upStateDischarging:
timeRemaining = float32(props["TimeToEmpty"].Value().(int64))
charging = false
case upStateEmpty:
charging = false
timeRemaining = 0
case upStateFull:
charging = true
timeRemaining = 0
case upStatePendingCharge:
charging = true
timeRemaining = float32(math.NaN())
case upStatePendingDischarge:
charging = false
timeRemaining = float32(math.NaN())
default:
charging = false
timeRemaining = float32(math.NaN())
}
ret := &PowerStatus{
ChargeLevel: float32(energy / energyFull),
TimeRemaining: timeRemaining,
Charging: charging,
}
return ret, nil
}
func (c *UPowerChecker) Stop() {
}