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:
parent
01d510957b
commit
6abe964792
@ -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
|
||||
|
@ -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
@ -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 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;
|
||||
@ -141,11 +212,11 @@ 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;
|
||||
}
|
||||
|
||||
@ -271,7 +342,7 @@ 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",
|
||||
@ -279,16 +350,16 @@ sub _ProcessingRetrieveData($$) {
|
||||
|
||||
if ( $self->{endpoint} eq 'weather' ) {
|
||||
$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}->{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",
|
||||
@ -309,12 +380,17 @@ 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 ),
|
||||
'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' =>
|
||||
@ -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
|
||||
),
|
||||
@ -422,26 +498,36 @@ sub _ProcessingRetrieveData($$) {
|
||||
),
|
||||
'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} },
|
||||
'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'},
|
||||
'rain3h' =>
|
||||
|
Loading…
Reference in New Issue
Block a user