changeset 208:c6bbf69afc57

Add some error handling and debugging.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 15 Aug 2014 10:18:23 +0300
parents 86bf1a0cd9e1
children 6b1a579fb288
files fetch_weather.pl
diffstat 1 files changed, 47 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/fetch_weather.pl	Mon Jul 21 16:01:34 2014 +0300
+++ b/fetch_weather.pl	Fri Aug 15 10:18:23 2014 +0300
@@ -50,7 +50,7 @@
 {
   my $agent = LWP::UserAgent->new;
   $agent->agent($settings{"http_user_agent"});
-  $agent->timeout(10);
+  $agent->timeout(20);
 
   my $req = HTTP::Request->new(GET => $_[0]);
   
@@ -418,57 +418,66 @@
   {
     my $xml = XMLin($res->decoded_content);
     my $time_base = time();
-    my $fdata = $xml->{"wfs:member"}{"omso:GridSeriesObservation"};
     
-    my $fshit = $fdata->{"om:result"}{"gmlcov:MultiPointCoverage"};
-    my @farray = ();
+    if (defined($xml->{"wfs:member"}{"omso:GridSeriesObservation"}))
+    {
+      my $fdata = $xml->{"wfs:member"}{"omso:GridSeriesObservation"};
+      my $fshit = $fdata->{"om:result"}{"gmlcov:MultiPointCoverage"};
+      my @farray = ();
 
-    foreach my $fline (split(/\n/, $fshit->{"gml:domainSet"}{"gmlcov:SimpleMultiPoint"}{"gmlcov:positions"}))
-    {
-      if ($fline =~ /^\s*([\+\-]?\d+\.\d*)\s+([\+\-]?\d+\.\d*)\s+(\d+)\s*$/)
+      foreach my $fline (split(/\n/, $fshit->{"gml:domainSet"}{"gmlcov:SimpleMultiPoint"}{"gmlcov:positions"}))
       {
-        push(@farray, {"lat" => $1, "long" => $2, "time" => $3});
+        if ($fline =~ /^\s*([\+\-]?\d+\.\d*)\s+([\+\-]?\d+\.\d*)\s+(\d+)\s*$/)
+        {
+          push(@farray, {"lat" => $1, "long" => $2, "time" => $3});
+        }
       }
-    }
 
-    my $findex = 0;    
-    foreach my $fline (split(/\n/, $fshit->{"gml:rangeSet"}{"gml:DataBlock"}{"gml:doubleOrNilReasonTupleList"}))
-    {
-      if ($fline =~ /^\s*([\+\-]?\d+\.\d*|NaN)\s+([\+\-]?\d+\.\d*|NaN)\s*$/)
+      my $findex = 0;    
+      foreach my $fline (split(/\n/, $fshit->{"gml:rangeSet"}{"gml:DataBlock"}{"gml:doubleOrNilReasonTupleList"}))
       {
-        $farray[$findex]{"temp"} = $1 if ($1 ne "NaN");
-        $farray[$findex]{"humidity"} = $2 if ($2 ne "NaN");
-        $findex++;
+        if ($fline =~ /^\s*([\+\-]?\d+\.\d*|NaN)\s+([\+\-]?\d+\.\d*|NaN)\s*$/)
+        {
+          $farray[$findex]{"temp"} = $1 if ($1 ne "NaN");
+          $farray[$findex]{"humidity"} = $2 if ($2 ne "NaN");
+          $findex++;
+        }
       }
-    }
-    # XXX Hashify the array into lat/long keys
-    
-    # This is horrible :S
-    my $fcrap = $fdata->{"om:featureOfInterest"}{"sams:SF_SpatialSamplingFeature"}{"sams:shape"}{"gml:MultiPoint"}{"gml:pointMember"};
-    foreach my $fnode (@{$fcrap})
-    {
-      my $floc = $fnode->{"gml:Point"};
-      if ($floc->{"gml:pos"} =~ /^\s*([\+\-]?\d+\.\d*)\s+([\+\-]?\d+\.\d*)\s*$/)
+      # XXX Hashify the array into lat/long keys
+      
+      # This is horrible :S
+      my $fcrap = $fdata->{"om:featureOfInterest"}{"sams:SF_SpatialSamplingFeature"}{"sams:shape"}{"gml:MultiPoint"}{"gml:pointMember"};
+      foreach my $fnode (@{$fcrap})
       {
-        my ($flat, $flong) = ($1, $2);
-        # Should use a hash -
-        foreach my $flol (@farray)
+        my $floc = $fnode->{"gml:Point"};
+        if ($floc->{"gml:pos"} =~ /^\s*([\+\-]?\d+\.\d*)\s+([\+\-]?\d+\.\d*)\s*$/)
         {
-          if ($flol->{"lat"} == $flat && $flol->{"long"} == $flong)
+          my ($flat, $flong) = ($1, $2);
+          # Should use a hash -
+          foreach my $flol (@farray)
           {
-            $weatherdata->{$floc->{"gml:name"}} =
-            [
-              1,
-              $flol->{"time"},
-              $flol->{"temp"},
-              "",
-              "",
-              defined($flol->{"humidity"}) ? $flol->{"humidity"} : "",
-            ];
+            if ($flol->{"lat"} == $flat && $flol->{"long"} == $flong)
+            {
+              $weatherdata->{$floc->{"gml:name"}} =
+              [
+                1,
+                $flol->{"time"},
+                $flol->{"temp"},
+                "",
+                "",
+                defined($flol->{"humidity"}) ? $flol->{"humidity"} : "",
+              ];
+            }
           }
         }
       }
+    } else {
+      # defined
+      print STDERR "Invalid XML received:\n";
+      print STDERR $res->decoded_content."\n\n";
     }
+  } else {
+    print STDERR "Error fetching FMI XML: ".$res->status_line."\n";
   }
 }