-
Notifications
You must be signed in to change notification settings - Fork 2
/
battery_notifier
executable file
·159 lines (135 loc) · 4.85 KB
/
battery_notifier
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/bash
# battery_notifier
# Checks battery level and sends notifiers when low and discharging
# Copyright (C) 2014-2015 Alexander Swen <alex@swen.nu>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Alexander Swen
# Private contact: alex@swen.nu
# CHANGELOG:
# 2015-02-15 A.Swen created.
# 2016-01-24 A.Swen Added logmessage to warn that beep isn't there.
# INSTALLATION
# > NOTE: Some systems have a desktop notifier `dunst` installed. That's a very
# basic notifier program. When possible xfce4-notifyd is much prettier
# This script needs three preparationscteps:
# 1. Install notify-send.
# 2. You need to tell cron to run this every now and then.
# like: */10 * * * * [ -x $HOME/bin/battery_notifier ] && $HOME/bin/battery_notifier >> battery_notifier.log 2>&1
# 3. You need to set the DBUS_SESSION_BUS_ADDRESS variable.
# By default cron does not have access to the variable. To remedy this put the
# following script somewhere and call it when the user logs in, For example put
# this in ~/.xprofile:
#
# [ -d $HOME/.dbus ] || mkdir $HOME/.dbus
# env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.dbus/Xdbus
# echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.dbus/Xdbus
# chmod 600 $HOME/.dbus/Xdbus
#
# If beep is installed there will be a beep when capacity is critical.
# SETTINGS
date=$(date +%Y%m%d)
me=$(basename $0)
mydir=$(dirname $0)
warn=25
crit=10
beep=true
beep_freq=100
beep_time=200
beep_times=3
# FUNCTIONS
die () {
shift
printf '%s\n' "=====================" >&2
printf '%s\n' "==== FATAL ERROR ====" >&2
printf '%s\n\n' "=====================" >&2
printf '%s\n\n' "$@" >&2
exit $rc
}
log () { printf '%s %s\n' "$(date +%F' '%T)" "$@"; }
is_int () { return $(test "$1" -eq "$1" > /dev/null 2>&1); }
get_battery_info () {
status=$(cat /sys/class/power_supply/BAT0/status)
capacity=$(cat /sys/class/power_supply/BAT0/capacity)
time_to_empty="$(time_to_empty)"
}
time_to_empty () {
upower -i /org/freedesktop/UPower/devices/battery_BAT0|awk '/time to empty:/ {print $4" "$5}'
}
time_to_full () {
upower -i /org/freedesktop/UPower/devices/battery_BAT0|awk '/time to full:/ {print $4" "$5}'
}
notify () {
log "Trying to warn the user."
case $1 in
warn)
local ttl=60000
local urgency=normal
local message=$(cat << EOF_MSG
Batterylevel is $capacity and $status.
Estimated time to empty: $time_to_empty.
EOF_MSG
)
[ $beep == true ] && try_beep
;;
crit)
local ttl=120000
local urgency=critical
local message=$(cat << EOF_MSG
Batterylevel is $capacity and $status.
Estimated time to empty: $time_to_empty.
Laptop will go into sleep in 30 seconds!
EOF_MSG
)
[ $beep == true ] && try_beep
;;
*) die 4 "You should use 'warn' or 'crit' as \$1 in the notify function.";;
esac
notify-send --expire-time=$ttl --urgency=$urgency "Batery Low Warning" "$message"
}
try_beep () {
if [ -x /usr/bin/beep ];then
beep -f $beep_freq -l $beep_time -r $beep_times -d 200
else
log "Beep not installed."
return
fi
}
# SCRIPT
is_int $warn || die 1 "Warn must be an integer."
is_int $crit || die 1 "Crit must be an integer."
[ $warn -lt $crit ] && die 1 "Warn should be greater than crit."
# Cron needs to know where to send the message to.
[ -f $HOME/.dbus/Xdbus ]&& source $HOME/.dbus/Xdbus || die 1 "$HOME/.dbus/Xdbus not found"
# Check if notify-send is installed.
[ -x /usr/bin/notify-send ]||die 2 "Notify-send not installed."
# Check if batteryinfo is available
[ -f /sys/class/power_supply/BAT0/capacity ]||die 2 "Dunno where to find battery info. /sys/class/power_supply/BAT0/capacity not found."
log "Checking batterystatus."
get_battery_info
[ -n "$status" ] || die 3 "Script failed to detect batterystatus."
[ -n "$capacity" ] || die 3 "Script failed to detect batterycapacity."
is_int $capacity || die 3 "Script didn't get an integer from the capacityfile."
if [[ "$status" == "Discharging" ]];then
log "The battery is $status and at ${capacity}%. Estimated time to empty: $time_to_empty."
if [[ $capacity -le $crit ]];then
notify crit
sleep 30
/usr/bin/i3lock -ti /home/alex/mug.png -c 000000 &
sudo /usr/sbin/pm-suspend
elif [[ $capacity -le $warn ]];then
notify warn
fi
else
log "The battery is $status and at ${capacity}%. Estimated time to full: $(time_to_full)."
fi
# END