dotfiles/x/battery

49 lines
1.6 KiB
Text
Raw Permalink Normal View History

2022-12-11 01:03:47 -07:00
#!/usr/bin/env perl
$| = 1;
my $CRITICAL = undef;
my $LOW = undef;
my $CHARGING = undef;
my $DEAD_LEVEL = 2;
my $CRITICAL_LEVEL = 5;
my $LOW_LEVEL = 10;
while (1) {
my @acpi = split " ", `acpi | grep "Discharging" | grep -v "rate information"`;
my $battery_level = $acpi[3];
my $status = $acpi[2];
my $sent = undef;
$battery_level =~ s/%,//g;
$status =~ s/,//g;
if ($status =~ "Discharging") {
$CHARGING = undef;
if (!$CRITICAL) {
if (int($battery_level) <= int($CRITICAL_LEVEL)) {
$CRITICAL = 'def';
$sent = 'def';
2023-09-03 00:13:21 -06:00
system "notify-send -i \"battery-empty-symbolic\" -t 0 -u critical \"BATTERY CRITICAL\" \"Battery level is ${battery_level}%\n\nCharge the system NOW.\""
2022-12-11 01:03:47 -07:00
}
} if (!$LOW && !$sent) {
if (int($battery_level) <= int($LOW_LEVEL)) {
$LOW = 'def';
$sent = 'def';
2023-09-03 00:13:21 -06:00
system "notify-send -i \"battery-caution-symbolic\" -t 0 -u normal \"BATTERY LOW\" \"Battery level is ${battery_level}%\n\nCharge the system soon.\""
2022-12-11 01:03:47 -07:00
}
} if (int($battery_level) <= int($DEAD_LEVEL)) {
system "notify-send -t 0 -u critical \"SHUTTING DOWN\" \"Battery level is too low. The system will shutdown in 2 minutes to prevent corruption.\n\nCharge the system NOW to cancel the shutdown.\"";
system "doas shutdown -Ph 2 &";
}
} else {
if (!$CHARGING) {
$CHARGING = 'def';
$CRITICAL = undef;
$LOW = undef;
system "doas shutdown -c";
2023-09-03 00:13:21 -06:00
system "notify-send -t 3000 -i \"battery-good-charging-symbolic\" \"System is now charging\"";
2022-12-11 01:03:47 -07:00
}
}
sleep 30;
}