-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
124 lines (107 loc) · 3.55 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
package main
import (
"flag"
"fmt"
"log"
"time"
"github.com/omeid/upower-notify/notify"
"github.com/omeid/upower-notify/upower"
)
func init() {
//See the notes in "github.com/omeid/upower-notify/upower"
//This setting is for HP Envy series late 2012.
}
var (
tick time.Duration
warn time.Duration
critical time.Duration
notificationExpiry time.Duration
device string
report bool
notificationExpiryMilliseconds int32
)
func main() {
flag.DurationVar(&tick, "tick", 10*time.Second, "Update rate")
flag.DurationVar(&warn, "warn", 20*time.Minute, "Time to start warning. (Warn)")
flag.DurationVar(&critical, "critical", 10*time.Minute, "Time to start warning. (Critical)")
flag.DurationVar(¬ificationExpiry, "notification-expiration", 10*time.Second, "Notifications expiry duration")
flag.StringVar(&device, "device", "DisplayDevice", "DBus device name for the battery")
flag.BoolVar(&report, "report", false, "Print out updates to stdout.")
flag.Parse()
notificationExpiryMilliseconds = int32(notificationExpiry / time.Millisecond)
up, err := upower.New(device)
if err != nil {
log.Fatal(err)
}
update, err := up.Get()
if err != nil {
log.Fatal(err)
}
notifier, err := notify.New("Upower Agent")
if err != nil {
log.Fatal(err)
}
err = notifier.Low("Ol Correct.", "Everything seems okay, I will keep you posted.", notificationExpiryMilliseconds)
if err != nil {
log.Fatal(err)
}
var old upower.Update
for range time.Tick(tick) {
update, err = up.Get()
if err != nil {
notifier.Critical("Oh Noosss!", fmt.Sprintf("Something went wrong: %s", err), notificationExpiryMilliseconds)
fmt.Printf("ERROR!!")
}
if update.Changed(old) {
sendNotify(update, notifier, old.State != update.State)
if report {
print(update, notifier)
}
}
old = update
}
}
func print(battery upower.Update, notifier *notify.Notifier) {
switch battery.State {
case upower.Charging:
fmt.Printf("C(%v%%):%v\n", battery.Percentage, battery.TimeToFull)
case upower.Discharging:
fmt.Printf("D(%v%%):%v\n", battery.Percentage, battery.TimeToEmpty)
case upower.Empty:
fmt.Printf("DEAD!\n")
case upower.FullCharged:
fmt.Printf("F:%v%%\n", battery.Percentage)
case upower.PendingCharge:
fmt.Printf("PC\n")
case upower.PendingDischarge:
fmt.Printf("PD\n")
default:
fmt.Printf("UNKN(%v)", battery.State)
}
}
func sendNotify(battery upower.Update, notifier *notify.Notifier, changed bool) {
if changed {
notifier.Normal("Power Change.", fmt.Sprintf("Heads up!! We are now %s.", battery.State), notificationExpiryMilliseconds)
}
switch battery.State {
case upower.Charging:
//Do nothing.
case upower.Discharging:
switch {
case battery.TimeToEmpty < critical:
notifier.Critical("BATTERY LOW!", fmt.Sprintf("Things are getting critical here. %s to go.", battery.TimeToEmpty), notificationExpiryMilliseconds)
time.Sleep(critical / 10)
case battery.TimeToEmpty < warn:
notifier.Normal("Heads up!!", fmt.Sprintf("We only got %s of juice. Any powerpoints around?", battery.TimeToEmpty), notificationExpiryMilliseconds)
time.Sleep(warn / 10)
default:
//Do nothing. Everything seems good.
}
case upower.Empty:
notifier.Critical("BATTERY DEAD!", fmt.Sprintf("Things are pretty bad. Battery is dead. %s to go.", battery.TimeToEmpty), notificationExpiryMilliseconds)
case upower.FullCharged, upower.PendingCharge, upower.PendingDischarge:
//Do nothing.
default:
notifier.Critical("Oh Noosss!", "I can't figure out battery state!", notificationExpiryMilliseconds)
}
}