From 9c04ac3eb83fdb6c515c5501b121424617c6cc01 Mon Sep 17 00:00:00 2001 From: LeonGaultier Date: Sat, 25 Apr 2020 23:53:13 +0000 Subject: [PATCH] OpenWeatherMapAPI: fix no visibility is available git-svn-id: https://svn.fhem.de/fhem/trunk@21777 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/OpenWeatherMapAPI.pm | 54 +++++++++++++++++----------------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index 8cb329d62..179ac36bf 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,6 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. + - bugfix: OpenWeatherMapAPI: fix no visibility is available - feature: 57_SSCal: set compatibility to Calendar package 2.3.4-0631, some changes according to PBP - change: 73_GardenaSmartBridge: change state value to Connected diff --git a/fhem/FHEM/OpenWeatherMapAPI.pm b/fhem/FHEM/OpenWeatherMapAPI.pm index 02e3fc3c3..df3ba061b 100644 --- a/fhem/FHEM/OpenWeatherMapAPI.pm +++ b/fhem/FHEM/OpenWeatherMapAPI.pm @@ -36,7 +36,6 @@ package OpenWeatherMapAPI; use strict; use warnings; use FHEM::Meta; -use Data::Dumper; FHEM::Meta::Load(__PACKAGE__); use version 0.50; our $VERSION = $main::packages{OpenWeatherMapAPI}{META}{version}; @@ -48,6 +47,8 @@ use warnings; use POSIX; use HttpUtils; +# use Data::Dumper; + # try to use JSON::MaybeXS wrapper # for chance of better performance + open code eval { @@ -124,7 +125,7 @@ eval "use Encode qw(encode_utf8);1" or $missingModul .= "Encode "; # use Data::Dumper; # for Debug only ## API URL -use constant URL => 'https://api.openweathermap.org/data/2.5/'; +use constant URL => 'https://api.openweathermap.org/data/2.5/'; ## URL . 'weather?' for current data ## URL . 'forecast?' for forecast data @@ -207,7 +208,8 @@ sub new { $self->{cachemaxage} = ( defined( $apioptions->{cachemaxage} ) ? $apioptions->{cachemaxage} - : 900 ); + : 900 + ); $self->{cached} = _CreateForecastRef($self); bless $self, $class; @@ -246,10 +248,10 @@ sub setRetrieveData { } sub setLocation { - my ($self,$lat,$long) = @_; + my ( $self, $lat, $long ) = @_; - $self->{lat} = $lat; - $self->{long} = $long; + $self->{lat} = $lat; + $self->{long} = $long; return 0; } @@ -270,15 +272,14 @@ sub _RetrieveDataFromOpenWeatherMap { my $self = shift; # retrieve data from cache - if ( ( time() - $self->{fetchTime} ) < $self->{cachemaxage} + if ( ( time() - $self->{fetchTime} ) < $self->{cachemaxage} and $self->{cached}->{lat} == $self->{lat} - and $self->{cached}->{long} == $self->{long} - ) + and $self->{cached}->{long} == $self->{long} ) { return _CallWeatherCallbackFn($self); } - $self->{cached}->{lat} = $self->{lat} + $self->{cached}->{lat} = $self->{lat} unless ( $self->{cached}->{lat} == $self->{lat} ); $self->{cached}->{long} = $self->{long} unless ( $self->{cached}->{long} == $self->{long} ); @@ -327,10 +328,10 @@ sub _RetrieveDataFromOpenWeatherMap { } sub _RetrieveDataFinished { - my $paramRef = shift; - my $err = shift; - my $response = shift; - my $self = $paramRef->{self}; + my $paramRef = shift; + my $err = shift; + my $response = shift; + my $self = $paramRef->{self}; if ( !$err ) { $self->{cached}->{status} = 'ok'; @@ -345,8 +346,8 @@ sub _RetrieveDataFinished { } sub _ProcessingRetrieveData { - my $self = shift; - my $response = shift; + my $self = shift; + my $response = shift; if ( $self->{cached}->{status} eq 'ok' and defined($response) @@ -424,8 +425,6 @@ sub _ProcessingRetrieveData { ), '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' => strftimeWrapper( @@ -438,12 +437,13 @@ sub _ProcessingRetrieveData { ), 'pubDate' => strftimeWrapper( "%a, %e %b %Y %H:%M", - ( exists $data->{dt} - ? localtime( $data->{dt} ) - : localtime( time ) - ) + localtime( $data->{dt} ) ), }; + + $self->{cached}->{current}->{'visibility'} = + int( sprintf( "%.1f", $data->{visibility} ) + 0.5 ) + if ( exists $data->{visibility} ); } if ( $self->{endpoint} eq 'forecast' ) { @@ -599,8 +599,8 @@ sub _CallWeatherCallbackFn { } sub _ErrorHandling { - my $self = shift; - my $err = shift; + my $self = shift; + my $err = shift; $self->{cached}->{current_date_time} = strftimeWrapper( "%a, %e %b %Y %H:%M", localtime( $self->{fetchTime} ) ), @@ -617,7 +617,8 @@ sub _CreateForecastRef { long => $self->{long}, apiMaintainer => 'Leon Gaultier (CoolTux)', - apiVersion => version->parse(OpenWeatherMapAPI->VERSION())->normal, + apiVersion => + version->parse( OpenWeatherMapAPI->VERSION() )->normal, } ); @@ -647,7 +648,6 @@ sub strftimeWrapper { 1; - =pod =encoding utf8 @@ -660,7 +660,7 @@ sub strftimeWrapper { "abstract": "Wetter API für OpenWeatherMap" } }, - "version": "v1.0.1", + "version": "v1.0.2", "author": [ "Marko Oldenburg " ],