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)

  1. #!/usr/bin/perl  
  2.   
  3. # $Id: hpsetdisp.pl 11 2006-03-22 01:21:03Z yaakov $  
  4.   
  5. # hpsetdisp.pl  
  6. # Connects to a JetDirect equipped HP printer and uses  
  7. # HP's control language to set the ready message on the  
  8. # LCD display.  Takes an IP address and message on the  
  9. # command line. My favorite message is "INSERT COIN".  
  10. # Keep in mind the limitations of the display when composing  
  11. # your clever verbiage.  
  12. #  
  13. # THIS PROGRAM IS PROVIDED WITH NO WARRANTY OF ANY KIND EXPRESSED OR IMPLIED  
  14. # THE AUTHOR CANNOT BE RESPONSIBLE FOR THE EFFECTS OF THIS PROGRAM  
  15. # IF YOU ARE UNCERTAIN ABOUT THE ADVISABILITY OF USING IT, DO NOT!  
  16. #  
  17. # Yaakov (http://kovaya.com/ )  
  18. #  
  19. # Modified by Garrett Hoofman  
  20. # 10/18/2007  
  21. # http://www.visionsofafar.com  
  22.   
  23. # Modified by David Jacoby  
  24. # 12/10/2008  
  25. # http://varlogrant.blogspot.com/  
  26.   
  27. use strict ;  
  28. use warnings ;  
  29. use 5.010 ;  
  30. use Getopt::Long ;  
  31. use IO::Interactive qw{ interactive } ;  
  32. use IO::Socket ;  
  33. use XML::DOM ;  
  34. use subs qw{  get_weather send_to_printer } ;  
  35.   
  36. my $zip         = '' ;  
  37. my $printer     = '' ;  
  38. my $interactive = '' ;  
  39. my $rdymsg      = "Ready" ;  
  40.   
  41. GetOptions( 'zipcode=s'   => \$zip,  
  42.             'printer=s'   => \$printer,  
  43.             'interactive' => \$interactive, ) ;  
  44.   
  45. chomp $printer ;  
  46. $zip !~ /\d/ and exit 0 ;  
  47. my $weather = get_weather $zip ;  
  48. $rdymsg = $weather =~ m{\d}mx ? $weather : '' ;  
  49.   
  50. if ( $interactive ) {  
  51.     say $rdymsg ;  
  52.     }  
  53. if ( $printer ) {  
  54.     send_to_printer $printer , $rdymsg ;  
  55.     }  
  56.   
  57. ########## ########## ########## ########## ########## ########## ##########  
  58. sub get_weather {  
  59.     my $rdymsg ;  
  60.     my $parser = XML::DOM::Parser->new() ;  
  61.     my $file   = 'http://www.google.com/ig/api?weather=XXXXX&hl=en' ;  
  62.     my $zip    = shift ;  
  63.     $zip =~ s{(\d{5})}{}mx ;  
  64.     $zip = $1 ;  
  65.     $file =~ s/XXXXX/$zip/mx ;  
  66.     my $doc = $parser->parsefile( $file ) ;  
  67.   
  68.     for my $species ( $doc->getElementsByTagName( 'current_conditions' ) ) {  
  69.         $rdymsg =  
  70.           $species->getElementsByTagName( 'temp_f' )->item( 0 )  
  71.           ->getAttribute( 'data' )  
  72.           . " Degrees F        "  
  73.           . $species->getElementsByTagName( 'condition' )->item( 0 )  
  74.           ->getAttribute( 'data' ) ;  
  75.         }  
  76.     return $rdymsg ;  
  77.     }  
  78. ########## ########## ########## ########## ########## ########## ##########  
  79.   
  80. ########## ########## ########## ########## ########## ########## ##########  
  81. sub send_to_printer {  
  82.     my $printer = shift ;  
  83.     my $msg     = shift ;  
  84.     my $socket = IO::Socket::INET->new( PeerAddr => $printer,  
  85.                                         PeerPort => "9100",  
  86.                                         Proto    => "tcp",  
  87.                                         Type     => SOCK_STREAM  
  88.                                         ) or die "Could not create socket: $!" ;  
  89.   
  90.     my $data = <<EOJ  
  91. \e%-12345X\@PJL JOB  
  92. \@PJL RDYMSG DISPLAY="$rdymsg"  
  93. \@PJL EOJ  
  94. \e%-12345X  
  95. EOJ  
  96.       ;  
  97.     print $socket $data ;  
  98.     }  
  99. ########## ########## ########## ########## ########## ########## ##########  

No comments:

Post a Comment