-
Notifications
You must be signed in to change notification settings - Fork 0
/
battery_monitor.sh
executable file
·163 lines (139 loc) · 3.85 KB
/
battery_monitor.sh
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
160
161
162
163
#!/bin/bash
CLASS="/sys/class/power_supply"
BATTERIES=("$CLASS/BAT"*)
STATUS_BAT="Discharging"
STATUS_AC="Charging"
STATUS_MAX="Full"
MON_SEC=60
THRESH_MIN=25
THRESH_MAX=80
# if missing: consider "battery"
ICON_LOW=battery-level-20-symbolic
ICON_HIGH=battery-level-80-charging-symbolic
monitor_battery() {
while true; do
for b in "${BATTERIES[@]}"; do
local status="$(cat "$b/status")"
local level="$(cat "$b/charge_now")"
local full="$(cat "$b/charge_full")"
local perc="$((level * 100 / full))"
#echo "timer:$b:$status"
case "$status" in
"$STATUS_BAT") ((perc <= THRESH_MIN)) && echo low_bat:"$b":"$perc" ;;
"$STATUS_AC") ((perc >= THRESH_MAX)) && echo high_bat:"$b":"$perc" ;;
"$STATUS_FULL") echo high_bat:"$b" ;;
esac
done
sleep $MON_SEC
done
}
monitor_udev() {
while read -r event; do
#echo "$event"
#printf '[%s]\n' "${BATTERIES[@]}"
for b in "${BATTERIES[@]}"; do
local status="$(cat "$b/status")"
#echo "udev:$b:$status"
case "$status" in
"$STATUS_BAT") echo unplug:"$b" ;;
"$STATUS_AC"|"$STATUS_FULL") echo plug:"$b" ;;
esac
done
done < <(
udevadm monitor -k -s power_supply | \
stdbuf -oL tail -n+4
)
}
handle_events() {
declare -A nids_hi nids_lo
while IFS=: read -ra event; do
#printf '[%s]\n' "${event[@]}"
#echo
local kind="${event[0]}"
local bat="${event[1]}"
local perc
local na nidB
case $kind in
low_bat)
perc="${event[2]}"
na=(
"$ICON_LOW"
"Batteria $perc%"
"Collega l'alimentatore"
)
nidB="${nids_lo["$bat"]}"
[[ -n "$nidB" ]] && rem_notification "$nidB"
nids_lo["$bat"]="$(add_notification "${na[@]}")"
;;
high_bat)
perc="${event[2]}"
na=(
"$ICON_HIGH"
"Batteria $perc%"
"Scollega l'alimentatore"
)
nidB="${nids_hi["$bat"]}"
[[ -n "$nidB" ]] && rem_notification "$nidB"
nids_hi["$bat"]="$(add_notification "${na[@]}")"
;;
unplug)
nidB="${nids_hi["$bat"]}"
[[ -n "$nidB" ]] && rem_notification "$nidB"
nids_hi["$bat"]=
;;
plug)
nidB="${nids_lo["$bat"]}"
[[ -n "$nidB" ]] && rem_notification "$nidB"
nids_lo["$bat"]=
;;
esac
done
}
add_notification() {
local i="${1:?Missing icon.}"
local s="${2:?Missing summary.}"
shift 2
local args=(
--printid
--appname="Livello batteria"
--urgency=critical
--icon="$i"
"$s"
"$@"
)
dunstify "${args[@]}"
sleep 1
}
rem_notification() {
local args=(
--close="${1:?Missing ID.}"
)
dunstify "${args[@]}"
}
dependencies() {
command -V dunstify # ...
}
debug() {
local bat1="${BATTERIES[0]}"
while read -r -s -N1 key; do
case "$key" in
h) echo high_bat:"$bat1":"$THRESH_MAX" ;;
l) echo low_bat:"$bat1":"$THRESH_MIN" ;;
u) echo unplug:"$bat1" ;;
p) echo plug:"$bat1" ;;
esac
done
}
main() {
if ! dependencies >/dev/null; then
return 1
fi
echo "Monitoring..."
{
monitor_battery &
monitor_udev &
debug &
} | handle_events
}
# execute only when non-interactive shell
[[ $- != *i* ]] && main "$@"