#!/usr/bin/perl # Copyright 2010 - Ross Garinger # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . $ARGC = @ARGV; if (@ARGV < 2) { die('Usage: freeway_status.pl --5|405 --nb|sb [--email]'); } $email = "thejusme!vtext.com"; $email =~ s/!/\@/; $img = "http://images.wsdot.wa.gov/nwflow/flowmaps/video_map_SeattleMetro.gif"; $white_cost = 0; $green_cost = 0; $yellow_cost = 1; $red_cost = 2 * $yellow_cost; $black_cost = 4 * $yellow_cost; # RGB => Traffic Cost %colors = (" 0, 0, 0", $black_cost, "255, 0, 0", $red_cost, "255,255, 0", $yellow_cost, " 32,224, 64", $green_cost, "255,255,255", $white_cost); if ($ARGV[0] eq "--5") { if ($ARGV[1] eq "--nb") { @samples = ("+47+690", "+45+680", "+43+670", "+40+660", "+41+650", "+47+640", "+47+630", "+51+620", "+51+610", "+48+600", "+51+590", "+54+580", "+58+570", "+61+560", "+62+550", "+63+540", "+72+530", "+89+520", "+106+510", #"++500", "+119+496", #"++490", "+119+480"); } elsif ($ARGV[1] eq "--sb") { @samples = ("+39+690", "+37+680", "+34+670", "+32+660", "+31+650", "+32+640", "+37+630", "+42+620", "+43+610", "+40+600", "+42+590", "+45+580", "+49+570", "+52+560", "+54+550", "+55+540", "+57+530", "+73+520", "+90+510", "+106+500", "+110+490", "+111+480", "+114+470"); } else { die('Freeway direction unknown!'); } } elsif ($ARGV[0] eq "--405") { if ($ARGV[1] eq "--nb") { @samples = ("+200+720", "+200+710", "+200+700", "+200+690", "+200+680", "+202+670", "+204+660", "+205+650", "+202+640", "+199+630", "+200+620", "+201+610", "+202+600", "+203+590", "+201+580", "+198+570", "+193+560", "+190+550"); } elsif ($ARGV[1] eq "--sb") { @samples = ("+192+720", "+192+710", "+192+700", "+192+690", "+192+680", "+194+670", "+196+660", "+197+650", "+194+640", "+191+630", "+192+620", "+193+610", "+194+600", "+195+590", "+193+580", "+189+570", "+185+560", "+174+550"); } else { die('Freeway direction unknown!'); } } else { die('Freeway unknown!'); } $count = @samples; $lpf = ($count / 2) * $yellow_cost; $hpf = ($count / 2) * $red_cost; system("wget $img -O freeway.gif > /dev/null 2>&1"); while ($? != 0) { sleep 1; system("wget $img -O freeway.gif > /dev/null 2>&1"); } # Initialize for safety $cost = 0; foreach $offset (@samples) { $rgb = `convert freeway.gif -crop 1x1$offset txt:- | grep '0,0:' | cut -d '(' -f2 | cut -d ')' -f1 2> /dev/null`; chomp $rgb; $color_cost = $colors{$rgb}; print "$offset: $color_cost\n"; $cost += $color_cost; } print "Traffic Total: $cost\n"; if ($cost > $hpf) { $status = "BAD!"; } elsif ($cost > $lpf) { $status = "Slow!"; } else { $status = "Good!"; } print "Freeway Status: $status\n"; if ($ARGV[2] eq "--email") { system("echo 'Traffic Total: $cost' | mail -s 'Freeway $status' $email"); } system("rm freeway.gif");