diff --git a/59_Weather.pm b/59_Weather.pm index 642af58..d0544e1 100755 --- a/59_Weather.pm +++ b/59_Weather.pm @@ -571,8 +571,19 @@ sub Weather_Set($@) { Weather_GetUpdate($hash); return undef; } + elsif ( ( @a == 3 ) && ( $a[1] eq "newLocation" ) ) { + if ( $hash->{API} eq 'DarkSkyAPI' + or $hash->{API} eq 'OpenWeatherMapAPI' ) + { + $hash->{fhem}->{api}->setLocation((split(':',$a[2]))[0],(split(':',$a[2]))[1]); + Weather_DisarmTimer($hash); + Weather_GetUpdate($hash); + return undef; + } + else { return 'this API is not ' . $a[1] .' supported' } + } else { - return "Unknown argument $cmd, choose one of update:noArg"; + return "Unknown argument $cmd, choose one of update:noArg newLocation"; } } diff --git a/DarkSkyAPI.pm b/DarkSkyAPI.pm index f8da078..b556131 100644 --- a/DarkSkyAPI.pm +++ b/DarkSkyAPI.pm @@ -195,6 +195,15 @@ sub setRetrieveData { return 0; } +sub setLocation { + my ($self,$lat,$long) = @_; + + $self->{lat} = $lat; + $self->{long} = $long; + + return 0; +} + sub getFetchTime { my $self = shift; @@ -211,10 +220,19 @@ sub _RetrieveDataFromDarkSky($) { 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} + ) + { return _CallWeatherCallbackFn($self); } + $self->{cached}->{lat} = $self->{lat} + unless ( $self->{cached}->{lat} == $self->{lat} ); + $self->{cached}->{long} = $self{long} + unless ( $self->{cached}->{long} == $self->{long} ); + my $paramRef = { timeout => 15, self => $self, diff --git a/OpenWeatherMapAPI.pm b/OpenWeatherMapAPI.pm index 51acbf6..913c203 100644 --- a/OpenWeatherMapAPI.pm +++ b/OpenWeatherMapAPI.pm @@ -116,7 +116,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.4.0'; +use constant VERSION => '0.5.0'; ## URL . 'weather?' for current data ## URL . 'forecast?' for forecast data @@ -237,6 +237,15 @@ sub setRetrieveData { return 0; } +sub setLocation { + my ($self,$lat,$long) = @_; + + $self->{lat} = $lat; + $self->{long} = $long; + + return 0; +} + sub getFetchTime { my $self = shift; @@ -253,12 +262,19 @@ sub _RetrieveDataFromOpenWeatherMap($) { my $self = shift; # retrieve data from cache - if ( $self->{endpoint} eq 'none' ) { - if ( ( time() - $self->{fetchTime} ) < $self->{cachemaxage} ) { - return _CallWeatherCallbackFn($self); - } + if ( ( time() - $self->{fetchTime} ) < $self->{cachemaxage} + and $self->{cached}->{lat} == $self->{lat} + and $self->{cached}->{long} == $self->{long} + ) + { + return _CallWeatherCallbackFn($self); } + $self->{cached}->{lat} = $self->{lat} + unless ( $self->{cached}->{lat} == $self->{lat} ); + $self->{cached}->{long} = $self->{long} + unless ( $self->{cached}->{long} == $self->{long} ); + my $paramRef = { timeout => 15, self => $self,