From 9915a9ea05515e2d9230314651aadf09f249d688 Mon Sep 17 00:00:00 2001 From: LeonGaultier Date: Tue, 19 Mar 2019 20:39:44 +0000 Subject: [PATCH] DarkSkyAPI: add extend=hourly to apioption git-svn-id: https://svn.fhem.de/fhem/trunk@18973 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/59_Weather.pm | 6 ++++-- fhem/FHEM/DarkSkyAPI.pm | 37 ++++++++++++++++++++++++---------- fhem/FHEM/OpenWeatherMapAPI.pm | 30 ++++++++++++++++++--------- 4 files changed, 51 insertions(+), 23 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index dd9a2a7f8..324502525 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. + - feature: DarkSkyAPI: add extend=hourly to apioption - feature: 93_DbRep: allow PRAGMA leading an SQLIte SQL-Statement in sqlCmd - bugfix/feature: 59_Weather/API's: fix weblink bug, add extended hourly data format diff --git a/fhem/FHEM/59_Weather.pm b/fhem/FHEM/59_Weather.pm index a09bc5d1d..c6e1d5e61 100755 --- a/fhem/FHEM/59_Weather.pm +++ b/fhem/FHEM/59_Weather.pm @@ -977,7 +977,8 @@ sub WeatherCheckOptions($@) { + in seconds to retrieve the forecast from the cache instead from the API
extend=hourly +
extends the number of hours forecast records to 149 + sondern aus dem Cache zurück geliefert wird.
extend=hourly +
erweitert die Anzahl der Datensätze für die Stundenvorhersage auf 149
APIDarkSkyAPI
apioptionscachemaxage=<cachemaxage>
duration - in seconds to retrieve the forecast from the cache instead from the API
location<latitude,longitude>
geographic coordinates in degrees of the location for which the weather is forecast; if missing, the values of the attributes @@ -1144,7 +1145,8 @@ sub WeatherCheckOptions($@) {
APIDarkSkyAPI
apioptionscachemaxage=<cachemaxage>
Zeitdauer in Sekunden, innerhalb derer die Wettervorhersage nicht neu abgerufen - sondern aus dem Cache zurück geliefert wird.
location<latitude,longitude>
Geographische Breite und Länge des Ortes in Grad, für den das Wetter vorhergesagt wird. Bei fehlender Angabe werden die Werte aus den gleichnamigen Attributen diff --git a/fhem/FHEM/DarkSkyAPI.pm b/fhem/FHEM/DarkSkyAPI.pm index f7b1715eb..a82a8289a 100644 --- a/fhem/FHEM/DarkSkyAPI.pm +++ b/fhem/FHEM/DarkSkyAPI.pm @@ -48,7 +48,7 @@ use constant DEMODATA => '{"latitude":50.112,"longitude":8.686,"timezone":"Europ use constant URL => 'https://api.darksky.net/forecast/'; -use constant VERSION => '0.2.8'; +use constant VERSION => '0.2.9'; my %codes = ( 'clear-day' => 32, @@ -69,6 +69,7 @@ my %codes = ( sub new { ### geliefert wird ein Hash my ( $class, $argsRef ) = @_; + my $apioptions = parseApiOptions($argsRef->{apioptions}); my $self = { devName => $argsRef->{devName}, @@ -77,27 +78,38 @@ sub new { ? $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->{cachemaxage} = ( defined($apioptions->{cachemaxage}) ? $apioptions->{cachemaxage} : 900 ); + $self->{extend} = ( defined($apioptions->{extend}) ? $apioptions->{extend} : 'none' ); $self->{cached} = _CreateForecastRef($self); bless $self, $class; + return $self; } +sub parseApiOptions($) { + my $apioptions = shift; + + my @params; + my %h; + + @params = split(',',$apioptions); + while (@params) { + my $param = shift(@params); + next if($param eq ''); + my ($key, $value) = split(':', $param, 2 ); + $h{$key} = $value; + } + + return \%h; +} + sub setFetchTime { my $self = shift; @@ -159,6 +171,9 @@ sub _RetrieveDataFromDarkSky($) { if ($missingModul); } else { + my $options = '&units=auto'; + $options .= '&extend=' . $self->{extend} if ( $self->{extend} ne 'none' ); + $paramRef->{url} = URL . $self->{key} . '/' @@ -166,7 +181,7 @@ sub _RetrieveDataFromDarkSky($) { . $self->{long} . '?lang=' . $self->{lang} - . '&units=auto&extend=hourly'; + . $options; if ( lc($self->{key}) eq 'demo' ) { _RetrieveDataFinished($paramRef,undef,DEMODATA); } diff --git a/fhem/FHEM/OpenWeatherMapAPI.pm b/fhem/FHEM/OpenWeatherMapAPI.pm index 1f3c79a7b..ca2d760fa 100644 --- a/fhem/FHEM/OpenWeatherMapAPI.pm +++ b/fhem/FHEM/OpenWeatherMapAPI.pm @@ -48,7 +48,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 VERSION => '0.2.4'; +use constant VERSION => '0.2.5'; ## URL . 'weather?' for current data ## URL . 'forecast?' for forecast data @@ -112,6 +112,7 @@ my %codes = ( sub new { ### geliefert wird ein Hash my ( $class, $argsRef ) = @_; + my $apioptions = parseApiOptions($argsRef->{apioptions}); my $self = { devName => $argsRef->{devName}, @@ -120,15 +121,6 @@ sub new { ? $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], @@ -136,12 +128,30 @@ sub new { endpoint => 'none', }; + $self->{cachemaxage} = ( defined($apioptions->{cachemaxage}) ? $apioptions->{cachemaxage} : 900 ); $self->{cached} = _CreateForecastRef($self); bless $self, $class; return $self; } +sub parseApiOptions($) { + my $apioptions = shift; + + my @params; + my %h; + + @params = split(',',$apioptions); + while (@params) { + my $param = shift(@params); + next if($param eq ''); + my ($key, $value) = split(':', $param, 2 ); + $h{$key} = $value; + } + + return \%h; +} + sub setFetchTime { my $self = shift;