-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.go
53 lines (46 loc) · 1.04 KB
/
daemon.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
package main
import (
"fmt"
"os/exec"
"time"
"github.com/gen2brain/beeep"
)
type Daemon struct {
batteryInfo *BatteryInfo
config *Config
}
func NewDaemon() *Daemon {
return &Daemon{
batteryInfo: NewBatteryInfo(),
config: NewConfig(),
}
}
func (d *Daemon) WarnLevelReached() bool {
if d.batteryInfo.level <= d.config.warnOnLevel {
return true
}
return false
}
func (d *Daemon) ExecLevelReached() bool {
if d.batteryInfo.level <= d.config.execOnLevel {
return true
}
return false
}
func (d *Daemon) Start() {
for {
if d.WarnLevelReached() {
beeep.Notify("powerd - Warn Level", d.config.warnMessage, "")
}
if d.ExecLevelReached() {
beeep.Alert("powerd - Command Execution", fmt.Sprintf("Executing '%s' in 3 seconds", d.config.execCommand), "")
time.Sleep(3 * time.Second)
output, err := exec.Command("sh", "-c", d.config.execCommand).CombinedOutput()
if err != nil {
panic(err)
}
beeep.Notify("powerd - Command Result", string(output), "")
}
time.Sleep(d.config.checkInterval * time.Second)
}
}