2022-05-18 21:44:50 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
POLLING_FREQUENCY=1
|
|
|
|
|
|
|
|
# ISO 8601
|
|
|
|
alias date="date '+%Y-%m-%dT%T'"
|
|
|
|
|
|
|
|
emit() {
|
|
|
|
printf "[%s] " "$(date)"
|
|
|
|
echo "$@"
|
|
|
|
}
|
|
|
|
|
2022-09-17 21:55:31 +00:00
|
|
|
! battery >/dev/null 2>&1 \
|
2022-06-26 20:46:44 +00:00
|
|
|
&& printf "Unable to get battery status on this system.\n" 1>&2 \
|
2022-05-18 21:44:50 +00:00
|
|
|
&& exit 1 \
|
|
|
|
|| true
|
|
|
|
|
2022-06-26 20:46:44 +00:00
|
|
|
current_level=$(battery)
|
2022-05-18 21:44:50 +00:00
|
|
|
previous_level=$current_level
|
|
|
|
|
|
|
|
emit "$(printf "Current level: %d%%" "$current_level")"
|
|
|
|
|
|
|
|
while true; do
|
2022-06-26 20:46:44 +00:00
|
|
|
current_level=$(battery)
|
2022-10-23 06:20:19 +00:00
|
|
|
if ! streq $current_level $previous_level; then
|
2022-05-18 21:44:50 +00:00
|
|
|
[ $current_level -lt $previous_level ] \
|
|
|
|
&& emit "$(printf "Discharged: %d%% -> %d%%\n" $previous_level $current_level )" \
|
|
|
|
|| emit "$(printf " Charged: %d%% -> %d%%\n" $previous_level $current_level )"
|
|
|
|
previous_level=$current_level
|
|
|
|
fi
|
|
|
|
|
|
|
|
sleep $POLLING_FREQUENCY
|
|
|
|
done
|