From 49a292cc503ec5f014cfca638c16e9ee30c38aa3 Mon Sep 17 00:00:00 2001 From: Marko Oldenburg Date: Wed, 9 Jan 2019 12:17:10 +0100 Subject: [PATCH] fix write Hash then ref current not present --- 59_Weather.pm | 24 ++- DarkSkyAPI.pm | 473 ++++++++++++++++++++++++------------------- OpenWeatherMapAPI.pm | 206 ++++++++++++------- 3 files changed, 410 insertions(+), 293 deletions(-) diff --git a/59_Weather.pm b/59_Weather.pm index 718ca13..80de882 100755 --- a/59_Weather.pm +++ b/59_Weather.pm @@ -369,17 +369,20 @@ sub Weather_WriteReadings($$) { # current - while( my ($r,$v) = each %{$dataRef->{current}} ) { - readingsBulkUpdate($hash, $r, $v); + if ( ref( $dataRef->{current} ) eq 'HASH' ) { + while( my ($r,$v) = each %{$dataRef->{current}} ) { + readingsBulkUpdate($hash, $r, $v); + } + + readingsBulkUpdate($hash, 'icon', $iconlist[$dataRef->{current}{code}]); + if ( defined($dataRef->{current}{wind_direction}) + and defined($dataRef->{current}{wind_speed} ) ) + { + my $wdir= degrees_to_direction($dataRef->{current}{wind_direction}, @directions_txt_i18n); + readingsBulkUpdate($hash, 'wind_condition', 'Wind: ' . $wdir . ' ' . $dataRef->{current}{wind_speed} . ' km/h'); + } } - readingsBulkUpdate($hash, 'icon', $iconlist[$dataRef->{current}{code}]); - if ( defined($dataRef->{current}{wind_direction}) - and defined($dataRef->{current}{wind_speed} ) ) - { - my $wdir= degrees_to_direction($dataRef->{current}{wind_direction}, @directions_txt_i18n); - readingsBulkUpdate($hash, 'wind_condition', 'Wind: ' . $wdir . ' ' . $dataRef->{current}{wind_speed} . ' km/h'); - } - + # forecast if ( ref( $dataRef->{forcast} ) eq 'HASH' ) { ## Forcast for hourly @@ -413,6 +416,7 @@ sub Weather_WriteReadings($$) { } } } + my $val= 'T:' . $dataRef->{current}{temperature} . '°C' .' ' . substr($status_items_txt_i18n{1}, 0, 1) . ':' . $dataRef->{current}{humidity} . '%' .' ' . substr($status_items_txt_i18n{0}, 0, 1) . ':' . $dataRef->{current}{wind} . 'km/h' diff --git a/DarkSkyAPI.pm b/DarkSkyAPI.pm index ebd9305..9b5e620 100644 --- a/DarkSkyAPI.pm +++ b/DarkSkyAPI.pm @@ -111,7 +111,7 @@ sub _RetrieveDataFromDarkSky($) { my $self = shift; # retrieve data from cache - if ( (time() - $self->{fetchTime}) < $self->{cachemaxage} ) { + if ( ( time() - $self->{fetchTime} ) < $self->{cachemaxage} ) { return _CallWeatherCallbackFn($self); } @@ -126,12 +126,15 @@ sub _RetrieveDataFromDarkSky($) { or $self->{key} eq 'none' or $missingModul ) { - _RetrieveDataFinished( $paramRef, - 'The given location is invalid. (wrong latitude or longitude?) put both as an attribute in the global device or set define option location=[LAT],[LONG]', - undef ) - if ( $self->{lat} eq 'error' or $self->{long} eq 'error' ); + _RetrieveDataFinished( + $paramRef, +'The given location is invalid. (wrong latitude or longitude?) put both as an attribute in the global device or set define option location=[LAT],[LONG]', + undef + ) if ( $self->{lat} eq 'error' or $self->{long} eq 'error' ); - _RetrieveDataFinished( $paramRef, 'No given api key. (define myWeather Weather apikey=[KEY])', undef ) + _RetrieveDataFinished( $paramRef, + 'No given api key. (define myWeather Weather apikey=[KEY])', + undef ) if ( $self->{key} eq 'none' ); _RetrieveDataFinished( $paramRef, @@ -157,9 +160,8 @@ sub _RetrieveDataFinished($$$) { my $self = $paramRef->{self}; if ( !$err ) { - $self->{cached}->{status} = 'ok'; - $self->{cached}->{validity} = 'up-to-date', - $self->{fetchTime} = time(); + $self->{cached}->{status} = 'ok'; + $self->{cached}->{validity} = 'up-to-date', $self->{fetchTime} = time(); _ProcessingRetrieveData( $self, $response ); } else { @@ -172,201 +174,260 @@ sub _RetrieveDataFinished($$$) { sub _ProcessingRetrieveData($$) { my ( $self, $response ) = @_; - if ( $self->{cached}->{status} eq 'ok' and defined($response) ) - { - my $data = eval { decode_json($response) }; + if ( $self->{cached}->{status} eq 'ok' and defined($response) ) { + my $data = eval { decode_json($response) }; - if ($@) { - _ErrorHandling( $self, 'DarkSky Weather decode JSON err ' . $@ ); - } - elsif ( defined( $data->{code} ) and defined( $data->{error} ) ) { - _ErrorHandling( $self, 'Code: ' . $data->{code} . ' Error: ' . $data->{error} ); - } - else { - # print Dumper $data; ## für Debugging + if ($@) { + _ErrorHandling( $self, 'DarkSky Weather decode JSON err ' . $@ ); + } + elsif ( defined( $data->{code} ) and defined( $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} = $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' => $data->{currently}->{pressure}, - 'wind' => $data->{currently}->{windSpeed}, - 'wind_speed' => $data->{currently}->{windSpeed}, - 'wind_direction' => $data->{currently}->{windBearing}, - 'windGust' => $data->{currently}->{windGust}, - 'cloudCover' => $data->{currently}->{cloudCover}, - 'uvIndex' => $data->{currently}->{uvIndex}, - 'visibility' => $data->{currently}->{visibility}, - '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'} ) + $self->{cached}->{current_date_time} = strftime( + "%a,%e %b %Y %H:%M %p", + localtime( $self->{fetchTime} ) + ), + $self->{cached}->{timezone} = $data->{timezone}; + $self->{cached}->{license} = $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' => $data->{currently}->{pressure}, + 'wind' => $data->{currently}->{windSpeed}, + 'wind_speed' => $data->{currently}->{windSpeed}, + 'wind_direction' => $data->{currently}->{windBearing}, + 'windGust' => $data->{currently}->{windGust}, + 'cloudCover' => $data->{currently}->{cloudCover}, + 'uvIndex' => $data->{currently}->{uvIndex}, + 'visibility' => $data->{currently}->{visibility}, + '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}, + }; + + my $i = 0; + if ( ref( $data->{daily}->{data} ) eq "ARRAY" + and scalar( @{ $data->{daily}->{data} } ) > 0 ) + { + foreach ( @{ $data->{daily}->{data} } ) { + push( + @{ $self->{cached}->{forcast}->{daily} }, + { + 'date' => strftime( + "%a, %d.%m.%Y", + 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} + ) ), - 'precipProbability' => - $data->{currently}->{precipProbability}, - 'apparentTemperature' => - int(sprintf("%.1f",$data->{currently}->{apparentTemperature}) + 0.5), 'precipIntensity' => - $data->{currently}->{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' => $data->{daily}->{data}[$i]{dewPoint}, + 'humidity' => $data->{daily}->{data}[$i]{humidity} + * 100, + 'cloudCover' => + $data->{daily}->{data}[$i]{cloudCover}, + 'precipType' => + $data->{daily}->{data}[$i]{precipType}, - my $i = 0; - if ( ref($data->{daily}->{data}) eq "ARRAY" - and scalar(@{ $data->{daily}->{data}}) > 0 ) - { - foreach (@{$data->{daily}->{data}}) { - push (@{$self->{cached}->{forcast}->{daily}}, - { - 'date' => strftime( - "%a, %d.%m.%Y", - 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' => $data->{daily}->{data}[$i]{dewPoint}, - 'humidity' => $data->{daily}->{data}[$i]{humidity} * 100, - 'cloudCover' => $data->{daily}->{data}[$i]{cloudCover}, - 'precipType' => $data->{daily}->{data}[$i]{precipType}, + 'wind_direction' => + $data->{daily}->{data}[$i]{windBearing}, + 'wind' => $data->{daily}->{data}[$i]{windSpeed}, + 'wind_speed' => + $data->{daily}->{data}[$i]{windSpeed}, + 'windGust' => $data->{daily}->{data}[$i]{windGust}, + '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} + ) + ), - 'wind_direction' => - $data->{daily}->{data}[$i]{windBearing}, - 'wind' => $data->{daily}->{data}[$i]{windSpeed}, - 'wind_speed' => $data->{daily}->{data}[$i]{windSpeed}, - 'windGust' => $data->{daily}->{data}[$i]{windGust}, - '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' => $data->{daily}->{data}[$i]{pressure}, - 'visibility' => $data->{daily}->{data}[$i]{visibility}, - }); - $i++; + 'precipProbability' => + $data->{daily}->{data}[$i]{precipProbability}, + 'pressure' => $data->{daily}->{data}[$i]{pressure}, + 'visibility' => + $data->{daily}->{data}[$i]{visibility}, } - } + ); + + $i++; + } } } + } ## Aufruf der callbackFn _CallWeatherCallbackFn($self); @@ -374,16 +435,17 @@ sub _ProcessingRetrieveData($$) { sub _CallWeatherCallbackFn($) { my $self = shift; - + # ## Aufruf der callbackFn main::Weather_RetrieveCallbackFn( $self->{devHash} ); } sub _ErrorHandling($$) { - my ($self,$err) = @_; + my ( $self, $err ) = @_; - $self->{cached}->{current_date_time} = strftime("%a,%e %b %Y %H:%M %p",localtime( $self->{fetchTime} )), - $self->{cached}->{status} = $err; + $self->{cached}->{current_date_time} = + strftime( "%a,%e %b %Y %H:%M %p", localtime( $self->{fetchTime} ) ), + $self->{cached}->{status} = $err; $self->{cached}->{validity} = 'stale'; } @@ -391,12 +453,13 @@ sub _CreateForcastRef($) { my $self = shift; my $forcastRef = ( - { - lat => $self->{lat}, - long => $self->{long}, - apiMaintainer => 'Leon Gaultier (CoolTux)', - } - ); + { + lat => $self->{lat}, + long => $self->{long}, + apiMaintainer => +'Leon Gaultier (CoolTux)', + } + ); return $forcastRef; } diff --git a/OpenWeatherMapAPI.pm b/OpenWeatherMapAPI.pm index 5cea027..39f8f68 100644 --- a/OpenWeatherMapAPI.pm +++ b/OpenWeatherMapAPI.pm @@ -53,9 +53,35 @@ use constant URL => 'https://api.openweathermap.org/data/2.5/'; ## URL . 'forcast?' for forcast data my %codes = ( - 200 => 45, 201 => 45, 202 => 45, 210 => 4, 211 => 4, 212 => 3, 221 => 4, 230 => 45, 231 => 45, 232 => 45, - 300 => 9, 301 => 9, 302 => 9, 310 => 9, 311 => 9, 312 => 9, 313 => 9, 314 => 9, 321 => 9, - 500 => 35, 501 => 35, 502 => 35, 503 => 35, 504 => 35, 511 => 35, 520 => 35, 521 => 35, 522 => 35, 531 => 35, + 200 => 45, + 201 => 45, + 202 => 45, + 210 => 4, + 211 => 4, + 212 => 3, + 221 => 4, + 230 => 45, + 231 => 45, + 232 => 45, + 300 => 9, + 301 => 9, + 302 => 9, + 310 => 9, + 311 => 9, + 312 => 9, + 313 => 9, + 314 => 9, + 321 => 9, + 500 => 35, + 501 => 35, + 502 => 35, + 503 => 35, + 504 => 35, + 511 => 35, + 520 => 35, + 521 => 35, + 522 => 35, + 531 => 35, ); sub new { @@ -72,7 +98,7 @@ sub new { fetchTime => 0, endpoint => 'none', }; - + $self->{cached} = _CreateForcastRef($self); bless $self, $class; @@ -109,7 +135,7 @@ sub _RetrieveDataFromOpenWeatherMap($) { my $self = shift; # retrieve data from cache - if ( (time() - $self->{fetchTime}) < $self->{cachemaxage} ) { + if ( ( time() - $self->{fetchTime} ) < $self->{cachemaxage} ) { return _CallWeatherCallbackFn($self); } @@ -127,12 +153,15 @@ sub _RetrieveDataFromOpenWeatherMap($) { or $self->{key} eq 'none' or $missingModul ) { - _RetrieveDataFinished( $paramRef, - 'The given location is invalid. (wrong latitude or longitude?) put both as an attribute in the global device or set define option location=[LAT],[LONG]', - undef ) - if ( $self->{lat} eq 'error' or $self->{long} eq 'error' ); + _RetrieveDataFinished( + $paramRef, +'The given location is invalid. (wrong latitude or longitude?) put both as an attribute in the global device or set define option location=[LAT],[LONG]', + undef + ) if ( $self->{lat} eq 'error' or $self->{long} eq 'error' ); - _RetrieveDataFinished( $paramRef, 'No given api key. (define myWeather Weather apikey=[KEY])', undef ) + _RetrieveDataFinished( $paramRef, + 'No given api key. (define myWeather Weather apikey=[KEY])', + undef ) if ( $self->{key} eq 'none' ); _RetrieveDataFinished( $paramRef, @@ -142,14 +171,11 @@ sub _RetrieveDataFromOpenWeatherMap($) { else { $paramRef->{url} = URL - . $paramRef->{endpoint} . '?' - . 'lat=' - . $self->{lat} . '&' - . 'lon=' + . $paramRef->{endpoint} . '?' . 'lat=' + . $self->{lat} . '&' . 'lon=' . $self->{long} . '&' . 'APPID=' - . $self->{key} . '&' - . 'lang=' + . $self->{key} . '&' . 'lang=' . $self->{lang}; main::HttpUtils_NonblockingGet($paramRef); @@ -161,9 +187,8 @@ sub _RetrieveDataFinished($$$) { my $self = $paramRef->{self}; if ( !$err ) { - $self->{cached}->{status} = 'ok'; - $self->{cached}->{validity} = 'up-to-date', - $self->{fetchTime} = time(); + $self->{cached}->{status} = 'ok'; + $self->{cached}->{validity} = 'up-to-date', $self->{fetchTime} = time(); _ProcessingRetrieveData( $self, $response ); } else { @@ -171,81 +196,105 @@ sub _RetrieveDataFinished($$$) { _ErrorHandling( $self, $err ); _ProcessingRetrieveData( $self, $response ); } - + $self->{endpoint} = $paramRef->{endpoint}; } sub _ProcessingRetrieveData($$) { my ( $self, $response ) = @_; - if ( $self->{cached}->{status} eq 'ok' and defined($response) ) - { - my $data = eval { decode_json($response) }; - #print 'Dumper1: ' . Dumper $data; + if ( $self->{cached}->{status} eq 'ok' and defined($response) ) { + my $data = eval { decode_json($response) }; - if ($@) { - _ErrorHandling( $self, 'OpenWeatherMap Weather decode JSON err ' . $@ ); - } - elsif ( defined($data->{cod}) and defined($data->{message}) ) { - print 'Dumper2: ' . Dumper $data; - _ErrorHandling( $self, $data->{cod} . ': ' . $data->{message} ); - } - else { - # print Dumper $data; ## für Debugging - return if ( $self->{endpoint} eq 'forcast' ); - +# print 'Dumper1: ' . Dumper $data; - $self->{cached}->{current_date_time} = strftime("%a,%e %b %Y %H:%M %p",localtime( $self->{fetchTime} )), - $self->{cached}->{country} = $data->{sys}->{country}; - $self->{cached}->{city} = $data->{name}; - $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' => $data->{main}->{pressure}, - 'wind' => $data->{wind}->{speed}, - 'wind_speed' => $data->{wind}->{speed}, - 'wind_direction' => $data->{wind}->{deg}, - 'cloudCover' => $data->{clouds}->{all}, - 'visibility' => $data->{visibility}, - # 'code' => $codes{ $data->{weather}[0]{icon} }, - '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 ( $self->{endpoint} eq 'weather' ); - } + if ($@) { + _ErrorHandling( $self, + 'OpenWeatherMap Weather decode JSON err ' . $@ ); } + elsif ( defined( $data->{cod} ) and defined( $data->{message} ) ) { + print 'Dumper2: ' . Dumper $data; + _ErrorHandling( $self, $data->{cod} . ': ' . $data->{message} ); + } + else { +# print Dumper $data; ## für Debugging + return if ( $self->{endpoint} eq 'forcast' ); -# $self->{cached} = $forcastRef; Vorsicht - - _RetrieveDataFromOpenWeatherMap($self) if ( $self->{endpoint} eq 'weather' ); + $self->{cached}->{current_date_time} = strftime( + "%a,%e %b %Y %H:%M %p", + localtime( $self->{fetchTime} ) + ), + $self->{cached}->{country} = $data->{sys}->{country}; + $self->{cached}->{city} = $data->{name}; + $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' => $data->{main}->{pressure}, + 'wind' => $data->{wind}->{speed}, + 'wind_speed' => $data->{wind}->{speed}, + 'wind_direction' => $data->{wind}->{deg}, + 'cloudCover' => $data->{clouds}->{all}, + 'visibility' => $data->{visibility}, + +# 'code' => $codes{ $data->{weather}[0]{icon} }, + '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 ( $self->{endpoint} eq 'weather' ); + } + } + + _RetrieveDataFromOpenWeatherMap($self) + if ( $self->{endpoint} eq 'weather' ); $self->{endpoint} = 'none' if ( $self->{endpoint} eq 'forcast' ); - + _CallWeatherCallbackFn($self); } sub _CallWeatherCallbackFn($) { my $self = shift; - + # ## Aufruf der callbackFn main::Weather_RetrieveCallbackFn( $self->{devHash} ); } sub _ErrorHandling($$) { - my ($self,$err) = @_; + my ( $self, $err ) = @_; - $self->{cached}->{current_date_time} = strftime("%a,%e %b %Y %H:%M %p",localtime( $self->{fetchTime} )), - $self->{cached}->{status} = $err; + $self->{cached}->{current_date_time} = + strftime( "%a,%e %b %Y %H:%M %p", localtime( $self->{fetchTime} ) ), + $self->{cached}->{status} = $err; $self->{cached}->{validity} = 'stale'; } @@ -253,12 +302,13 @@ sub _CreateForcastRef($) { my $self = shift; my $forcastRef = ( - { - lat => $self->{lat}, - long => $self->{long}, - apiMaintainer => 'Leon Gaultier (CoolTux)', - } - ); + { + lat => $self->{lat}, + long => $self->{long}, + apiMaintainer => +'Leon Gaultier (CoolTux)', + } + ); return $forcastRef; }