![]() |
my life as drawn by phdcomics.com |
The problem at least for me, is that it I'm well-isolated from the outside world, so that it could just as easily 9° or 90° outside, and I wouldn't know. I've felt apprehension to wander upstairs, only to find that, once I got there, it wasn't all that bad.
With a previous printer, I would write the current conditions to the front-panel LCD, because Perl, but we've moved from HP to Xerox, and I no longer have that ability. I now have it tell the time and temp at the top and bottom of the hour, to keep from being so deep in the zone that I think it's lunchtime when it's really quittin' time. I also had code to generate notify balloons, but a recent switch from Ubuntu to Debian made that unusable.
I've been playing with NoSQL databases and have found that I spend most of my time with MongoDB, but once I started thinking about the weather problem — I need it to be available to any of my n terminals, I don't need structure — I decided that Redis was the way to go.
Each NoSQL database works in a different way. MongoDB works as a Document Store, holding a piece of structured data until you call it back. I have code to record daily status updates and send it to bosses where each day holds an array full of objects holding a timestamp and status report, and at 6pm, if the day's array holds data, it formats that data and sends the mail. This is more structure than I really need in this context.
What I really need is essentially a hash table that I can access from many different programs. If it falls down, that's fine; I don't need to remember what the temperature was at 4am, I just need to know what it is now, or within the last 10 minutes. Redis works like that, and (unless you have it commit to disc), it's only in memory, so fast. (I think; I've only played with Redis, not put production data into it.) I think this feature would be good in, for example, Raspberry Pi uses, where excessive access could prematurely kill your SD card.
Anyway, here's my code to store the temperature, to pull it back out, and a replacement $PS1 to show you how to add it to your prompt. It backends to forecast.io, my most recent go-to for weather information. The API is very useful and easy to use. (I don't put "Powered by Forecast" in my prompt but I do put it in the program that uses notify-osd and Pushover.) Use it in good weather.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS1="${debian_chroot:+($debian_chroot)}\u@\h:\$(~/bin/get_temp.pl):\w\n\$ " |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# retrieves the current temperature from REDIS to be used in the bash prompt | |
use feature qw{ say state unicode_eval unicode_strings } ; | |
use strict ; | |
use warnings ; | |
use utf8 ; | |
binmode STDOUT, ':utf8' ; | |
use Redis ; | |
use Data::Dumper ; | |
my $r = Redis->new() ; | |
print $r->get( 'curr_temp' ) . '°F' || ''; | |
$r->quit ; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# stores current temperature in REDIS so that get_temp.pl can be used | |
# in the bash prompt to display current temperature | |
use feature qw{ say state } ; | |
use strict ; | |
use warnings ; | |
use Data::Dumper ; | |
use IO::Interactive qw{ interactive } ; | |
use JSON ; | |
use LWP::UserAgent ; | |
use Redis ; | |
use YAML qw{ LoadFile } ; | |
my $config = config() ; | |
my $url = | |
'https://api.forecast.io/forecast/' | |
. $config->{ apikey } . '/' | |
. ( join ',', map { $config->{ $_ } } qw{ latitude longitude } ) ; | |
my $agent = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 } ) ; | |
my $response = $agent->get( $url ) ; | |
if ( $response->is_success ) { | |
my $content = $response->content ; | |
my $forecast = decode_json $content ; | |
my $current = $forecast->{ currently } ; | |
my $temp_f = int $current->{ temperature } ; | |
my $r = Redis->new() ; | |
$r->set( 'curr_temp', $temp_f ) ; | |
$r->quit ; | |
} | |
exit ; | |
# ====================================================================== | |
# Reads configuration data from YAML file. Dies if no valid config file | |
# if no other value is given, it will choose current | |
# | |
# Shows I need to put this into a module | |
sub config { | |
my $config_file = $ENV{ HOME } . '/.forecast.yaml' ; | |
my $output = {} ; | |
if ( defined $config_file && -f $config_file ) { | |
my $output = LoadFile( $config_file ) ; | |
$output->{ current } = 1 ; | |
return $output ; | |
} | |
croak( 'No Config File' ) ; | |
} | |
__DATA__ | |
What ~/.forecast.yaml should look like, set for Lafayette, IN | |
--- | |
apikey: You can't have my API key | |
latitude: 40.422778 | |
longitude: -86.915278 |
No comments:
Post a Comment