Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2009/02/20

Talkin' About The Weather...

About a year ago, maybe more, a man released code that would allow you to send the current weather conditions to the display of your HP printer. He, being a pilot, used the deep, intense, fully-featured weather information that pilots need to know if they can fly, but is perhaps a bit too complex for mere mortals.

So, at some point in the year between then and now, another guy rewrote it, using Google's weather API, which gives you XML, which he parses with XML::DOM.

I've moved the parts into subroutines, made it quiet for non-interactive uses (like crontab) and did a few minor checks. Also, I made it talk to Twitter, but that code isn't ready for the world yet.

(Links are in the code)


#!/usr/bin/perl

# $Id: hpsetdisp.pl 11 2006-03-22 01:21:03Z yaakov $

# hpsetdisp.pl
# Connects to a JetDirect equipped HP printer and uses
# HP's control language to set the ready message on the
# LCD display. Takes an IP address and message on the
# command line. My favorite message is "INSERT COIN".
# Keep in mind the limitations of the display when composing
# your clever verbiage.
#
# THIS PROGRAM IS PROVIDED WITH NO WARRANTY OF ANY KIND EXPRESSED OR IMPLIED
# THE AUTHOR CANNOT BE RESPONSIBLE FOR THE EFFECTS OF THIS PROGRAM
# IF YOU ARE UNCERTAIN ABOUT THE ADVISABILITY OF USING IT, DO NOT!
#
# Yaakov (http://kovaya.com/ )
#
# Modified by Garrett Hoofman
# 10/18/2007
# http://www.visionsofafar.com

# Modified by David Jacoby
# 12/10/2008
# http://varlogrant.blogspot.com/

use strict ;
use warnings ;
use 5.010 ;
use Getopt::Long ;
use IO::Interactive qw{ interactive } ;
use IO::Socket ;
use XML::DOM ;
use subs qw{ get_weather send_to_printer } ;

my $zip = '' ;
my $printer = '' ;
my $interactive = '' ;
my $rdymsg = "Ready" ;

GetOptions( 'zipcode=s' => \$zip,
'printer=s' => \$printer,
'interactive' => \$interactive, ) ;

chomp $printer ;
$zip !~ /\d/ and exit 0 ;
my $weather = get_weather $zip ;
$rdymsg = $weather =~ m{\d}mx ? $weather : '' ;

if ( $interactive ) {
say $rdymsg ;
}
if ( $printer ) {
send_to_printer $printer , $rdymsg ;
}

########## ########## ########## ########## ########## ########## ##########
sub get_weather {
my $rdymsg ;
my $parser = XML::DOM::Parser->new() ;
my $file = 'http://www.google.com/ig/api?weather=XXXXX&hl=en' ;
my $zip = shift ;
$zip =~ s{(\d{5})}{}mx ;
$zip = $1 ;
$file =~ s/XXXXX/$zip/mx ;
my $doc = $parser->parsefile( $file ) ;

for my $species ( $doc->getElementsByTagName( 'current_conditions' ) ) {
$rdymsg =
$species->getElementsByTagName( 'temp_f' )->item( 0 )
->getAttribute( 'data' )
. " Degrees F "
. $species->getElementsByTagName( 'condition' )->item( 0 )
->getAttribute( 'data' ) ;
}
return $rdymsg ;
}
########## ########## ########## ########## ########## ########## ##########

########## ########## ########## ########## ########## ########## ##########
sub send_to_printer {
my $printer = shift ;
my $msg = shift ;
my $socket = IO::Socket::INET->new( PeerAddr => $printer,
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 ;
}
########## ########## ########## ########## ########## ########## ##########

No comments:

Post a Comment