#!/usr/bin/perl # use Date::Time; open LOG, ">>/var/log/powerwatch"; select (LOG); $| = 1; &log ("PowerWatch Starting"); my $cpu; my $brightness; my $lastremaining; my @lastremainingreal; my $shutdown = 0; my $battery = `/usr/bin/vaiobat`; if ($battery =~ /(\d+)\.(\d+)\%/) { $lastremaining = $1; $lastremainingreal[0] = $1 . "." . $2; } &log ("Initial State: $battery"); if ($battery =~ /AC/) { `/usr/bin/setbrightness 255`; $brightness = 255; `/usr/bin/crusoe -f`; $cpu = 1; } else { `/usr/bin/setbrightness 0`; $brightness = 0; `/usr/bin/crusoe -s`; $cpu = 0; } while (1) { sleep (15); my $battery = `/usr/bin/vaiobat`; my $remaining = 50; my $remainingreal = 50.0; if ($battery =~ /(\d+)\.(\d+)\%/) { $remaining = $1; $remainingreal = $1 . "." . $2; } open STATUS, ">/var/log/vaiobat"; print STATUS $battery; close STATUS; if ($battery =~ /AC/) { &log ("Using AC Power: $battery"); # reset our queue of old power levels.. @lastremainingreal = (); # we are plugged in if ($shutdown == 1) { &log ("Now plugged in so we can cancel the shutdown."); `/sbin/shutdown -c`; $shutdown = 0; } if ($cpu == 0) { &log ("Throttle CPU to full speed."); `/usr/bin/crusoe -f`; $cpu = 1; } if ($brightness < 255) { &log ("Set brightness to full."); `/usr/bin/setbrightness 255`; $brightness = 255; } if (($remaining == 100) && ($lastremaining < 100)) { &log ("Message users that the battery is charged."); `wall The Battery is now 100% charged.`; } } else { # on battery &log ("Using Battery: $battery"); # try and calculate the amount of time left based upon the # drop in the last 15 seconds. if ((defined ($lastremainingreal[scalar(@lastremainingreal)-1])) && ($lastremainingreal[scalar(@lastremainingreal)-1] > $remainingreal)) { my $timeleft = ((0.25 * scalar(@lastremainingreal) * $remainingreal) / ($lastremainingreal[scalar(@lastremainingreal)-1] - $remainingreal)); # round to 1 decimal point $timeleft = (int($timeleft * 10) / 10); &log ("I'm guessing you have about $timeleft minutes left."); } if ($cpu == 1) { &log ("Throttling CPU to half speed."); `/usr/bin/crusoe -s`; $cpu = 0; } if ($brightness > 0) { &log ("Setting brightness to 0."); `/usr/bin/setbrightness 0`; $brightness = 0; } if (($remaining <= 10) && ($remaining >= 5) && ($lastremaining > 10)) { &log ("Battery has dropped below 10%."); `wall Battery is at $remaining\%..`; } elsif (($remaining < 5) && ($remaining > 2)) { `wall Battery is at $remaining\%. Please plug me in!`; &log ("Battery has dropped below 5%."); if (!$shutdown) { &log ("Initiating shutdown in ten minutes."); `/sbin/shutdown -h 10min &`; $shutdown = 1; } } elsif ($remaining <= 2) { &log ("Battery below 2%. Initiating immediate shutdown."); `wall Shutting down system due to low battery.`; if ($shutdown) { `/sbin/shutdown -c`; } `/sbin/shutdown -h now`; $shutdown = 1; close LOG; exit (0); } } $lastremaining = $remaining; unshift @lastremainingreal, $remainingreal; } close LOG; exit (0); sub log { my $message = shift; chomp $message; $date = `date`; chomp $date; print LOG "[", $date, "] ", $message , "\n"; }