blob: cfcd41cd3a4d8e5fa3d2771c9741ed9e656aaa13 (
plain)
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
|
#!/bin/sh
battery_path='/sys/class/power_supply/BAT0'
percentage=$(head -n1 "${battery_path}/capacity")
state=$(head -n1 "${battery_path}/status")
rate=$([ -f "${battery_path}/current_now" ] && head -n1 "${battery_path}/current_now" || head -n1 "${battery_path}/power_now")
volt=$(head -n1 "${battery_path}/voltage_now")
now=$([ -f "${battery_path}/charge_now" ] && head -n1 "${battery_path}/charge_now" || head -n1 "${battery_path}/energy_now")
max=$([ -f "${battery_path}/current_full" ] && head -n1 "${battery_path}/current_full" || head -n1 "${battery_path}/energy_full")
cap=$([ "$state" = "Charging" ] && printf "%d-%d\n" "${max}" "${now}" | bc || echo "${now}")
remaining=$( printf "%d/%d\n" "${cap}" "${volt}" | bc -l )
current_rate=$( printf "%d/%d\n" "${rate}" "${volt}" | bc -l )
#echo $( [ "${current_rate}" != "0" ] && echo "not zero" || echo "is zero" )
seconds=$( [ "${current_rate}" != "0" ] && printf "3600*%f/%f\n" "${remaining}" "${current_rate}" | bc || echo "0")
time_remaining=$( date -ud "@${seconds}" "+%H:%M:%S" )
#echo "percentage ${percentage}%"
#echo "state ${state}"
#echo "rate ${rate}"
#echo "volt ${volt}"
#echo "now ${now}"
#echo "max ${max}"
#echo "cap ${cap}"
#echo "remaining ${remaining}"
#echo "current_rate ${current_rate}"
#echo "seconds ${seconds}"
#echo "time ${time_remaining}"
battery_level=`acpi -b | cut -d ' ' -f 4 | grep -o '[0-9]*'`
battery_level=$(head -n1 /sys/class/power_supply/BAT0/capacity)
battery_state=$(acpi | grep 'Battery' | sed 's/Battery\s[0-9]*: //' | sed 's/, [0-9][0-9]*\%.*//')
battery_state=$(head -n1 /sys/class/power_supply/BAT0/status)
battery_remaining=$(acpi | grep -oh '[0-9:]* remaining' | sed 's/:\w\w remaining$/ Minutes/' | sed 's/00://' | sed 's/:/h /')
if [ ! -f "/tmp/.battery" ]; then
echo "$battery_level" > /tmp/.battery
echo "$battery_state" >> /tmp/.battery
exit
fi
previous_battery_level=$(head -n 1 < /tmp/.battery)
previous_battery_state=$(tail -n 1 < /tmp/.battery)
echo "$battery_level" > /tmp/.battery
echo "$battery_state" >> /tmp/.battery
checkBatteryLevel() {
if [ "$battery_state" != "Discharging" ] || [ "${battery_level}" = "${previous_battery_level}" ]; then
exit
fi
if [ "$battery_level" -le 4 ]; then
sudo systemctl suspend
elif [ "$battery_level" -le 7 ]; then
notify-send "Low Battery" "(${time_remaining}) Your computer will suspend soon unless plugged into a power outlet." -u critical
elif [ "$battery_level" -le 10 ]; then
notify-send "Low Battery" "${battery_level}% (${time_remaining}) of battery remaining." -u normal
fi
}
checkBatteryStateChange() {
if [ "$battery_state" != "Discharging" ] && [ "$previous_battery_state" = "Discharging" ]; then
notify-send "Charging" "Battery is now plugged in." -u low
fi
if [ "$battery_state" = "Discharging" ] && [ "$previous_battery_state" != "Discharging" ]; then
notify-send "Power Unplugged" "Your computer has been disconnected from power." -u low
fi
}
checkBatteryStateChange
checkBatteryLevel
|