From 47fa94eaa52f3767699bac479af8bb0b86847cd4 Mon Sep 17 00:00:00 2001 From: Blallo Date: Fri, 21 Oct 2022 15:52:43 +0200 Subject: [PATCH] Init --- Makefile | 43 ++++++++++++++++++++++++++ battery-common-lib.sh | 29 +++++++++++++++++ battery-low-alert.sh | 17 ++++++++++ battery-status-toggle.path | 5 +++ battery-status-toggle.service.template | 10 ++++++ battery-status-toggle.sh | 19 ++++++++++++ 6 files changed, 123 insertions(+) create mode 100644 Makefile create mode 100644 battery-common-lib.sh create mode 100755 battery-low-alert.sh create mode 100644 battery-status-toggle.path create mode 100755 battery-status-toggle.service.template create mode 100755 battery-status-toggle.sh diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bccf324 --- /dev/null +++ b/Makefile @@ -0,0 +1,43 @@ +SUDO ?= sudo + +LIB ?= /usr/lib/battery-status +SYSTEMD_PATH ?= /usr/lib/systemd/system + +LIB_FILES := battery-common-lib.sh battery-low-alert.sh battery-status-toggle.sh +LIB_TARGETS := $(addprefix $(LIB)/,$(LIB_FILES)) + +SYSTEMD_FILES := battery-status-toggle.service battery-status-toggle.path +SYSTEMD_TARGETS := $(addprefix $(SYSTEMD_PATH)/,$(SYSTEMD_FILES)) + +$(LIB): + mkdir $@ + +$(LIB)/battery-common-lib.sh: + $(SUDO) cp ./battery-common-lib.sh $@ + +$(LIB)/battery-low-alert.sh: + $(SUDO) cp ./battery-low-alert.sh $@ + +$(LIB)/battery-status-toggle.sh: + $(SUDO) cp ./battery-status-toggle.sh $@ + +./battery-status-toggle.service: + PREFIX=$(SYSTEMD_PATH) ./battery-status-toggle.service.template + +$(SYSTEMD_PATH)/battery-status-toggle.service: ./battery-status-toggle.service + $(SUDO) cp ./battery-status-toggle.service $@ + +$(SYSTEMD_PATH)/battery-status-toggle.path: + $(SUDO) cp ./battery-status-toggle.path $@ + +install: $(LIB_TARGETS) $(SYSTEMD_TARGETS) + $(SUDO) systemctl daemon-reload + +enable: install + $(SUDO) systemctl enable --now battery-status-toggle.path + +clean: + $(SUDO) rm -f $(SYSTEMD_TARGETS) $(LIB_TARGETS) + rm -f ./battery-status-toggle.service + +.PHONY: install enable clean diff --git a/battery-common-lib.sh b/battery-common-lib.sh new file mode 100644 index 0000000..01c12e8 --- /dev/null +++ b/battery-common-lib.sh @@ -0,0 +1,29 @@ +bat_files="/sys/class/power_supply/BAT0" +bat_status=$(cat ${bat_files}/status) +capacity=$(cat "${bat_files}/capacity") + +function set_capacity() { + if [[ ${capacity} -lt 10 ]]; then + cap=0 + elif [[ ${capacity} -lt 20 ]]; then + cap=10 + elif [[ ${capacity} -lt 30 ]]; then + cap=20 + elif [[ ${capacity} -lt 40 ]]; then + cap=30 + elif [[ ${capacity} -lt 50 ]]; then + cap=40 + elif [[ ${capacity} -lt 60 ]]; then + cap=50 + elif [[ ${capacity} -lt 70 ]]; then + cap=60 + elif [[ ${capacity} -lt 80 ]]; then + cap=70 + elif [[ ${capacity} -lt 90 ]]; then + cap=80 + elif [[ ${capacity} -lt 100 ]]; then + cap=90 + else + cap=100 + fi +} diff --git a/battery-low-alert.sh b/battery-low-alert.sh new file mode 100755 index 0000000..c5ae7c7 --- /dev/null +++ b/battery-low-alert.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +source ./battery-common-lib.sh + +set_capacity + +if [[ "${bat_status}"=="Discharging" ]]; then + notify-send \ + --icon="/usr/share/icons/Adwaita/64x64/status/battery-level-${cap}-symbolic.symbolic.png" \ + "Discharging" \ + "AC disconnected" +elif [[ "${bat_status}"=="Charging" ]]; then + notify-send \ + --icon="/usr/share/icons/Adwaita/64x64/status/battery-level-${cap}-charged-symbolic.symbolic.png" \ + "Discharging" \ + "AC disconnected" +fi diff --git a/battery-status-toggle.path b/battery-status-toggle.path new file mode 100644 index 0000000..f2ae87a --- /dev/null +++ b/battery-status-toggle.path @@ -0,0 +1,5 @@ +[Unit] +Description=React to battery status change + +[Path] +PathModified=/sys/class/power_supply/BAT0 diff --git a/battery-status-toggle.service.template b/battery-status-toggle.service.template new file mode 100755 index 0000000..8d74aae --- /dev/null +++ b/battery-status-toggle.service.template @@ -0,0 +1,10 @@ +#!/bin/bash + +cat > battery-status-toggle.service << EOU +[Unit] +Description=Notify when battery status changes + +[Service] +Type=oneshot +ExecStart=${PREFIX}/battery-status-toggle.sh +EOU diff --git a/battery-status-toggle.sh b/battery-status-toggle.sh new file mode 100755 index 0000000..7c8ac69 --- /dev/null +++ b/battery-status-toggle.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +source ./battery-common-lib.sh + +set_capacity + +if [[ ${bat_status}=="Discharging" && ${capacity} -le 10 ]]; then + echo "Battery alert - ${capacity}%" + notify-send \ + --icon="/usr/share/icons/Adwaita/64x64/status/battery-level-${cap}-symbolic.symbolic.png" \ + "Critical battery" \ + "Only ${capacity}% battery remaining" +elif [[ ${bat_status}=="Discharging" && ${capacity} -le 20 ]]; then + echo "Battery alert - ${capacity}%" + notify-send \ + --icon="/usr/share/icons/Adwaita/64x64/status/battery-level-${cap}-symbolic.symbolic.png" \ + "Low battery" \ + "Only ${capacity}% battery remaining" +fi