2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-03 16:56:54 +00:00

OpenWeatherMapAPI: fix no visibility is available

git-svn-id: https://svn.fhem.de/fhem/trunk@21777 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
LeonGaultier 2020-04-25 23:53:13 +00:00
parent f758136372
commit 9c04ac3eb8
2 changed files with 28 additions and 27 deletions

View File

@ -1,5 +1,6 @@
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # 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. # 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, - feature: 57_SSCal: set compatibility to Calendar package 2.3.4-0631,
some changes according to PBP some changes according to PBP
- change: 73_GardenaSmartBridge: change state value to Connected - change: 73_GardenaSmartBridge: change state value to Connected

View File

@ -36,7 +36,6 @@ package OpenWeatherMapAPI;
use strict; use strict;
use warnings; use warnings;
use FHEM::Meta; use FHEM::Meta;
use Data::Dumper;
FHEM::Meta::Load(__PACKAGE__); FHEM::Meta::Load(__PACKAGE__);
use version 0.50; our $VERSION = $main::packages{OpenWeatherMapAPI}{META}{version}; use version 0.50; our $VERSION = $main::packages{OpenWeatherMapAPI}{META}{version};
@ -48,6 +47,8 @@ use warnings;
use POSIX; use POSIX;
use HttpUtils; use HttpUtils;
# use Data::Dumper;
# try to use JSON::MaybeXS wrapper # try to use JSON::MaybeXS wrapper
# for chance of better performance + open code # for chance of better performance + open code
eval { eval {
@ -124,7 +125,7 @@ eval "use Encode qw(encode_utf8);1" or $missingModul .= "Encode ";
# use Data::Dumper; # for Debug only # use Data::Dumper; # for Debug only
## API URL ## 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 . 'weather?' for current data
## URL . 'forecast?' for forecast data ## URL . 'forecast?' for forecast data
@ -207,7 +208,8 @@ sub new {
$self->{cachemaxage} = ( $self->{cachemaxage} = (
defined( $apioptions->{cachemaxage} ) defined( $apioptions->{cachemaxage} )
? $apioptions->{cachemaxage} ? $apioptions->{cachemaxage}
: 900 ); : 900
);
$self->{cached} = _CreateForecastRef($self); $self->{cached} = _CreateForecastRef($self);
bless $self, $class; bless $self, $class;
@ -246,10 +248,10 @@ sub setRetrieveData {
} }
sub setLocation { sub setLocation {
my ($self,$lat,$long) = @_; my ( $self, $lat, $long ) = @_;
$self->{lat} = $lat; $self->{lat} = $lat;
$self->{long} = $long; $self->{long} = $long;
return 0; return 0;
} }
@ -270,15 +272,14 @@ sub _RetrieveDataFromOpenWeatherMap {
my $self = shift; my $self = shift;
# retrieve data from cache # 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}->{lat} == $self->{lat}
and $self->{cached}->{long} == $self->{long} and $self->{cached}->{long} == $self->{long} )
)
{ {
return _CallWeatherCallbackFn($self); return _CallWeatherCallbackFn($self);
} }
$self->{cached}->{lat} = $self->{lat} $self->{cached}->{lat} = $self->{lat}
unless ( $self->{cached}->{lat} == $self->{lat} ); unless ( $self->{cached}->{lat} == $self->{lat} );
$self->{cached}->{long} = $self->{long} $self->{cached}->{long} = $self->{long}
unless ( $self->{cached}->{long} == $self->{long} ); unless ( $self->{cached}->{long} == $self->{long} );
@ -327,10 +328,10 @@ sub _RetrieveDataFromOpenWeatherMap {
} }
sub _RetrieveDataFinished { sub _RetrieveDataFinished {
my $paramRef = shift; my $paramRef = shift;
my $err = shift; my $err = shift;
my $response = shift; my $response = shift;
my $self = $paramRef->{self}; my $self = $paramRef->{self};
if ( !$err ) { if ( !$err ) {
$self->{cached}->{status} = 'ok'; $self->{cached}->{status} = 'ok';
@ -345,8 +346,8 @@ sub _RetrieveDataFinished {
} }
sub _ProcessingRetrieveData { sub _ProcessingRetrieveData {
my $self = shift; my $self = shift;
my $response = shift; my $response = shift;
if ( $self->{cached}->{status} eq 'ok' if ( $self->{cached}->{status} eq 'ok'
and defined($response) and defined($response)
@ -424,8 +425,6 @@ sub _ProcessingRetrieveData {
), ),
'wind_direction' => $data->{wind}->{deg}, 'wind_direction' => $data->{wind}->{deg},
'cloudCover' => $data->{clouds}->{all}, 'cloudCover' => $data->{clouds}->{all},
'visibility' =>
int( sprintf( "%.1f", $data->{visibility} ) + 0.5 ),
'code' => $codes{ $data->{weather}->[0]->{id} }, 'code' => $codes{ $data->{weather}->[0]->{id} },
'iconAPI' => $data->{weather}->[0]->{icon}, 'iconAPI' => $data->{weather}->[0]->{icon},
'sunsetTime' => strftimeWrapper( 'sunsetTime' => strftimeWrapper(
@ -438,12 +437,13 @@ sub _ProcessingRetrieveData {
), ),
'pubDate' => strftimeWrapper( 'pubDate' => strftimeWrapper(
"%a, %e %b %Y %H:%M", "%a, %e %b %Y %H:%M",
( exists $data->{dt} localtime( $data->{dt} )
? localtime( $data->{dt} )
: localtime( time )
)
), ),
}; };
$self->{cached}->{current}->{'visibility'} =
int( sprintf( "%.1f", $data->{visibility} ) + 0.5 )
if ( exists $data->{visibility} );
} }
if ( $self->{endpoint} eq 'forecast' ) { if ( $self->{endpoint} eq 'forecast' ) {
@ -599,8 +599,8 @@ sub _CallWeatherCallbackFn {
} }
sub _ErrorHandling { sub _ErrorHandling {
my $self = shift; my $self = shift;
my $err = shift; my $err = shift;
$self->{cached}->{current_date_time} = $self->{cached}->{current_date_time} =
strftimeWrapper( "%a, %e %b %Y %H:%M", localtime( $self->{fetchTime} ) ), strftimeWrapper( "%a, %e %b %Y %H:%M", localtime( $self->{fetchTime} ) ),
@ -617,7 +617,8 @@ sub _CreateForecastRef {
long => $self->{long}, long => $self->{long},
apiMaintainer => apiMaintainer =>
'Leon Gaultier (<a href=https://forum.fhem.de/index.php?action=profile;u=13684>CoolTux</a>)', 'Leon Gaultier (<a href=https://forum.fhem.de/index.php?action=profile;u=13684>CoolTux</a>)',
apiVersion => version->parse(OpenWeatherMapAPI->VERSION())->normal, apiVersion =>
version->parse( OpenWeatherMapAPI->VERSION() )->normal,
} }
); );
@ -647,7 +648,6 @@ sub strftimeWrapper {
1; 1;
=pod =pod
=encoding utf8 =encoding utf8
@ -660,7 +660,7 @@ sub strftimeWrapper {
"abstract": "Wetter API für OpenWeatherMap" "abstract": "Wetter API für OpenWeatherMap"
} }
}, },
"version": "v1.0.1", "version": "v1.0.2",
"author": [ "author": [
"Marko Oldenburg <leongaultier@gmail.com>" "Marko Oldenburg <leongaultier@gmail.com>"
], ],