testing #29

Merged
marko merged 73 commits from testing into main 2023-01-07 12:49:01 +00:00
4 changed files with 1076 additions and 660 deletions
Showing only changes of commit 67508e453c - Show all commits

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,9 @@
# $Id: $ # $Id: $
############################################################################### ###############################################################################
# #
# Developed with Kate # Developed with VSCodium and richterger perl plugin
# #
# (c) 2019 Copyright: Marko Oldenburg (leongaultier at gmail dot com) # (c) 2019-2023 Copyright: Marko Oldenburg (fhemdevelopment at cooltux dot net)
# All rights reserved # All rights reserved
# #
# Special thanks goes to: # Special thanks goes to:
@ -157,12 +157,15 @@ sub new {
lat => ( split( ',', $argsRef->{location} ) )[0], lat => ( split( ',', $argsRef->{location} ) )[0],
long => ( split( ',', $argsRef->{location} ) )[1], long => ( split( ',', $argsRef->{location} ) )[1],
fetchTime => 0, fetchTime => 0,
forecast => $argsRef->{forecast},
alerts => $argsRef->{alerts},
}; };
$self->{cachemaxage} = ( $self->{cachemaxage} = (
defined( $apioptions->{cachemaxage} ) defined( $apioptions->{cachemaxage} )
? $apioptions->{cachemaxage} ? $apioptions->{cachemaxage}
: 900 ); : 900
);
$self->{extend} = $self->{extend} =
( defined( $apioptions->{extend} ) ? $apioptions->{extend} : 'none' ); ( defined( $apioptions->{extend} ) ? $apioptions->{extend} : 'none' );
$self->{cached} = _CreateForecastRef($self); $self->{cached} = _CreateForecastRef($self);
@ -189,6 +192,22 @@ sub parseApiOptions($) {
return \%h; return \%h;
} }
sub setAlerts {
my $self = shift;
my $alerts = shift // 0;
$self->{alerts} = $alerts;
return;
}
sub setForecast {
my $self = shift;
my $forecast = shift // '';
$self->{forecast} = $forecast;
return;
}
sub setFetchTime { sub setFetchTime {
my $self = shift; my $self = shift;
@ -204,7 +223,7 @@ 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;
@ -230,8 +249,7 @@ sub _RetrieveDataFromDarkSky($) {
# 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);
} }
@ -468,8 +486,8 @@ sub _ProcessingRetrieveData($$) {
->{apparentTemperatureHighTime} ->{apparentTemperatureHighTime}
) )
), ),
'code' => 'code' => $codes{
$codes{ $data->{daily}->{data}->[$i]->{icon} $data->{daily}->{data}->[$i]->{icon}
}, },
'iconAPI' => 'iconAPI' =>
$data->{daily}->{data}->[$i]->{icon}, $data->{daily}->{data}->[$i]->{icon},
@ -644,9 +662,9 @@ sub _ProcessingRetrieveData($$) {
'temperature' => sprintf( "%.1f", 'temperature' => sprintf( "%.1f",
$data->{hourly}->{data}->[$i] $data->{hourly}->{data}->[$i]
->{temperature} ), ->{temperature} ),
'code' => 'code' => $codes{
$codes{ $data->{hourly}->{data}->[$i] $data->{hourly}->{data}->[$i]->{icon}
->{icon} }, },
'iconAPI' => 'iconAPI' =>
$data->{hourly}->{data}->[$i]->{icon}, $data->{hourly}->{data}->[$i]->{icon},
'condition' => encode_utf8( 'condition' => encode_utf8(
@ -772,7 +790,7 @@ sub _CreateForecastRef($) {
long => $self->{long}, long => $self->{long},
apiMaintainer => apiMaintainer =>
'Marko Oldenburg (<a href=https://forum.fhem.de/index.php?action=profile;u=13684>CoolTux</a>)', 'Marko Oldenburg (<a href=https://forum.fhem.de/index.php?action=profile;u=13684>CoolTux</a>)',
apiVersion => version->parse(DarkSkyAPI->VERSION())->normal, apiVersion => version->parse( DarkSkyAPI->VERSION() )->normal,
} }
); );
@ -802,7 +820,6 @@ sub strftimeWrapper(@) {
1; 1;
=pod =pod
=encoding utf8 =encoding utf8
@ -815,15 +832,15 @@ sub strftimeWrapper(@) {
"abstract": "Wetter API für Weather DarkSky" "abstract": "Wetter API für Weather DarkSky"
} }
}, },
"version": "v1.0.0", "version": "v1.2.0",
"author": [ "author": [
"Marko Oldenburg <leongaultier@gmail.com>" "Marko Oldenburg <fhemdevelopment@cooltux.net>"
], ],
"x_fhem_maintainer": [ "x_fhem_maintainer": [
"CoolTux" "CoolTux"
], ],
"x_fhem_maintainer_github": [ "x_fhem_maintainer_github": [
"LeonGaultier" "CoolTuxNet"
], ],
"prereqs": { "prereqs": {
"runtime": { "runtime": {

View File

@ -1,12 +1,13 @@
# $Id: $ # $Id: $
############################################################################### ###############################################################################
# #
# Developed with Kate # Developed with VSCodium and richterger perl plugin
# #
# (c) 2019 Copyright: Marko Oldenburg (leongaultier at gmail dot com) # (c) 2019-2023 Copyright: Marko Oldenburg (fhemdevelopment at cooltux dot net)
# All rights reserved # All rights reserved
# #
# Special thanks goes to: # Special thanks goes to:
# - Harry (harryman) for many tests and patch that implements onecall API
# #
# #
# This script is free software; you can redistribute it and/or modify # This script is free software; you can redistribute it and/or modify
@ -30,6 +31,7 @@
### Beispielaufruf ### Beispielaufruf
# https://api.openweathermap.org/data/2.5/weather?lat=[lat]&lon=[long]&APPID=[API] Current # https://api.openweathermap.org/data/2.5/weather?lat=[lat]&lon=[long]&APPID=[API] Current
# https://api.openweathermap.org/data/2.5/forecast?lat=[lat]&lon=[long]&APPID=[API] Forecast # https://api.openweathermap.org/data/2.5/forecast?lat=[lat]&lon=[long]&APPID=[API] Forecast
# https://api.openweathermap.org/data/2.5/onecall?lat=[lat]&lon=[long]&APPID=[API] Forecast
# https://openweathermap.org/weather-conditions Icons und Conditions ID's # https://openweathermap.org/weather-conditions Icons und Conditions ID's
package OpenWeatherMapAPI; package OpenWeatherMapAPI;
@ -38,7 +40,7 @@ use warnings;
use FHEM::Meta; use FHEM::Meta;
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 = $::packages{OpenWeatherMapAPI}{META}{version};
package OpenWeatherMapAPI::Weather; package OpenWeatherMapAPI::Weather;
use strict; use strict;
@ -46,6 +48,7 @@ use warnings;
use POSIX; use POSIX;
use HttpUtils; use HttpUtils;
use experimental qw /switch/;
# use Data::Dumper; # use Data::Dumper;
@ -55,15 +58,11 @@ eval {
require JSON::MaybeXS; require JSON::MaybeXS;
import JSON::MaybeXS qw( decode_json encode_json ); import JSON::MaybeXS qw( decode_json encode_json );
1; 1;
}; } or do {
if ($@) {
$@ = undef;
# try to use JSON wrapper # try to use JSON wrapper
# for chance of better performance # for chance of better performance
eval { eval {
# JSON preference order # JSON preference order
local $ENV{PERL_JSON_BACKEND} = local $ENV{PERL_JSON_BACKEND} =
'Cpanel::JSON::XS,JSON::XS,JSON::PP,JSON::backportPP' 'Cpanel::JSON::XS,JSON::XS,JSON::PP,JSON::backportPP'
@ -72,10 +71,7 @@ if ($@) {
require JSON; require JSON;
import JSON qw( decode_json encode_json ); import JSON qw( decode_json encode_json );
1; 1;
}; } or do {
if ($@) {
$@ = undef;
# In rare cases, Cpanel::JSON::XS may # In rare cases, Cpanel::JSON::XS may
# be installed but JSON|JSON::MaybeXS not ... # be installed but JSON|JSON::MaybeXS not ...
@ -83,10 +79,7 @@ if ($@) {
require Cpanel::JSON::XS; require Cpanel::JSON::XS;
import Cpanel::JSON::XS qw(decode_json encode_json); import Cpanel::JSON::XS qw(decode_json encode_json);
1; 1;
}; } or do {
if ($@) {
$@ = undef;
# In rare cases, JSON::XS may # In rare cases, JSON::XS may
# be installed but JSON not ... # be installed but JSON not ...
@ -94,10 +87,7 @@ if ($@) {
require JSON::XS; require JSON::XS;
import JSON::XS qw(decode_json encode_json); import JSON::XS qw(decode_json encode_json);
1; 1;
}; } or do {
if ($@) {
$@ = undef;
# Fallback to built-in JSON which SHOULD # Fallback to built-in JSON which SHOULD
# be available since 5.014 ... # be available since 5.014 ...
@ -105,29 +95,32 @@ if ($@) {
require JSON::PP; require JSON::PP;
import JSON::PP qw(decode_json encode_json); import JSON::PP qw(decode_json encode_json);
1; 1;
}; } or do {
if ($@) {
$@ = undef;
# Fallback to JSON::backportPP in really rare cases # Fallback to JSON::backportPP in really rare cases
require JSON::backportPP; require JSON::backportPP;
import JSON::backportPP qw(decode_json encode_json); import JSON::backportPP qw(decode_json encode_json);
1; 1;
} };
} };
} };
} };
} };
my $missingModul = ''; my $missingModul = '';
eval "use Encode qw(encode_utf8);1" or $missingModul .= "Encode "; ## no critic (Conditional "use" statement. Use "require" to conditionally include a module (Modules::ProhibitConditionalUseStatements))
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/'; eval { use Readonly; 1 }
or $missingModul .= 'Readonly '; # apt install libreadonly-perl
## use critic
# Readonly my $URL => 'https://api.openweathermap.org/data/2.5/';
Readonly my $URL => 'https://api.openweathermap.org/data/';
## URL . 'weather?' for current data ## URL . 'weather?' for current data
## URL . 'forecast?' for forecast data ## URL . 'onecall?' for forecast data
my %codes = ( my %codes = (
200 => 45, 200 => 45,
@ -164,6 +157,7 @@ my %codes = (
602 => 13, 602 => 13,
611 => 46, 611 => 46,
612 => 46, 612 => 46,
613 => 46,
615 => 5, 615 => 5,
616 => 5, 616 => 5,
620 => 14, 620 => 14,
@ -188,13 +182,15 @@ my %codes = (
sub new { sub new {
### geliefert wird ein Hash ### geliefert wird ein Hash
my ( $class, $argsRef ) = @_; my $class = shift;
my $apioptions = parseApiOptions( $argsRef->{apioptions} ); my $argsRef = shift;
my $apioptions = _parseApiOptions( $argsRef->{apioptions} );
my $self = { my $self = {
devName => $argsRef->{devName}, devName => $argsRef->{devName},
key => ( key => (
( defined( $argsRef->{apikey} ) and $argsRef->{apikey} ) ( defined( $argsRef->{apikey} ) && $argsRef->{apikey} )
? $argsRef->{apikey} ? $argsRef->{apikey}
: 'none' : 'none'
), ),
@ -203,6 +199,8 @@ sub new {
long => ( split( ',', $argsRef->{location} ) )[1], long => ( split( ',', $argsRef->{location} ) )[1],
fetchTime => 0, fetchTime => 0,
endpoint => 'none', endpoint => 'none',
forecast => '',
alerts => 0,
}; };
$self->{cachemaxage} = ( $self->{cachemaxage} = (
@ -210,13 +208,17 @@ sub new {
? $apioptions->{cachemaxage} ? $apioptions->{cachemaxage}
: 900 : 900
); );
$self->{apiversion} =
( $apioptions->{version} ? $apioptions->{version} : '2.5' );
$self->{cached} = _CreateForecastRef($self); $self->{cached} = _CreateForecastRef($self);
bless $self, $class; bless $self, $class;
return $self; return $self;
} }
sub parseApiOptions { sub _parseApiOptions {
my $apioptions = shift; my $apioptions = shift;
my @params; my @params;
@ -233,27 +235,45 @@ sub parseApiOptions {
return \%h; return \%h;
} }
sub setAlerts {
my $self = shift;
my $alerts = shift // 0;
$self->{alerts} = $alerts;
return;
}
sub setForecast {
my $self = shift;
my $forecast = shift // '';
$self->{forecast} = $forecast;
return;
}
sub setFetchTime { sub setFetchTime {
my $self = shift; my $self = shift;
$self->{fetchTime} = time(); $self->{fetchTime} = time();
return 0; return;
} }
sub setRetrieveData { sub setRetrieveData {
my $self = shift; my $self = shift;
_RetrieveDataFromOpenWeatherMap($self); _RetrieveDataFromOpenWeatherMap($self);
return 0; return;
} }
sub setLocation { sub setLocation {
my ( $self, $lat, $long ) = @_; my $self = shift;
my $lat = shift;
my $long = shift;
$self->{lat} = $lat; $self->{lat} = $lat;
$self->{long} = $long; $self->{long} = $long;
return 0; return;
} }
sub getFetchTime { sub getFetchTime {
@ -273,8 +293,9 @@ sub _RetrieveDataFromOpenWeatherMap {
# 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} && $self->{cached}->{lat} == $self->{lat}
and $self->{cached}->{long} == $self->{long} ) && $self->{cached}->{long} == $self->{long}
&& $self->{endpoint} eq 'none' )
{ {
return _CallWeatherCallbackFn($self); return _CallWeatherCallbackFn($self);
} }
@ -287,22 +308,22 @@ sub _RetrieveDataFromOpenWeatherMap {
my $paramRef = { my $paramRef = {
timeout => 15, timeout => 15,
self => $self, self => $self,
endpoint => ( $self->{endpoint} eq 'none' ? 'weather' : 'forecast' ), endpoint => $self->{endpoint} eq 'none' ? 'onecall' : 'none',
callback => \&_RetrieveDataFinished, callback => \&_RetrieveDataFinished,
}; };
$self->{endpoint} = $paramRef->{endpoint}; $self->{endpoint} = $paramRef->{endpoint};
if ( $self->{lat} eq 'error' if ( $self->{lat} eq 'error'
or $self->{long} eq 'error' || $self->{long} eq 'error'
or $self->{key} eq 'none' || $self->{key} eq 'none'
or $missingModul ) || $missingModul )
{ {
_RetrieveDataFinished( _RetrieveDataFinished(
$paramRef, $paramRef,
'The given location is invalid. (wrong latitude or longitude?) put both as an attribute in the global device or set define option location=[LAT],[LONG]', 'The given location is invalid. (wrong latitude or longitude?) put both as an attribute in the global device or set define option location=[LAT],[LONG]',
undef undef
) if ( $self->{lat} eq 'error' or $self->{long} eq 'error' ); ) if ( $self->{lat} eq 'error' || $self->{long} eq 'error' );
_RetrieveDataFinished( $paramRef, _RetrieveDataFinished( $paramRef,
'No given api key. (define myWeather Weather apikey=[KEY])', 'No given api key. (define myWeather Weather apikey=[KEY])',
@ -315,16 +336,37 @@ sub _RetrieveDataFromOpenWeatherMap {
} }
else { else {
$paramRef->{url} = $paramRef->{url} =
URL $URL
. $self->{apiversion} . '/'
. $paramRef->{endpoint} . '?' . 'lat=' . $paramRef->{endpoint} . '?' . 'lat='
. $self->{lat} . '&' . 'lon=' . $self->{lat} . '&' . 'lon='
. $self->{long} . '&' . $self->{long} . '&'
. 'APPID=' . 'APPID='
. $self->{key} . '&' . 'lang=' . $self->{key} . '&'
. $self->{lang}; . 'units='
. 'metric' . '&' . 'lang='
. $self->{lang} . '&'
. 'exclude='
. _CreateExcludeString( $self->{forecast}, $self->{alerts} );
main::HttpUtils_NonblockingGet($paramRef); ::HttpUtils_NonblockingGet($paramRef);
} }
return;
}
sub _CreateExcludeString {
my $forecast = shift;
my $alerts = shift;
my @exclude = qw/alerts minutely hourly daily/;
my @forecast = split( ',', $forecast );
my @alerts = ( $alerts ? 'alerts' : '' );
my %in_forecast = map { $_ => 1 } @forecast, @alerts;
my @diff = grep { not $in_forecast{$_} } @exclude;
return join( ',', @diff );
} }
sub _RetrieveDataFinished { sub _RetrieveDataFinished {
@ -335,7 +377,8 @@ sub _RetrieveDataFinished {
if ( !$err ) { if ( !$err ) {
$self->{cached}->{status} = 'ok'; $self->{cached}->{status} = 'ok';
$self->{cached}->{validity} = 'up-to-date', $self->{fetchTime} = time(); $self->{cached}->{validity} = 'up-to-date';
$self->{fetchTime} = time();
_ProcessingRetrieveData( $self, $response ); _ProcessingRetrieveData( $self, $response );
} }
else { else {
@ -343,6 +386,8 @@ sub _RetrieveDataFinished {
_ErrorHandling( $self, $err ); _ErrorHandling( $self, $err );
_ProcessingRetrieveData( $self, $response ); _ProcessingRetrieveData( $self, $response );
} }
return;
} }
sub _ProcessingRetrieveData { sub _ProcessingRetrieveData {
@ -350,201 +395,169 @@ sub _ProcessingRetrieveData {
my $response = shift; my $response = shift;
if ( $self->{cached}->{status} eq 'ok' if ( $self->{cached}->{status} eq 'ok'
and defined($response) && defined($response)
and $response ) && $response )
{ {
if ( $response =~ m/^{.*}$/ ) { if ( $response =~ m/^{.*}$/x ) {
my $data = eval { decode_json($response) }; my $data = eval { decode_json($response) };
if ($@) { if ($@) {
_ErrorHandling( $self, _ErrorHandling( $self,
'OpenWeatherMap Weather decode JSON err ' . $@ ); 'OpenWeatherMap Weather decode JSON err ' . $@ );
} }
elsif ( defined( $data->{cod} ) elsif (defined( $data->{cod} )
and $data->{cod} && $data->{cod}
and $data->{cod} != 200 && $data->{cod} != 200
and defined( $data->{message} ) && defined( $data->{message} )
and $data->{message} ) && $data->{message} )
{ {
_ErrorHandling( $self, $data->{cod} . ': ' . $data->{message} ); _ErrorHandling( $self, $data->{cod} . ': ' . $data->{message} );
} }
else { else {
### Debug ### Debug
# print 'Response: ' . Dumper $data; # print '!!! DEBUG !!! - Endpoint: ' . $self->{endpoint} . "\n";
# print '!!! DEBUG !!! - Response: ' . Dumper $data;
###### Ab hier wird die ResponseHash Referenze für die Rückgabe zusammen gestellt ###### Ab hier wird die ResponseHash Referenze für die Rückgabe zusammen gestellt
$self->{cached}->{current_date_time} = $self->{cached}->{current_date_time} =
strftimeWrapper( "%a, %e %b %Y %H:%M", strftimeWrapper( "%a, %e %b %Y %H:%M",
localtime( $self->{fetchTime} ) ); localtime( $self->{fetchTime} ) );
if ( $self->{endpoint} eq 'weather' ) { given ( $self->{endpoint} ) {
$self->{cached}->{country} = $data->{sys}->{country}; when ('onecall') {
$self->{cached}->{city} = encode_utf8( $data->{name} ); ## löschen des alten current Datensatzes
$self->{cached}->{license}{text} = 'none'; delete $self->{cached}->{current};
## löschen des alten forecast Datensatzes
delete $self->{cached}->{forecast};
## löschen des alten Alerts Datensatzes
delete $self->{cached}->{alerts};
$self->{cached}->{current} = { $self->{cached}->{current} = {
'temperature' => int( 'temperature' => int(
sprintf( "%.1f", sprintf( "%.0f", $data->{current}->{temp} )
( $data->{main}->{temp} - 273.15 ) ) + 0.5
), ),
'temp_c' => int( 'temp_c' => int(
sprintf( "%.1f", sprintf( "%.0f", $data->{current}->{temp} )
( $data->{main}->{temp} - 273.15 ) ) + 0.5
),
'low_c' => int(
sprintf( "%.1f",
( $data->{main}->{temp_min} - 273.15 ) ) + 0.5
),
'high_c' => int(
sprintf( "%.1f",
( $data->{main}->{temp_max} - 273.15 ) ) + 0.5
),
'tempLow' => int(
sprintf( "%.1f",
( $data->{main}->{temp_min} - 273.15 ) ) + 0.5
),
'tempHigh' => int(
sprintf( "%.1f",
( $data->{main}->{temp_max} - 273.15 ) ) + 0.5
), ),
'tempFeelsLike_c' => int( 'tempFeelsLike_c' => int(
sprintf( "%.1f", sprintf( "%.0f",
( $data->{main}->{feels_like} - 273.15 ) ) + 0.5 $data->{current}->{feels_like} )
),
'dew_point' => int(
sprintf(
"%.0f", $data->{current}->{dew_point}
)
),
'humidity' => $data->{current}->{humidity},
'condition' => encode_utf8(
$data->{current}->{weather}->[0]->{description}
), ),
'humidity' => $data->{main}->{humidity},
'condition' =>
encode_utf8( $data->{weather}->[0]->{description} ),
'pressure' => int( 'pressure' => int(
sprintf( "%.1f", $data->{main}->{pressure} ) + 0.5 sprintf( "%.1f", $data->{current}->{pressure} )
+ 0.5
), ),
'wind' => int( 'wind' => int(
sprintf( "%.1f", ( $data->{wind}->{speed} * 3.6 ) ) sprintf( "%.1f",
( $data->{current}->{wind_speed} * 3.6 ) )
+ 0.5 + 0.5
), ),
'wind_speed' => int( 'wind_speed' => int(
sprintf( "%.1f", ( $data->{wind}->{speed} * 3.6 ) ) sprintf( "%.1f",
( $data->{current}->{wind_speed} * 3.6 ) )
+ 0.5 + 0.5
), ),
'wind_gust' => int( 'wind_gust' => int(
sprintf( "%.1f", ( $data->{wind}->{gust} * 3.6 ) ) sprintf( "%.1f",
+ 0.5 ( $data->{current}->{wind_gust} * 3.6 ) ) +
0.5
),
'wind_direction' => $data->{current}->{wind_deg},
'rain_1h' => $data->{rain}->{'1h'},
'cloudCover' => $data->{current}->{clouds},
'code' =>
$codes{ $data->{current}->{weather}->[0]->{id} },
'iconAPI' =>
$data->{current}->{weather}->[0]->{icon},
'condition' => encode_utf8(
$data->{current}->{weather}->[0]->{description}
), ),
'wind_direction' => $data->{wind}->{deg},
'cloudCover' => $data->{clouds}->{all},
'code' => $codes{ $data->{weather}->[0]->{id} },
'iconAPI' => $data->{weather}->[0]->{icon},
'sunsetTime' => strftimeWrapper( 'sunsetTime' => strftimeWrapper(
"%a, %e %b %Y %H:%M", "%a, %e %b %Y %H:%M",
localtime( $data->{sys}->{sunset} ) localtime( $data->{current}->{sunset} )
), ),
'sunriseTime' => strftimeWrapper( 'sunriseTime' => strftimeWrapper(
"%a, %e %b %Y %H:%M", "%a, %e %b %Y %H:%M",
localtime( $data->{sys}->{sunrise} ) localtime( $data->{current}->{sunrise} )
), ),
'pubDate' => strftimeWrapper( 'pubDate' => strftimeWrapper(
"%a, %e %b %Y %H:%M", "%a, %e %b %Y %H:%M",
localtime( $data->{dt} ) localtime( $data->{current}->{dt} )
), ),
'visibility' => int(
sprintf( "%.1f",
$data->{current}->{visibility} ) + 0.5
),
'uvi' => $data->{current}->{uvi},
'timezone' => $data->{timezone},
'timezone_offset' => $data->{timezone_offset},
}; };
$self->{cached}->{current}->{'visibility'} = if ( ref( $data->{hourly} ) eq "ARRAY"
int( sprintf( "%.1f", $data->{visibility} ) + 0.5 ) && scalar( @{ $data->{hourly} } ) > 0 )
if ( exists $data->{visibility} );
}
if ( $self->{endpoint} eq 'forecast' ) {
if ( ref( $data->{list} ) eq "ARRAY"
and scalar( @{ $data->{list} } ) > 0 )
{ {
## löschen des alten Datensatzes
delete $self->{cached}->{forecast};
my $i = 0; my $i = 0;
for ( @{ $data->{list} } ) { for ( @{ $data->{hourly} } ) {
push( push(
@{ $self->{cached}->{forecast}->{hourly} }, @{ $self->{cached}->{forecast}->{hourly} },
{ {
'pubDate' => strftimeWrapper( 'pubDate' => strftimeWrapper(
"%a, %e %b %Y %H:%M", "%a, %e %b %Y %H:%M",
localtime( localtime(
( $data->{list}->[$i]->{dt} ) - 3600 $data->{hourly}->[$i]->{dt}
) )
), ),
'day_of_week' => strftime( 'day_of_week' => strftime(
"%a, %H:%M", "%a, %H:%M",
localtime( localtime(
( $data->{list}->[$i]->{dt} ) - 3600 $data->{hourly}->[$i]->{dt}
) )
), ),
'temperature' => int( 'temperature' => int(
sprintf( sprintf( "%.0f",
"%.1f", $data->{hourly}->[$i]->{temp} )
(
$data->{list}->[$i]->{main}
->{temp} - 273.15
)
) + 0.5
), ),
'temp_c' => int( 'temp_c' => int(
sprintf( sprintf( "%.0f",
"%.1f", $data->{hourly}->[$i]->{temp} )
(
$data->{list}->[$i]->{main}
->{temp} - 273.15
)
) + 0.5
), ),
'low_c' => int( 'tempFeelsLike' => int(
sprintf( sprintf( "%.0f",
"%.1f", $data->{hourly}->[$i]
( ->{feels_like} )
$data->{list}->[$i]->{main}
->{temp_min} - 273.15
)
) + 0.5
), ),
'high_c' => int( 'dew_point' => int(
sprintf( sprintf( "%.0f",
"%.1f", $data->{hourly}->[$i]
( ->{dew_point} )
$data->{list}->[$i]->{main}
->{temp_max} - 273.15
)
) + 0.5
),
'tempLow' => int(
sprintf(
"%.1f",
(
$data->{list}->[$i]->{main}
->{temp_min} - 273.15
)
) + 0.5
),
'tempHigh' => int(
sprintf(
"%.1f",
(
$data->{list}->[$i]->{main}
->{temp_max} - 273.15
)
) + 0.5
), ),
'humidity' => 'humidity' =>
$data->{list}->[$i]->{main}->{humidity}, $data->{hourly}->[$i]->{humidity},
'condition' => encode_utf8( 'condition' => encode_utf8(
$data->{list}->[$i]->{weather}->[0] $data->{hourly}->[$i]->{weather}
->{description} ->[0]->{description}
), ),
'pressure' => int( 'pressure' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{list}->[$i]->{main} $data->{hourly}->[$i]
->{pressure} ) + 0.5 ->{pressure} ) + 0.5
), ),
'wind' => int( 'wind' => int(
sprintf( sprintf(
"%.1f", "%.1f",
( (
$data->{list}->[$i]->{wind} $data->{hourly}->[$i]
->{speed} * 3.6 ->{wind_speed} * 3.6
) )
) + 0.5 ) + 0.5
), ),
@ -552,8 +565,8 @@ sub _ProcessingRetrieveData {
sprintf( sprintf(
"%.1f", "%.1f",
( (
$data->{list}->[$i]->{wind} $data->{hourly}->[$i]
->{speed} * 3.6 ->{wind_speed} * 3.6
) )
) + 0.5 ) + 0.5
), ),
@ -561,28 +574,259 @@ sub _ProcessingRetrieveData {
sprintf( sprintf(
"%.1f", "%.1f",
( (
$data->{list}->[$i]->{wind} $data->{hourly}->[$i]
->{gust} * 3.6 ->{wind_gust} * 3.6
) )
) + 0.5 ) + 0.5
), ),
'wind_direction' =>
$data->{hourly}->[$i]->{wind_deg},
'cloudCover' => 'cloudCover' =>
$data->{list}->[$i]->{clouds}->{all}, $data->{hourly}->[$i]->{clouds},
'code' => $codes{ 'code' => $codes{
$data->{list}->[$i]->{weather}->[0] $data->{hourly}->[$i]->{weather}
->{id} ->[0]->{id}
}, },
'iconAPI' => 'iconAPI' =>
$data->{list}->[$i]->{weather}->[0] $data->{hourly}->[$i]->{weather}->[0]
->{icon}, ->{icon},
'rain1h' => 'rain1h' =>
$data->{list}->[$i]->{rain}->{'1h'}, $data->{hourly}->[$i]->{rain}->{'1h'},
'rain3h' =>
$data->{list}->[$i]->{rain}->{'3h'},
'snow1h' => 'snow1h' =>
$data->{list}->[$i]->{snow}->{'1h'}, $data->{hourly}->[$i]->{snow}->{'1h'},
'snow3h' => 'uvi' => $data->{hourly}->[$i]->{uvi},
$data->{list}->[$i]->{snow}->{'3h'}, 'visibility' => int(
sprintf( "%.1f",
$data->{hourly}->[$i]
->{visibility} ) + 0.5
),
},
);
$i++;
}
}
if ( ref( $data->{daily} ) eq "ARRAY"
&& scalar( @{ $data->{daily} } ) > 0 )
{
my $i = 0;
for ( @{ $data->{daily} } ) {
push(
@{ $self->{cached}->{forecast}->{daily} },
{
'pubDate' => strftimeWrapper(
"%a, %e %b %Y %H:%M",
localtime(
$data->{daily}->[$i]->{dt}
)
),
'day_of_week' => strftime(
"%a, %H:%M",
localtime(
$data->{daily}->[$i]->{dt}
)
),
'sunrise' => strftime(
"%H:%M",
localtime(
$data->{daily}->[$i]->{sunrise}
)
),
'sunset' => strftime(
"%a, %H:%M",
localtime(
$data->{daily}->[$i]->{sunset}
)
),
'moonrise' => strftime(
"%a, %H:%M",
localtime(
$data->{daily}->[$i]->{moonrise}
)
),
'moon_phase' =>
$data->{daily}->[$i]->{moon_phase},
'moonset' => strftime(
"%a, %H:%M",
localtime(
$data->{daily}->[$i]->{moonset}
)
),
'temperature' => int(
sprintf( "%.0f",
$data->{daily}->[$i]->{temp}
->{day} )
),
'temperature_morn' => int(
sprintf( "%.0f",
$data->{daily}->[$i]->{temp}
->{morn} )
),
'temperature_eve' => int(
sprintf( "%.0f",
$data->{daily}->[$i]->{temp}
->{eve} )
),
'temperature_night' => int(
sprintf( "%.0f",
$data->{daily}->[$i]->{temp}
->{night} )
),
'tempFeelsLike_morn' => int(
sprintf( "%.0f",
$data->{daily}->[$i]
->{feels_like}->{morn} )
),
'tempFeelsLike_eve' => int(
sprintf( "%.0f",
$data->{daily}->[$i]
->{feels_like}->{eve} )
),
'tempFeelsLike_night' => int(
sprintf( "%.0f",
$data->{daily}->[$i]
->{feels_like}->{night} )
),
'tempFeelsLike_day' => int(
sprintf( "%.0f",
$data->{daily}->[$i]
->{feels_like}->{day} )
),
'temp_c' => int(
sprintf( "%.0f",
$data->{daily}->[$i]->{temp}
->{day} )
),
'low_c' => int(
sprintf( "%.0f",
$data->{daily}->[$i]->{temp}
->{min} )
),
'high_c' => int(
sprintf( "%.0f",
$data->{daily}->[$i]->{temp}
->{max} )
),
'tempLow' => int(
sprintf( "%.0f",
$data->{daily}->[$i]->{temp}
->{min} )
),
'tempHigh' => int(
sprintf( "%.0f",
$data->{daily}->[$i]->{temp}
->{max} )
),
'dew_point' => int(
sprintf( "%.0f",
$data->{daily}->[$i]
->{dew_point} )
),
'humidity' =>
$data->{daily}->[$i]->{humidity},
'condition' => encode_utf8(
$data->{daily}->[$i]->{weather}
->[0]->{description}
),
'code' => $codes{
$data->{daily}->[$i]->{weather}
->[0]->{id}
},
'iconAPI' =>
$data->{daily}->[$i]->{weather}->[0]
->{icon},
'pressure' => int(
sprintf( "%.1f",
$data->{daily}->[$i]->{pressure}
) + 0.5
),
'wind' => int(
sprintf(
"%.1f",
(
$data->{daily}->[$i]
->{wind_speed} * 3.6
)
) + 0.5
),
'wind_speed' => int(
sprintf(
"%.1f",
(
$data->{daily}->[$i]
->{wind_speed} * 3.6
)
) + 0.5
),
'wind_gust' => int(
sprintf(
"%.1f",
(
$data->{daily}->[$i]
->{wind_gust} * 3.6
)
) + 0.5
),
'wind_direction' => int(
sprintf(
"%.1f",
(
$data->{daily}->[$i]
->{wind_deg}
)
)
),
'cloudCover' =>
$data->{daily}->[$i]->{clouds},
'code' => $codes{
$data->{daily}->[$i]->{weather}
->[0]->{id}
},
'rain' => $data->{daily}->[$i]->{rain},
'snow' => $data->{daily}->[$i]->{snow},
'uvi' => $data->{daily}->[$i]->{uvi},
},
);
$i++;
}
}
if ( ref( $data->{alerts} ) eq "ARRAY"
&& scalar( @{ $data->{alerts} } ) > 0 )
{
my $i = 0;
for ( @{ $data->{alerts} } ) {
push(
@{ $self->{cached}->{alerts} },
{
'End' => strftimeWrapper(
"%a, %e %b %Y %H:%M",
localtime(
(
$data->{alerts}->[$i]->{end}
)
)
),
'Start' => strftimeWrapper(
"%a, %e %b %Y %H:%M",
localtime(
(
$data->{alerts}->[$i]
->{start}
)
)
),
'Description' => encode_utf8(
$data->{alerts}->[$i]->{description}
),
'SenderName' => encode_utf8(
$data->{alerts}->[$i]->{sender_name}
),
'Event' => encode_utf8(
$data->{alerts}->[$i]->{event}
),
}, },
); );
@ -592,15 +836,18 @@ sub _ProcessingRetrieveData {
} }
} }
} }
}
else { _ErrorHandling( $self, 'OpenWeatherMap ' . $response ); } else { _ErrorHandling( $self, 'OpenWeatherMap ' . $response ); }
} }
$self->{endpoint} = 'none' if ( $self->{endpoint} eq 'forecast' ); $self->{endpoint} = 'none' if ( $self->{endpoint} eq 'onecall' );
_RetrieveDataFromOpenWeatherMap($self) _RetrieveDataFromOpenWeatherMap($self)
if ( $self->{endpoint} eq 'weather' ); if ( $self->{endpoint} eq 'weather' );
_CallWeatherCallbackFn($self) if ( $self->{endpoint} eq 'none' ); _CallWeatherCallbackFn($self) if ( $self->{endpoint} eq 'none' );
return;
} }
sub _CallWeatherCallbackFn { sub _CallWeatherCallbackFn {
@ -608,7 +855,9 @@ sub _CallWeatherCallbackFn {
# print 'Dumperausgabe: ' . Dumper $self; # print 'Dumperausgabe: ' . Dumper $self;
### Aufruf der callbackFn ### Aufruf der callbackFn
main::Weather_RetrieveCallbackFn( $self->{devName} ); ::Weather_RetrieveCallbackFn( $self->{devName} );
return;
} }
sub _ErrorHandling { sub _ErrorHandling {
@ -616,9 +865,11 @@ sub _ErrorHandling {
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} ) );
$self->{cached}->{status} = $err; $self->{cached}->{status} = $err;
$self->{cached}->{validity} = 'stale'; $self->{cached}->{validity} = 'stale';
return;
} }
sub _CreateForecastRef { sub _CreateForecastRef {
@ -639,20 +890,21 @@ sub _CreateForecastRef {
} }
sub strftimeWrapper { sub strftimeWrapper {
my $string = POSIX::strftime(@_); my @data = @_;
my $string = POSIX::strftime(@data);
$string =~ s/\xe4/ä/g; $string =~ s/\xe4/ä/xg;
$string =~ s/\xc4/Ä/g; $string =~ s/\xc4/Ä/xg;
$string =~ s/\xf6/ö/g; $string =~ s/\xf6/ö/xg;
$string =~ s/\xd6/Ö/g; $string =~ s/\xd6/Ö/xg;
$string =~ s/\xfc/ü/g; $string =~ s/\xfc/ü/xg;
$string =~ s/\xdc/Ü/g; $string =~ s/\xdc/Ü/xg;
$string =~ s/\xdf/ß/g; $string =~ s/\xdf/ß/xg;
$string =~ s/\xdf/ß/g; $string =~ s/\xdf/ß/xg;
$string =~ s/\xe1/á/g; $string =~ s/\xe1/á/xg;
$string =~ s/\xe9/é/g; $string =~ s/\xe9/é/xg;
$string =~ s/\xc1/Á/g; $string =~ s/\xc1/Á/xg;
$string =~ s/\xc9/É/g; $string =~ s/\xc9/É/xg;
return $string; return $string;
} }
@ -673,15 +925,15 @@ sub strftimeWrapper {
"abstract": "Wetter API für OpenWeatherMap" "abstract": "Wetter API für OpenWeatherMap"
} }
}, },
"version": "v1.0.3", "version": "v3.0.2",
"author": [ "author": [
"Marko Oldenburg <leongaultier@gmail.com>" "Marko Oldenburg <fhemdevelopment@cooltux.net>"
], ],
"x_fhem_maintainer": [ "x_fhem_maintainer": [
"CoolTux" "CoolTux"
], ],
"x_fhem_maintainer_github": [ "x_fhem_maintainer_github": [
"LeonGaultier" "CoolTuxNet"
], ],
"prereqs": { "prereqs": {
"runtime": { "runtime": {
@ -707,3 +959,5 @@ sub strftimeWrapper {
=end :application/json;q=META.json =end :application/json;q=META.json
=cut =cut
__END__

File diff suppressed because one or more lines are too long