From 46cc10e80e568112a315ffca635a150d6a876cc8 Mon Sep 17 00:00:00 2001 From: Marko Oldenburg Date: Wed, 14 Dec 2022 10:38:04 +0100 Subject: [PATCH] fix forecast exclude and change commandref --- 59_Weather.pm | 84 ++++++++++++++++++++++++++++++++------------ OpenWeatherMapAPI.pm | 10 +++--- 2 files changed, 66 insertions(+), 28 deletions(-) diff --git a/59_Weather.pm b/59_Weather.pm index 0969f91..ff30d3a 100755 --- a/59_Weather.pm +++ b/59_Weather.pm @@ -331,12 +331,21 @@ sub Weather_ReturnWithError { sub Weather_DeleteReadings { my $hash = shift; - my $name = $hash->{NAME}; - my $delReadingRegEx; + my $name = $hash->{NAME}; + my $forecastConfig = Weather_ForcastConfig($hash); + my $forecastLimit = AttrVal( $name, 'forecastLimit', 5 ); + my $forecastLimitNoForecast = 1; - CommandDeleteReading( undef, $name . ' .?(ASC)_.*' ); - CommandDeleteReading( undef, $name . ' ' . $delReadingRegEx ); + $forecastLimit = $forecastLimitNoForecast + if ( !$forecastConfig->{daily} ); + CommandDeleteReading( undef, + $name . ' ' . 'fc[' . $forecastLimit . '-99]_.*' ); + + $forecastLimit = $forecastLimitNoForecast + if ( !$forecastConfig->{hourly} ); + CommandDeleteReading( undef, + $name . ' ' . 'hfc[' . $forecastLimit . '-99]_.*' ); return; } @@ -360,14 +369,28 @@ sub Weather_RetrieveCallbackFn { return; } +sub Weather_ForcastConfig { + my $hash = shift; + + my $name = $hash->{NAME}; + my %forecastConfig; + + $forecastConfig{hourly} = + ( AttrVal( $name, 'forecast', '' ) =~ m{hourly}xms ? 1 : 0 ); + $forecastConfig{daily} = + ( AttrVal( $name, 'forecast', '' ) =~ m{daily}xms ? 1 : 0 ); + $forecastConfig{alerts} = + ( AttrVal( $name, 'forecast', '' ) =~ m{alerts}xms ? 1 : 0 ); + + return \%forecastConfig; +} + sub Weather_WriteReadings { my $hash = shift; my $dataRef = shift; - my $name = $hash->{NAME}; - my $hourly = ( AttrVal( $name, 'forecast', '' ) =~ m{hourly}xms ? 1 : 0 ); - my $daily = ( AttrVal( $name, 'forecast', '' ) =~ m{daily}xms ? 1 : 0 ); - my $alerts = ( AttrVal( $name, 'forecast', '' ) =~ m{alerts}xms ? 1 : 0 ); + my $forecastConfig = Weather_ForcastConfig($hash); + my $name = $hash->{NAME}; readingsBeginUpdate($hash); @@ -410,16 +433,16 @@ sub Weather_WriteReadings { ### forecast if ( ref( $dataRef->{forecast} ) eq 'HASH' - && ( $hourly || $daily ) ) + && ( $forecastConfig->{hourly} || $forecastConfig->{daily} ) ) { ## hourly if ( defined( $dataRef->{forecast}->{hourly} ) && ref( $dataRef->{forecast}->{hourly} ) eq 'ARRAY' && scalar( @{ $dataRef->{forecast}->{hourly} } ) > 0 - && $hourly ) + && $forecastConfig->{hourly} ) { my $i = 0; - my $limit = AttrVal( $name, 'forecastLimit', -1 ); + my $limit = AttrVal( $name, 'forecastLimit', 5 ); foreach my $fc ( @{ $dataRef->{forecast}->{hourly} } ) { $i++; my $f = "hfc" . $i . "_"; @@ -469,10 +492,10 @@ sub Weather_WriteReadings { if ( defined( $dataRef->{forecast}->{daily} ) && ref( $dataRef->{forecast}->{daily} ) eq 'ARRAY' && scalar( @{ $dataRef->{forecast}->{daily} } ) > 0 - && $daily ) + && $forecastConfig->{daily} ) { my $i = 0; - my $limit = AttrVal( $name, 'forecastLimit', -1 ); + my $limit = AttrVal( $name, 'forecastLimit', 5 ); foreach my $fc ( @{ $dataRef->{forecast}->{daily} } ) { $i++; my $f = "fc" . $i . "_"; @@ -519,7 +542,7 @@ sub Weather_WriteReadings { } if ( ref( $dataRef->{alerts} ) eq 'HASH' - && $alerts ) + && $forecastConfig->{alerts} ) { while ( my ( $r, $v ) = each %{ $dataRef->{alerts} } ) { readingsBulkUpdate( $hash, $r, $v ) @@ -751,8 +774,6 @@ sub Weather_Define { location => $hash->{fhem}->{LOCATION}, apioptions => $hash->{APIOPTIONS}, language => $hash->{LANG}, - forecast => AttrVal( $name, 'forecast', '' ), - alerts => AttrVal( $name, 'alerts', 0 ) } ); @@ -780,16 +801,26 @@ sub Weather_Attr { if ( $cmd eq 'set' ) { $hash->{fhem}->{api}->setForecast($attrVal); } + elsif ( $cmd eq 'del' ) { + $hash->{fhem}->{api}->setForecast(); + } - $hash->{fhem}->{api}->setForecast(); + InternalTimer( gettimeofday() + 1, \&Weather_DeleteReadings, + $hash ); + } + + when ('forecastLimit') { + InternalTimer( gettimeofday() + 1, \&Weather_DeleteReadings, + $hash ); } when ('alerts') { if ( $cmd eq 'set' ) { $hash->{fhem}->{api}->setAlerts($attrVal); } - - $hash->{fhem}->{api}->setAlerts(); + elsif ( $cmd eq 'del' ) { + $hash->{fhem}->{api}->setAlerts(); + } } } @@ -1119,7 +1150,9 @@ sub WeatherCheckOptions { + in seconds to retrieve the forecast from the cache instead from the API + + sondern aus dem Cache zurück geliefert wird. +
APIOpenWeatherMapAPI
apioptionscachemaxage:<cachemaxage>
duration - in seconds to retrieve the forecast from the cache instead from the API
version:<version> API version which should be used. + 2.5 by default, 3.0 is still possible but only with an additional subscription
location<latitude,longitude>
geographic coordinates in degrees of the location for which the weather is forecast; if missing, the values of the attributes @@ -1229,8 +1262,9 @@ sub WeatherCheckOptions {
  • disable: disables the retrieval of weather data - the timer runs according to schedule, though no data is requested from the API.
  • readingFnAttributes
  • -
  • forecast - every/hourly/daily/off, show of forecast data. All, only hour forecast, only day forecast, none.
  • +
  • forecast - hourly/daily, display of forecast data.
  • forecastLimit - Number of forecast data records which should be written as a reading.
  • +
  • alerts - 0/1 should alert messages be written similar to Unwetterwarnung

  • @@ -1314,7 +1348,9 @@ sub WeatherCheckOptions {
    APIOpenWeatherMapAPI
    apioptionscachemaxage:<cachemaxage> Zeitdauer in Sekunden, innerhalb derer die Wettervorhersage nicht neu abgerufen - sondern aus dem Cache zurück geliefert wird.
    version:<version> API Version welche verwendet werden soll. + Per Default 2.5, möglich ist noch 3.0 aber nur mit Zusatzsubscription
    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 @@ -1426,8 +1462,10 @@ sub WeatherCheckOptions { gemäß Plan doch es werden keine Daten vom API angefordert.
  • readingFnAttributes
  • -
  • forecast - every/hourly/daily/off, Anzeige von forecast Daten. Alle, nur Stundenforecast, nur Tageforecast, keine.
  • +
  • forecast - hourly/daily, Anzeige von forecast Daten.
  • forecastLimit - Anzahl der Forecast-Datensätze welche als Reading geschrieben werden sollen.
  • +
  • alerts - 0/1 Sollen Alert Meldungen änlich Unwetterwarnung geschrieben werden.
  • +
    diff --git a/OpenWeatherMapAPI.pm b/OpenWeatherMapAPI.pm index 1873aba..a54576d 100644 --- a/OpenWeatherMapAPI.pm +++ b/OpenWeatherMapAPI.pm @@ -195,8 +195,8 @@ sub new { long => ( split( ',', $argsRef->{location} ) )[1], fetchTime => 0, endpoint => 'none', - forecast => $argsRef->{forecast}, - alerts => $argsRef->{alerts}, + forecast => '', + alerts => 0, }; $self->{cachemaxage} = ( @@ -345,7 +345,7 @@ sub _RetrieveDataFromOpenWeatherMap { . 'metric' . '&' . 'lang=' . $self->{lang} . '&' . 'exclude=' - . _createExcludeString( $self->{forecast}, $self->{alerts} ); + . _CreateExcludeString( $self->{forecast}, $self->{alerts} ); ::HttpUtils_NonblockingGet($paramRef); } @@ -353,7 +353,7 @@ sub _RetrieveDataFromOpenWeatherMap { return; } -sub _createExcludeString { +sub _CreateExcludeString { my $forecast = shift; my $alerts = shift; @@ -364,7 +364,7 @@ sub _createExcludeString { my %in_forecast = map { $_ => 1 } @forecast, @alerts; my @diff = grep { not $in_forecast{$_} } @exclude; - return join( ',', @alerts ); + return join( ',', @diff ); } sub _RetrieveDataFinished {