-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (98 loc) · 2.44 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
package main
import(
"os"
"bufio"
"strconv"
"time"
"os/exec"
"strings"
)
func getBatteryPercentage() int {
file, err := os.Open("/sys/class/power_supply/BAT0/capacity")
if err != nil {
return -1
}
defer file.Close()
c := bufio.NewScanner(file)
c.Scan()
percentage, err := strconv.Atoi(c.Text())
if err != nil {
return -1
} else {
return percentage
}
}
func getPowerStatus() bool {
file, err := os.Open("/sys/class/power_supply/BAT0/status")
if err != nil {
return false
}
defer file.Close()
c := bufio.NewScanner(file)
c.Scan()
switch status := c.Text(); status {
case "Charging", "Full", "Unknown":
return true
default:
return false
}
}
func sendNotification(notification string) bool {
args := []string{"-a", "powermon", "-u"}
switch notification {
case "Discharging":
args = append(args, "normal", "Descarregando")
case "Charging":
args = append(args, "low", "Carregando")
default:
args = append(args, "critical", "Bateria Fraca")
}
batteryPercentageMsg := []string{"Bateria ", "em ", strconv.Itoa(getBatteryPercentage()), "%"}
args = append(args, strings.Join(batteryPercentageMsg, ""))
if err := exec.Command("notify-send", args...).Run(); err != nil {
return false
} else {
return true
}
}
func main() {
type timerProfile struct {
poolingIntervalSec int
notifyIntervalMin int
}
type powerProfile struct {
notify bool
batteryPercentage int
charger bool
}
timer := timerProfile {poolingIntervalSec: 2, notifyIntervalMin: 5}
power := powerProfile {notify: false, batteryPercentage: 15, charger: false}
notifyCounter := 0
for {
if getPowerStatus() {
notifyCounter = 0
if !power.charger {
sendNotification("Charging")
power.charger = true
power.notify = true
}
} else {
if power.charger {
sendNotification("Discharging")
power.charger = false
power.notify = false
}
if getBatteryPercentage() <= power.batteryPercentage && !power.notify {
sendNotification("*")
power.notify = true
notifyCounter = 0
}
if (notifyCounter * timer.poolingIntervalSec) >= (timer.notifyIntervalMin * 60) && getBatteryPercentage() <= power.batteryPercentage {
sendNotification("*")
notifyCounter = 0
}
}
notifyCounter++
time.Sleep(time.Duration(timer.poolingIntervalSec) * time.Second)
}
}