#!/usr/bin/perl # # nph-bg.cgi - Server-push multipart png stream from postscript template # $Id: nph-bg.cgi,v 1.1.1.1 2004/06/07 13:59:09 felix Exp $ ### # (C) 2004, Félix Hauri - Felix@F-Hauri.CH # # This is part of demo ``active background'' # # Files: # The script read ``current'' ``tank'' and ``lspeed'' before # pushing a png. # # It's only a prototyp! use CGI::Push qw(:standard :html3); use POSIX qw(strftime setlocale LC_TIME); use IPC::Open2; $refrtime=6; $resol=80; $query = new CGI; setlocale(LC_TIME,'fr_CH'); $resol=$1 if defined param('resol') and param('resol') =~ /^(\d+)$/ and 60 <= $1 and $1 <= 240; $refrtime=$1 if defined param('refresh') and param('refresh') =~ /^(\d+)$/ and 4 <= $1 and $1 <= 60; $pi=4*atan2(1,1); sub doit { my ($hmin,$hmax)=(5895,495); # peeked on template $_=`cat current`; chomp; my $temp=$1 if /^(\d+\.?\d*)$/ and $1 >= 14 and 98 >= $1; my ($tmin,$tmax)=(14.0,98.0); my $warm=($temp-$tmin)/($tmax-$tmin); $_=`cat tank`; chomp; my $tank=$1 if /^(\d+\.?\d*)$/ and $1 >= 0 and 100 >= $1; $_=`cat lspeed`; chomp; my $speed=$1 if /^(\d+\.?\d*)$/ and $1 >= 0 and 20_500 >= $1; my $cspeed=$speed/20_500; return mpict(strftime("%c",localtime()), sprintf("%.0f",$hmin+($hmax-$hmin)*$tank/100), sprintf("%.0f",$speed), $temp, sprintf("%.3f 0.400 %.3f",.2+.8*$warm,1-.8*$warm), sprintf("%.3f %.3f %.3f", .1+.9*$cspeed,.2+.8*$cspeed,.04+.1*$cspeed)); } sub mpict { my ($date,$h1,$speed,$temp,$color1,$color2)=@_; my $geom=sprintf("-r%d -g%.0fx%.0f",$resol / 2, 884 / 160 * $resol, 663 / 160 * $resol); my $command=join("","gs -q -dSAFER -sDEVICE=png256", " -dTextAlphaBits=2 -dGraphicsAlphaBits=2", " ",$geom, " -sOutputFile=- - -c quit 2>/dev/null "); $pid = open2(\*RDRFH, \*WTRFH, $command) or die "Can't open shell"; open FH,"image1.tpl" or die "Can't open template"; while () { s/TMPLDATE/$date/g; s/TMPLH1/$h1/g; s/TMPLSPEED/$speed/g; s/TMPLTEMP/$temp/g; s/TMPLCLR1/$color1/g; s/TMPLCLR2/$color2/g; print WTRFH $_; } close FH or die "Can't close template"; close WTRFH or die "Can't close shell's STDIN"; my ($res,$buf)=(""); while (read RDRFH,$buf,65535) { $res=$res.$buf; }; close RDRFH or die "Can't close shell's STDOUT"; waitpid $pid,0; return $res; } $SIG{'PIPE'}=sub { exit 0; }; do_push(-next_page=>\&doit,-delay=>$refrtime,-type=>'image/png');