121 lines
3.4 KiB
Bash
Executable File
121 lines
3.4 KiB
Bash
Executable File
#!/usr/bin/perl -w
|
|
use strict;
|
|
use warnings;
|
|
use Math::Trig;
|
|
use JSON;
|
|
|
|
my @path = split("/", $0);
|
|
pop @path;
|
|
my $p = join("/", @path);
|
|
my $config_file = -e '~/.location.json' ? '~/.location.json' : 'location_example.json';
|
|
|
|
# Load up config data
|
|
open (CONFIG, $config_file) or goto NOCONFIG;
|
|
my $raw_config = "";
|
|
while (<CONFIG>) { $raw_config .= $_; }
|
|
my %config = %{decode_json($raw_config)};
|
|
close(CONFIG);
|
|
|
|
NOCONFIG:
|
|
my $lat = $config{lat} or scalar(40.48967);
|
|
my $lon = $config{lon} or scalar(-111.93882);
|
|
my $WB_providerID = $config{WBproviderID} or scalar(23);
|
|
my $WB_stationID = $config{WBstationID} or scalar("C3430");
|
|
my $WU_stationID = $config{WUstationID} or scalar("KUTRIVER63");
|
|
my $city = $config{city} or scalar("bluffdale");
|
|
|
|
sub checksources {
|
|
my $weather = (`$p/wttrin.sh $city` || `$p/weatherunder.sh $WU_stationID` || `$p/weatherbug.sh $lat $lon $WB_providerID $WB_stationID` || `$p/weathergov.sh $lat $lon`) or print "Not Available\n\n#FF0000" and exit 33;
|
|
return $weather;
|
|
}
|
|
|
|
sub wavegen {
|
|
my $x = shift;
|
|
my $period = shift;
|
|
my $phaseshift = shift;
|
|
my $magnitude = shift;
|
|
my $y_adjust = shift;
|
|
return sin($x/($period/pi)+$phaseshift)*$magnitude+$y_adjust;
|
|
}
|
|
|
|
sub hexcolor {
|
|
my ($temp,$cold,$hot,$rwave,$gwave,$bwave) = @_;
|
|
my $temp_gauge = ($temp - $cold)/($hot - $cold)*100;
|
|
|
|
my $r = (100 * sin($temp_gauge / 10)) + ($temp_gauge - 30) * 2;
|
|
my $g = ($temp_gauge - 50) * ($temp_gauge - 75) * (0 - 1) / 10 + 100;
|
|
my $b = ($temp_gauge - 25) * ($temp_gauge - 25) * (0 - 1) / 5 + 100;
|
|
|
|
$r *= 2;
|
|
$g *= 2;
|
|
$b *= 2;
|
|
|
|
#brightness
|
|
my $brightness = 270; #out of a max of 768 (white)
|
|
|
|
#sanitize
|
|
$r = $r > 0 ? $r : 0;
|
|
$g = $g > 0 ? $g : 0;
|
|
$b = $b > 0 ? $b : 0;
|
|
$r = $r < 255 ? $r : 255;
|
|
$g = $g < 255 ? $g : 255;
|
|
$b = $b < 255 ? $b : 255;
|
|
|
|
if ($r+$g+$b < $brightness) {
|
|
$brightness -= ($r+$g+$b);
|
|
$r += ($brightness/3);
|
|
$g += ($brightness/3);
|
|
$b += ($brightness/3);
|
|
}
|
|
|
|
if ($rwave) { $r = $rwave }
|
|
if ($gwave) { $g = $gwave }
|
|
if ($bwave) { $b = $bwave }
|
|
|
|
my $CR = unpack("H2",pack("I",$r));
|
|
my $CG = unpack("H2",pack("I",$g));
|
|
my $CB = unpack("H2",pack("I",$b));
|
|
|
|
return "#".$CR.$CG.$CB;
|
|
}
|
|
|
|
sub getdir {
|
|
my $degree = shift;
|
|
if ($degree <= 22 ) { return "↑";}
|
|
if ($degree <= 67 ) { return "↗";}
|
|
if ($degree <= 112 ) { return "→";}
|
|
if ($degree <= 157 ) { return "↘";}
|
|
if ($degree <= 202 ) { return "↓";}
|
|
if ($degree <= 247 ) { return "↙";}
|
|
if ($degree <= 292 ) { return "←";}
|
|
if ($degree <= 337 ) { return "↖";}
|
|
return "↑";
|
|
}
|
|
|
|
my @weather = split(":",checksources) or print "Not Available\n\n#FF0000" and exit 33;
|
|
|
|
my $type = $weather[0];
|
|
my $temp = sprintf("%.0f", ($weather[1] - 32) * (5/9) ); #converts from F to C
|
|
my $wind = $weather[2];
|
|
my $wdir = getdir($weather[3]);
|
|
|
|
my $color = hexcolor($temp,-17,42);
|
|
|
|
if($type =~ /Rain/) {$type = "";}
|
|
elsif ($type =~ /Overcast/) {$type = "";}
|
|
elsif ($type =~ /cloud/) {$type = "";}
|
|
elsif ($type =~ /Clear/) {$type = "";}
|
|
elsif ($type =~ /Sun/) {$type = "";}
|
|
elsif ($type eq "NA") {$type = "";}
|
|
elsif ($type =~ /Snow/) {$type = "";}
|
|
else {$type=$type."...";}
|
|
|
|
my $loc = substr(($weather[4] or "?????"), 0, 5);
|
|
|
|
my $format = '%5.5s - %5.5s %03dC %s %03dmph';
|
|
printf( $format, $loc, $type, $temp, $wdir, $wind);
|
|
print "\n";
|
|
printf( $format, $loc, $type, $temp, $wdir, $wind);
|
|
print "\n";
|
|
print $color."\n";
|