2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 12:49:34 +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. # 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. # 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_cmd
addcmd lock_remote addcmd lock_remote
change devstateicon/cmd change devstateicon/cmd

View File

@ -576,6 +576,7 @@ sub Weather_Set($@) {
sub Weather_RearmTimer($$) { sub Weather_RearmTimer($$) {
my ( $hash, $t ) = @_; my ( $hash, $t ) = @_;
Log3( $hash, 4, "Weather $hash->{NAME}: Rearm new Timer" );
InternalTimer( $t, "Weather_GetUpdate", $hash, 0 ); InternalTimer( $t, "Weather_GetUpdate", $hash, 0 );
} }
@ -913,10 +914,11 @@ sub WeatherCheckOptions($@) {
if ( !$defs{$d} || $defs{$d}->{TYPE} ne "Weather" ); if ( !$defs{$d} || $defs{$d}->{TYPE} ne "Weather" );
if ( AttrVal( $d, 'forecast', 'none' ) ne 'none' ) { if ( AttrVal( $d, 'forecast', 'none' ) ne 'none' ) {
$f = $f = (
( AttrVal( $d, 'forecast', 'none' ) eq 'daily' AttrVal( $d, 'forecast', 'none' ) eq 'daily'
? 'd' ? 'd'
: ( AttrVal( $d, 'forecast', 'none' ) eq 'every' ? $f : 'h' ) ); : ( AttrVal( $d, 'forecast', 'none' ) eq 'every' ? $f : 'h' )
);
} }
$f = 'h' if ( !$f || length($f) > 1 ); $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 POSIX;
use HttpUtils; 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 = ''; 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 "; 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/'; use constant URL => 'https://api.openweathermap.org/data/2.5/';
use constant VERSION => '0.2.5'; use constant VERSION => '0.4.0';
## URL . 'weather?' for current data ## URL . 'weather?' for current data
## URL . 'forecast?' for forecast data ## URL . 'forecast?' for forecast data
@ -112,7 +180,7 @@ my %codes = (
sub new { sub new {
### geliefert wird ein Hash ### geliefert wird ein Hash
my ( $class, $argsRef ) = @_; my ( $class, $argsRef ) = @_;
my $apioptions = parseApiOptions($argsRef->{apioptions}); my $apioptions = parseApiOptions( $argsRef->{apioptions} );
my $self = { my $self = {
devName => $argsRef->{devName}, devName => $argsRef->{devName},
@ -128,7 +196,10 @@ sub new {
endpoint => 'none', endpoint => 'none',
}; };
$self->{cachemaxage} = ( defined($apioptions->{cachemaxage}) ? $apioptions->{cachemaxage} : 900 ); $self->{cachemaxage} = (
defined( $apioptions->{cachemaxage} )
? $apioptions->{cachemaxage}
: 900 );
$self->{cached} = _CreateForecastRef($self); $self->{cached} = _CreateForecastRef($self);
bless $self, $class; bless $self, $class;
@ -141,11 +212,11 @@ sub parseApiOptions($) {
my @params; my @params;
my %h; my %h;
@params = split(',',$apioptions); @params = split( ',', $apioptions );
while (@params) { while (@params) {
my $param = shift(@params); my $param = shift(@params);
next if($param eq ''); next if ( $param eq '' );
my ($key, $value) = split(':', $param, 2 ); my ( $key, $value ) = split( ':', $param, 2 );
$h{$key} = $value; $h{$key} = $value;
} }
@ -271,7 +342,7 @@ sub _ProcessingRetrieveData($$) {
} }
else { else {
### Debug ### Debug
# print 'Response: ' . Dumper $data; # print '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",
@ -279,16 +350,16 @@ sub _ProcessingRetrieveData($$) {
if ( $self->{endpoint} eq 'weather' ) { if ( $self->{endpoint} eq 'weather' ) {
$self->{cached}->{country} = $data->{sys}->{country}; $self->{cached}->{country} = $data->{sys}->{country};
$self->{cached}->{city} = encode_utf8($data->{name}); $self->{cached}->{city} = encode_utf8( $data->{name} );
$self->{cached}->{license}{text} = 'none'; $self->{cached}->{license}{text} = 'none';
$self->{cached}->{current} = { $self->{cached}->{current} = {
'temperature' => int( 'temperature' => int(
sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 ) ) + sprintf( "%.1f",
0.5 ( $data->{main}->{temp} - 273.15 ) ) + 0.5
), ),
'temp_c' => int( 'temp_c' => int(
sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 ) ) + sprintf( "%.1f",
0.5 ( $data->{main}->{temp} - 273.15 ) ) + 0.5
), ),
'low_c' => int( 'low_c' => int(
sprintf( "%.1f", sprintf( "%.1f",
@ -309,12 +380,17 @@ sub _ProcessingRetrieveData($$) {
'humidity' => $data->{main}->{humidity}, 'humidity' => $data->{main}->{humidity},
'condition' => 'condition' =>
encode_utf8( $data->{weather}->[0]->{description} ), encode_utf8( $data->{weather}->[0]->{description} ),
'pressure' => 'pressure' => int(
int( sprintf( "%.1f", $data->{main}->{pressure} ) + 0.5 ), sprintf( "%.1f", $data->{main}->{pressure} ) + 0.5
'wind' => ),
int( sprintf( "%.1f", ($data->{wind}->{speed} * 3.6) ) + 0.5 ), 'wind' => int(
'wind_speed' => sprintf( "%.1f", ( $data->{wind}->{speed} * 3.6 ) )
int( sprintf( "%.1f", ($data->{wind}->{speed} * 3.6) ) + 0.5 ), + 0.5
),
'wind_speed' => int(
sprintf( "%.1f", ( $data->{wind}->{speed} * 3.6 ) )
+ 0.5
),
'wind_direction' => $data->{wind}->{deg}, 'wind_direction' => $data->{wind}->{deg},
'cloudCover' => $data->{clouds}->{all}, 'cloudCover' => $data->{clouds}->{all},
'visibility' => 'visibility' =>
@ -364,8 +440,8 @@ sub _ProcessingRetrieveData($$) {
sprintf( sprintf(
"%.1f", "%.1f",
( (
$data->{list}->[$i]->{main}->{temp} $data->{list}->[$i]->{main}
- 273.15 ->{temp} - 273.15
) )
) + 0.5 ) + 0.5
), ),
@ -373,8 +449,8 @@ sub _ProcessingRetrieveData($$) {
sprintf( sprintf(
"%.1f", "%.1f",
( (
$data->{list}->[$i]->{main}->{temp} $data->{list}->[$i]->{main}
- 273.15 ->{temp} - 273.15
) )
) + 0.5 ) + 0.5
), ),
@ -422,26 +498,36 @@ sub _ProcessingRetrieveData($$) {
), ),
'pressure' => int( 'pressure' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{list}->[$i]->{main}->{pressure} $data->{list}->[$i]->{main}
) + 0.5 ->{pressure} ) + 0.5
), ),
'wind' => int( 'wind' => int(
sprintf( "%.1f", sprintf(
($data->{list}->[$i]->{wind}->{speed} * 3.6) ) "%.1f",
+ 0.5 (
$data->{list}->[$i]->{wind}
->{speed} * 3.6
)
) + 0.5
), ),
'wind_speed' => int( 'wind_speed' => int(
sprintf( "%.1f", sprintf(
($data->{list}->[$i]->{wind}->{speed} * 3.6) ) "%.1f",
+ 0.5 (
$data->{list}->[$i]->{wind}
->{speed} * 3.6
)
) + 0.5
), ),
'cloudCover' => 'cloudCover' =>
$data->{list}->[$i]->{clouds}->{all}, $data->{list}->[$i]->{clouds}->{all},
'code' => 'code' => $codes{
$codes{ $data->{list}->[$i]->{weather}->[0] $data->{list}->[$i]->{weather}->[0]
->{id} }, ->{id}
},
'iconAPI' => 'iconAPI' =>
$data->{list}->[$i]->{weather}->[0]->{icon}, $data->{list}->[$i]->{weather}->[0]
->{icon},
'rain1h' => 'rain1h' =>
$data->{list}->[$i]->{rain}->{'1h'}, $data->{list}->[$i]->{rain}->{'1h'},
'rain3h' => 'rain3h' =>