add sub for apioptions, add darksky api extend option

This commit is contained in:
Marko Oldenburg 2019-03-19 21:35:59 +01:00
parent f7bbcd46e7
commit 06f0167d79
3 changed files with 50 additions and 23 deletions

View File

@ -977,7 +977,8 @@ sub WeatherCheckOptions($@) {
<table border="1">
<tr><td>API</td><td><code>DarkSkyAPI</code></td></tr>
<tr><td>apioptions</td><td><code>cachemaxage=&lt;cachemaxage&gt;</code><br>duration
in seconds to retrieve the forecast from the cache instead from the API</td></tr>
in seconds to retrieve the forecast from the cache instead from the API<br><code>extend=hourly</code>
<br>extends the number of hours forecast records to 149</td></tr>
<tr><td>location</td><td><code>&lt;latitude,longitude&gt;</code><br>
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($@) {
<tr><td>API</td><td><code>DarkSkyAPI</code></td></tr>
<tr><td>apioptions</td><td><code>cachemaxage=&lt;cachemaxage&gt;</code><br>Zeitdauer in
Sekunden, innerhalb derer die Wettervorhersage nicht neu abgerufen
sondern aus dem Cache zur&uuml;ck geliefert wird.</td></tr>
sondern aus dem Cache zur&uuml;ck geliefert wird.<br><code>extend=hourly</code>
<br>erweitert die Anzahl der Datens&auml;tze f&uuml;r die Stundenvorhersage auf 149</td></tr>
<tr><td>location</td><td><code>&lt;latitude,longitude&gt;</code><br> Geographische Breite
und L&auml;nge des Ortes in Grad, f&uuml;r den das Wetter vorhergesagt wird.
Bei fehlender Angabe werden die Werte aus den gleichnamigen Attributen

View File

@ -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); }

View File

@ -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;