Skip to content

Instantly share code, notes, and snippets.

@quaintdev
Created August 30, 2021 17:01
Show Gist options
  • Save quaintdev/f04807ad9ccd68737182ff306e08e682 to your computer and use it in GitHub Desktop.
Save quaintdev/f04807ad9ccd68737182ff306e08e682 to your computer and use it in GitHub Desktop.
A battery low and full notifier for sway along with systemd service config
package main
import (
"github.com/distatus/battery"
"github.com/gen2brain/beeep"
"log"
"time"
)
// check battery every 'interval' seconds
const interval int = 60
// report warning below
const percentageWarningBelow int = 15
func main() {
log.Printf("Querying battery for status every %d secs.", interval)
log.Printf("Reporting battery low warning below %d", percentageWarningBelow)
for range time.Tick(time.Second * time.Duration(interval)) {
batteries, _ := battery.GetAll()
bat0 := batteries[0]
capacity := int(bat0.Current * 100 / bat0.Full)
if bat0.State == battery.Discharging && capacity < percentageWarningBelow {
beeep.Alert("Battery Low", "Please connect a power source", "assets/warning.png")
} else if batteries[0].State == battery.Charging && capacity == 100 {
beeep.Alert("Battery Full", "Please unplug power source", "assets/warning.png")
}
}
}
[Unit]
Description=A battery monitoring service
After=network.target
[Service]
User=username
ExecStart=/home/user/go/bin/batterymon
Restart=always
RestartSec=10
StartLimitIntervalSec=0
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment