fix error in empty Hash check

This commit is contained in:
Marko Oldenburg 2019-01-10 22:35:57 +01:00
parent 9fddd33c83
commit b9bb92d6f4
3 changed files with 266 additions and 170 deletions

View File

@ -379,7 +379,10 @@ sub Weather_WriteReadings($$) {
readingsBulkUpdate($hash, 'icon', $iconlist[$dataRef->{current}->{code}]); readingsBulkUpdate($hash, 'icon', $iconlist[$dataRef->{current}->{code}]);
if ( defined($dataRef->{current}->{wind_direction}) if ( defined($dataRef->{current}->{wind_direction})
and defined($dataRef->{current}->{wind_speed} ) ) and $dataRef->{current}->{wind_direction}
and defined($dataRef->{current}->{wind_speed})
and $dataRef->{current}->{wind_speed}
)
{ {
my $wdir= degrees_to_direction($dataRef->{current}->{wind_direction}, @directions_txt_i18n); my $wdir= degrees_to_direction($dataRef->{current}->{wind_direction}, @directions_txt_i18n);
readingsBulkUpdate($hash, 'wind_condition', 'Wind: ' . $wdir . ' ' . $dataRef->{current}->{wind_speed} . ' km/h'); readingsBulkUpdate($hash, 'wind_condition', 'Wind: ' . $wdir . ' ' . $dataRef->{current}->{wind_speed} . ' km/h');
@ -389,8 +392,9 @@ sub Weather_WriteReadings($$) {
### forecast ### forecast
if ( ref( $dataRef->{forecast} ) eq 'HASH' ) { if ( ref( $dataRef->{forecast} ) eq 'HASH' ) {
## hourly ## hourly
if ( defined($dataRef->{forecast}->{hourly}) and ref( $dataRef->{forecast}->{hourly} ) eq 'ARRAY' if ( defined($dataRef->{forecast}->{hourly})
and scalar( @{ $dataRef->{forecast}->{hourly} } ) > 0 ) and ref( $dataRef->{forecast}->{hourly} ) eq 'ARRAY'
and scalar( @{ $dataRef->{forecast}->{hourly} } ) > 0 )
{ {
my $i= 0; my $i= 0;
foreach my $fc (@{$dataRef->{forecast}->{hourly}}) { foreach my $fc (@{$dataRef->{forecast}->{hourly}}) {
@ -404,7 +408,10 @@ sub Weather_WriteReadings($$) {
# readingsBulkUpdate($hash, $f . "day_of_week", $wdays_txt_i18n{$fc->{day}}); # readingsBulkUpdate($hash, $f . "day_of_week", $wdays_txt_i18n{$fc->{day}});
readingsBulkUpdate($hash, $f . 'icon', $iconlist[$dataRef->{forecast}->{hourly}[$i-1]{code}]); readingsBulkUpdate($hash, $f . 'icon', $iconlist[$dataRef->{forecast}->{hourly}[$i-1]{code}]);
if ( defined($dataRef->{forecast}->{hourly}[$i-1]{wind_direction}) if ( defined($dataRef->{forecast}->{hourly}[$i-1]{wind_direction})
and defined($dataRef->{forecast}->{hourly}[$i-1]{wind_speed}) ) and $dataRef->{forecast}->{hourly}[$i-1]{wind_direction}
and defined($dataRef->{forecast}->{hourly}[$i-1]{wind_speed})
and $dataRef->{forecast}->{hourly}[$i-1]{wind_speed}
)
{ {
my $wdir= degrees_to_direction($dataRef->{forecast}->{hourly}[$i-1]{wind_direction}, @directions_txt_i18n); my $wdir= degrees_to_direction($dataRef->{forecast}->{hourly}[$i-1]{wind_direction}, @directions_txt_i18n);
readingsBulkUpdate($hash, $f . 'wind_condition', 'Wind: ' . $wdir . ' ' . $dataRef->{forecast}->{hourly}[$i-1]{wind_speed} . ' km/h'); readingsBulkUpdate($hash, $f . 'wind_condition', 'Wind: ' . $wdir . ' ' . $dataRef->{forecast}->{hourly}[$i-1]{wind_speed} . ' km/h');
@ -415,7 +422,7 @@ sub Weather_WriteReadings($$) {
## daily ## daily
if ( defined($dataRef->{forecast}->{daily}) and ref( $dataRef->{forecast}->{daily} ) eq 'ARRAY' if ( defined($dataRef->{forecast}->{daily}) and ref( $dataRef->{forecast}->{daily} ) eq 'ARRAY'
and scalar( @{ $dataRef->{forecast}->{daily} } ) > 0 ) and scalar( @{ $dataRef->{forecast}->{daily} } ) > 0 )
{ {
my $i= 0; my $i= 0;
foreach my $fc (@{$dataRef->{forecast}->{daily}}) { foreach my $fc (@{$dataRef->{forecast}->{daily}}) {
@ -429,7 +436,10 @@ sub Weather_WriteReadings($$) {
# readingsBulkUpdate($hash, $f . "day_of_week", $wdays_txt_i18n{$fc->{day}}); # readingsBulkUpdate($hash, $f . "day_of_week", $wdays_txt_i18n{$fc->{day}});
readingsBulkUpdate($hash, $f . 'icon', $iconlist[$dataRef->{forecast}->{daily}[$i-1]{code}]); readingsBulkUpdate($hash, $f . 'icon', $iconlist[$dataRef->{forecast}->{daily}[$i-1]{code}]);
if ( defined($dataRef->{forecast}->{daily}[$i-1]{wind_direction}) if ( defined($dataRef->{forecast}->{daily}[$i-1]{wind_direction})
and defined($dataRef->{forecast}->{daily}[$i-1]{wind_speed}) ) and $dataRef->{forecast}->{daily}[$i-1]{wind_direction}
and defined($dataRef->{forecast}->{daily}[$i-1]{wind_speed})
and $dataRef->{forecast}->{daily}[$i-1]{wind_speed}
)
{ {
my $wdir= degrees_to_direction($dataRef->{forecast}->{daily}[$i-1]{wind_direction}, @directions_txt_i18n); my $wdir= degrees_to_direction($dataRef->{forecast}->{daily}[$i-1]{wind_direction}, @directions_txt_i18n);
readingsBulkUpdate($hash, $f . 'wind_condition', 'Wind: ' . $wdir . ' ' . $dataRef->{forecast}->{daily}[$i-1]{wind_speed} . ' km/h'); readingsBulkUpdate($hash, $f . 'wind_condition', 'Wind: ' . $wdir . ' ' . $dataRef->{forecast}->{daily}[$i-1]{wind_speed} . ' km/h');
@ -609,9 +619,9 @@ sub Weather_Define($$) {
$hash->{NOTIFYDEV} = "global"; $hash->{NOTIFYDEV} = "global";
$hash->{fhem}->{interfaces}= "temperature;humidity;wind"; $hash->{fhem}->{interfaces}= "temperature;humidity;wind";
$hash->{LOCATION} = ( defined($location) ? $location : AttrVal( 'global', 'latitude', 'error' ).','.AttrVal( 'global', 'longitude', 'error' ) ); $hash->{LOCATION} = ( (defined($location) and $location) ? $location : AttrVal( 'global', 'latitude', 'error' ).','.AttrVal( 'global', 'longitude', 'error' ) );
$hash->{INTERVAL} = $interval; $hash->{INTERVAL} = $interval;
$hash->{LANG} = ( defined($lang) ? $lang : lc(AttrVal('global','language','de')) ); $hash->{LANG} = ( (defined($lang) and $lang) ? $lang : lc(AttrVal('global','language','de')) );
$hash->{API} = $api; $hash->{API} = $api;
$hash->{APIKEY} = $apikey; $hash->{APIKEY} = $apikey;
$hash->{APIOPTIONS} = $apioptions; $hash->{APIOPTIONS} = $apioptions;

View File

@ -67,12 +67,22 @@ sub new {
my $self = { my $self = {
devName => $argsRef->{devName}, devName => $argsRef->{devName},
key => ( defined( $argsRef->{apikey} ) ? $argsRef->{apikey} : 'none' ), key => (
cachemaxage => ( (split(':',$argsRef->{apioptions}))[0] eq 'cachemaxage' ? (split(':',$argsRef->{apioptions}))[1] : 900 ), ( defined( $argsRef->{apikey} ) and $argsRef->{apikey} )
lang => $argsRef->{language}, ? $argsRef->{apikey}
lat => ( split( ',', $argsRef->{location} ) )[0], : 'none'
long => ( split( ',', $argsRef->{location} ) )[1], ),
fetchTime => 0, cachemaxage => (
( defined( $argsRef->{apioptions} ) and $argsRef->{apioptions} )
? ( ( split( ':', $argsRef->{apioptions} ) )[0] eq 'cachemaxage'
? ( split( ':', $argsRef->{apioptions} ) )[1]
: 900 )
: 900
),
lang => $argsRef->{language},
lat => ( split( ',', $argsRef->{location} ) )[0],
long => ( split( ',', $argsRef->{location} ) )[1],
fetchTime => 0,
}; };
$self->{cached} = _CreateForecastRef($self); $self->{cached} = _CreateForecastRef($self);
@ -174,25 +184,32 @@ sub _RetrieveDataFinished($$$) {
sub _ProcessingRetrieveData($$) { sub _ProcessingRetrieveData($$) {
my ( $self, $response ) = @_; my ( $self, $response ) = @_;
if ( $self->{cached}->{status} eq 'ok' and defined($response) ) { if ( $self->{cached}->{status} eq 'ok'
and defined($response)
and $response )
{
my $data = eval { decode_json($response) }; my $data = eval { decode_json($response) };
if ($@) { if ($@) {
_ErrorHandling( $self, 'DarkSky Weather decode JSON err ' . $@ ); _ErrorHandling( $self, 'DarkSky Weather decode JSON err ' . $@ );
} }
elsif ( defined( $data->{code} ) and defined( $data->{error} ) ) { elsif ( defined( $data->{code} )
and $data->{code}
and defined( $data->{error} )
and $data->{error} )
{
_ErrorHandling( $self, _ErrorHandling( $self,
'Code: ' . $data->{code} . ' Error: ' . $data->{error} ); 'Code: ' . $data->{code} . ' Error: ' . $data->{error} );
} }
else { else {
# print Dumper $data; ## für Debugging # print Dumper $data; ## für Debugging
$self->{cached}->{current_date_time} = strftime( $self->{cached}->{current_date_time} =
"%a,%e %b %Y %H:%M %p", strftime( "%a,%e %b %Y %H:%M %p",
localtime( $self->{fetchTime} ) localtime( $self->{fetchTime} ) );
);
$self->{cached}->{timezone} = $data->{timezone}; $self->{cached}->{timezone} = $data->{timezone};
$self->{cached}->{license}{text} = $data->{flags}->{'meteoalarm-license'}; $self->{cached}->{license}{text} =
$data->{flags}->{'meteoalarm-license'};
$self->{cached}->{current} = { $self->{cached}->{current} = {
'temperature' => int( 'temperature' => int(
sprintf( "%.1f", $data->{currently}->{temperature} ) + 0.5 sprintf( "%.1f", $data->{currently}->{temperature} ) + 0.5
@ -203,12 +220,12 @@ sub _ProcessingRetrieveData($$) {
'dewPoint' => int( 'dewPoint' => int(
sprintf( "%.1f", $data->{currently}->{dewPoint} ) + 0.5 sprintf( "%.1f", $data->{currently}->{dewPoint} ) + 0.5
), ),
'humidity' => $data->{currently}->{humidity} * 100, 'humidity' => $data->{currently}->{humidity} * 100,
'condition' => encode_utf8( $data->{currently}->{summary} ), 'condition' => encode_utf8( $data->{currently}->{summary} ),
'pressure' => int( 'pressure' => int(
sprintf( "%.1f", $data->{currently}->{pressure} ) + 0.5 sprintf( "%.1f", $data->{currently}->{pressure} ) + 0.5
), ),
'wind' => int( 'wind' => int(
sprintf( "%.1f", $data->{currently}->{windSpeed} ) + 0.5 sprintf( "%.1f", $data->{currently}->{windSpeed} ) + 0.5
), ),
'wind_speed' => int( 'wind_speed' => int(
@ -218,15 +235,15 @@ sub _ProcessingRetrieveData($$) {
'windGust' => int( 'windGust' => int(
sprintf( "%.1f", $data->{currently}->{windGust} ) + 0.5 sprintf( "%.1f", $data->{currently}->{windGust} ) + 0.5
), ),
'cloudCover' => $data->{currently}->{cloudCover} * 100, 'cloudCover' => $data->{currently}->{cloudCover} * 100,
'uvIndex' => $data->{currently}->{uvIndex}, 'uvIndex' => $data->{currently}->{uvIndex},
'visibility' => int( 'visibility' => int(
sprintf( "%.1f", $data->{currently}->{visibility} ) + 0.5 sprintf( "%.1f", $data->{currently}->{visibility} ) + 0.5
), ),
'ozone' => $data->{currently}->{ozone}, 'ozone' => $data->{currently}->{ozone},
'code' => $codes{ $data->{currently}->{icon} }, 'code' => $codes{ $data->{currently}->{icon} },
'iconAPI' => $data->{currently}->{icon}, 'iconAPI' => $data->{currently}->{icon},
'pubDate' => strftime( 'pubDate' => strftime(
"%a,%e %b %Y %H:%M %p", "%a,%e %b %Y %H:%M %p",
localtime( $data->{currently}->{'time'} ) localtime( $data->{currently}->{'time'} )
), ),
@ -244,7 +261,7 @@ sub _ProcessingRetrieveData($$) {
{ {
### löschen des alten Datensatzes ### löschen des alten Datensatzes
delete $self->{cached}->{forecast}; delete $self->{cached}->{forecast};
my $i = 0; my $i = 0;
foreach ( @{ $data->{daily}->{data} } ) { foreach ( @{ $data->{daily}->{data} } ) {
push( push(
@ -252,120 +269,120 @@ sub _ProcessingRetrieveData($$) {
{ {
'date' => strftime( 'date' => strftime(
"%a, %d.%m.%Y", "%a, %d.%m.%Y",
localtime( $data->{daily}->{data}->[$i]->{'time'} ) localtime(
$data->{daily}->{data}->[$i]->{'time'}
)
), ),
'day_of_week' => strftime( 'day_of_week' => strftime(
"%a", "%a",
localtime( $data->{daily}->{data}->[$i]->{'time'} ) localtime(
$data->{daily}->{data}->[$i]->{'time'}
)
), ),
'low_c' => int( 'low_c' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily}->{data}->[$i]->{temperatureLow} ) $data->{daily}->{data}->[$i]
+ 0.5 ->{temperatureLow} ) + 0.5
), ),
'high_c' => int( 'high_c' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily}->{data}->[$i]->{temperatureHigh} $data->{daily}->{data}->[$i]
) + 0.5 ->{temperatureHigh} ) + 0.5
), ),
'tempMin' => int( 'tempMin' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily}->{data}->[$i]->{temperatureMin} ) $data->{daily}->{data}->[$i]
+ 0.5 ->{temperatureMin} ) + 0.5
), ),
'tempMinTime' => strftime( 'tempMinTime' => strftime(
"%a,%e %b %Y %H:%M %p", "%a,%e %b %Y %H:%M %p",
localtime( localtime(
$data->{daily} $data->{daily}->{data}->[$i]
->{data}->[$i]->{temperatureMinTime} ->{temperatureMinTime}
) )
), ),
'tempMax' => int( 'tempMax' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily}->{data}->[$i]->{temperatureMax} ) $data->{daily}->{data}->[$i]
+ 0.5 ->{temperatureMax} ) + 0.5
), ),
'tempMaxTime' => strftime( 'tempMaxTime' => strftime(
"%a,%e %b %Y %H:%M %p", "%a,%e %b %Y %H:%M %p",
localtime( localtime(
$data->{daily} $data->{daily}->{data}->[$i]
->{data}->[$i]->{temperatureMaxTime} ->{temperatureMaxTime}
) )
), ),
'tempLow' => int( 'tempLow' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily}->{data}->[$i]->{temperatureLow} ) $data->{daily}->{data}->[$i]
+ 0.5 ->{temperatureLow} ) + 0.5
), ),
'tempLowTime' => strftime( 'tempLowTime' => strftime(
"%a,%e %b %Y %H:%M %p", "%a,%e %b %Y %H:%M %p",
localtime( localtime(
$data->{daily} $data->{daily}->{data}->[$i]
->{data}->[$i]->{temperatureLowTime} ->{temperatureLowTime}
) )
), ),
'tempHigh' => int( 'tempHigh' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily}->{data}->[$i]->{temperatureHigh} $data->{daily}->{data}->[$i]
) + 0.5 ->{temperatureHigh} ) + 0.5
), ),
'tempHighTime' => strftime( 'tempHighTime' => strftime(
"%a,%e %b %Y %H:%M %p", "%a,%e %b %Y %H:%M %p",
localtime( localtime(
$data->{daily} $data->{daily}->{data}->[$i]
->{data}->[$i]->{temperatureHighTime} ->{temperatureHighTime}
) )
), ),
'apparentTempLow' => int( 'apparentTempLow' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily} $data->{daily}->{data}->[$i]
->{data}->[$i]->{apparentTemperatureLow} ) + ->{apparentTemperatureLow} ) + 0.5
0.5
), ),
'apparentTempLowTime' => strftime( 'apparentTempLowTime' => strftime(
"%a,%e %b %Y %H:%M %p", "%a,%e %b %Y %H:%M %p",
localtime( localtime(
$data->{daily} $data->{daily}->{data}->[$i]
->{data}->[$i]->{apparentTemperatureLowTime} ->{apparentTemperatureLowTime}
) )
), ),
'apparentTempHigh' => int( 'apparentTempHigh' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily} $data->{daily}->{data}->[$i]
->{data}->[$i]->{apparentTemperatureHigh} ) + ->{apparentTemperatureHigh} ) + 0.5
0.5
), ),
'apparentTempHighTime' => strftime( 'apparentTempHighTime' => strftime(
"%a,%e %b %Y %H:%M %p", "%a,%e %b %Y %H:%M %p",
localtime( localtime(
$data->{daily} $data->{daily}->{data}->[$i]
->{data}->[$i]->{apparentTemperatureHighTime} ->{apparentTemperatureHighTime}
) )
), ),
'apparenttempMin' => int( 'apparenttempMin' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily} $data->{daily}->{data}->[$i]
->{data}->[$i]->{apparentTemperatureMin} ) + ->{apparentTemperatureMin} ) + 0.5
0.5
), ),
'apparenttempMinTime' => strftime( 'apparenttempMinTime' => strftime(
"%a,%e %b %Y %H:%M %p", "%a,%e %b %Y %H:%M %p",
localtime( localtime(
$data->{daily} $data->{daily}->{data}->[$i]
->{data}->[$i]->{apparentTemperatureMinTime} ->{apparentTemperatureMinTime}
) )
), ),
'apparenttempMax' => int( 'apparenttempMax' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily} $data->{daily}->{data}->[$i]
->{data}->[$i]->{apparentTemperatureMax} ) + ->{apparentTemperatureMax} ) + 0.5
0.5
), ),
'apparenttempMaxTime' => strftime( 'apparenttempMaxTime' => strftime(
"%a,%e %b %Y %H:%M %p", "%a,%e %b %Y %H:%M %p",
localtime( localtime(
$data->{daily} $data->{daily}->{data}->[$i]
->{data}->[$i]->{apparentTemperatureMaxTime} ->{apparentTemperatureMaxTime}
) )
), ),
'code' => 'code' =>
@ -374,8 +391,9 @@ sub _ProcessingRetrieveData($$) {
'condition' => encode_utf8( 'condition' => encode_utf8(
$data->{daily}->{data}->[$i]->{summary} $data->{daily}->{data}->[$i]->{summary}
), ),
'ozone' => $data->{daily}->{data}->[$i]->{ozone}, 'ozone' => $data->{daily}->{data}->[$i]->{ozone},
'uvIndex' => $data->{daily}->{data}->[$i]->{uvIndex}, 'uvIndex' =>
$data->{daily}->{data}->[$i]->{uvIndex},
'uvIndexTime' => strftime( 'uvIndexTime' => strftime(
"%a,%e %b %Y %H:%M %p", "%a,%e %b %Y %H:%M %p",
localtime( localtime(
@ -385,21 +403,22 @@ sub _ProcessingRetrieveData($$) {
'precipIntensity' => 'precipIntensity' =>
$data->{daily}->{data}->[$i]->{precipIntensity}, $data->{daily}->{data}->[$i]->{precipIntensity},
'precipIntensityMax' => 'precipIntensityMax' =>
$data->{daily}->{data}->[$i]->{precipIntensityMax}, $data->{daily}->{data}->[$i]
->{precipIntensityMax},
'precipIntensityMaxTime' => strftime( 'precipIntensityMaxTime' => strftime(
"%a,%e %b %Y %H:%M %p", "%a,%e %b %Y %H:%M %p",
localtime( localtime(
$data->{daily} $data->{daily}->{data}->[$i]
->{data}->[$i]->{precipIntensityMaxTime} ->{precipIntensityMaxTime}
) )
), ),
'dewPoint' => int( 'dewPoint' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily}->{data}->[$i]->{dewPoint} $data->{daily}->{data}->[$i]->{dewPoint} )
) + 0.5 + 0.5
), ),
'humidity' => $data->{daily}->{data}->[$i]->{humidity} 'humidity' =>
* 100, $data->{daily}->{data}->[$i]->{humidity} * 100,
'cloudCover' => 'cloudCover' =>
$data->{daily}->{data}->[$i]->{cloudCover} * 100, $data->{daily}->{data}->[$i]->{cloudCover} * 100,
'precipType' => 'precipType' =>
@ -409,17 +428,18 @@ sub _ProcessingRetrieveData($$) {
$data->{daily}->{data}->[$i]->{windBearing}, $data->{daily}->{data}->[$i]->{windBearing},
'wind' => int( 'wind' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily}->{data}->[$i]->{windSpeed} $data->{daily}->{data}->[$i]->{windSpeed} )
) + 0.5 + 0.5
), ),
'wind_speed' => int( 'wind_speed' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily}->{data}->[$i]->{windSpeed} $data->{daily}->{data}->[$i]->{windSpeed} )
) + 0.5 + 0.5
), ),
'windGust' => int( 'windGust' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily}->{data}->[$i]->{windGust}) + 0.5 $data->{daily}->{data}->[$i]->{windGust} )
+ 0.5
), ),
'windGustTime' => strftime( 'windGustTime' => strftime(
"%a,%e %b %Y %H:%M %p", "%a,%e %b %Y %H:%M %p",
@ -446,15 +466,17 @@ sub _ProcessingRetrieveData($$) {
$data->{daily}->{data}->[$i]->{precipProbability}, $data->{daily}->{data}->[$i]->{precipProbability},
'pressure' => int( 'pressure' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily}->{data}->[$i]->{pressure}) + 0.5 $data->{daily}->{data}->[$i]->{pressure} )
+ 0.5
), ),
'visibility' => int( 'visibility' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily}->{data}->[$i]->{visibility}) + 0.5 $data->{daily}->{data}->[$i]->{visibility} )
+ 0.5
), ),
} }
); );
$i++; $i++;
} }
} }
@ -476,8 +498,8 @@ sub _ErrorHandling($$) {
my ( $self, $err ) = @_; my ( $self, $err ) = @_;
$self->{cached}->{current_date_time} = $self->{cached}->{current_date_time} =
strftime( "%a,%e %b %Y %H:%M %p", localtime( $self->{fetchTime} ) ), strftime( "%a,%e %b %Y %H:%M %p", localtime( $self->{fetchTime} ) ),
$self->{cached}->{status} = $err; $self->{cached}->{status} = $err;
$self->{cached}->{validity} = 'stale'; $self->{cached}->{validity} = 'stale';
} }

View File

@ -114,14 +114,24 @@ sub new {
my ( $class, $argsRef ) = @_; my ( $class, $argsRef ) = @_;
my $self = { my $self = {
devName => $argsRef->{devName}, devName => $argsRef->{devName},
key => ( defined( $argsRef->{apikey} ) ? $argsRef->{apikey} : 'none' ), key => (
cachemaxage => ( (split(':',$argsRef->{apioptions}))[0] eq 'cachemaxage' ? (split(':',$argsRef->{apioptions}))[1] : 900 ), ( defined( $argsRef->{apikey} ) and $argsRef->{apikey} )
lang => $argsRef->{language}, ? $argsRef->{apikey}
lat => ( split( ',', $argsRef->{location} ) )[0], : 'none'
long => ( split( ',', $argsRef->{location} ) )[1], ),
fetchTime => 0, cachemaxage => (
endpoint => 'none', ( defined( $argsRef->{apioptions} ) and $argsRef->{apioptions} )
? ( ( split( ':', $argsRef->{apioptions} ) )[0] eq 'cachemaxage'
? ( split( ':', $argsRef->{apioptions} ) )[1]
: 900 )
: 900
),
lang => $argsRef->{language},
lat => ( split( ',', $argsRef->{location} ) )[0],
long => ( split( ',', $argsRef->{location} ) )[1],
fetchTime => 0,
endpoint => 'none',
}; };
$self->{cached} = _CreateForecastRef($self); $self->{cached} = _CreateForecastRef($self);
@ -160,9 +170,8 @@ sub _RetrieveDataFromOpenWeatherMap($) {
my $self = shift; my $self = shift;
# retrieve data from cache # retrieve data from cache
if( $self->{endpoint} eq 'none' ) { if ( $self->{endpoint} eq 'none' ) {
if ( ( time() - $self->{fetchTime} ) < $self->{cachemaxage} ) if ( ( time() - $self->{fetchTime} ) < $self->{cachemaxage} ) {
{
return _CallWeatherCallbackFn($self); return _CallWeatherCallbackFn($self);
} }
} }
@ -229,67 +238,74 @@ sub _RetrieveDataFinished($$$) {
sub _ProcessingRetrieveData($$) { sub _ProcessingRetrieveData($$) {
my ( $self, $response ) = @_; my ( $self, $response ) = @_;
if ( $self->{cached}->{status} eq 'ok' and defined($response) ) { if ( $self->{cached}->{status} eq 'ok'
and defined($response)
and $response )
{
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} ) and $data->{cod} != 200 and defined( $data->{message} ) ) { elsif ( defined( $data->{cod} )
and $data->{cod}
and $data->{cod} != 200
and defined( $data->{message} )
and $data->{message} )
{
_ErrorHandling( $self, $data->{cod} . ': ' . $data->{message} ); _ErrorHandling( $self, $data->{cod} . ': ' . $data->{message} );
} }
else { else {
###### 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} = strftime( $self->{cached}->{current_date_time} =
"%a,%e %b %Y %H:%M %p", strftime( "%a,%e %b %Y %H:%M %p",
localtime( $self->{fetchTime} ) localtime( $self->{fetchTime} ) );
);
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} = $data->{name}; $self->{cached}->{city} = $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", ( $data->{main}->{temp} - 273.15 ) ) +
) + 0.5 0.5
), ),
'temp_c' => int( 'temp_c' => int(
sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 ) sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 ) ) +
) + 0.5 0.5
), ),
'low_c' => int( 'low_c' => int(
sprintf( "%.1f", ( $data->{main}->{temp_min} - 273.15 ) sprintf( "%.1f",
) + 0.5 ( $data->{main}->{temp_min} - 273.15 ) ) + 0.5
), ),
'high_c' => int( 'high_c' => int(
sprintf( "%.1f", ( $data->{main}->{temp_max} - 273.15 ) sprintf( "%.1f",
) + 0.5 ( $data->{main}->{temp_max} - 273.15 ) ) + 0.5
), ),
'tempLow' => int( 'tempLow' => int(
sprintf( "%.1f", ( $data->{main}->{temp_min} - 273.15 ) sprintf( "%.1f",
) + 0.5 ( $data->{main}->{temp_min} - 273.15 ) ) + 0.5
), ),
'tempHigh' => int( 'tempHigh' => int(
sprintf( "%.1f", ( $data->{main}->{temp_max} - 273.15 ) sprintf( "%.1f",
) + 0.5 ( $data->{main}->{temp_max} - 273.15 ) ) + 0.5
), ),
'humidity' => $data->{main}->{humidity}, 'humidity' => $data->{main}->{humidity},
'condition' => encode_utf8( $data->{weather}->[0]->{description} ), 'condition' =>
'pressure' => int(sprintf( "%.1f", encode_utf8( $data->{weather}->[0]->{description} ),
$data->{main}->{pressure}) + 0.5 'pressure' =>
), int( sprintf( "%.1f", $data->{main}->{pressure} ) + 0.5 ),
'wind' => int(sprintf( "%.1f", 'wind' =>
$data->{wind}->{speed}) + 0.5 ), int( sprintf( "%.1f", $data->{wind}->{speed} ) + 0.5 ),
'wind_speed' => int(sprintf( "%.1f", 'wind_speed' =>
$data->{wind}->{speed}) + 0.5 ), int( sprintf( "%.1f", $data->{wind}->{speed} ) + 0.5 ),
'wind_direction' => $data->{wind}->{deg}, 'wind_direction' => $data->{wind}->{deg},
'cloudCover' => $data->{clouds}->{all}, 'cloudCover' => $data->{clouds}->{all},
'visibility' => int(sprintf( "%.1f", 'visibility' =>
$data->{visibility}) + 0.5 ), int( sprintf( "%.1f", $data->{visibility} ) + 0.5 ),
'code' => $codes{ $data->{weather}->[0]->{id} }, 'code' => $codes{ $data->{weather}->[0]->{id} },
'iconAPI' => $data->{weather}->[0]->{icon}, 'iconAPI' => $data->{weather}->[0]->{icon},
'sunsetTime' => strftime( 'sunsetTime' => strftime(
"%a,%e %b %Y %H:%M %p", "%a,%e %b %Y %H:%M %p",
@ -299,8 +315,10 @@ sub _ProcessingRetrieveData($$) {
"%a,%e %b %Y %H:%M %p", "%a,%e %b %Y %H:%M %p",
localtime( $data->{sys}->{sunrise} ) localtime( $data->{sys}->{sunrise} )
), ),
'pubDate' => 'pubDate' => strftime(
strftime( "%a,%e %b %Y %H:%M %p", localtime( $data->{dt} ) ), "%a,%e %b %Y %H:%M %p",
localtime( $data->{dt} )
),
}; };
} }
@ -310,65 +328,110 @@ sub _ProcessingRetrieveData($$) {
{ {
## löschen des alten Datensatzes ## löschen des alten Datensatzes
delete $self->{cached}->{forecast}; delete $self->{cached}->{forecast};
my $i = 0; my $i = 0;
foreach ( @{ $data->{list} } ) { foreach ( @{ $data->{list} } ) {
push( push(
@{ $self->{cached}->{forecast}->{hourly} }, @{ $self->{cached}->{forecast}->{hourly} },
{ {
'temperature' => int( 'temperature' => int(
sprintf( "%.1f", ( $data->{list}->[$i]->{main}->{temp} - 273.15 ) sprintf(
"%.1f",
(
$data->{list}->[$i]->{main}->{temp}
- 273.15
)
) + 0.5 ) + 0.5
), ),
'temp_c' => int( 'temp_c' => int(
sprintf( "%.1f", ( $data->{list}->[$i]->{main}->{temp} - 273.15 ) sprintf(
"%.1f",
(
$data->{list}->[$i]->{main}->{temp}
- 273.15
)
) + 0.5 ) + 0.5
), ),
'low_c' => int( 'low_c' => int(
sprintf( "%.1f", ( $data->{list}->[$i]->{main}->{temp_min} - 273.15 ) sprintf(
"%.1f",
(
$data->{list}->[$i]->{main}
->{temp_min} - 273.15
)
) + 0.5 ) + 0.5
), ),
'high_c' => int( 'high_c' => int(
sprintf( "%.1f", ( $data->{list}->[$i]->{main}->{temp_max} - 273.15 ) sprintf(
"%.1f",
(
$data->{list}->[$i]->{main}
->{temp_max} - 273.15
)
) + 0.5 ) + 0.5
), ),
'tempLow' => int( 'tempLow' => int(
sprintf( "%.1f", ( $data->{list}->[$i]->{main}->{temp_min} - 273.15 ) sprintf(
"%.1f",
(
$data->{list}->[$i]->{main}
->{temp_min} - 273.15
)
) + 0.5 ) + 0.5
), ),
'tempHigh' => int( 'tempHigh' => int(
sprintf( "%.1f", ( $data->{list}->[$i]->{main}->{temp_max} - 273.15 ) sprintf(
"%.1f",
(
$data->{list}->[$i]->{main}
->{temp_max} - 273.15
)
) + 0.5 ) + 0.5
), ),
'humidity' => $data->{list}->[$i]->{main}->{humidity}, 'humidity' =>
'condition' => encode_utf8( $data->{list}->[$i]->{weather}->[0]->{description} ), $data->{list}->[$i]->{main}->{humidity},
'pressure' => int(sprintf( "%.1f", 'condition' => encode_utf8(
$data->{list}->[$i]->{main}->{pressure}) $data->{list}->[$i]->{weather}->[0]
+ 0.5 ->{description}
), ),
'wind' => int(sprintf( "%.1f", 'pressure' => int(
$data->{list}->[$i]->{wind}->{speed} sprintf( "%.1f",
) + 0.5 $data->{list}->[$i]->{main}->{pressure}
) + 0.5
), ),
'wind_speed' => int(sprintf( "%.1f", 'wind' => int(
$data->{list}->[$i]->{wind}->{speed} sprintf( "%.1f",
) + 0.5 $data->{list}->[$i]->{wind}->{speed} )
+ 0.5
),
'wind_speed' => int(
sprintf( "%.1f",
$data->{list}->[$i]->{wind}->{speed} )
+ 0.5
),
'cloudCover' =>
$data->{list}->[$i]->{clouds}->{all},
'code' =>
$codes{ $data->{list}->[$i]->{weather}->[0]
->{id} },
'iconAPI' =>
$data->{list}->[$i]->{weather}->[0]->{icon},
'pubDate' => strftime(
"%a,%e %b %Y %H:%M %p",
localtime(
( $data->{list}->[$i]->{dt} ) - 3600
)
), ),
'cloudCover' => $data->{list}->[$i]->{clouds}->{all},
'code' => $codes{ $data->{list}->[$i]->{weather}->[0]->{id} },
'iconAPI' => $data->{list}->[$i]->{weather}->[0]->{icon},
'pubDate' =>
strftime( "%a,%e %b %Y %H:%M %p", localtime( ($data->{list}->[$i]->{dt}) - 3600 ) ),
}, },
); );
$i++; $i++;
} }
} }
} }
} }
} }
$self->{endpoint} = 'none' if ( $self->{endpoint} eq 'forecast' ); $self->{endpoint} = 'none' if ( $self->{endpoint} eq 'forecast' );
_RetrieveDataFromOpenWeatherMap($self) _RetrieveDataFromOpenWeatherMap($self)
@ -379,7 +442,8 @@ sub _ProcessingRetrieveData($$) {
sub _CallWeatherCallbackFn($) { sub _CallWeatherCallbackFn($) {
my $self = shift; my $self = shift;
# print 'Dumperausgabe: ' . Dumper $self;
# print 'Dumperausgabe: ' . Dumper $self;
### Aufruf der callbackFn ### Aufruf der callbackFn
main::Weather_RetrieveCallbackFn( $self->{devName} ); main::Weather_RetrieveCallbackFn( $self->{devName} );
} }
@ -388,8 +452,8 @@ sub _ErrorHandling($$) {
my ( $self, $err ) = @_; my ( $self, $err ) = @_;
$self->{cached}->{current_date_time} = $self->{cached}->{current_date_time} =
strftime( "%a,%e %b %Y %H:%M %p", localtime( $self->{fetchTime} ) ), strftime( "%a,%e %b %Y %H:%M %p", localtime( $self->{fetchTime} ) ),
$self->{cached}->{status} = $err; $self->{cached}->{status} = $err;
$self->{cached}->{validity} = 'stale'; $self->{cached}->{validity} = 'stale';
} }