feat(script): update BatteryNotify.sh

This commit is contained in:
sankalp committed 2024-01-21 21:35:31 +05:45
1 parent 26c5bea6dc
commit 347a63ee11
11 files changed
+286 -349

No files matched your search

+162 -73
View File
@@ -1,41 +1,22 @@
#!/bin/bash
# Prasanthrangan Work
batterynotify_conf=$HOME/.config/hypr/scripts/batterynotify.conf
config_info() {
cat << EOF
trap resume_processes SIGINT
in_range() { local num=$1 local min=$2 local max=$3 ; [[ $num =~ ^[0-9]+$ ]] && (( num >= min && num <= max )); }
mnc=5 mxc=80 mnl=20 mxl=50 mnu=80 mxu=100 mnt=60 mxt=1000 mnf=80 mxf=100 mnn=1 mxn=60 mni=1 mxi=10 verbose=false undock=false #Defaults Ranges
while (( "$#" )); do # Parse command-line arguments and defaults
case "$1" in
"--full"|"-f") if in_range "$2" $mnf $mxf; then battery_full_threshold=$2 ; shift 2 ; else echo "$1 Error: Full Threshold must be $mnf - $mxf." >&2 ; exit 1 ; fi;;
"--critical"|"-c") if in_range "$2" $mnc $mxc; then battery_critical_threshold=$2 ; shift 2 ; else echo "$1 ERROR: Critical Threshold must be $mnc - $mxc." >&2 ; exit 1 ; fi;;
"--low"|"-l") if in_range "$2" $mnl $mnu; then battery_low_threshold=$2 ; shift 2 ; else echo "$1 ERROR: Low Threshold $mnl - $mnu." >&2 ; exit 1 ; fi;;
"--unplug"|"-u") if in_range "$2" $mnu $mxu; then unplug_charger_threshold=$2 ; shift 2 ; else echo "$1 ERROR: Unplug Threshold must be $mnu $mxu." >&2 ; exit 1 ; fi;;
"--timer"|"-t") if in_range "$2" $mnt $mxt; then timer=$2 ; shift 2 ; else echo "$1 ERROR: Timer must be $mnt - $mxt." >&2 ; exit 1 ; fi;;
"--notify"|"-n") if in_range "$2" $mnn $mxn; then notify=$2 ; shift 2 ; else echo "$1 ERROR: Notify must be $mnn - $mxn in minutes." >&2 ; exit 1 ; fi;;
"--interval"|"-i") if in_range "$2" $mni $mxi; then interval=$2 ; shift 2 ; else echo "$1 ERROR: Interval must be by $mni% - $mxi% intervals." >&2 ; exit 1 ; fi;;
"--verbose"|"-v") verbose=true ; shift ;;
"--undock"|"on") undock=true ; shift ;;
"--execute"|"-e") execute=$2 ; shift 2 ;;
Edit $batterynotify_conf for options.
STATUS THRESHOLD INTERVAL
Full $battery_full_threshold $notify Minutes
Critical $battery_critical_threshold $timer Seconds then '$execute_critical'
Low $battery_low_threshold $interval Percent then '$execute_low'
Unplug $unplug_charger_threshold $interval Percent then '$execute_unplug'
Charging: $execute_charging
Discharging: $execute_discharging
EOF
}
*|"--help"|"-h")
echo "Usage: $0 [options]"
echo " --verbose, -v Verbose Mode"
echo " --undock, on Shows Battery Plug In/Out/Full Notification"
echo " --full, -f Set battery full threshold (default: 100% percent)"
echo " --critical, -c Set battery critical threshold (default: 10% percent)"
echo " --low, -l Set battery low threshold (default: 20% percent)"
echo " --unplug, -u Set unplug charger threshold (default: 80% percent )"
echo " --timer, -t Set countdown timer (default: 120 seconds)"
echo " --interval, -i Set notify interval on LOW UNPLUG Status (default: 5% percent)"
echo " --notify, -n Set notify interval for Battery Full Status (default: 1140 mins/ 1 day)"
echo " --execute, -e Set command/script to execute if battery on critical threshold (default: systemctl suspend)"
echo " --help, -h Show this help message
Visit https://github.com/prasanthrangan/hyprdots for the Github Repo"
exit 0
;;
esac
done
is_laptop() { # Check if the system is a laptop
if grep -q "Battery" /sys/class/power_supply/BAT*/type; then
return 0 # It's a laptop
@@ -51,8 +32,6 @@ cat << VERBOSE
Battery Status: $battery_status
Battery Percentage: $battery_percentage
=============================================
VERBOSE
fi
}
@@ -69,7 +48,7 @@ fn_percentage () {
while [ $count -gt 0 ] && [[ $battery_status == "Discharging"* ]]; do
for battery in /sys/class/power_supply/BAT*; do battery_status=$(< "$battery/status") ; done
if [[ $battery_status != "Discharging" ]] ; then break ; fi
fn_notify "-t 5000 -r 69 " "CRITICAL" "Battery Critically Low" "$battery_percentage% is critically low. Device will execute $execute in $((count/60)):$((count%60)) ."
fn_notify "-t 5000 -r 69 " "CRITICAL" "Battery Critically Low" "$battery_percentage% is critically low. Device will execute $execute_critical in $((count/60)):$((count%60)) ."
count=$((count-1))
sleep 1
done
@@ -79,13 +58,13 @@ fn_percentage () {
last_notified_percentage=$battery_percentage
fi
}
fn_action () { #handles the $execute command
fn_action () { #handles the $execute_critical command #? This is special as it will try to execute always
count=$(( timer > $mnt ? timer : $mnt )) # reset count
nohup $execute
nohup $execute_critical
}
fn_status () {
if [[ $battery_percentage -ge $battery_full_threshold ]] && [ "$battery_status" == *"Charging"* ]; then echo "Full and $battery_status"
if [[ $battery_percentage -ge $battery_full_threshold ]] && [[ "$battery_status" != *"Discharging"* ]]; then echo "Full and $battery_status"
battery_status="Full" ;fi
case "$battery_status" in # Handle the power supply status
"Discharging") if $verbose; then echo "Case:$battery_status Level: $battery_percentage" ;fi
@@ -93,32 +72,27 @@ case "$battery_status" in # Handle the power supply status
prev_status=$battery_status
urgency=$([[ $battery_percentage -le "$battery_low_threshold" ]] && echo "CRITICAL" || echo "NORMAL")
fn_notify "-t 5000 -r 54321 " "$urgency" "Charger Plug OUT" "Battery is at $battery_percentage%."
$execute_discharging
fi
fn_percentage
;;
"Not"*|"Charging") if $verbose; then echo "Case:$battery_status Level: $battery_percentage" ;fi
# Due to modifications of some devices Not Charging after reaching 99 or limits
#if [[ ! -f "/tmp/hyprdots.batterynotify.status.$battery_status-$$" ]] && [[ "$battery_status" == "Not"* ]] ; then
#touch "/tmp/hyprdots.batterynotify.status.$battery_status-$$"
#count=$(( timer > $mnt ? timer : $mnt )) # reset count
#echo "Status: '==>> "$battery_status" <<==' Device Reports Not Charging!,This may be device Specific errors."
#fn_notify "-t 5000 " "CRITICAL" "Charger Plug In" "Battery is at $battery_percentage%."
#fi
if [[ "$prev_status" == "Discharging" ]] || [[ "$prev_status" == "Not"* ]] ; then
prev_status=$battery_status
count=$(( timer > $mnt ? timer : $mnt )) # reset count
urgency=$([[ "$battery_percentage" -ge $unplug_charger_threshold ]] && echo "CRITICAL" || echo "NORMAL")
fn_notify "-t 5000 -r 54321 " "$urgency" "Charger Plug In" "Battery is at $battery_percentage%."
$execute_charging
fi
fn_percentage
;;
"Full") if $verbose; then echo "Case:$battery_status Level: $battery_percentage" ;fi
if [[ $battery_status != "Discharging" ]]; then
now=$(date +%s)
if [[ "$prev_status" == *"harging"* ]] || ((now - lt >= $((notify*60)) )); then
fn_notify "-t 5000 -r 54321" "CRITICAL" "Battery Full" "Please unplug your Charger"
prev_status=$battery_status lt=$now
$execute_charging
fi
fi
;;
@@ -132,49 +106,164 @@ case "$battery_status" in # Handle the power supply status
esac
}
fn_status_change () { # Handle when status changes
for battery in /sys/class/power_supply/BAT*; do battery_status=$(< "$battery/status") battery_percentage=$(< "$battery/capacity")
if [ "$battery_status" != "$last_battery_status" ] || [ "$battery_percentage" != "$last_battery_percentage" ]; then last_battery_status=$battery_status last_battery_percentage=$battery_percentage # Check if battery status or percentage has changed
fn_verbose
fn_percentage
if $undock; then fn_status echo yes ; fi
fi
done
get_battery_info() { #? Might change this if we can get an effective way to parse dbus.
total_percentage=0 battery_count=0
for battery in /sys/class/power_supply/BAT*; do
battery_status=$(<"$battery/status") battery_percentage=$(<"$battery/capacity")
total_percentage=$((total_percentage + battery_percentage))
battery_count=$((battery_count + 1))
done
battery_percentage=$((total_percentage / battery_count)) #? For Multiple Battery
}
resume_processes() { for pid in $pids ; do if [ $pid -ne $current_pid ] ; then kill -CONT $pid ; notify-send -a "Battery Notify" -t 2000 -r 9889 -u "CRITICAL" "Debugging ENDED, Resuming Regular Process" ; fi ; done }
fn_status_change () { # Handle when status changes
get_battery_info
# Add these two lines at the beginning of the function
local executed_low=false
local executed_unplug=false
if [ "$battery_status" != "$last_battery_status" ] || [ "$battery_percentage" != "$last_battery_percentage" ]; then
last_battery_status=$battery_status
last_battery_percentage=$battery_percentage # Check if battery status or percentage has changed
fn_verbose
fn_percentage
if [[ "$battery_percentage" -le "$battery_low_threshold" ]] && ! $executed_low; then $execute_low
executed_low=true executed_unplug=false
fi
if [[ "$battery_percentage" -ge "$unplug_charger_threshold" ]] && ! $executed_unplug; then $execute_unplug
executed_unplug=true executed_low=false
fi
if $undock; then fn_status echo yes ; fi
fi
}
resume_processes() { for pid in $pids ; do if [ "$pid" -ne "$current_pid" ] ; then kill -CONT $pid ; notify-send -a "Battery Notify" -t 2000 -r 9889 -u "CRITICAL" "Debugging ENDED, Resuming Regular Process" ; fi ; done }
main() { # Main function
if is_laptop; then
rm -fr /tmp/hyprdots.batterynotify* # Cleaning the lock file
battery_full_threshold=${battery_full_threshold:-100}
battery_critical_threshold=${battery_critical_threshold:-10}
battery_critical_threshold=${battery_critical_threshold:-5}
unplug_charger_threshold=${unplug_charger_threshold:-80}
battery_low_threshold=${battery_low_threshold:-20}
timer=${timer:-120}
notify=${notify:-1140}
interval=${interval:-5}
execute_critical=${execute_critical:-"systemctl suspend"}
execute_low=${execute_low:-}
execute_unplug=${execute_unplug:-}
execute=${execute:-"systemctl suspend"}
cat << EOF
Script is running...
Check $0 --help for options.
STATUS THRESHOLD INTERVAL
Full $battery_full_threshold $notify Minutes
Critical $battery_critical_threshold $timer Seconds then "$execute"
Low $battery_low_threshold $interval Percent
Unplug $unplug_charger_threshold $interval Percent
EOF
config_info
if $verbose; then for line in "Verbose Mode is ON..." "" "" "" "" ; do echo $line ; done;
current_pid=$$
pids=$(pgrep -f "/bin/bash $HOME/.config/hypr/scripts/batterynotify.sh" )
for pid in $pids ; do if [ $pid -ne $current_pid ] ;then kill -STOP $pid ;notify-send -a "Battery Notify" -t 2000 -r 9889 -u "CRITICAL" "Debugging STARTED, Pausing Regular Process" ;fi ; done ; trap resume_processes SIGINT ; fi
fn_status_change # initiate the function
for pid in $pids ; do if [ "$pid" -ne $current_pid ] ;then kill -STOP "$pid" ;notify-send -a "Battery Notify" -t 2000 -r 9889 -u "CRITICAL" "Debugging STARTED, Pausing Regular Process" ;fi ; done ; trap resume_processes SIGINT ; fi
get_battery_info # initiate the function
last_notified_percentage=$battery_percentage
prev_status=$battery_status
dbus-monitor --system "type='signal',interface='org.freedesktop.DBus.Properties',path='$(upower -e | grep battery)'" 2> /dev/null | while read -r battery_status_change; do fn_status_change ; done
fi
}
verbose=false undock=false
if [ ! -f "$batterynotify_conf" ]; then
mkdir -p "$(dirname "$batterynotify_conf")"
touch "$batterynotify_conf"
echo "[CREATED] $batterynotify_conf"
echo "
#? Full battery threshold (default: 100%)
battery_full_threshold=100
#? Critical battery threshold (default: 10%)
battery_critical_threshold=10
#? Low battery threshold (default: 20%)
battery_low_threshold=20
#? Unplug charger threshold (default: 80%)
unplug_charger_threshold=80
#? Countdown timer before executing execute_critical (default: 120 seconds)
timer=120
#? Notify interval for Battery Full Status (default: 1140 mins/ 1 day)
notify=1140
#? Notify interval on LOW and UNPLUG Status (default: 5%)
interval=5
#? Shows Battery Plug In/Out/Full Notification
undock=false
#? Command/script to execute at minimum unplug_charger_threshold
execute_unplug=\"\"
#? Command/script to execute at maximum battery_low_threshold
execute_low=\"\"
#? Command/script to execute if battery on critical threshold (default: systemctl suspend)
execute_critical=\"systemctl suspend\"
#? Command/script to execute when battery is discharging, Required undock=true
execute_discharging=\"\"
#? Command/script to execute when battery is charging, Required undock=true
execute_charging=\"\"
" > "$batterynotify_conf"
fi
source "$batterynotify_conf"
case "$1" in
--modify)
EDITOR="${EDITOR:-code}" #* Use VS Code as the default editor
echo -e "[Editor]: $EDITOR \n To change editor, run 'export EDITOR=prefered-editor' \n[Modifying]: $batterynotify_conf \nPress Any Key if done editing"
#kitty -o allow_remote_control=yes -o listen_on=unix:/tmp/mykitty $(which $EDITOR) "$batterynotify_conf" > /dev/null 2>&1 &
kitty "$(which $EDITOR)" "$batterynotify_conf" > /dev/null 2>&1 &
LAST_MD5SUM=$(md5sum "$batterynotify_conf")
while true; do CURRENT_MD5SUM=$(md5sum "$batterynotify_conf")
if [ "$CURRENT_MD5SUM" != "$LAST_MD5SUM" ]; then
notify-send "Config Changed: Needs reboot or restart $0"
LAST_MD5SUM="$CURRENT_MD5SUM"
fi ; read -t 2 -n 1 > /dev/null && break #? loop every 2 seconds
done
exit 0
;;
--info)
config_info
exit 0
;;
--reset)
rm -r "$batterynotify_conf"
echo "[RESET] Batterynotify configuration"
exit 0
;;
--verbose)
verbose=true
;;
-h|--help)
cat << HELP
Usage: $0 [options]
[--modify] Modify configuration file
[--info] Display configuration information
[--reset] Reset configuration
[--verbose] Debugging mode
[-h|--help] This Message
HELP
exit 0
;;
esac
mnc=2 mxc=50 mnl=10 mxl=80 mnu=50 mxu=100 mnt=60 mxt=1000 mnf=80 mxf=100 mnn=1 mxn=1140 mni=1 mxi=10 #Defaults Ranges
check_range() {
local var=$1 min=$2 max=$3 error_message=$4
if [[ $var =~ ^[0-9]+$ ]] && (( var >= min && var <= max )); then
var=$var ; shift 2
else
echo "$1 ERROR: $error_message must be $min - $max." >&2 ; exit 1
fi
}
check_range "$battery_full_threshold" $mnf $mxf "Full Threshold"
check_range "$battery_critical_threshold" $mnc $mxc "Critical Threshold"
check_range "$battery_low_threshold" $mnl $mxl "Low Threshold"
check_range "$unplug_charger_threshold" $mnu $mxu "Unplug Threshold"
check_range "$timer" $mnt $mxt "Timer"
check_range "$notify" $mnn $mxn "Notify"
check_range "$interval" $mni $mxi "Interval"
trap resume_processes SIGINT
main
+28
View File
@@ -0,0 +1,28 @@
#? Full battery threshold (default: 100%)
battery_full_threshold=100
#? Critical battery threshold (default: 10%)
battery_critical_threshold=20
#? Low battery threshold (default: 20%)
battery_low_threshold=40
#? Unplug charger threshold (default: 80%)
unplug_charger_threshold=80
#? Countdown timer before executing execute_critical (default: 120 seconds)
timer=120
#? Notify interval for Battery Full Status (default: 1140 mins/ 1 day)
notify=1140
#? Notify interval on LOW and UNPLUG Status (default: 5%)
interval=3
#? Shows Battery Plug In/Out/Full Notification
undock=true
#? Command/script to execute at minimum unplug_charger_threshold
execute_unplug=""
#? Command/script to execute at maximum battery_low_threshold
execute_low=""
#? Command/script to execute if battery on critical threshold (default: systemctl suspend)
execute_critical="systemctl suspend"
#? Command/script to execute when battery is discharging, Required undock=true
execute_discharging="Power_Manager.sh save -ba"
#? Command/script to execute when battery is charging, Required undock=true
execute_charging="Power_Manager reset"