2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 06:39:11 +00:00

try to use JSON::MaybeXS wrapper for chance of better performance + open code, add more log entry

git-svn-id: https://svn.fhem.de/fhem/trunk@19628 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
LeonGaultier 2019-06-15 07:49:18 +00:00
parent 01d510957b
commit 6abe964792
4 changed files with 445 additions and 160 deletions

View File

@ -1,6 +1,8 @@
# 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.
- update 98_Siro.pm: V1.3 fix perlwarnings
- update: 59_Weather.pm: API Files try to use JSON::MaybeXS wrapper for chance
of better performance + open code, more log entry
- update: 98_Siro.pm: V1.3 fix perlwarnings
addcmd lock_cmd
addcmd lock_remote
change devstateicon/cmd

View File

@ -576,6 +576,7 @@ sub Weather_Set($@) {
sub Weather_RearmTimer($$) {
my ( $hash, $t ) = @_;
Log3( $hash, 4, "Weather $hash->{NAME}: Rearm new Timer" );
InternalTimer( $t, "Weather_GetUpdate", $hash, 0 );
}
@ -913,10 +914,11 @@ sub WeatherCheckOptions($@) {
if ( !$defs{$d} || $defs{$d}->{TYPE} ne "Weather" );
if ( AttrVal( $d, 'forecast', 'none' ) ne 'none' ) {
$f =
( AttrVal( $d, 'forecast', 'none' ) eq 'daily'
$f = (
AttrVal( $d, 'forecast', 'none' ) eq 'daily'
? 'd'
: ( AttrVal( $d, 'forecast', 'none' ) eq 'every' ? $f : 'h' ) );
: ( AttrVal( $d, 'forecast', 'none' ) eq 'every' ? $f : 'h' )
);
}
$f = 'h' if ( !$f || length($f) > 1 );

File diff suppressed because one or more lines are too long

View File

@ -39,16 +39,84 @@ use warnings;
use POSIX;
use HttpUtils;
# try to use JSON::MaybeXS wrapper
# for chance of better performance + open code
eval {
require JSON::MaybeXS;
import JSON::MaybeXS qw( decode_json encode_json );
1;
};
if ($@) {
$@ = undef;
# try to use JSON wrapper
# for chance of better performance
eval {
# JSON preference order
local $ENV{PERL_JSON_BACKEND} =
'Cpanel::JSON::XS,JSON::XS,JSON::PP,JSON::backportPP'
unless ( defined( $ENV{PERL_JSON_BACKEND} ) );
require JSON;
import JSON qw( decode_json encode_json );
1;
};
if ($@) {
$@ = undef;
# In rare cases, Cpanel::JSON::XS may
# be installed but JSON|JSON::MaybeXS not ...
eval {
require Cpanel::JSON::XS;
import Cpanel::JSON::XS qw(decode_json encode_json);
1;
};
if ($@) {
$@ = undef;
# In rare cases, JSON::XS may
# be installed but JSON not ...
eval {
require JSON::XS;
import JSON::XS qw(decode_json encode_json);
1;
};
if ($@) {
$@ = undef;
# Fallback to built-in JSON which SHOULD
# be available since 5.014 ...
eval {
require JSON::PP;
import JSON::PP qw(decode_json encode_json);
1;
};
if ($@) {
$@ = undef;
# Fallback to JSON::backportPP in really rare cases
require JSON::backportPP;
import JSON::backportPP qw(decode_json encode_json);
1;
}
}
}
}
}
my $missingModul = '';
eval "use JSON;1"
or $missingModul .=
"JSON "; # apt-get install libperl-JSON on Debian and derivatives
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.5';
use constant URL => 'https://api.openweathermap.org/data/2.5/';
use constant VERSION => '0.4.0';
## URL . 'weather?' for current data
## URL . 'forecast?' for forecast data
@ -112,7 +180,7 @@ my %codes = (
sub new {
### geliefert wird ein Hash
my ( $class, $argsRef ) = @_;
my $apioptions = parseApiOptions($argsRef->{apioptions});
my $apioptions = parseApiOptions( $argsRef->{apioptions} );
my $self = {
devName => $argsRef->{devName},
@ -128,7 +196,10 @@ sub new {
endpoint => 'none',
};
$self->{cachemaxage} = ( defined($apioptions->{cachemaxage}) ? $apioptions->{cachemaxage} : 900 );
$self->{cachemaxage} = (
defined( $apioptions->{cachemaxage} )
? $apioptions->{cachemaxage}
: 900 );
$self->{cached} = _CreateForecastRef($self);
bless $self, $class;
@ -140,15 +211,15 @@ sub parseApiOptions($) {
my @params;
my %h;
@params = split(',',$apioptions);
@params = split( ',', $apioptions );
while (@params) {
my $param = shift(@params);
next if($param eq '');
my ($key, $value) = split(':', $param, 2 );
next if ( $param eq '' );
my ( $key, $value ) = split( ':', $param, 2 );
$h{$key} = $value;
}
return \%h;
}
@ -271,24 +342,24 @@ sub _ProcessingRetrieveData($$) {
}
else {
### Debug
# print 'Response: ' . Dumper $data;
# print 'Response: ' . Dumper $data;
###### Ab hier wird die ResponseHash Referenze für die Rückgabe zusammen gestellt
$self->{cached}->{current_date_time} =
strftimeWrapper( "%a, %e %b %Y %H:%M",
strftimeWrapper( "%a, %e %b %Y %H:%M",
localtime( $self->{fetchTime} ) );
if ( $self->{endpoint} eq 'weather' ) {
$self->{cached}->{country} = $data->{sys}->{country};
$self->{cached}->{city} = encode_utf8($data->{name});
$self->{cached}->{country} = $data->{sys}->{country};
$self->{cached}->{city} = encode_utf8( $data->{name} );
$self->{cached}->{license}{text} = 'none';
$self->{cached}->{current} = {
$self->{cached}->{current} = {
'temperature' => int(
sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 ) ) +
0.5
sprintf( "%.1f",
( $data->{main}->{temp} - 273.15 ) ) + 0.5
),
'temp_c' => int(
sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 ) ) +
0.5
sprintf( "%.1f",
( $data->{main}->{temp} - 273.15 ) ) + 0.5
),
'low_c' => int(
sprintf( "%.1f",
@ -308,17 +379,22 @@ sub _ProcessingRetrieveData($$) {
),
'humidity' => $data->{main}->{humidity},
'condition' =>
encode_utf8( $data->{weather}->[0]->{description} ),
'pressure' =>
int( sprintf( "%.1f", $data->{main}->{pressure} ) + 0.5 ),
'wind' =>
int( sprintf( "%.1f", ($data->{wind}->{speed} * 3.6) ) + 0.5 ),
'wind_speed' =>
int( sprintf( "%.1f", ($data->{wind}->{speed} * 3.6) ) + 0.5 ),
encode_utf8( $data->{weather}->[0]->{description} ),
'pressure' => int(
sprintf( "%.1f", $data->{main}->{pressure} ) + 0.5
),
'wind' => int(
sprintf( "%.1f", ( $data->{wind}->{speed} * 3.6 ) )
+ 0.5
),
'wind_speed' => int(
sprintf( "%.1f", ( $data->{wind}->{speed} * 3.6 ) )
+ 0.5
),
'wind_direction' => $data->{wind}->{deg},
'cloudCover' => $data->{clouds}->{all},
'visibility' =>
int( sprintf( "%.1f", $data->{visibility} ) + 0.5 ),
int( sprintf( "%.1f", $data->{visibility} ) + 0.5 ),
'code' => $codes{ $data->{weather}->[0]->{id} },
'iconAPI' => $data->{weather}->[0]->{icon},
'sunsetTime' => strftimeWrapper(
@ -364,8 +440,8 @@ sub _ProcessingRetrieveData($$) {
sprintf(
"%.1f",
(
$data->{list}->[$i]->{main}->{temp}
- 273.15
$data->{list}->[$i]->{main}
->{temp} - 273.15
)
) + 0.5
),
@ -373,8 +449,8 @@ sub _ProcessingRetrieveData($$) {
sprintf(
"%.1f",
(
$data->{list}->[$i]->{main}->{temp}
- 273.15
$data->{list}->[$i]->{main}
->{temp} - 273.15
)
) + 0.5
),
@ -383,7 +459,7 @@ sub _ProcessingRetrieveData($$) {
"%.1f",
(
$data->{list}->[$i]->{main}
->{temp_min} - 273.15
->{temp_min} - 273.15
)
) + 0.5
),
@ -392,7 +468,7 @@ sub _ProcessingRetrieveData($$) {
"%.1f",
(
$data->{list}->[$i]->{main}
->{temp_max} - 273.15
->{temp_max} - 273.15
)
) + 0.5
),
@ -401,7 +477,7 @@ sub _ProcessingRetrieveData($$) {
"%.1f",
(
$data->{list}->[$i]->{main}
->{temp_min} - 273.15
->{temp_min} - 273.15
)
) + 0.5
),
@ -410,46 +486,56 @@ sub _ProcessingRetrieveData($$) {
"%.1f",
(
$data->{list}->[$i]->{main}
->{temp_max} - 273.15
->{temp_max} - 273.15
)
) + 0.5
),
'humidity' =>
$data->{list}->[$i]->{main}->{humidity},
$data->{list}->[$i]->{main}->{humidity},
'condition' => encode_utf8(
$data->{list}->[$i]->{weather}->[0]
->{description}
->{description}
),
'pressure' => int(
sprintf( "%.1f",
$data->{list}->[$i]->{main}->{pressure}
) + 0.5
$data->{list}->[$i]->{main}
->{pressure} ) + 0.5
),
'wind' => int(
sprintf( "%.1f",
($data->{list}->[$i]->{wind}->{speed} * 3.6) )
+ 0.5
sprintf(
"%.1f",
(
$data->{list}->[$i]->{wind}
->{speed} * 3.6
)
) + 0.5
),
'wind_speed' => int(
sprintf( "%.1f",
($data->{list}->[$i]->{wind}->{speed} * 3.6) )
+ 0.5
sprintf(
"%.1f",
(
$data->{list}->[$i]->{wind}
->{speed} * 3.6
)
) + 0.5
),
'cloudCover' =>
$data->{list}->[$i]->{clouds}->{all},
'code' =>
$codes{ $data->{list}->[$i]->{weather}->[0]
->{id} },
$data->{list}->[$i]->{clouds}->{all},
'code' => $codes{
$data->{list}->[$i]->{weather}->[0]
->{id}
},
'iconAPI' =>
$data->{list}->[$i]->{weather}->[0]->{icon},
$data->{list}->[$i]->{weather}->[0]
->{icon},
'rain1h' =>
$data->{list}->[$i]->{rain}->{'1h'},
$data->{list}->[$i]->{rain}->{'1h'},
'rain3h' =>
$data->{list}->[$i]->{rain}->{'3h'},
$data->{list}->[$i]->{rain}->{'3h'},
'snow1h' =>
$data->{list}->[$i]->{snow}->{'1h'},
$data->{list}->[$i]->{snow}->{'1h'},
'snow3h' =>
$data->{list}->[$i]->{snow}->{'3h'},
$data->{list}->[$i]->{snow}->{'3h'},
},
);