From 1dde02d570ba57e4ba8aafefad7d207761a86f9c Mon Sep 17 00:00:00 2001 From: Marko Oldenburg Date: Sun, 13 Jan 2019 09:22:28 +0100 Subject: [PATCH] expand error handling, fix typo in commandref --- 59_Weather.pm | 2 +- DarkSkyAPI.pm | 571 ++++++++++++++++++++++--------------------- OpenWeatherMapAPI.pm | 373 ++++++++++++++-------------- 3 files changed, 476 insertions(+), 470 deletions(-) diff --git a/59_Weather.pm b/59_Weather.pm index 8a4ecbd..5e1d596 100755 --- a/59_Weather.pm +++ b/59_Weather.pm @@ -811,7 +811,7 @@ sub WeatherAsHtmlD($;$) Beispiele:
       define Forecast Weather apikey=987498ghjgf864
-      define MyWeather Weather api=OpenWeatherMapAPI,cachemaxage:600 apikey=09878945fdskv876 location=52.4545,13.4545 interval=3600 language=de
+      define MyWeather Weather API=OpenWeatherMapAPI,cachemaxage:600 apikey=09878945fdskv876 location=52.4545,13.4545 interval=3600 language=de
     
Es folgt die API-spezifische Dokumentation.

diff --git a/DarkSkyAPI.pm b/DarkSkyAPI.pm index 5ab80e9..99ca787 100644 --- a/DarkSkyAPI.pm +++ b/DarkSkyAPI.pm @@ -189,299 +189,302 @@ sub _ProcessingRetrieveData($$) { and defined($response) and $response ) { - my $data = eval { decode_json($response) }; + if ( $response =~ m/^{.*}$/ ) { + my $data = eval { decode_json($response) }; - if ($@) { - _ErrorHandling( $self, 'DarkSky Weather decode JSON err ' . $@ ); - } - elsif ( defined( $data->{code} ) - and $data->{code} - and defined( $data->{error} ) - and $data->{error} ) - { - _ErrorHandling( $self, - 'Code: ' . $data->{code} . ' Error: ' . $data->{error} ); - } - else { - # print Dumper $data; ## für Debugging - - $self->{cached}->{current_date_time} = - strftime( "%a, %e %b %Y %H:%M %p", - localtime( $self->{fetchTime} ) ); - $self->{cached}->{timezone} = $data->{timezone}; - $self->{cached}->{license}{text} = - $data->{flags}->{'meteoalarm-license'}; - $self->{cached}->{current} = { - 'temperature' => int( - sprintf( "%.1f", $data->{currently}->{temperature} ) + 0.5 - ), - 'temp_c' => int( - sprintf( "%.1f", $data->{currently}->{temperature} ) + 0.5 - ), - 'dewPoint' => int( - sprintf( "%.1f", $data->{currently}->{dewPoint} ) + 0.5 - ), - 'humidity' => $data->{currently}->{humidity} * 100, - 'condition' => encode_utf8( $data->{currently}->{summary} ), - 'pressure' => int( - sprintf( "%.1f", $data->{currently}->{pressure} ) + 0.5 - ), - 'wind' => int( - sprintf( "%.1f", $data->{currently}->{windSpeed} ) + 0.5 - ), - 'wind_speed' => int( - sprintf( "%.1f", $data->{currently}->{windSpeed} ) + 0.5 - ), - 'wind_direction' => $data->{currently}->{windBearing}, - 'windGust' => int( - sprintf( "%.1f", $data->{currently}->{windGust} ) + 0.5 - ), - 'cloudCover' => $data->{currently}->{cloudCover} * 100, - 'uvIndex' => $data->{currently}->{uvIndex}, - 'visibility' => int( - sprintf( "%.1f", $data->{currently}->{visibility} ) + 0.5 - ), - 'ozone' => $data->{currently}->{ozone}, - 'code' => $codes{ $data->{currently}->{icon} }, - 'iconAPI' => $data->{currently}->{icon}, - 'pubDate' => strftime( - "%a, %e %b %Y %H:%M %p", - localtime( $data->{currently}->{'time'} ) - ), - 'precipProbability' => $data->{currently}->{precipProbability}, - 'apparentTemperature' => int( - sprintf( - "%.1f", $data->{currently}->{apparentTemperature} - ) + 0.5 - ), - 'precipIntensity' => $data->{currently}->{precipIntensity}, - }; - - if ( ref( $data->{daily}->{data} ) eq "ARRAY" - and scalar( @{ $data->{daily}->{data} } ) > 0 ) + if ($@) { + _ErrorHandling( $self, 'DarkSky Weather decode JSON err ' . $@ ); + } + elsif ( defined( $data->{code} ) + and $data->{code} + and defined( $data->{error} ) + and $data->{error} ) { - ### löschen des alten Datensatzes - delete $self->{cached}->{forecast}; + _ErrorHandling( $self, + 'Code: ' . $data->{code} . ' Error: ' . $data->{error} ); + } + else { + # print Dumper $data; ## für Debugging - my $i = 0; - foreach ( @{ $data->{daily}->{data} } ) { - push( - @{ $self->{cached}->{forecast}->{daily} }, - { - 'pubDate' => strftime( - "%a, %e %b %Y %H:%M %p", - localtime( - $data->{daily}->{data}->[$i]->{'time'} - ) - ), - 'day_of_week' => strftime( - "%a", - localtime( - $data->{daily}->{data}->[$i]->{'time'} - ) - ), - 'low_c' => int( - sprintf( "%.1f", - $data->{daily}->{data}->[$i] - ->{temperatureLow} ) + 0.5 - ), - 'high_c' => int( - sprintf( "%.1f", - $data->{daily}->{data}->[$i] - ->{temperatureHigh} ) + 0.5 - ), - 'tempMin' => int( - sprintf( "%.1f", - $data->{daily}->{data}->[$i] - ->{temperatureMin} ) + 0.5 - ), - 'tempMinTime' => strftime( - "%a, %e %b %Y %H:%M %p", - localtime( - $data->{daily}->{data}->[$i] - ->{temperatureMinTime} - ) - ), - 'tempMax' => int( - sprintf( "%.1f", - $data->{daily}->{data}->[$i] - ->{temperatureMax} ) + 0.5 - ), - 'tempMaxTime' => strftime( - "%a, %e %b %Y %H:%M %p", - localtime( - $data->{daily}->{data}->[$i] - ->{temperatureMaxTime} - ) - ), - 'tempLow' => int( - sprintf( "%.1f", - $data->{daily}->{data}->[$i] - ->{temperatureLow} ) + 0.5 - ), - 'tempLowTime' => strftime( - "%a, %e %b %Y %H:%M %p", - localtime( - $data->{daily}->{data}->[$i] - ->{temperatureLowTime} - ) - ), - 'tempHigh' => int( - sprintf( "%.1f", - $data->{daily}->{data}->[$i] - ->{temperatureHigh} ) + 0.5 - ), - 'tempHighTime' => strftime( - "%a, %e %b %Y %H:%M %p", - localtime( - $data->{daily}->{data}->[$i] - ->{temperatureHighTime} - ) - ), - 'apparentTempLow' => int( - sprintf( "%.1f", - $data->{daily}->{data}->[$i] - ->{apparentTemperatureLow} ) + 0.5 - ), - 'apparentTempLowTime' => strftime( - "%a, %e %b %Y %H:%M %p", - localtime( - $data->{daily}->{data}->[$i] - ->{apparentTemperatureLowTime} - ) - ), - 'apparentTempHigh' => int( - sprintf( "%.1f", - $data->{daily}->{data}->[$i] - ->{apparentTemperatureHigh} ) + 0.5 - ), - 'apparentTempHighTime' => strftime( - "%a, %e %b %Y %H:%M %p", - localtime( - $data->{daily}->{data}->[$i] - ->{apparentTemperatureHighTime} - ) - ), - 'apparenttempMin' => int( - sprintf( "%.1f", - $data->{daily}->{data}->[$i] - ->{apparentTemperatureMin} ) + 0.5 - ), - 'apparenttempMinTime' => strftime( - "%a, %e %b %Y %H:%M %p", - localtime( - $data->{daily}->{data}->[$i] - ->{apparentTemperatureMinTime} - ) - ), - 'apparenttempMax' => int( - sprintf( "%.1f", - $data->{daily}->{data}->[$i] - ->{apparentTemperatureMax} ) + 0.5 - ), - 'apparenttempMaxTime' => strftime( - "%a, %e %b %Y %H:%M %p", - localtime( - $data->{daily}->{data}->[$i] - ->{apparentTemperatureMaxTime} - ) - ), - 'code' => - $codes{ $data->{daily}->{data}->[$i]->{icon} }, - 'iconAPI' => $data->{daily}->{data}->[$i]->{icon}, - 'condition' => encode_utf8( - $data->{daily}->{data}->[$i]->{summary} - ), - 'ozone' => $data->{daily}->{data}->[$i]->{ozone}, - 'uvIndex' => - $data->{daily}->{data}->[$i]->{uvIndex}, - 'uvIndexTime' => strftime( - "%a, %e %b %Y %H:%M %p", - localtime( - $data->{daily}->{data}->[$i]->{uvIndexTime} - ) - ), - 'precipIntensity' => - $data->{daily}->{data}->[$i]->{precipIntensity}, - 'precipIntensityMax' => - $data->{daily}->{data}->[$i] - ->{precipIntensityMax}, - 'precipIntensityMaxTime' => strftime( - "%a, %e %b %Y %H:%M %p", - localtime( - $data->{daily}->{data}->[$i] - ->{precipIntensityMaxTime} - ) - ), - 'dewPoint' => int( - sprintf( "%.1f", - $data->{daily}->{data}->[$i]->{dewPoint} ) - + 0.5 - ), - 'humidity' => - $data->{daily}->{data}->[$i]->{humidity} * 100, - 'cloudCover' => - $data->{daily}->{data}->[$i]->{cloudCover} * 100, - 'precipType' => - $data->{daily}->{data}->[$i]->{precipType}, + $self->{cached}->{current_date_time} = + strftime( "%a, %e %b %Y %H:%M %p", + localtime( $self->{fetchTime} ) ); + $self->{cached}->{timezone} = $data->{timezone}; + $self->{cached}->{license}{text} = + $data->{flags}->{'meteoalarm-license'}; + $self->{cached}->{current} = { + 'temperature' => int( + sprintf( "%.1f", $data->{currently}->{temperature} ) + 0.5 + ), + 'temp_c' => int( + sprintf( "%.1f", $data->{currently}->{temperature} ) + 0.5 + ), + 'dewPoint' => int( + sprintf( "%.1f", $data->{currently}->{dewPoint} ) + 0.5 + ), + 'humidity' => $data->{currently}->{humidity} * 100, + 'condition' => encode_utf8( $data->{currently}->{summary} ), + 'pressure' => int( + sprintf( "%.1f", $data->{currently}->{pressure} ) + 0.5 + ), + 'wind' => int( + sprintf( "%.1f", $data->{currently}->{windSpeed} ) + 0.5 + ), + 'wind_speed' => int( + sprintf( "%.1f", $data->{currently}->{windSpeed} ) + 0.5 + ), + 'wind_direction' => $data->{currently}->{windBearing}, + 'windGust' => int( + sprintf( "%.1f", $data->{currently}->{windGust} ) + 0.5 + ), + 'cloudCover' => $data->{currently}->{cloudCover} * 100, + 'uvIndex' => $data->{currently}->{uvIndex}, + 'visibility' => int( + sprintf( "%.1f", $data->{currently}->{visibility} ) + 0.5 + ), + 'ozone' => $data->{currently}->{ozone}, + 'code' => $codes{ $data->{currently}->{icon} }, + 'iconAPI' => $data->{currently}->{icon}, + 'pubDate' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( $data->{currently}->{'time'} ) + ), + 'precipProbability' => $data->{currently}->{precipProbability}, + 'apparentTemperature' => int( + sprintf( + "%.1f", $data->{currently}->{apparentTemperature} + ) + 0.5 + ), + 'precipIntensity' => $data->{currently}->{precipIntensity}, + }; - 'wind_direction' => - $data->{daily}->{data}->[$i]->{windBearing}, - 'wind' => int( - sprintf( "%.1f", - $data->{daily}->{data}->[$i]->{windSpeed} ) - + 0.5 - ), - 'wind_speed' => int( - sprintf( "%.1f", - $data->{daily}->{data}->[$i]->{windSpeed} ) - + 0.5 - ), - 'windGust' => int( - sprintf( "%.1f", - $data->{daily}->{data}->[$i]->{windGust} ) - + 0.5 - ), - 'windGustTime' => strftime( - "%a, %e %b %Y %H:%M %p", - localtime( - $data->{daily}->{data}->[$i]->{windGustTime} - ) - ), - 'moonPhase' => - $data->{daily}->{data}->[$i]->{moonPhase}, - 'sunsetTime' => strftime( - "%a, %e %b %Y %H:%M %p", - localtime( - $data->{daily}->{data}->[$i]->{sunsetTime} - ) - ), - 'sunriseTime' => strftime( - "%a, %e %b %Y %H:%M %p", - localtime( - $data->{daily}->{data}->[$i]->{sunriseTime} - ) - ), + if ( ref( $data->{daily}->{data} ) eq "ARRAY" + and scalar( @{ $data->{daily}->{data} } ) > 0 ) + { + ### löschen des alten Datensatzes + delete $self->{cached}->{forecast}; - 'precipProbability' => - $data->{daily}->{data}->[$i]->{precipProbability}, - 'pressure' => int( - sprintf( "%.1f", - $data->{daily}->{data}->[$i]->{pressure} ) - + 0.5 - ), - 'visibility' => int( - sprintf( "%.1f", - $data->{daily}->{data}->[$i]->{visibility} ) - + 0.5 - ), - } - ); + my $i = 0; + foreach ( @{ $data->{daily}->{data} } ) { + push( + @{ $self->{cached}->{forecast}->{daily} }, + { + 'pubDate' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i]->{'time'} + ) + ), + 'day_of_week' => strftime( + "%a", + localtime( + $data->{daily}->{data}->[$i]->{'time'} + ) + ), + 'low_c' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{temperatureLow} ) + 0.5 + ), + 'high_c' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{temperatureHigh} ) + 0.5 + ), + 'tempMin' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{temperatureMin} ) + 0.5 + ), + 'tempMinTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i] + ->{temperatureMinTime} + ) + ), + 'tempMax' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{temperatureMax} ) + 0.5 + ), + 'tempMaxTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i] + ->{temperatureMaxTime} + ) + ), + 'tempLow' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{temperatureLow} ) + 0.5 + ), + 'tempLowTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i] + ->{temperatureLowTime} + ) + ), + 'tempHigh' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{temperatureHigh} ) + 0.5 + ), + 'tempHighTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i] + ->{temperatureHighTime} + ) + ), + 'apparentTempLow' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{apparentTemperatureLow} ) + 0.5 + ), + 'apparentTempLowTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i] + ->{apparentTemperatureLowTime} + ) + ), + 'apparentTempHigh' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{apparentTemperatureHigh} ) + 0.5 + ), + 'apparentTempHighTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i] + ->{apparentTemperatureHighTime} + ) + ), + 'apparenttempMin' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{apparentTemperatureMin} ) + 0.5 + ), + 'apparenttempMinTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i] + ->{apparentTemperatureMinTime} + ) + ), + 'apparenttempMax' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i] + ->{apparentTemperatureMax} ) + 0.5 + ), + 'apparenttempMaxTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i] + ->{apparentTemperatureMaxTime} + ) + ), + 'code' => + $codes{ $data->{daily}->{data}->[$i]->{icon} }, + 'iconAPI' => $data->{daily}->{data}->[$i]->{icon}, + 'condition' => encode_utf8( + $data->{daily}->{data}->[$i]->{summary} + ), + 'ozone' => $data->{daily}->{data}->[$i]->{ozone}, + 'uvIndex' => + $data->{daily}->{data}->[$i]->{uvIndex}, + 'uvIndexTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i]->{uvIndexTime} + ) + ), + 'precipIntensity' => + $data->{daily}->{data}->[$i]->{precipIntensity}, + 'precipIntensityMax' => + $data->{daily}->{data}->[$i] + ->{precipIntensityMax}, + 'precipIntensityMaxTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i] + ->{precipIntensityMaxTime} + ) + ), + 'dewPoint' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i]->{dewPoint} ) + + 0.5 + ), + 'humidity' => + $data->{daily}->{data}->[$i]->{humidity} * 100, + 'cloudCover' => + $data->{daily}->{data}->[$i]->{cloudCover} * 100, + 'precipType' => + $data->{daily}->{data}->[$i]->{precipType}, - $i++; + 'wind_direction' => + $data->{daily}->{data}->[$i]->{windBearing}, + 'wind' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i]->{windSpeed} ) + + 0.5 + ), + 'wind_speed' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i]->{windSpeed} ) + + 0.5 + ), + 'windGust' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i]->{windGust} ) + + 0.5 + ), + 'windGustTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i]->{windGustTime} + ) + ), + 'moonPhase' => + $data->{daily}->{data}->[$i]->{moonPhase}, + 'sunsetTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i]->{sunsetTime} + ) + ), + 'sunriseTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + $data->{daily}->{data}->[$i]->{sunriseTime} + ) + ), + + 'precipProbability' => + $data->{daily}->{data}->[$i]->{precipProbability}, + 'pressure' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i]->{pressure} ) + + 0.5 + ), + 'visibility' => int( + sprintf( "%.1f", + $data->{daily}->{data}->[$i]->{visibility} ) + + 0.5 + ), + } + ); + + $i++; + } } } } + else { _ErrorHandling( $self, 'DarkSky Weather ' . $response ); } } ## Aufruf der callbackFn diff --git a/OpenWeatherMapAPI.pm b/OpenWeatherMapAPI.pm index 886ddf3..119b420 100644 --- a/OpenWeatherMapAPI.pm +++ b/OpenWeatherMapAPI.pm @@ -243,200 +243,203 @@ sub _ProcessingRetrieveData($$) { and defined($response) and $response ) { - my $data = eval { decode_json($response) }; + if ( $response =~ m/^{.*}$/ ) { + my $data = eval { decode_json($response) }; - if ($@) { - _ErrorHandling( $self, - 'OpenWeatherMap Weather decode JSON err ' . $@ ); - } - elsif ( defined( $data->{cod} ) - and $data->{cod} - and $data->{cod} != 200 - and defined( $data->{message} ) - and $data->{message} ) - { - _ErrorHandling( $self, $data->{cod} . ': ' . $data->{message} ); - } - else { - - ###### Ab hier wird die ResponseHash Referenze für die Rückgabe zusammen gestellt - $self->{cached}->{current_date_time} = - strftime( "%a, %e %b %Y %H:%M %p", - localtime( $self->{fetchTime} ) ); - - if ( $self->{endpoint} eq 'weather' ) { - $self->{cached}->{country} = $data->{sys}->{country}; - $self->{cached}->{city} = $data->{name}; - $self->{cached}->{license}{text} = 'none'; - $self->{cached}->{current} = { - 'temperature' => int( - sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 ) ) + - 0.5 - ), - 'temp_c' => int( - sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 ) ) + - 0.5 - ), - 'low_c' => int( - sprintf( "%.1f", - ( $data->{main}->{temp_min} - 273.15 ) ) + 0.5 - ), - 'high_c' => int( - sprintf( "%.1f", - ( $data->{main}->{temp_max} - 273.15 ) ) + 0.5 - ), - 'tempLow' => int( - sprintf( "%.1f", - ( $data->{main}->{temp_min} - 273.15 ) ) + 0.5 - ), - 'tempHigh' => int( - sprintf( "%.1f", - ( $data->{main}->{temp_max} - 273.15 ) ) + 0.5 - ), - 'humidity' => $data->{main}->{humidity}, - 'condition' => - encode_utf8( $data->{weather}->[0]->{description} ), - 'pressure' => - int( sprintf( "%.1f", $data->{main}->{pressure} ) + 0.5 ), - 'wind' => - int( sprintf( "%.1f", $data->{wind}->{speed} ) + 0.5 ), - 'wind_speed' => - int( sprintf( "%.1f", $data->{wind}->{speed} ) + 0.5 ), - 'wind_direction' => $data->{wind}->{deg}, - 'cloudCover' => $data->{clouds}->{all}, - 'visibility' => - int( sprintf( "%.1f", $data->{visibility} ) + 0.5 ), - 'code' => $codes{ $data->{weather}->[0]->{id} }, - 'iconAPI' => $data->{weather}->[0]->{icon}, - 'sunsetTime' => strftime( - "%a, %e %b %Y %H:%M %p", - localtime( $data->{sys}->{sunset} ) - ), - 'sunriseTime' => strftime( - "%a, %e %b %Y %H:%M %p", - localtime( $data->{sys}->{sunrise} ) - ), - 'pubDate' => strftime( - "%a, %e %b %Y %H:%M %p", - localtime( $data->{dt} ) - ), - }; + if ($@) { + _ErrorHandling( $self, + 'OpenWeatherMap Weather decode JSON err ' . $@ ); } + elsif ( defined( $data->{cod} ) + and $data->{cod} + and $data->{cod} != 200 + and defined( $data->{message} ) + and $data->{message} ) + { + _ErrorHandling( $self, $data->{cod} . ': ' . $data->{message} ); + } + else { - if ( $self->{endpoint} eq 'forecast' ) { - if ( ref( $data->{list} ) eq "ARRAY" - and scalar( @{ $data->{list} } ) > 0 ) - { - ## löschen des alten Datensatzes - delete $self->{cached}->{forecast}; + ###### Ab hier wird die ResponseHash Referenze für die Rückgabe zusammen gestellt + $self->{cached}->{current_date_time} = + strftime( "%a, %e %b %Y %H:%M %p", + localtime( $self->{fetchTime} ) ); - my $i = 0; - foreach ( @{ $data->{list} } ) { - push( - @{ $self->{cached}->{forecast}->{hourly} }, - { - 'pubDate' => strftime( - "%a, %e %b %Y %H:%M %p", - localtime( - ( $data->{list}->[$i]->{dt} ) - 3600 - ) - ), - 'day_of_week' => strftime( - "%a", - localtime( - ( $data->{list}->[$i]->{dt} ) - 3600 - ) - ), - 'temperature' => int( - sprintf( - "%.1f", - ( - $data->{list}->[$i]->{main}->{temp} - - 273.15 - ) - ) + 0.5 - ), - 'temp_c' => int( - sprintf( - "%.1f", - ( - $data->{list}->[$i]->{main}->{temp} - - 273.15 - ) - ) + 0.5 - ), - 'low_c' => int( - sprintf( - "%.1f", - ( - $data->{list}->[$i]->{main} - ->{temp_min} - 273.15 - ) - ) + 0.5 - ), - 'high_c' => int( - sprintf( - "%.1f", - ( - $data->{list}->[$i]->{main} - ->{temp_max} - 273.15 - ) - ) + 0.5 - ), - 'tempLow' => int( - sprintf( - "%.1f", - ( - $data->{list}->[$i]->{main} - ->{temp_min} - 273.15 - ) - ) + 0.5 - ), - 'tempHigh' => int( - sprintf( - "%.1f", - ( - $data->{list}->[$i]->{main} - ->{temp_max} - 273.15 - ) - ) + 0.5 - ), - 'humidity' => - $data->{list}->[$i]->{main}->{humidity}, - 'condition' => encode_utf8( - $data->{list}->[$i]->{weather}->[0] - ->{description} - ), - 'pressure' => int( - sprintf( "%.1f", - $data->{list}->[$i]->{main}->{pressure} - ) + 0.5 - ), - 'wind' => int( - sprintf( "%.1f", - $data->{list}->[$i]->{wind}->{speed} ) - + 0.5 - ), - 'wind_speed' => int( - sprintf( "%.1f", - $data->{list}->[$i]->{wind}->{speed} ) - + 0.5 - ), - 'cloudCover' => - $data->{list}->[$i]->{clouds}->{all}, - 'code' => - $codes{ $data->{list}->[$i]->{weather}->[0] - ->{id} }, - 'iconAPI' => - $data->{list}->[$i]->{weather}->[0]->{icon}, - }, - ); + if ( $self->{endpoint} eq 'weather' ) { + $self->{cached}->{country} = $data->{sys}->{country}; + $self->{cached}->{city} = $data->{name}; + $self->{cached}->{license}{text} = 'none'; + $self->{cached}->{current} = { + 'temperature' => int( + sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 ) ) + + 0.5 + ), + 'temp_c' => int( + sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 ) ) + + 0.5 + ), + 'low_c' => int( + sprintf( "%.1f", + ( $data->{main}->{temp_min} - 273.15 ) ) + 0.5 + ), + 'high_c' => int( + sprintf( "%.1f", + ( $data->{main}->{temp_max} - 273.15 ) ) + 0.5 + ), + 'tempLow' => int( + sprintf( "%.1f", + ( $data->{main}->{temp_min} - 273.15 ) ) + 0.5 + ), + 'tempHigh' => int( + sprintf( "%.1f", + ( $data->{main}->{temp_max} - 273.15 ) ) + 0.5 + ), + 'humidity' => $data->{main}->{humidity}, + 'condition' => + encode_utf8( $data->{weather}->[0]->{description} ), + 'pressure' => + int( sprintf( "%.1f", $data->{main}->{pressure} ) + 0.5 ), + 'wind' => + int( sprintf( "%.1f", $data->{wind}->{speed} ) + 0.5 ), + 'wind_speed' => + int( sprintf( "%.1f", $data->{wind}->{speed} ) + 0.5 ), + 'wind_direction' => $data->{wind}->{deg}, + 'cloudCover' => $data->{clouds}->{all}, + 'visibility' => + int( sprintf( "%.1f", $data->{visibility} ) + 0.5 ), + 'code' => $codes{ $data->{weather}->[0]->{id} }, + 'iconAPI' => $data->{weather}->[0]->{icon}, + 'sunsetTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( $data->{sys}->{sunset} ) + ), + 'sunriseTime' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( $data->{sys}->{sunrise} ) + ), + 'pubDate' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( $data->{dt} ) + ), + }; + } - $i++; + if ( $self->{endpoint} eq 'forecast' ) { + if ( ref( $data->{list} ) eq "ARRAY" + and scalar( @{ $data->{list} } ) > 0 ) + { + ## löschen des alten Datensatzes + delete $self->{cached}->{forecast}; + + my $i = 0; + foreach ( @{ $data->{list} } ) { + push( + @{ $self->{cached}->{forecast}->{hourly} }, + { + 'pubDate' => strftime( + "%a, %e %b %Y %H:%M %p", + localtime( + ( $data->{list}->[$i]->{dt} ) - 3600 + ) + ), + 'day_of_week' => strftime( + "%a", + localtime( + ( $data->{list}->[$i]->{dt} ) - 3600 + ) + ), + 'temperature' => int( + sprintf( + "%.1f", + ( + $data->{list}->[$i]->{main}->{temp} + - 273.15 + ) + ) + 0.5 + ), + 'temp_c' => int( + sprintf( + "%.1f", + ( + $data->{list}->[$i]->{main}->{temp} + - 273.15 + ) + ) + 0.5 + ), + 'low_c' => int( + sprintf( + "%.1f", + ( + $data->{list}->[$i]->{main} + ->{temp_min} - 273.15 + ) + ) + 0.5 + ), + 'high_c' => int( + sprintf( + "%.1f", + ( + $data->{list}->[$i]->{main} + ->{temp_max} - 273.15 + ) + ) + 0.5 + ), + 'tempLow' => int( + sprintf( + "%.1f", + ( + $data->{list}->[$i]->{main} + ->{temp_min} - 273.15 + ) + ) + 0.5 + ), + 'tempHigh' => int( + sprintf( + "%.1f", + ( + $data->{list}->[$i]->{main} + ->{temp_max} - 273.15 + ) + ) + 0.5 + ), + 'humidity' => + $data->{list}->[$i]->{main}->{humidity}, + 'condition' => encode_utf8( + $data->{list}->[$i]->{weather}->[0] + ->{description} + ), + 'pressure' => int( + sprintf( "%.1f", + $data->{list}->[$i]->{main}->{pressure} + ) + 0.5 + ), + 'wind' => int( + sprintf( "%.1f", + $data->{list}->[$i]->{wind}->{speed} ) + + 0.5 + ), + 'wind_speed' => int( + sprintf( "%.1f", + $data->{list}->[$i]->{wind}->{speed} ) + + 0.5 + ), + 'cloudCover' => + $data->{list}->[$i]->{clouds}->{all}, + 'code' => + $codes{ $data->{list}->[$i]->{weather}->[0] + ->{id} }, + 'iconAPI' => + $data->{list}->[$i]->{weather}->[0]->{icon}, + }, + ); + + $i++; + } } } } } + else { _ErrorHandling( $self, 'OpenWeatherMap ' . $response ); } } $self->{endpoint} = 'none' if ( $self->{endpoint} eq 'forecast' );