diff --git a/59_Weather.pm b/59_Weather.pm index deb887a..2d1a74a 100755 --- a/59_Weather.pm +++ b/59_Weather.pm @@ -379,7 +379,10 @@ sub Weather_WriteReadings($$) { readingsBulkUpdate($hash, 'icon', $iconlist[$dataRef->{current}->{code}]); if ( defined($dataRef->{current}->{wind_direction}) - and defined($dataRef->{current}->{wind_speed} ) ) + and $dataRef->{current}->{wind_direction} + and defined($dataRef->{current}->{wind_speed}) + and $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'); @@ -389,8 +392,9 @@ sub Weather_WriteReadings($$) { ### forecast if ( ref( $dataRef->{forecast} ) eq 'HASH' ) { ## hourly - if ( defined($dataRef->{forecast}->{hourly}) and ref( $dataRef->{forecast}->{hourly} ) eq 'ARRAY' - and scalar( @{ $dataRef->{forecast}->{hourly} } ) > 0 ) + if ( defined($dataRef->{forecast}->{hourly}) + and ref( $dataRef->{forecast}->{hourly} ) eq 'ARRAY' + and scalar( @{ $dataRef->{forecast}->{hourly} } ) > 0 ) { my $i= 0; foreach my $fc (@{$dataRef->{forecast}->{hourly}}) { @@ -404,7 +408,10 @@ sub Weather_WriteReadings($$) { # readingsBulkUpdate($hash, $f . "day_of_week", $wdays_txt_i18n{$fc->{day}}); readingsBulkUpdate($hash, $f . 'icon', $iconlist[$dataRef->{forecast}->{hourly}[$i-1]{code}]); if ( defined($dataRef->{forecast}->{hourly}[$i-1]{wind_direction}) - and defined($dataRef->{forecast}->{hourly}[$i-1]{wind_speed}) ) + and $dataRef->{forecast}->{hourly}[$i-1]{wind_direction} + and defined($dataRef->{forecast}->{hourly}[$i-1]{wind_speed}) + and $dataRef->{forecast}->{hourly}[$i-1]{wind_speed} + ) { my $wdir= degrees_to_direction($dataRef->{forecast}->{hourly}[$i-1]{wind_direction}, @directions_txt_i18n); readingsBulkUpdate($hash, $f . 'wind_condition', 'Wind: ' . $wdir . ' ' . $dataRef->{forecast}->{hourly}[$i-1]{wind_speed} . ' km/h'); @@ -415,7 +422,7 @@ sub Weather_WriteReadings($$) { ## daily if ( defined($dataRef->{forecast}->{daily}) and ref( $dataRef->{forecast}->{daily} ) eq 'ARRAY' - and scalar( @{ $dataRef->{forecast}->{daily} } ) > 0 ) + and scalar( @{ $dataRef->{forecast}->{daily} } ) > 0 ) { my $i= 0; foreach my $fc (@{$dataRef->{forecast}->{daily}}) { @@ -429,7 +436,10 @@ sub Weather_WriteReadings($$) { # readingsBulkUpdate($hash, $f . "day_of_week", $wdays_txt_i18n{$fc->{day}}); readingsBulkUpdate($hash, $f . 'icon', $iconlist[$dataRef->{forecast}->{daily}[$i-1]{code}]); if ( defined($dataRef->{forecast}->{daily}[$i-1]{wind_direction}) - and defined($dataRef->{forecast}->{daily}[$i-1]{wind_speed}) ) + and $dataRef->{forecast}->{daily}[$i-1]{wind_direction} + and defined($dataRef->{forecast}->{daily}[$i-1]{wind_speed}) + and $dataRef->{forecast}->{daily}[$i-1]{wind_speed} + ) { my $wdir= degrees_to_direction($dataRef->{forecast}->{daily}[$i-1]{wind_direction}, @directions_txt_i18n); readingsBulkUpdate($hash, $f . 'wind_condition', 'Wind: ' . $wdir . ' ' . $dataRef->{forecast}->{daily}[$i-1]{wind_speed} . ' km/h'); @@ -609,9 +619,9 @@ sub Weather_Define($$) { $hash->{NOTIFYDEV} = "global"; $hash->{fhem}->{interfaces}= "temperature;humidity;wind"; - $hash->{LOCATION} = ( defined($location) ? $location : AttrVal( 'global', 'latitude', 'error' ).','.AttrVal( 'global', 'longitude', 'error' ) ); + $hash->{LOCATION} = ( (defined($location) and $location) ? $location : AttrVal( 'global', 'latitude', 'error' ).','.AttrVal( 'global', 'longitude', 'error' ) ); $hash->{INTERVAL} = $interval; - $hash->{LANG} = ( defined($lang) ? $lang : lc(AttrVal('global','language','de')) ); + $hash->{LANG} = ( (defined($lang) and $lang) ? $lang : lc(AttrVal('global','language','de')) ); $hash->{API} = $api; $hash->{APIKEY} = $apikey; $hash->{APIOPTIONS} = $apioptions; diff --git a/DarkSkyAPI.pm b/DarkSkyAPI.pm index d47882a..666ee3e 100644 --- a/DarkSkyAPI.pm +++ b/DarkSkyAPI.pm @@ -67,12 +67,22 @@ sub new { my $self = { devName => $argsRef->{devName}, - key => ( defined( $argsRef->{apikey} ) ? $argsRef->{apikey} : 'none' ), - cachemaxage => ( (split(':',$argsRef->{apioptions}))[0] eq 'cachemaxage' ? (split(':',$argsRef->{apioptions}))[1] : 900 ), - lang => $argsRef->{language}, - lat => ( split( ',', $argsRef->{location} ) )[0], - long => ( split( ',', $argsRef->{location} ) )[1], - fetchTime => 0, + key => ( + ( defined( $argsRef->{apikey} ) and $argsRef->{apikey} ) + ? $argsRef->{apikey} + : 'none' + ), + cachemaxage => ( + ( defined( $argsRef->{apioptions} ) and $argsRef->{apioptions} ) + ? ( ( split( ':', $argsRef->{apioptions} ) )[0] eq 'cachemaxage' + ? ( split( ':', $argsRef->{apioptions} ) )[1] + : 900 ) + : 900 + ), + lang => $argsRef->{language}, + lat => ( split( ',', $argsRef->{location} ) )[0], + long => ( split( ',', $argsRef->{location} ) )[1], + fetchTime => 0, }; $self->{cached} = _CreateForecastRef($self); @@ -174,25 +184,32 @@ sub _RetrieveDataFinished($$$) { sub _ProcessingRetrieveData($$) { my ( $self, $response ) = @_; - if ( $self->{cached}->{status} eq 'ok' and defined($response) ) { + if ( $self->{cached}->{status} eq 'ok' + and defined($response) + and $response ) + { my $data = eval { decode_json($response) }; if ($@) { _ErrorHandling( $self, 'DarkSky Weather decode JSON err ' . $@ ); } - elsif ( defined( $data->{code} ) and defined( $data->{error} ) ) { + 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}->{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}->{license}{text} = + $data->{flags}->{'meteoalarm-license'}; $self->{cached}->{current} = { 'temperature' => int( sprintf( "%.1f", $data->{currently}->{temperature} ) + 0.5 @@ -203,12 +220,12 @@ sub _ProcessingRetrieveData($$) { 'dewPoint' => int( sprintf( "%.1f", $data->{currently}->{dewPoint} ) + 0.5 ), - 'humidity' => $data->{currently}->{humidity} * 100, - 'condition' => encode_utf8( $data->{currently}->{summary} ), - 'pressure' => int( + 'humidity' => $data->{currently}->{humidity} * 100, + 'condition' => encode_utf8( $data->{currently}->{summary} ), + 'pressure' => int( sprintf( "%.1f", $data->{currently}->{pressure} ) + 0.5 ), - 'wind' => int( + 'wind' => int( sprintf( "%.1f", $data->{currently}->{windSpeed} ) + 0.5 ), 'wind_speed' => int( @@ -218,15 +235,15 @@ sub _ProcessingRetrieveData($$) { 'windGust' => int( sprintf( "%.1f", $data->{currently}->{windGust} ) + 0.5 ), - 'cloudCover' => $data->{currently}->{cloudCover} * 100, - 'uvIndex' => $data->{currently}->{uvIndex}, - 'visibility' => int( + '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( + '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'} ) ), @@ -244,7 +261,7 @@ sub _ProcessingRetrieveData($$) { { ### löschen des alten Datensatzes delete $self->{cached}->{forecast}; - + my $i = 0; foreach ( @{ $data->{daily}->{data} } ) { push( @@ -252,120 +269,120 @@ sub _ProcessingRetrieveData($$) { { 'date' => strftime( "%a, %d.%m.%Y", - localtime( $data->{daily}->{data}->[$i]->{'time'} ) + localtime( + $data->{daily}->{data}->[$i]->{'time'} + ) ), 'day_of_week' => strftime( "%a", - localtime( $data->{daily}->{data}->[$i]->{'time'} ) + localtime( + $data->{daily}->{data}->[$i]->{'time'} + ) ), 'low_c' => int( sprintf( "%.1f", - $data->{daily}->{data}->[$i]->{temperatureLow} ) - + 0.5 + $data->{daily}->{data}->[$i] + ->{temperatureLow} ) + 0.5 ), 'high_c' => int( sprintf( "%.1f", - $data->{daily}->{data}->[$i]->{temperatureHigh} - ) + 0.5 + $data->{daily}->{data}->[$i] + ->{temperatureHigh} ) + 0.5 ), 'tempMin' => int( sprintf( "%.1f", - $data->{daily}->{data}->[$i]->{temperatureMin} ) - + 0.5 + $data->{daily}->{data}->[$i] + ->{temperatureMin} ) + 0.5 ), 'tempMinTime' => strftime( "%a,%e %b %Y %H:%M %p", localtime( - $data->{daily} - ->{data}->[$i]->{temperatureMinTime} + $data->{daily}->{data}->[$i] + ->{temperatureMinTime} ) ), 'tempMax' => int( sprintf( "%.1f", - $data->{daily}->{data}->[$i]->{temperatureMax} ) - + 0.5 + $data->{daily}->{data}->[$i] + ->{temperatureMax} ) + 0.5 ), 'tempMaxTime' => strftime( "%a,%e %b %Y %H:%M %p", localtime( - $data->{daily} - ->{data}->[$i]->{temperatureMaxTime} + $data->{daily}->{data}->[$i] + ->{temperatureMaxTime} ) ), 'tempLow' => int( sprintf( "%.1f", - $data->{daily}->{data}->[$i]->{temperatureLow} ) - + 0.5 + $data->{daily}->{data}->[$i] + ->{temperatureLow} ) + 0.5 ), 'tempLowTime' => strftime( "%a,%e %b %Y %H:%M %p", localtime( - $data->{daily} - ->{data}->[$i]->{temperatureLowTime} + $data->{daily}->{data}->[$i] + ->{temperatureLowTime} ) ), 'tempHigh' => int( sprintf( "%.1f", - $data->{daily}->{data}->[$i]->{temperatureHigh} - ) + 0.5 + $data->{daily}->{data}->[$i] + ->{temperatureHigh} ) + 0.5 ), 'tempHighTime' => strftime( "%a,%e %b %Y %H:%M %p", localtime( - $data->{daily} - ->{data}->[$i]->{temperatureHighTime} + $data->{daily}->{data}->[$i] + ->{temperatureHighTime} ) ), 'apparentTempLow' => int( sprintf( "%.1f", - $data->{daily} - ->{data}->[$i]->{apparentTemperatureLow} ) + - 0.5 + $data->{daily}->{data}->[$i] + ->{apparentTemperatureLow} ) + 0.5 ), 'apparentTempLowTime' => strftime( "%a,%e %b %Y %H:%M %p", localtime( - $data->{daily} - ->{data}->[$i]->{apparentTemperatureLowTime} + $data->{daily}->{data}->[$i] + ->{apparentTemperatureLowTime} ) ), 'apparentTempHigh' => int( sprintf( "%.1f", - $data->{daily} - ->{data}->[$i]->{apparentTemperatureHigh} ) + - 0.5 + $data->{daily}->{data}->[$i] + ->{apparentTemperatureHigh} ) + 0.5 ), 'apparentTempHighTime' => strftime( "%a,%e %b %Y %H:%M %p", localtime( - $data->{daily} - ->{data}->[$i]->{apparentTemperatureHighTime} + $data->{daily}->{data}->[$i] + ->{apparentTemperatureHighTime} ) ), 'apparenttempMin' => int( sprintf( "%.1f", - $data->{daily} - ->{data}->[$i]->{apparentTemperatureMin} ) + - 0.5 + $data->{daily}->{data}->[$i] + ->{apparentTemperatureMin} ) + 0.5 ), 'apparenttempMinTime' => strftime( "%a,%e %b %Y %H:%M %p", localtime( - $data->{daily} - ->{data}->[$i]->{apparentTemperatureMinTime} + $data->{daily}->{data}->[$i] + ->{apparentTemperatureMinTime} ) ), 'apparenttempMax' => int( sprintf( "%.1f", - $data->{daily} - ->{data}->[$i]->{apparentTemperatureMax} ) + - 0.5 + $data->{daily}->{data}->[$i] + ->{apparentTemperatureMax} ) + 0.5 ), 'apparenttempMaxTime' => strftime( "%a,%e %b %Y %H:%M %p", localtime( - $data->{daily} - ->{data}->[$i]->{apparentTemperatureMaxTime} + $data->{daily}->{data}->[$i] + ->{apparentTemperatureMaxTime} ) ), 'code' => @@ -374,8 +391,9 @@ sub _ProcessingRetrieveData($$) { 'condition' => encode_utf8( $data->{daily}->{data}->[$i]->{summary} ), - 'ozone' => $data->{daily}->{data}->[$i]->{ozone}, - 'uvIndex' => $data->{daily}->{data}->[$i]->{uvIndex}, + 'ozone' => $data->{daily}->{data}->[$i]->{ozone}, + 'uvIndex' => + $data->{daily}->{data}->[$i]->{uvIndex}, 'uvIndexTime' => strftime( "%a,%e %b %Y %H:%M %p", localtime( @@ -385,21 +403,22 @@ sub _ProcessingRetrieveData($$) { 'precipIntensity' => $data->{daily}->{data}->[$i]->{precipIntensity}, 'precipIntensityMax' => - $data->{daily}->{data}->[$i]->{precipIntensityMax}, + $data->{daily}->{data}->[$i] + ->{precipIntensityMax}, 'precipIntensityMaxTime' => strftime( "%a,%e %b %Y %H:%M %p", localtime( - $data->{daily} - ->{data}->[$i]->{precipIntensityMaxTime} + $data->{daily}->{data}->[$i] + ->{precipIntensityMaxTime} ) ), 'dewPoint' => int( sprintf( "%.1f", - $data->{daily}->{data}->[$i]->{dewPoint} - ) + 0.5 + $data->{daily}->{data}->[$i]->{dewPoint} ) + + 0.5 ), - 'humidity' => $data->{daily}->{data}->[$i]->{humidity} - * 100, + 'humidity' => + $data->{daily}->{data}->[$i]->{humidity} * 100, 'cloudCover' => $data->{daily}->{data}->[$i]->{cloudCover} * 100, 'precipType' => @@ -409,17 +428,18 @@ sub _ProcessingRetrieveData($$) { $data->{daily}->{data}->[$i]->{windBearing}, 'wind' => int( sprintf( "%.1f", - $data->{daily}->{data}->[$i]->{windSpeed} - ) + 0.5 + $data->{daily}->{data}->[$i]->{windSpeed} ) + + 0.5 ), 'wind_speed' => int( sprintf( "%.1f", - $data->{daily}->{data}->[$i]->{windSpeed} - ) + 0.5 + $data->{daily}->{data}->[$i]->{windSpeed} ) + + 0.5 ), 'windGust' => int( sprintf( "%.1f", - $data->{daily}->{data}->[$i]->{windGust}) + 0.5 + $data->{daily}->{data}->[$i]->{windGust} ) + + 0.5 ), 'windGustTime' => strftime( "%a,%e %b %Y %H:%M %p", @@ -446,15 +466,17 @@ sub _ProcessingRetrieveData($$) { $data->{daily}->{data}->[$i]->{precipProbability}, 'pressure' => int( sprintf( "%.1f", - $data->{daily}->{data}->[$i]->{pressure}) + 0.5 + $data->{daily}->{data}->[$i]->{pressure} ) + + 0.5 ), 'visibility' => int( sprintf( "%.1f", - $data->{daily}->{data}->[$i]->{visibility}) + 0.5 + $data->{daily}->{data}->[$i]->{visibility} ) + + 0.5 ), } ); - + $i++; } } @@ -476,8 +498,8 @@ sub _ErrorHandling($$) { my ( $self, $err ) = @_; $self->{cached}->{current_date_time} = - strftime( "%a,%e %b %Y %H:%M %p", localtime( $self->{fetchTime} ) ), - $self->{cached}->{status} = $err; + strftime( "%a,%e %b %Y %H:%M %p", localtime( $self->{fetchTime} ) ), + $self->{cached}->{status} = $err; $self->{cached}->{validity} = 'stale'; } diff --git a/OpenWeatherMapAPI.pm b/OpenWeatherMapAPI.pm index 80f98e6..446bc06 100644 --- a/OpenWeatherMapAPI.pm +++ b/OpenWeatherMapAPI.pm @@ -114,14 +114,24 @@ sub new { my ( $class, $argsRef ) = @_; my $self = { - devName => $argsRef->{devName}, - key => ( defined( $argsRef->{apikey} ) ? $argsRef->{apikey} : 'none' ), - cachemaxage => ( (split(':',$argsRef->{apioptions}))[0] eq 'cachemaxage' ? (split(':',$argsRef->{apioptions}))[1] : 900 ), - lang => $argsRef->{language}, - lat => ( split( ',', $argsRef->{location} ) )[0], - long => ( split( ',', $argsRef->{location} ) )[1], - fetchTime => 0, - endpoint => 'none', + devName => $argsRef->{devName}, + key => ( + ( defined( $argsRef->{apikey} ) and $argsRef->{apikey} ) + ? $argsRef->{apikey} + : 'none' + ), + cachemaxage => ( + ( defined( $argsRef->{apioptions} ) and $argsRef->{apioptions} ) + ? ( ( split( ':', $argsRef->{apioptions} ) )[0] eq 'cachemaxage' + ? ( split( ':', $argsRef->{apioptions} ) )[1] + : 900 ) + : 900 + ), + lang => $argsRef->{language}, + lat => ( split( ',', $argsRef->{location} ) )[0], + long => ( split( ',', $argsRef->{location} ) )[1], + fetchTime => 0, + endpoint => 'none', }; $self->{cached} = _CreateForecastRef($self); @@ -160,9 +170,8 @@ sub _RetrieveDataFromOpenWeatherMap($) { my $self = shift; # retrieve data from cache - if( $self->{endpoint} eq 'none' ) { - if ( ( time() - $self->{fetchTime} ) < $self->{cachemaxage} ) - { + if ( $self->{endpoint} eq 'none' ) { + if ( ( time() - $self->{fetchTime} ) < $self->{cachemaxage} ) { return _CallWeatherCallbackFn($self); } } @@ -229,67 +238,74 @@ sub _RetrieveDataFinished($$$) { sub _ProcessingRetrieveData($$) { my ( $self, $response ) = @_; - if ( $self->{cached}->{status} eq 'ok' and defined($response) ) { + if ( $self->{cached}->{status} eq 'ok' + and defined($response) + and $response ) + { my $data = eval { decode_json($response) }; if ($@) { _ErrorHandling( $self, 'OpenWeatherMap Weather decode JSON err ' . $@ ); } - elsif ( defined( $data->{cod} ) and $data->{cod} != 200 and defined( $data->{message} ) ) { + 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} ) - ); - + ###### 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}->{country} = $data->{sys}->{country}; + $self->{cached}->{city} = $data->{name}; $self->{cached}->{license}{text} = 'none'; - $self->{cached}->{current} = { + $self->{cached}->{current} = { 'temperature' => int( - sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 ) - ) + 0.5 + sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 ) ) + + 0.5 ), 'temp_c' => int( - sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 ) - ) + 0.5 + sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 ) ) + + 0.5 ), 'low_c' => int( - sprintf( "%.1f", ( $data->{main}->{temp_min} - 273.15 ) - ) + 0.5 + sprintf( "%.1f", + ( $data->{main}->{temp_min} - 273.15 ) ) + 0.5 ), 'high_c' => int( - sprintf( "%.1f", ( $data->{main}->{temp_max} - 273.15 ) - ) + 0.5 + sprintf( "%.1f", + ( $data->{main}->{temp_max} - 273.15 ) ) + 0.5 ), 'tempLow' => int( - sprintf( "%.1f", ( $data->{main}->{temp_min} - 273.15 ) - ) + 0.5 + sprintf( "%.1f", + ( $data->{main}->{temp_min} - 273.15 ) ) + 0.5 ), 'tempHigh' => int( - sprintf( "%.1f", ( $data->{main}->{temp_max} - 273.15 ) - ) + 0.5 + 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 ), + '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} }, + '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", @@ -299,8 +315,10 @@ sub _ProcessingRetrieveData($$) { "%a,%e %b %Y %H:%M %p", localtime( $data->{sys}->{sunrise} ) ), - 'pubDate' => - strftime( "%a,%e %b %Y %H:%M %p", localtime( $data->{dt} ) ), + 'pubDate' => strftime( + "%a,%e %b %Y %H:%M %p", + localtime( $data->{dt} ) + ), }; } @@ -310,65 +328,110 @@ sub _ProcessingRetrieveData($$) { { ## löschen des alten Datensatzes delete $self->{cached}->{forecast}; - + my $i = 0; foreach ( @{ $data->{list} } ) { push( @{ $self->{cached}->{forecast}->{hourly} }, { 'temperature' => int( - sprintf( "%.1f", ( $data->{list}->[$i]->{main}->{temp} - 273.15 ) + sprintf( + "%.1f", + ( + $data->{list}->[$i]->{main}->{temp} + - 273.15 + ) ) + 0.5 ), 'temp_c' => int( - sprintf( "%.1f", ( $data->{list}->[$i]->{main}->{temp} - 273.15 ) + sprintf( + "%.1f", + ( + $data->{list}->[$i]->{main}->{temp} + - 273.15 + ) ) + 0.5 ), 'low_c' => int( - sprintf( "%.1f", ( $data->{list}->[$i]->{main}->{temp_min} - 273.15 ) + 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 ) + sprintf( + "%.1f", + ( + $data->{list}->[$i]->{main} + ->{temp_max} - 273.15 + ) ) + 0.5 ), 'tempLow' => int( - sprintf( "%.1f", ( $data->{list}->[$i]->{main}->{temp_min} - 273.15 ) + sprintf( + "%.1f", + ( + $data->{list}->[$i]->{main} + ->{temp_min} - 273.15 + ) ) + 0.5 ), 'tempHigh' => int( - sprintf( "%.1f", ( $data->{list}->[$i]->{main}->{temp_max} - 273.15 ) + 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 + 'humidity' => + $data->{list}->[$i]->{main}->{humidity}, + 'condition' => encode_utf8( + $data->{list}->[$i]->{weather}->[0] + ->{description} ), - 'wind' => int(sprintf( "%.1f", - $data->{list}->[$i]->{wind}->{speed} - ) + 0.5 + 'pressure' => int( + sprintf( "%.1f", + $data->{list}->[$i]->{main}->{pressure} + ) + 0.5 ), - 'wind_speed' => int(sprintf( "%.1f", - $data->{list}->[$i]->{wind}->{speed} - ) + 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}, + 'pubDate' => strftime( + "%a,%e %b %Y %H:%M %p", + localtime( + ( $data->{list}->[$i]->{dt} ) - 3600 + ) ), - 'cloudCover' => $data->{list}->[$i]->{clouds}->{all}, - 'code' => $codes{ $data->{list}->[$i]->{weather}->[0]->{id} }, - 'iconAPI' => $data->{list}->[$i]->{weather}->[0]->{icon}, - 'pubDate' => - strftime( "%a,%e %b %Y %H:%M %p", localtime( ($data->{list}->[$i]->{dt}) - 3600 ) ), }, ); - + $i++; } } } } } - + $self->{endpoint} = 'none' if ( $self->{endpoint} eq 'forecast' ); _RetrieveDataFromOpenWeatherMap($self) @@ -379,7 +442,8 @@ sub _ProcessingRetrieveData($$) { sub _CallWeatherCallbackFn($) { my $self = shift; -# print 'Dumperausgabe: ' . Dumper $self; + + # print 'Dumperausgabe: ' . Dumper $self; ### Aufruf der callbackFn main::Weather_RetrieveCallbackFn( $self->{devName} ); } @@ -388,8 +452,8 @@ sub _ErrorHandling($$) { my ( $self, $err ) = @_; $self->{cached}->{current_date_time} = - strftime( "%a,%e %b %Y %H:%M %p", localtime( $self->{fetchTime} ) ), - $self->{cached}->{status} = $err; + strftime( "%a,%e %b %Y %H:%M %p", localtime( $self->{fetchTime} ) ), + $self->{cached}->{status} = $err; $self->{cached}->{validity} = 'stale'; }