2021-08-12 11:04:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func notify(msg string) {
|
2023-03-05 14:09:44 +00:00
|
|
|
cmd := exec.Command("notify-send", "-t", "5000", "-u", "critical", "bat-alert", msg)
|
2021-08-12 11:04:48 +00:00
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var targetFlag = flag.Int("target", 15, "when to send the alert")
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
sent := false
|
|
|
|
for {
|
|
|
|
capacity, err := os.ReadFile("/sys/class/power_supply/BAT0/capacity")
|
|
|
|
if err != nil {
|
|
|
|
notify("error: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
numCap, err := strconv.Atoi(strings.Trim(string(capacity), "\n"))
|
|
|
|
if err != nil {
|
|
|
|
notify("error: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if numCap < 15 {
|
|
|
|
if !sent {
|
|
|
|
sent = true
|
|
|
|
notify("battery running low!")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sent = false
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 10)
|
|
|
|
}
|
|
|
|
}
|