This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
121 lines (101 loc) · 2.86 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
package main
import (
"flag"
"fmt"
"os"
"time"
"github.com/esiqveland/notify"
"github.com/godbus/dbus/v5"
"github.com/rsjethani/sysinfo"
)
var (
commit string
version string
buildTime string
)
var notifID uint32
var argWatch bool
var argThreshold uint
var argPercentage bool
var argShowVersion bool
var argLowInterval time.Duration
var argNormalInterval time.Duration
func init() {
flag.BoolVar(&argShowVersion, "v", false, "Show application version")
flag.DurationVar(&argLowInterval, "l", time.Minute*2, "battery check interval during low (< threshold) battery")
flag.DurationVar(&argNormalInterval, "n", time.Minute*5, "battery check interval during good/normal (> threshold) battery")
flag.BoolVar(&argWatch, "w", false, "continuously watch battery level at preset interval. The interval depends on values of '-n' and '-l'")
flag.UintVar(&argThreshold, "t", 20, "battery percentage threshold, below which the battery will be considered as *low* and the user will start getting desktop notifications about low battery.")
flag.BoolVar(&argPercentage, "p", false, "just show the percentage of the battery.")
}
func getBatteryStatus() (uint, string, error) {
info, err := sysinfo.GetInfo("hardware", "battery")
if err != nil {
return 0, "", err
}
c, _ := info.Attribute(0, "CAPACITY")
capacity, _ := c.(uint)
s, _ := info.Attribute(0, "STATUS")
state, _ := s.(string)
return capacity, state, nil
}
func sendNotification(percentage uint, chargingState string) error {
conn, err := dbus.SessionBus()
if err != nil {
return err
}
urgency := 1 // normal
icon := "battery-low"
if percentage < 10 {
urgency = 2 //critical
icon = "battery-empty"
}
n := notify.Notification{
AppName: "Battery Notifier",
ReplacesID: notifID,
AppIcon: icon,
Summary: fmt.Sprintf("Only %v%% battery remaining!!!", percentage),
Hints: map[string]dbus.Variant{"urgency": dbus.MakeVariant(urgency)},
ExpireTimeout: 3 * time.Second,
}
notifID, err = notify.SendNotification(conn, n)
return err
}
func main() {
flag.Parse()
if argShowVersion {
fmt.Printf("battery-notifier %s \n\t%s\n\tgit:%s\n", version, buildTime, commit)
return
}
if argPercentage {
capacity, _, err := getBatteryStatus()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Print(capacity)
return
}
for {
capacity, state, err := getBatteryStatus()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Current Capacity: %v%%, Current State: %v\n", capacity, state)
// If -w not given, exit
if !argWatch {
break
}
sleepInterval := argNormalInterval
if state == "Discharging" && capacity < argThreshold {
sleepInterval = argLowInterval
err = sendNotification(capacity, state)
if err != nil {
fmt.Printf("Error while sending desktop notification: %v\n", err)
os.Exit(2)
}
}
time.Sleep(sleepInterval)
}
}