The Daily Static
  The Daily Static
UF Archives
Register
UF Membership
Ad Free Site
Postcards
Community

Geekfinder
UFie Gear
Advertise on UF

Forum Rules
& FAQ


Username

Password


Create a New Account

 
 

Back to UserFriendly Strip Comments Index

uficle: perl by spartan.ii.117 2007-08-13 00:13:13
i use a perl script (or four) to monitor my folding at home progress, but everytime i start a new work unit, the script quits with a devide by zero error. here is the relavent part of the script (i think):
sub calc{

$percentage = `cat $UNITINFO | tail -n 1 | sed -e s/Progress..//`;
chomp($percentage);
$percentage =~ s/(\d+)\%.+/\1/;

$name = `cat $UNITINFO | tail -n 4 | head -n 1 | sed -e s/Name..//`;
chomp($name);

$start = `cat $UNITINFO | tail -n 3 | head -n 1 | sed -e s/Download.time..//`;
chomp($start);
$start = `date -d "$start" +%s`;
chomp($start);

$tperper = (time() - $start) / $percentage;
$seconds = (100 - $percentage)*$tperper;

$hours = int($seconds / 3600);

if ($hours>23) {

$days = int($seconds / 86400);
$seconds -= 86400 * $days;
$hours = int($seconds / 3600);
$seconds -= 3600 * $hours;

$tr = sprintf("%d d %d h",$days, $hours);

} else {

$hours = int($seconds / 3600);
$seconds -= 3600 * $hours;

$tr = sprintf("%d h",$hours);
}

$tremaining = "tRem ".$tr;

$seconds = $percentage * $tperper;

$hours = int($seconds / 3600);

if ($hours>23) {

$days = int($seconds / 86400);
$seconds -= 86400 * $days;
$hours = int($seconds / 3600);
$seconds -= 3600 * $hours;

$tr = sprintf("%d d %d h",$days, $hours);

} else {

$hours = int($seconds / 3600);
$seconds -= 3600 * $hours;

$tr = sprintf("%d h",$hours);
}

$trunning = "tRun ".$tr;

return ($name,$tremaining,$trunning,$percentage);
}



and here is the entire script, (if any one can tell me how to make this read four files at once, that would rock.)

#!/usr/bin/perl
use IO::Socket;
use Fcntl;





$LCDHOST = "192.168.1.3";
$LCDPORT = "13666";
$UNITINFO = "/opt/foldingathome/1/unitinfo.txt";
$CYCLE = 10;
$NAME = "Desktop Core 1";

sub lcd {
($data) = @_;
print $lcdproc $data."\n";
$res = <$lcdproc>;
}

sub updatescreen {

if ($oldline1 ne $line1) {
$oldline1=$line1;
lcd("widget_set fathlcd line1 1 2 {$line1}");
};
if ($oldline2 ne $line2) {
$oldline2=$line2;
lcd("widget_set fathlcd line2 1 3 {$line2}");
};
if ($oldline3 ne $line3) {
$oldline3=$line3;
lcd("widget_set fathlcd line3 1 4 {$line3}");
};
if ($oldhbar ne $hbar) {
$oldhbar=$hbar;
lcd("widget_set fathlcd hbar 6 3 $hbar");
};

};

sub calc{

$percentage = `cat $UNITINFO | tail -n 1 | sed -e s/Progress..//`;
chomp($percentage);
$percentage =~ s/(\d+)\%.+/\1/;

$name = `cat $UNITINFO | tail -n 4 | head -n 1 | sed -e s/Name..//`;
chomp($name);

$start = `cat $UNITINFO | tail -n 3 | head -n 1 | sed -e s/Download.time..//`;
chomp($start);
$start = `date -d "$start" +%s`;
chomp($start);

$tperper = (time() - $start) / $percentage;
$seconds = (100 - $percentage)*$tperper;

$hours = int($seconds / 3600);

if ($hours>23) {

$days = int($seconds / 86400);
$seconds -= 86400 * $days;
$hours = int($seconds / 3600);
$seconds -= 3600 * $hours;

$tr = sprintf("%d d %d h",$days, $hours);

} else {

$hours = int($seconds / 3600);
$seconds -= 3600 * $hours;

$tr = sprintf("%d h",$hours);
}

$tremaining = "tRem ".$tr;

$seconds = $percentage * $tperper;

$hours = int($seconds / 3600);

if ($hours>23) {

$days = int($seconds / 86400);
$seconds -= 86400 * $days;
$hours = int($seconds / 3600);
$seconds -= 3600 * $hours;

$tr = sprintf("%d d %d h",$days, $hours);

} else {

$hours = int($seconds / 3600);
$seconds -= 3600 * $hours;

$tr = sprintf("%d h",$hours);
}

$trunning = "tRun ".$tr;

return ($name,$tremaining,$trunning,$percentage);
}

$lcdproc = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => $LCDHOST,
PeerPort => $LCDPORT,
) || die "Cannot connect to LCDproc port on $LCDHOST:$LCDPORT\n";

$lcdproc->autoflush(1);

sleep 1;

print $lcdproc "hello\n";
@lcd_specs = split(/ /,<$lcdproc>);

$screen{wid} = $lcd_specs[7];
$screen{hgt} = $lcd_specs[9];
$screen{cellwid} = $lcd_specs[11];
$screen{cellhgt} = $lcd_specs[13];

$screen{pixwid} = $screen{wid}*$screen{cellwid};
$screen{pixhgt} = $screen{hgt}*$screen{cellhgt};

fcntl($lcdproc, F_SETFL, O_NONBLOCK);

lcd("client_set name {$NAME}");
lcd("screen_add fathlcd");
lcd("screen_set fathlcd name {$NAME}");

# if you like heartbeat , comment this line out
lcd("screen_set fathlcd heartbeat off");

lcd("widget_add fathlcd title title");
lcd("widget_set fathlcd title {$NAME}");

lcd("widget_add fathlcd line1 string");
lcd("widget_add fathlcd line2 string");
lcd("widget_add fathlcd line3 string");
lcd("widget_add fathlcd hbar hbar");

$line1 = " [initializing] ";
$line2 = " fathlcd 0.1 BETA ";
$line3 = " buZz 2005 ";

$oldline1 = "";
$oldline2 = "";
$oldline3 = "";
$oldhbar = "";
$hbar = "";

updatescreen;
$count=0;

while(1)
{
$ignore=0;

while(defined($input = <$lcdproc>)) {

@lines = split(/\n/, $input);
foreach $line (@lines)
{
if ( $line =~ /^success$/ ) { next; }
if ( $line =~ /^ignore (\S)/ ) { $ignore = -1; }
}


}

if (!$ignore) {
updatescreen;
}


sleep 1;

($name,$tr,$trun,$percentage) = &calc;

$line1 = "$name";
$line2 = $percentage."% [..........]";
$hbar = $percentage;

if ($count<($CYCLE*2)) {
$line3 = $trun;
}
if ($count<$CYCLE) {
$line3 = $tr;
}
if ($count==($CYCLE*2)) {
$count=0;
$line3 = $tr;
}

$count++;
}

close ($lcdproc) || die "close: $!";
exit;
[ Reply ]
  When you divide throuhg something .... by aix tom2007-08-13 00:19:35
    Awesome by spartan.ii.1172007-08-13 00:39:14
  $tperper = (time() - $start) / $percentage; by Didactylos2007-08-13 00:21:13
    exactly (n/t) by spartan.ii.1172007-08-13 00:31:58

 

[Todays Cartoon Discussion] [News Index]

Come get yer ARS (Account Registration System) Source Code here!
All images, characters, content and text are copyrighted and trademarks of J.D. Frazer except where other ownership applies. Don't do bad things, we have lawyers.
UserFriendly.Org and its operators are not liable for comments or content posted by its visitors, and will cheerfully assist the lawful authorities in hunting down script-kiddies, spammers and other net scum. And if you're really bad, we'll call your mom. (We're not kidding, we've done it before.)