-
Notifications
You must be signed in to change notification settings - Fork 0
/
batterycheck
executable file
·68 lines (50 loc) · 2.25 KB
/
batterycheck
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
# Monitor the battery level and send a notification if it falls below 20%.
# Also notify if the battery is fully charged.
# Path to sysfs power_supply
POW_PATH="/sys/class/power_supply"
# Get adapter status. '0' if offline/discharging, '1' if online/charging.
STATUS=$(cat $POW_PATH/ADP0/online)
# Get battery capacity.
BAT_LVL=$(cat $POW_PATH/BAT0/capacity)
# Hard limits for cautions
LOW_CAP=20 # how low is too low?
FULL_CAP=100 # full capacity
# Eye Candy
ICON_DIR="/usr/share/icons/gnome/32x32/status"
LOW="$ICON_DIR/battery-caution.png"
FULL="$ICON_DIR/battery-full-charged.png"
CHARGING="$ICON_DIR/battery-low-charging.png"
DISCHARGING="$ICON_DIR/battery-good.png"
# Persistence for one-time notifications
CHARGE_FILE="/home/bharath/scripts/charge"
DISCHARGE_FILE="/home/bharath/scripts/discharge"
# TODO: How much time is left for the battery to discharge?
# Case 1, caution that battery is low.
[ "$STATUS" -eq 0 -a $BAT_LVL -le $LOW_CAP ] && $(which notify-send) -i $LOW \
-t 5000 -u critical \
"Battery Low!" "Battery level is ${BAT_LVL}%.\n Plug in your charger."
# Case 2, caution that battery is full.
[ "$STATUS" -eq 1 -a $BAT_LVL -eq $FULL_CAP ] && $(which notify-send) -i $FULL \
-t 5000 -u normal \
"Battery Full." "Battery at ${BAT_LVL}%.\n Unplug your charger."
# Case 3, notify that battery is charging, once, when plugging into the wall.
[ "$STATUS" -eq 1 -a ! -f $CHARGE_FILE ] && $(which notify-send) -i $CHARGING \
-t 3000 -u low \
"Battery Charging." "Current battery level is ${BAT_LVL}%." &&
touch $CHARGE_FILE &&
[ -f $DISCHARGE_FILE ] && rm $DISCHARGE_FILE
# Case 4, notify that battery is discharging, once, when plugging out of the wall.
# TODO: Fix this! Notifies about discharging every minute!
[ "$STATUS" -eq 0 -a ! -f $DISCHARGE_FILE ] &&
T=$(echo "( $(cat $POW_PATH/BAT0/energy_now) / $(cat $POW_PATH/BAT0/power_now) )" | bc -l ) \
H=$(echo "scale=0; $T / 1" | bc ) \
M=$(echo "scale=0; (($T - $H) * 60) / 1" | bc ) \
S=$(echo "scale=0; ((($T - $H) * 60 - $M) * 60) / 1" | bc ) \
REM="${H}h ${M}m ${S}s Remaining" &&
$(which notify-send) -i $DISCHARGING \
-t 3000 -u low \
"Battery Discharging." "Current battery level is ${BAT_LVL}%.\n$REM." &&
touch $DISCHARGE_FILE &&
[ -f $CHARGE_FILE ] && rm $CHARGE_FILE
exit 0