From f900b78dc323a0630a31cbd7281abd4018273560 Mon Sep 17 00:00:00 2001 From: koralowiec Date: Fri, 30 Apr 2021 16:56:17 +0200 Subject: [PATCH] Init commit with working program (checking battery level, state and sending notification) --- go.mod | 10 +++++++++ go.sum | 17 +++++++++++++++ main.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a9d0d73 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module gitlab.com/koralowiec/battery-notifier + +go 1.16 + +require ( + github.com/distatus/battery v0.10.0 // indirect + github.com/keybase/go-notifier v0.0.0-20160519183414-17405ab3fd75 // indirect + golang.org/x/sys v0.0.0-20210426230700-d19ff857e887 // indirect + howett.net/plist v0.0.0-20201203080718-1454fab16a06 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..eeb74dd --- /dev/null +++ b/go.sum @@ -0,0 +1,17 @@ +github.com/distatus/battery v0.10.0 h1:YbizvmV33mqqC1fPCAEaQGV3bBhfYOfM+2XmL+mvt5o= +github.com/distatus/battery v0.10.0/go.mod h1:STnSvFLX//eEpkaN7qWRxCWxrWOcssTDgnG4yqq9BRE= +github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/keybase/go-notifier v0.0.0-20160519183414-17405ab3fd75 h1:bfPY7MnS2XL4MpZZG45QPzcd1YxF6kTj3KbzN7bpupA= +github.com/keybase/go-notifier v0.0.0-20160519183414-17405ab3fd75/go.mod h1:0HgowL4xc7FNcQ06SpNVT8fnDjh7fqFqxNNZxxvqxYA= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +golang.org/x/sys v0.0.0-20190912141932-bc967efca4b8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210426230700-d19ff857e887 h1:dXfMednGJh/SUUFjTLsWJz3P+TQt9qnR11GgeI3vWKs= +golang.org/x/sys v0.0.0-20210426230700-d19ff857e887/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +howett.net/plist v0.0.0-20181124034731-591f970eefbb/go.mod h1:vMygbs4qMhSZSc4lCUl2OEE+rDiIIJAIdR4m7MiMcm0= +howett.net/plist v0.0.0-20201203080718-1454fab16a06 h1:QDxUo/w2COstK1wIBYpzQlHX/NqaQTcf9jyz347nI58= +howett.net/plist v0.0.0-20201203080718-1454fab16a06/go.mod h1:vMygbs4qMhSZSc4lCUl2OEE+rDiIIJAIdR4m7MiMcm0= diff --git a/main.go b/main.go new file mode 100644 index 0000000..5220798 --- /dev/null +++ b/main.go @@ -0,0 +1,67 @@ +package main + +import ( + "flag" + "fmt" + "log" + "time" + + "github.com/distatus/battery" + "github.com/keybase/go-notifier" +) + +const notificationTitle = "Low Battery!" +const notificationMsgTemplate = "Battery at %.2f%%" + +var batteryThreshold float64 +var minutesBetweenCheck int + +func sendNotification(currentPercent float64) { + message := fmt.Sprintf(notificationMsgTemplate, currentPercent) + + notification := notifier.Notification{} + notification.Title = notificationTitle + notification.Message = message + + notifier, err := notifier.NewNotifier() + if err != nil { + log.Fatal(err) + return + } + + if err := notifier.DeliverNotification(notification); err != nil { + log.Fatal(err) + return + } +} + +func checkBatteries() { + batteries, err := battery.GetAll() + + if err != nil { + log.Fatal("Could not get battery info!") + return + } + + for _, bat := range batteries { + percent := bat.Current / bat.Full * 100 + + if percent <= batteryThreshold && bat.State == battery.Discharging { + sendNotification(percent) + } + } +} + +func init() { + flag.Float64Var(&batteryThreshold, "t", 30.0, "Battery threshold. Battery below this level (and battery in discharging state) will cause sending notification.") + flag.IntVar(&minutesBetweenCheck, "m", 1, "Checking interval in minutes.") + flag.Parse() +} + +func main() { + ticker := time.NewTicker(time.Duration(minutesBetweenCheck) * time.Minute) + + for range ticker.C { + go checkBatteries() + } +}