Il meteo sul display della stampante

febbraio 13, 2009

Ispirato da questo, ho fatto una modifica allo script proposto da Yaakov al fine di stampare i risultati nel caro, vecchio sistema metrico decimale e per importare i dati del METAR di Forlì.

4200wx

E’ necessario installare da CPAN le librerie

use LWP::Simple;
use IO::Socket;
use Geo::METAR;

e modificare l’indirizzo IP della stampante ed il codice ICAO che, nel caso di Forlì, è LIPK.

Lo script è stato testato su una HP4350, che ha un display 20×4. Your mileage may vary.

use strict;
use POSIX qw(ceil floor);

use LWP::Simple;
use LWP::UserAgent;
use IO::Socket;

use Geo::METAR;
my $m = new Geo::METAR;

my $wx = getmetar('LIPK');          # Put your ICAO code here
setdisplay($wx, '10.0.1.52');   # This should be your printer's IP

sub getmetar {
  my $icao = shift;

  my $page = get("http://weather.noaa.gov/cgi-bin/mgetmetar.pl?cccc=$icao") or exit;

  $page =~ /($icao .+)/;
  my $report = $1;

  $m->metar($report);

  my $wx;
    $wx = "  CHE TEMPO CHE FA  ";   # This can be edited to localize it.
                                    # Keep it the same length and center
                                    # the text in the quotes with spaces.

    my $temp = "Temp. ".$m->TEMP_C."C";
    my $pad = (20 - length $temp)/2;
    $wx .= " " x $pad.$temp." " x $pad;

    my $vis = $m->VISIBILITY;
    $vis =~ /(\d+)/;
	my $tmp = ceil($1*1.6/1000);
    my $atmos = "Visib. $tmp km";
    $pad = (20 - length $atmos)/2;
    $wx .= " " x $pad.$atmos." " x $pad;

    my $wspd = $m->WIND_MPH;
	$wspd = floor($wspd*1.6);
    my $wind = "Vento $wspd kmh";
    $pad = (20 - length $wind)/2;
    $wx .= " " x $pad.$wind." " x $pad;

return $wx;

}

sub setdisplay {

  my $rdymsg = shift; my $peeraddr = shift;
  my $socket = IO::Socket::INET->new(
      PeerAddr	=> $peeraddr,
      PeerPort	=> "9100",
      Proto     => "tcp",
      Type	=> SOCK_STREAM
  ) or die "Could not create socket: $!";

my $data = <<EOJ
\e%-12345X\@PJL JOB
\@PJL RDYMSG DISPLAY="$rdymsg"
\@PJL EOJ
\e%-12345X
EOJ
;
  print $socket $data;
  print $rdymsg;

}

2 Responses to “Il meteo sul display della stampante”

  1. utilissimo.
    lo scarico IMMEDIATAMENTE.

  2. utilissimo.
    lo scarico IMMEDIATAMENTE.

Leave a Reply