Skip to content

Commit

Permalink
📦 NEW: Send dunst notification about the battery percentage to the us…
Browse files Browse the repository at this point in the history
…er using systemd timer
  • Loading branch information
jcjgraf committed Jan 31, 2020
1 parent b3d2e0d commit 3786835
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 7 deletions.
91 changes: 84 additions & 7 deletions Scripts/battery.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/bin/bash

# Icons
chargingIcon=""
batteryFull=""
batteryThreeQuaters=""
batteryHalf=""
batteryQuater=""
batteryEmpty=""

statusCache=~/.cache/battery/lastStatus

# Fetch data
## Status: Charging, Full, Discharging
Expand Down Expand Up @@ -38,10 +40,10 @@ time="$( acpi | grep -oP 'Battery 0:.*?\K\d\d:\d\d+' )"

getColoredPercentage() {
## Return colored percentage depending on battery level

# Fetch colors from .Xresources
local critical=$(xrdb -query | grep '*color1:'| awk '{print $NF}')
local low=$(xrdb -query | grep '*color3:'| awk '{print $NF}')
local critical=$(xrdb -query | grep '*color1:'| awk '{print $NF}')
local low=$(xrdb -query | grep '*color3:'| awk '{print $NF}')

if [ $percentage -le 15 ]
then
Expand Down Expand Up @@ -75,8 +77,8 @@ getBatteryIcon() {
}

getStatusMessage() {
## Echo status message for the status bar
## Echo status message for the status bar

case "$status" in
Charging)
local out="$chargingIcon $percentage% $time"
Expand All @@ -98,14 +100,89 @@ getStatusMessage() {
echo "$out"
}

while getopts 's' flag
doStatusNotify() {
## Send dunst messages in specific occasions

# Check if cache file exists
if [[ -f $statusCache ]]
then
# Read previous status and analyse difference
local previousStatusCache=$(<$statusCache)

## TODO Do some usefull stuff
local previousStatus="$( echo $previousStatusCache | grep -oP '^(\w)+' )"
local previousPercentage="$( echo $previousStatusCache | grep -oP '(\w)+$' )"
#echo "$previousStatus $previousPercentage"

## AC gets unplugged
#if [[ "$previousStatus" == "Full" ]] && [[ "$status" == "Discharging" ]]
#then
# ~/bin/notifyer -a "batteryChecker" -u normal "AC Disconnected"
#fi

## AC gets plugged in
#if [[ "$previousStatus" != "Full" && "$previousStatus" != "Charging" ]] && [[ "$status" == "Charging" || "$status" == "Full" ]]
#then
# ~/bin/notifyer -a "batteryChecker" -u normal "AC Connected"
#fi

# Send percentage notification when on battery
if [[ "$status" == "Discharging" ]]
then
if (( $previousPercentage > 75 && $percentage <= 75 ))
then
~/bin/notifyer -a "batteryChecker" -u low "Battery" "75%"
elif (( $previousPercentage > 50 && $percentage <= 50 ))
then
~/bin/notifyer -a "batteryChecker" -u normal "Battery" "50%"
elif (( $previousPercentage > 35 && $percentage <= 35 ))
then
~/bin/notifyer -a "batteryChecker" -u normal "Battery" "35"
elif (( $previousPercentage > 25 && $percentage <= 25 ))
then
~/bin/notifyer -a "batteryChecker" -u normal "Low Battery" "25%"
elif (( $previousPercentage > 20 && $percentage <= 20 ))
then
~/bin/notifyer -a "batteryChecker" -u normal "Low Battery" "20%"
elif (( $previousPercentage > 15 && $percentage <= 15 ))
then
~/bin/notifyer -a "batteryChecker" -u critical "Low Battery" "15%"
elif (( $previousPercentage > 10 && $percentage <= 10 ))
then
~/bin/notifyer -a "batteryChecker" -u critical "Critical Battery" "10%"
elif (( $previousPercentage > 5 && $percentage <= 5 ))
then
~/bin/notifyer -a "batteryChecker" -u critical "Critical Battery" "5%"
elif (( $previousPercentage > 2 && $percentage <= 2 ))
then
~/bin/notifyer -a "batteryChecker" -u critical "2%"
fi
fi

# Write current status to file
if [[ "$status" != "Unknown" ]]
then
echo "$status, $percentage" > $statusCache
fi

else
# Create dir and write current status to file
mkdir -p "$(dirname $statusCache)"
echo "$status, $percentage" > $statusCache
fi
}

while getopts 'sc' flag
do
case "${flag}" in
s)
echo "$(getStatusMessage)"
;;
c)
echo "$(doStatusNotify)"
;;
*)
echo "Flags: -s"
echo "Flags: -s -c"
exit 1 ;;
esac
done
1 change: 1 addition & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ linker "$dotfiles/Scripts/notifyer" ~/bin/notifyer
linker "$dotfiles/Scripts/lock.sh" ~/bin/lock.sh
linker "$dotfiles/Scripts/battery.sh" ~/bin/battery.sh
linker "$dotfiles/Scripts/mailChecker.sh" ~/bin/mailChecker.sh
linker "$dotfiles/Scripts/battery.sh" ~/bin/battery.sh

## WM
linker "$dotfiles/wm/i3" ~/.config/i3
Expand Down
6 changes: 6 additions & 0 deletions systemd/user/batteryNotification.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[Unit]
Description=Battery notifyer

[Service]
Type=oneshot
ExecStart=/home/jeanclaude/bin/battery.sh -c
10 changes: 10 additions & 0 deletions systemd/user/batteryNotification.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Unit]
Description=Battery notification timer

[Timer]
OnBootSec=20
OnUnitActiveSec=30
Unit=batteryNotification.service

[Install]
WantedBy=timers.target
1 change: 1 addition & 0 deletions systemd/user/timers.target.wants/batteryNotification.timer

0 comments on commit 3786835

Please sign in to comment.