From 499cc7463bba0464673ffeb03d6b2f83cbaa1d98 Mon Sep 17 00:00:00 2001 From: Pig Monkey Date: Sat, 30 Jan 2016 13:33:43 -0800 Subject: [PATCH] reorganize lowbatt script Check for status and only continue if the battery is discharging. --- roles/laptop/files/lowbatt.sh | 57 ++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/roles/laptop/files/lowbatt.sh b/roles/laptop/files/lowbatt.sh index bcb7dfb..ee96b74 100755 --- a/roles/laptop/files/lowbatt.sh +++ b/roles/laptop/files/lowbatt.sh @@ -4,7 +4,7 @@ BATTERY="BAT0" low() { # Warn if the battery is low. - message="Battery $BATTERY is at $CAPACITY. We need more power, Scotty!" + message="Battery $BATTERY is at $CAPACITY%. We need more power, Scotty!" systemd-cat -t 'lowbatt' -p warning echo "$message" notify-send --urgency=critical "Low Battery" "$message" wall "Battery is low. $message" @@ -16,24 +16,47 @@ critical() { /usr/bin/systemctl suspend } +get_status() { + # Get the status of the battery. + if [ -f /sys/class/power_supply/"$BATTERY"/status ]; then + # Get the remaining capacity of the battery. + STATUS=`cat /sys/class/power_supply/"$BATTERY"/status` + else + echo "Could not get status for battery $BATTERY." + exit 1 + fi + # If the battery is not discharging, exit silently. + if [ "$STATUS" != "Discharging" ]; then + exit + fi +} + +get_capacity() { + # Only continue if we can get the capacity of the battery. + if [ -f /sys/class/power_supply/"$BATTERY"/capacity ]; then + # Get the remaining capacity of the battery. + CAPACITY=`cat /sys/class/power_supply/"$BATTERY"/capacity` + else + echo "Could not get capacity for battery $BATTERY." + exit 1 + fi +} + +check_capacity() { + # If the capacity is between 5 and 10, it is low. + if [ "$CAPACITY" -gt 5 -a "$CAPACITY" -le 10 ]; then + low + # If the capacity is 5 or less, it is critical. + elif [ "$CAPACITY" -le 5 ]; then + critical + fi +} + # Allow the user to specify a different battery. if [ -n "$1" ]; then BATTERY="$1" fi -# Only continue if we can get the capacity of the battery. -if [ -f /sys/class/power_supply/"$BATTERY"/capacity ]; then - # Get the remaining capacity of the battery. - CAPACITY=`cat /sys/class/power_supply/"$BATTERY"/capacity` -else - echo "Could not get capacity for battery $BATTERY." - exit 1 -fi - -# If the capacity is between 5 and 10, it is low. -if [ "$CAPACITY" -gt 5 -a "$CAPACITY" -le 10 ]; then - low -# If the capacity is 5 or less, it is critical. -elif [ "$CAPACITY" -le 5 ]; then - critical -fi +get_status +get_capacity +check_capacity