-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
161 lines (139 loc) · 3.68 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"context"
"fmt"
"log/slog"
"math"
"os"
"os/signal"
"syscall"
"github.com/esiqveland/notify"
"github.com/godbus/dbus/v5"
)
const (
appName = "battery-notify"
batteryPath = dbus.ObjectPath("/org/freedesktop/UPower/devices/battery_BAT0")
)
const (
stateCharging uint32 = iota + 1
stateDischarging
stateEmpty
stateFullyCharged
statePendingCharge
statePendingDischarge
)
var stateMap = map[uint32]string{
stateCharging: "Charging",
stateDischarging: "Discharging",
stateEmpty: "Empty",
stateFullyCharged: "Fully Charged",
statePendingCharge: "Pending Charge",
statePendingDischarge: "Pending Discharge",
}
// TODO: Use command line flags to set these
const (
thresholdCritital = 15
thresholdLow = 30
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
sysConn, err := dbus.SystemBus()
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}
defer sysConn.Close()
sessionConn, err := dbus.SessionBus()
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}
defer sessionConn.Close()
signalChan := make(chan *dbus.Signal, 10)
sysConn.Signal(signalChan)
err = sysConn.AddMatchSignal(
dbus.WithMatchInterface("org.freedesktop.DBus.Properties"),
dbus.WithMatchObjectPath(batteryPath),
dbus.WithMatchMember("PropertiesChanged"),
)
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}
var lastNotificationID uint32
slog.Info("Listening for changes in battery")
for {
select {
case <-ctx.Done():
slog.Info("Quitting")
os.Exit(1)
case signal := <-signalChan:
// Handling signal body format
if len(signal.Body) < 2 {
continue
}
interfaceName, ok := signal.Body[0].(string)
if !ok || interfaceName != "org.freedesktop.UPower.Device" {
continue
}
properties, ok := signal.Body[1].(map[string]dbus.Variant)
if !ok {
continue
}
val, exists := properties["Percentage"]
if !exists {
continue
}
var percentage float64
if percentage, ok = val.Value().(float64); !ok {
continue
}
var state uint32
err := sysConn.Object("org.freedesktop.UPower", signal.Path).
Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.UPower.Device", "State").
Store(&state)
if err != nil {
slog.Error(err.Error())
continue
}
var model string
err = sysConn.Object("org.freedesktop.UPower", signal.Path).
Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.UPower.Device", "Model").
Store(&model)
if err != nil {
slog.Error(err.Error())
continue
}
if state != stateDischarging {
slog.Info(fmt.Sprintf("Skipping notification. State: %s", stateMap[state]))
continue
}
if percentage > thresholdLow {
slog.Info(fmt.Sprintf("Skipping notification. Battery level: %.0f%%", percentage))
continue
}
notification := notify.Notification{
AppName: appName,
ReplacesID: lastNotificationID,
Summary: fmt.Sprintf("Battery: %s", model),
Body: fmt.Sprintf(" Current level: %.0f%%", percentage),
ExpireTimeout: notify.ExpireTimeoutSetByNotificationServer,
Hints: map[string]dbus.Variant{
"value": dbus.MakeVariant(int(math.Round(percentage))),
},
}
if percentage <= thresholdCritital {
notification.ExpireTimeout = notify.ExpireTimeoutNever
notification.SetUrgency(notify.UrgencyCritical)
} else if percentage <= thresholdLow {
notification.SetUrgency(notify.UrgencyLow)
}
slog.Info("Sending notification")
lastNotificationID, err = notify.SendNotification(sessionConn, notification)
if err != nil {
slog.Error(err.Error())
}
}
}
}