testing #29

Merged
marko merged 73 commits from testing into main 2023-01-07 12:49:01 +00:00
Showing only changes of commit c600937332 - Show all commits

View File

@ -23,14 +23,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'
@ -39,10 +36,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 ...
@ -50,10 +44,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 ...
@ -61,10 +52,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 ...
@ -72,20 +60,17 @@ 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;
} };
} };
} };
} };
} };
use Data::Dumper; # for Debug only use Data::Dumper; # for Debug only
## API URL ## API URL
@ -109,6 +94,8 @@ 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} = (
@ -173,14 +160,30 @@ 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;
return 0; return 0;
} }
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 getFetchTime { sub getFetchTime {
my $self = shift; my $self = shift;
@ -197,15 +200,14 @@ sub _RetrieveDataFromWU($) {
my $self = shift; my $self = shift;
# 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);
} }
$self->{cached}->{lat} = $self->{lat} $self->{cached}->{lat} = $self->{lat}
unless ( $self->{cached}->{lat} == $self->{lat} ); unless ( $self->{cached}->{lat} == $self->{lat} );
$self->{cached}->{long} = $self->{long} $self->{cached}->{long} = $self->{long}
unless ( $self->{cached}->{long} == $self->{long} ); unless ( $self->{cached}->{long} == $self->{long} );
@ -239,6 +241,7 @@ sub _RetrieveDataFromWU($) {
my $options = 'geocode=' . $self->{lat} . ',' . $self->{long}; my $options = 'geocode=' . $self->{lat} . ',' . $self->{long};
$options .= '&format=json'; $options .= '&format=json';
$options .= '&units=' . $self->{units}; $options .= '&units=' . $self->{units};
$options .= '&numericPrecision=decimal';
$options .= '&language=' $options .= '&language='
. ( . (
$self->{lang} eq 'en' $self->{lang} eq 'en'
@ -293,7 +296,7 @@ sub _RetrieveDataFinished($$$) {
# we got PWS and forecast data # we got PWS and forecast data
if ( defined( $paramRef->{forecast} ) ) { if ( defined( $paramRef->{forecast} ) ) {
if ( !$data || $data eq '' ) { if ( !$data || $data eq '' ) {
$err = 'No Data Found for specific PWS' unless ($err); $err = 'No Data Found for specific PWS' unless ($err);
$response = $paramRef->{forecast}; $response = $paramRef->{forecast};
} }
elsif ( $paramRef->{forecast} =~ m/^\{(.*)\}$/ ) { elsif ( $paramRef->{forecast} =~ m/^\{(.*)\}$/ ) {
@ -303,12 +306,12 @@ sub _RetrieveDataFinished($$$) {
$response = '{' . $fc . ',' . $1 . '}'; $response = '{' . $fc . ',' . $1 . '}';
} }
else { else {
$err = 'PWS data is not in JSON format' unless ($err); $err = 'PWS data is not in JSON format' unless ($err);
$response = $data; $response = $data;
} }
} }
else { else {
$err = 'Forecast data is not in JSON format' unless ($err); $err = 'Forecast data is not in JSON format' unless ($err);
$response = $data; $response = $data;
} }
} }
@ -324,7 +327,7 @@ 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 );
} }
@ -647,7 +650,7 @@ sub _ProcessingRetrieveData($$) {
'narrative' => $data->{narrative}[$i], 'narrative' => $data->{narrative}[$i],
'precipChance' => $data->{precipChance}[$i], 'precipChance' => $data->{precipChance}[$i],
'precipType' => $data->{precipType}[$i], 'precipType' => $data->{precipType}[$i],
'precipProbability' => $data->{qpf}[$i], 'precipProbability' => $data->{qpf}[$i],
'precipProbabilitySnow' => 'precipProbabilitySnow' =>
$data->{qpfSnow}[$i], $data->{qpfSnow}[$i],
'qualifierPhrase' => 'qualifierPhrase' =>
@ -656,7 +659,7 @@ sub _ProcessingRetrieveData($$) {
'snowRange' => $data->{snowRange}[$i], 'snowRange' => $data->{snowRange}[$i],
'temp_c' => $data->{temperature}[$i], 'temp_c' => $data->{temperature}[$i],
'temperature' => $data->{temperature}[$i], 'temperature' => $data->{temperature}[$i],
'heatIndex' => 'heatIndex' =>
$data->{temperatureHeatIndex}[$i], $data->{temperatureHeatIndex}[$i],
'wind_chill' => 'wind_chill' =>
$data->{temperatureWindChill}[$i], $data->{temperatureWindChill}[$i],
@ -665,7 +668,7 @@ sub _ProcessingRetrieveData($$) {
'thunderIndex' => $data->{thunderIndex}[$i], 'thunderIndex' => $data->{thunderIndex}[$i],
'uvDescription' => 'uvDescription' =>
$data->{uvDescription}[$i], $data->{uvDescription}[$i],
'uvIndex' => $data->{uvIndex}[$i], 'uvIndex' => $data->{uvIndex}[$i],
'wind_direction' => 'wind_direction' =>
$data->{windDirection}[$i], $data->{windDirection}[$i],
'wind_directionCardinal' => 'wind_directionCardinal' =>