#! /usr/bin/perl # # displayWeather.cgi # # Reads most recent outside temperature record from the weather database # and generates a little HTML on stdout with that value embedded in it. # # For more information about: # # Postgres generally www.postgresql.org # Postgres' dialect of SQL "man sql" # psql, the interactive query tool "man psql" # The select statement "man select" # To connect to the weather database using psql "psql -d weather" # (when logged-in to quark as weather) # Perl5 extension for Postgres "perldoc Pg" # # Who When What # ---------------------------------------------------------------------- # charliep 07-Jan-2000 Created. # # Lew Riley 05-Feb-2000 Added humidity, wind speed & heading, # and "uncalibrated" disclaimer. # # Lew Riley 23-Feb-2000 Added Fahrenheit, mph use Pg; use strict; my $connection; my $sql; my $result; my $timestamp; my $out_temperature; my $out_humidity; my $wind_speed; my $wind_heading; print "Content-Type: text/html\n\n"; $connection = Pg::connectdb("dbname=weather"); if ($connection->status ne PGRES_CONNECTION_OK) { print "

Error retrieving weather data (connectdb), ", $connection->errorMessage, "

\n"; exit 0; } $sql = "select timestamp, out_temperature, out_humidity, wind_speed, wind_heading " . "from readings " . "where timestamp = (select max(timestamp) " . " from readings); "; $result = $connection->exec($sql); if ($result->resultStatus ne PGRES_TUPLES_OK) { print "

Error retrieving weather data (exec), ", $connection->errorMessage, "

\n"; exit 0; } ($timestamp, $out_temperature, $out_humidity, $wind_speed, $wind_heading) = $result->fetchrow; my $heading; if ($wind_heading > 337.5 || $wind_heading <= 22.5) { $heading = "N"; } elsif ($wind_heading > 22.5 && $wind_heading <= 67.5) { $heading = "NE"; } elsif ($wind_heading > 67.5 && $wind_heading <= 112.5) { $heading = "E"; } elsif ($wind_heading > 112.5 && $wind_heading <= 157.5) { $heading = "SE"; } elsif ($wind_heading > 157.5 && $wind_heading <= 202.5) { $heading = "S"; } elsif ($wind_heading > 202.5 && $wind_heading <= 247.5) { $heading = "SW"; } elsif ($wind_heading > 247.5 && $wind_heading <= 292.5) { $heading = "W"; } elsif ($wind_heading > 292.5 && $wind_heading <= 337.5) { $heading = "NW"; } else { print "

Error: illegal wind heading: $wind_heading

\n"; } my $out_temp_F = $out_temperature * 9/5 + 32; $out_temperature = sprintf("%3.0f", $out_temperature); $out_temp_F = sprintf("%3.0f", $out_temp_F); my $wind_speed_mph = $wind_speed / 0.447; $wind_speed_mph = sprintf("%2.0f", $wind_speed_mph); if ($result->resultStatus ne PGRES_TUPLES_OK) { print "

Error retrieving weather data (fetchrow), ", $connection->errorMessage, "

\n"; } else { my $html = <<"_END_"; Conditions on the roof of Dennis Hall as of $timestamp: Caution! Take this data lightly! We have not yet calibrated our instruments! _END_ print "$html\n"; } exit 0;