Skip to content

Commit

Permalink
Init commit with working program (checking battery level, state and s…
Browse files Browse the repository at this point in the history
…ending notification)
  • Loading branch information
koralowiec committed Apr 30, 2021
0 parents commit f900b78
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
67 changes: 67 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit f900b78

Please sign in to comment.