Pi66

pino-rs
Rust notification daemon.
git clone https://git.pi66.xyz/pino-rs

scripts/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
#!/bin/bash

UP_THRESHOLD=90
DOWN_THRESHOLD=20
CHECK_INTERVAL=0.3

BATTERY_PATH=$(find /sys/class/power_supply/ -name "BAT*" | head -1)
AC_PATH=$(find /sys/class/power_supply/ -name "AC*" | head -1)
[ -z "$BATTERY_PATH" ] && echo "No battery found" && exit 1

last_level=<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mo stretchy="false">&#x00028;</mo><mi>c</mi><mi>a</mi><mi>t</mi><mi>"</mi></mrow></math>BATTERY_PATH/capacity")
last_ac_status=<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mo stretchy="false">&#x00028;</mo><mi>c</mi><mi>a</mi><mi>t</mi><mi>"</mi></mrow></math>AC_PATH/online" 2>/dev/null || echo "0")

while true; do
    level=<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mo stretchy="false">&#x00028;</mo><mi>c</mi><mi>a</mi><mi>t</mi><mi>"</mi></mrow></math>BATTERY_PATH/capacity")
    ac_status=<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mo stretchy="false">&#x00028;</mo><mi>c</mi><mi>a</mi><mi>t</mi><mi>"</mi></mrow></math>AC_PATH/online" 2>/dev/null || echo "0")

    if [ "<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>a</mi><msub><mi>c</mi><mi>s</mi></msub><mi>t</mi><mi>a</mi><mi>t</mi><mi>u</mi><mi>s</mi><mi>"</mi><mo>&#x02212;</mo><mi>e</mi><mi>q</mi><mn>1</mn><mo stretchy="false">]</mo><mi>&</mi><mi>&</mi><mo stretchy="false">[</mo><mi>"</mi></mrow></math>last_ac_status" -eq 0 ]; then
        pino -t "Plugged In" -m "Now charging ($level%)" -d 5

    elif [ "<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>a</mi><msub><mi>c</mi><mi>s</mi></msub><mi>t</mi><mi>a</mi><mi>t</mi><mi>u</mi><mi>s</mi><mi>"</mi><mo>&#x02212;</mo><mi>e</mi><mi>q</mi><mn>0</mn><mo stretchy="false">]</mo><mi>&</mi><mi>&</mi><mo stretchy="false">[</mo><mi>"</mi></mrow></math>last_ac_status" -eq 1 ]; then
        pino -t "Unplugged" -m "On battery ($level%)" -d 5

    elif [ "<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>l</mi><mi>e</mi><mi>v</mi><mi>e</mi><mi>l</mi><mi>"</mi><mo>&#x02212;</mo><mi>g</mi><mi>e</mi></mrow></math>UP_THRESHOLD ] && [ "<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>l</mi><mi>a</mi><mi>s</mi><msub><mi>t</mi><mi>l</mi></msub><mi>e</mi><mi>v</mi><mi>e</mi><mi>l</mi><mi>"</mi><mo>&#x02212;</mo><mi>l</mi><mi>t</mi></mrow></math>UP_THRESHOLD ] && [ "$ac_status" -eq 0 ]; then
        pino -t "Battery High" -m "Reached $level%" -d 5

    elif [ "<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>l</mi><mi>e</mi><mi>v</mi><mi>e</mi><mi>l</mi><mi>"</mi><mo>&#x02212;</mo><mi>l</mi><mi>e</mi></mrow></math>DOWN_THRESHOLD ] && [ "<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>l</mi><mi>a</mi><mi>s</mi><msub><mi>t</mi><mi>l</mi></msub><mi>e</mi><mi>v</mi><mi>e</mi><mi>l</mi><mi>"</mi><mo>&#x02212;</mo><mi>g</mi><mi>t</mi></mrow></math>DOWN_THRESHOLD ] && [ "$ac_status" -eq 0 ]; then
        pino -t "Battery Low" -m "Only $level% left" -d 5
    fi

    last_level=$level
    last_ac_status=$ac_status
    sleep $CHECK_INTERVAL
done