comparison fetch_weather.pl @ 693:fd45c52d4297

fetch_weather: Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 23 Jun 2023 17:24:32 +0300
parents 8a6cca58e853
children
comparison
equal deleted inserted replaced
692:c262cef9dc54 693:fd45c52d4297
329 my $uri = opt_get("tiehallinto_rw_url"); 329 my $uri = opt_get("tiehallinto_rw_url");
330 print STDERR "Fetching Tiehallinto road weather data from ".$uri."\n" if (opt_get_int("debug") > 0); 330 print STDERR "Fetching Tiehallinto road weather data from ".$uri."\n" if (opt_get_int("debug") > 0);
331 my $res = fetch_http($uri); 331 my $res = fetch_http($uri);
332 if ($res->code >= 200 && $res->code <= 201) 332 if ($res->code >= 200 && $res->code <= 201)
333 { 333 {
334 my $json_str = force_decode_utf8($res->decoded_content); 334 my $data_json = JSON->new->decode(force_decode_utf8($res->decoded_content));
335 my $data = JSON->new->decode($json_str); 335
336 336 print Dumper($data_json) if opt_chk_bool("dump");
337 print $json_str if opt_chk_bool("dump"); 337
338 338 if (!defined($data_json->{"dataUpdatedTime"}) || !defined($data_json->{"stations"}))
339 if (!defined($data->{"dataUpdatedTime"}) || !defined($data->{"stations"}))
340 { 339 {
341 print STDERR "ERROR: REST/JSON call result did not contain required data.\n"; 340 print STDERR "ERROR: REST/JSON call result did not contain required data.\n";
342 print STDERR $json_str."\n\n";
343 } 341 }
344 else 342 else
345 { 343 {
346 # Check if we need to update the static meta data 344 # Check if we need to update the static meta data
347 my $meta_file = opt_get("tiehallinto_meta"); 345 my $meta_file = opt_get("tiehallinto_meta");
368 366
369 print STDERR "Is meta_str UTF8? ".(utf8::is_utf8($meta_str) ? "yes" : "NO!")."\n" if (opt_get_int("debug") > 0); 367 print STDERR "Is meta_str UTF8? ".(utf8::is_utf8($meta_str) ? "yes" : "NO!")."\n" if (opt_get_int("debug") > 0);
370 368
371 # Parse the data .. 369 # Parse the data ..
372 my $meta_data = {}; 370 my $meta_data = {};
373 my $json = JSON->new->decode($meta_str); 371 my $meta_json = JSON->new->decode($meta_str);
374 372
375 print $meta_str if opt_chk_bool("dump"); 373 print Dumper($meta_json) if opt_chk_bool("dump");
376 374
377 if ($fetch_meta) 375 if ($fetch_meta)
378 { 376 {
379 # Save new cache, in more optimal form, if needed. 377 # Save new cache, in more optimal form, if needed.
380 print STDERR "Storing to cache '".$meta_file."'.\n" if (opt_get_int("debug") > 0); 378 print STDERR "Storing to cache '".$meta_file."'.\n" if (opt_get_int("debug") > 0);
381 write_binary($meta_file, JSON->new->utf8->encode($json)); 379 write_binary($meta_file, JSON->new->utf8->encode($meta_json));
382 } 380 }
383 381
384 foreach my $ms (@{$json->{"features"}}) 382 foreach my $ms (@{$meta_json->{"features"}})
385 { 383 {
386 # Filter functional stations? 384 # Filter functional stations?
387 if (defined($ms->{"properties"}) && 385 if (defined($ms->{"properties"}) &&
388 defined($ms->{"geometry"}{"coordinates"}) && 386 defined($ms->{"geometry"}{"coordinates"}) &&
389 $ms->{"properties"}{"collectionStatus"} eq "GATHERING" && 387 $ms->{"properties"}{"collectionStatus"} eq "GATHERING" &&
393 $meta_data->{$ms->{"id"}} = $ms; 391 $meta_data->{$ms->{"id"}} = $ms;
394 } 392 }
395 } 393 }
396 394
397 my $nrecords = 0; 395 my $nrecords = 0;
398 foreach my $wdata (@{$data->{"stations"}}) 396 foreach my $wdata (@{$data_json->{"stations"}})
399 { 397 {
400 my $wid = $wdata->{"id"}; 398 my $wid = $wdata->{"id"};
401 if (defined($meta_data->{$wid}) && 399 if (defined($meta_data->{$wid}) &&
402 defined($wdata->{"sensorValues"})) 400 defined($wdata->{"sensorValues"}))
403 { 401 {
470 if ($res->code >= 200 && $res->code <= 201) 468 if ($res->code >= 200 && $res->code <= 201)
471 { 469 {
472 my $xml = XMLin(force_decode_utf8($res->decoded_content)); 470 my $xml = XMLin(force_decode_utf8($res->decoded_content));
473 my $time_base = time(); 471 my $time_base = time();
474 472
475 print $res->decoded_content if opt_chk_bool("dump"); 473 print Dumper($xml) if opt_chk_bool("dump");
476 474
477 if (defined($xml->{"wfs:member"}{"omso:GridSeriesObservation"})) 475 if (defined($xml->{"wfs:member"}{"omso:GridSeriesObservation"}))
478 { 476 {
479 my $fdata = $xml->{"wfs:member"}{"omso:GridSeriesObservation"}; 477 my $fdata = $xml->{"wfs:member"}{"omso:GridSeriesObservation"};
480 my $fshit = $fdata->{"om:result"}{"gmlcov:MultiPointCoverage"}; 478 my $fshit = $fdata->{"om:result"}{"gmlcov:MultiPointCoverage"};