forcast Datastructure change to HashRef daily and hourly

This commit is contained in:
Marko Oldenburg 2019-01-10 14:08:31 +01:00
parent 1a9d7ad760
commit 9c9e790d88
3 changed files with 152 additions and 82 deletions

View File

@ -191,13 +191,12 @@ sub Weather_ReturnWithError($$) {
# readingsBulkUpdate($hash, "pubDateRemote", $pubDate) if(defined($pubDate)); # readingsBulkUpdate($hash, "pubDateRemote", $pubDate) if(defined($pubDate));
# readingsBulkUpdate($hash, "validity", "stale"); # readingsBulkUpdate($hash, "validity", "stale");
# readingsEndUpdate($hash, $doTrigger); # readingsEndUpdate($hash, $doTrigger);
print 'In der Return with Error Funktion' . "\n";
readingsBeginUpdate($hash); readingsBeginUpdate($hash);
readingsBulkUpdate($hash, 'lastError', $responseRef->{status}); readingsBulkUpdate($hash, 'lastError', $responseRef->{status});
foreach my $r (keys %{$responseRef} ) { foreach my $r (keys %{$responseRef} ) {
readingsBulkUpdate($hash, $r, $responseRef->{$r}) if ($r ne 'status' or ($r ne 'current' and ref($r) ne 'HASH') ); readingsBulkUpdate($hash, $r, $responseRef->{$r}) if ( ref($responseRef->{$r}) ne 'HASH' );
} }
readingsBulkUpdate($hash, 'state', 'API Maintainer: ' . $responseRef->{apiMaintainer} . ' ErrorMsg: ' . $responseRef->{status}); readingsBulkUpdate($hash, 'state', 'API Maintainer: ' . $responseRef->{apiMaintainer} . ' ErrorMsg: ' . $responseRef->{status});
readingsEndUpdate($hash, 1); readingsEndUpdate($hash, 1);
@ -370,7 +369,7 @@ sub Weather_WriteReadings($$) {
readingsBulkUpdate($hash, "validity", "up-to-date"); readingsBulkUpdate($hash, "validity", "up-to-date");
# current ### current
if ( defined($dataRef->{current}) and ref( $dataRef->{current} ) eq 'HASH' ) { if ( defined($dataRef->{current}) and ref( $dataRef->{current} ) eq 'HASH' ) {
while( my ($r,$v) = each %{$dataRef->{current}} ) { while( my ($r,$v) = each %{$dataRef->{current}} ) {
readingsBulkUpdate($hash, $r, $v) readingsBulkUpdate($hash, $r, $v)
@ -386,28 +385,56 @@ sub Weather_WriteReadings($$) {
} }
} }
# forecast ### forecast
if ( defined($dataRef->{forecast}) and ref( $dataRef->{forecast} ) eq 'ARRAY' if ( ref( $dataRef->{forecast} ) eq 'HASH' ) {
and scalar( @{ $dataRef->{forecast} } ) > 0 ) ## hourly
{ if ( defined($dataRef->{forecast}->{hourly}) and ref( $dataRef->{forecast}->{hourly} ) eq 'ARRAY'
my $i= 0; and scalar( @{ $dataRef->{forecast}->{hourly} } ) > 0 )
foreach my $fc (@{$dataRef->{forecast}}) { {
$i++; my $i= 0;
my $f= "fc" . $i ."_"; foreach my $fc (@{$dataRef->{forecast}->{hourly}}) {
$i++;
while( my ($r,$v) = each %{$fc} ) { my $f= "hfc" . $i ."_";
readingsBulkUpdate($hash, $f.$r, $v)
if ( ref($dataRef->{$r}) ne 'HASH' and ref($dataRef->{$r}) ne 'ARRAY' ); while( my ($r,$v) = each %{$fc} ) {
readingsBulkUpdate($hash, $f.$r, $v)
if ( ref($dataRef->{$r}) ne 'HASH' and ref($dataRef->{$r}) ne 'ARRAY' );
}
# readingsBulkUpdate($hash, $f . "day_of_week", $wdays_txt_i18n{$fc->{day}});
readingsBulkUpdate($hash, $f . 'icon', $iconlist[$dataRef->{forecast}->{hourly}[$i-1]{code}]);
if ( defined($dataRef->{forecast}->{hourly}[$i-1]{wind_direction})
and defined($dataRef->{forecast}->{hourly}[$i-1]{wind_speed}) )
{
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 . 'day_of_week', $wdays_txt_i18n{substr($dataRef->{forecast}->{hourly}[$i-1]{date},0,3)});
} }
# readingsBulkUpdate($hash, $f . "day_of_week", $wdays_txt_i18n{$fc->{day}}); }
readingsBulkUpdate($hash, $f . 'icon', $iconlist[$dataRef->{forecast}[$i-1]{code}]);
if ( defined($dataRef->{forecast}[$i-1]{wind_direction}) ## daily
and defined($dataRef->{forecast}[$i-1]{wind_speed}) ) if ( defined($dataRef->{forecast}->{daily}) and ref( $dataRef->{forecast}->{daily} ) eq 'ARRAY'
{ and scalar( @{ $dataRef->{forecast}->{daily} } ) > 0 )
my $wdir= degrees_to_direction($dataRef->{forecast}[$i-1]{wind_direction}, @directions_txt_i18n); {
readingsBulkUpdate($hash, $f . 'wind_condition', 'Wind: ' . $wdir . ' ' . $dataRef->{forecast}[$i-1]{wind_speed} . ' km/h'); my $i= 0;
foreach my $fc (@{$dataRef->{forecast}->{daily}}) {
$i++;
my $f= "fc" . $i ."_";
while( my ($r,$v) = each %{$fc} ) {
readingsBulkUpdate($hash, $f.$r, $v)
if ( ref($dataRef->{$r}) ne 'HASH' and ref($dataRef->{$r}) ne 'ARRAY' );
}
# readingsBulkUpdate($hash, $f . "day_of_week", $wdays_txt_i18n{$fc->{day}});
readingsBulkUpdate($hash, $f . 'icon', $iconlist[$dataRef->{forecast}->{daily}[$i-1]{code}]);
if ( defined($dataRef->{forecast}->{daily}[$i-1]{wind_direction})
and defined($dataRef->{forecast}->{daily}[$i-1]{wind_speed}) )
{
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 . 'day_of_week', $wdays_txt_i18n{substr($dataRef->{forecast}->{daily}[$i-1]{date},0,3)});
} }
# readingsBulkUpdate($hash, $f . 'day_of_week', $wdays_txt_i18n{substr($dataRef->{forecast}[$i-1]{date},0,3)});
} }
} }

View File

@ -192,7 +192,7 @@ sub _ProcessingRetrieveData($$) {
localtime( $self->{fetchTime} ) localtime( $self->{fetchTime} )
); );
$self->{cached}->{timezone} = $data->{timezone}; $self->{cached}->{timezone} = $data->{timezone};
$self->{cached}->{license} = $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
@ -205,14 +205,24 @@ sub _ProcessingRetrieveData($$) {
), ),
'humidity' => $data->{currently}->{humidity} * 100, 'humidity' => $data->{currently}->{humidity} * 100,
'condition' => encode_utf8( $data->{currently}->{summary} ), 'condition' => encode_utf8( $data->{currently}->{summary} ),
'pressure' => $data->{currently}->{pressure}, 'pressure' => int(
'wind' => $data->{currently}->{windSpeed}, sprintf( "%.1f", $data->{currently}->{pressure} ) + 0.5
'wind_speed' => $data->{currently}->{windSpeed}, ),
'wind' => int(
sprintf( "%.1f", $data->{currently}->{windSpeed} ) + 0.5
),
'wind_speed' => int(
sprintf( "%.1f", $data->{currently}->{windSpeed} ) + 0.5
),
'wind_direction' => $data->{currently}->{windBearing}, 'wind_direction' => $data->{currently}->{windBearing},
'windGust' => $data->{currently}->{windGust}, 'windGust' => int(
'cloudCover' => $data->{currently}->{cloudCover}, sprintf( "%.1f", $data->{currently}->{windGust} ) + 0.5
),
'cloudCover' => $data->{currently}->{cloudCover} * 100,
'uvIndex' => $data->{currently}->{uvIndex}, 'uvIndex' => $data->{currently}->{uvIndex},
'visibility' => $data->{currently}->{visibility}, 'visibility' => int(
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},
@ -238,7 +248,7 @@ sub _ProcessingRetrieveData($$) {
my $i = 0; my $i = 0;
foreach ( @{ $data->{daily}->{data} } ) { foreach ( @{ $data->{daily}->{data} } ) {
push( push(
@{ $self->{cached}->{forecast} }, @{ $self->{cached}->{forecast}->{daily} },
{ {
'date' => strftime( 'date' => strftime(
"%a, %d.%m.%Y", "%a, %d.%m.%Y",
@ -383,20 +393,34 @@ sub _ProcessingRetrieveData($$) {
->{data}->[$i]->{precipIntensityMaxTime} ->{data}->[$i]->{precipIntensityMaxTime}
) )
), ),
'dewPoint' => $data->{daily}->{data}->[$i]->{dewPoint}, 'dewPoint' => int(
sprintf( "%.1f",
$data->{daily}->{data}->[$i]->{dewPoint}
) + 0.5
),
'humidity' => $data->{daily}->{data}->[$i]->{humidity} 'humidity' => $data->{daily}->{data}->[$i]->{humidity}
* 100, * 100,
'cloudCover' => 'cloudCover' =>
$data->{daily}->{data}->[$i]->{cloudCover}, $data->{daily}->{data}->[$i]->{cloudCover} * 100,
'precipType' => 'precipType' =>
$data->{daily}->{data}->[$i]->{precipType}, $data->{daily}->{data}->[$i]->{precipType},
'wind_direction' => 'wind_direction' =>
$data->{daily}->{data}->[$i]->{windBearing}, $data->{daily}->{data}->[$i]->{windBearing},
'wind' => $data->{daily}->{data}->[$i]->{windSpeed}, 'wind' => int(
'wind_speed' => sprintf( "%.1f",
$data->{daily}->{data}->[$i]->{windSpeed}, $data->{daily}->{data}->[$i]->{windSpeed}
'windGust' => $data->{daily}->{data}->[$i]->{windGust}, ) + 0.5
),
'wind_speed' => int(
sprintf( "%.1f",
$data->{daily}->{data}->[$i]->{windSpeed}
) + 0.5
),
'windGust' => int(
sprintf( "%.1f",
$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",
localtime( localtime(
@ -420,9 +444,14 @@ sub _ProcessingRetrieveData($$) {
'precipProbability' => 'precipProbability' =>
$data->{daily}->{data}->[$i]->{precipProbability}, $data->{daily}->{data}->[$i]->{precipProbability},
'pressure' => $data->{daily}->{data}->[$i]->{pressure}, 'pressure' => int(
'visibility' => sprintf( "%.1f",
$data->{daily}->{data}->[$i]->{visibility}, $data->{daily}->{data}->[$i]->{pressure}) + 0.5
),
'visibility' => int(
sprintf( "%.1f",
$data->{daily}->{data}->[$i]->{visibility}) + 0.5
),
} }
); );

View File

@ -252,38 +252,44 @@ sub _ProcessingRetrieveData($$) {
$self->{cached}->{city} = $data->{name}; $self->{cached}->{city} = $data->{name};
$self->{cached}->{current} = { $self->{cached}->{current} = {
'temperature' => int( 'temperature' => int(
sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 ) ) + 0.5 sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 )
) + 0.5
), ),
'temp_c' => int( 'temp_c' => int(
sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 ) ) + 0.5 sprintf( "%.1f", ( $data->{main}->{temp} - 273.15 )
) + 0.5
), ),
'low_c' => int( 'low_c' => int(
sprintf( "%.1f", ( $data->{main}->{temp_min} - 273.15 ) ) + sprintf( "%.1f", ( $data->{main}->{temp_min} - 273.15 )
0.5 ) + 0.5
), ),
'high_c' => int( 'high_c' => int(
sprintf( "%.1f", ( $data->{main}->{temp_max} - 273.15 ) ) + sprintf( "%.1f", ( $data->{main}->{temp_max} - 273.15 )
0.5 ) + 0.5
), ),
'tempLow' => int( 'tempLow' => int(
sprintf( "%.1f", ( $data->{main}->{temp_min} - 273.15 ) ) + sprintf( "%.1f", ( $data->{main}->{temp_min} - 273.15 )
0.5 ) + 0.5
), ),
'tempHigh' => int( 'tempHigh' => int(
sprintf( "%.1f", ( $data->{main}->{temp_max} - 273.15 ) ) + sprintf( "%.1f", ( $data->{main}->{temp_max} - 273.15 )
0.5 ) + 0.5
), ),
'humidity' => $data->{main}->{humidity}, 'humidity' => $data->{main}->{humidity},
'condition' => encode_utf8( $data->{weather}[0]{description} ), 'condition' => encode_utf8( $data->{weather}->[0]->{description} ),
'pressure' => $data->{main}->{pressure}, 'pressure' => int(sprintf( "%.1f",
'wind' => $data->{wind}->{speed}, $data->{main}->{pressure}) + 0.5
'wind_speed' => $data->{wind}->{speed}, ),
'wind' => int(sprintf( "%.1f",
$data->{wind}->{speed}) + 0.5 ),
'wind_speed' => 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' => $data->{visibility}, 'visibility' => 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",
localtime( $data->{sys}->{sunset} ) localtime( $data->{sys}->{sunset} )
@ -307,44 +313,52 @@ sub _ProcessingRetrieveData($$) {
my $i = 0; my $i = 0;
foreach ( @{ $data->{list} } ) { foreach ( @{ $data->{list} } ) {
push( push(
@{ $self->{cached}->{forecast} }, @{ $self->{cached}->{forecast}->{hourly} },
{ {
'temperature' => int( 'temperature' => int(
sprintf( "%.1f", ( $data->{list}[$i]{main}->{temp} - 273.15 ) ) + 0.5 sprintf( "%.1f", ( $data->{list}->[$i]->{main}->{temp} - 273.15 )
) + 0.5
), ),
'temp_c' => int( 'temp_c' => int(
sprintf( "%.1f", ( $data->{list}[$i]{main}->{temp} - 273.15 ) ) + 0.5 sprintf( "%.1f", ( $data->{list}->[$i]->{main}->{temp} - 273.15 )
) + 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' => $data->{list}->[$i]->{main}->{humidity},
'condition' => encode_utf8( $data->{list}[$i]{weather}[0]{description} ), 'condition' => encode_utf8( $data->{list}->[$i]->{weather}->[0]->{description} ),
'pressure' => $data->{list}[$i]{main}->{pressure}, 'pressure' => int(sprintf( "%.1f",
'wind' => $data->{list}[$i]{wind}->{speed}, $data->{list}->[$i]->{main}->{pressure})
'wind_speed' => $data->{list}[$i]{wind}->{speed}, + 0.5
),
'cloudCover' => $data->{list}[$i]{clouds}->{all}, 'wind' => int(sprintf( "%.1f",
'visibility' => $data->{visibility}, $data->{list}->[$i]->{wind}->{speed}
'code' => $codes{ $data->{list}[$i]{weather}[0]{id} }, ) + 0.5
'iconAPI' => $data->{list}[$i]{weather}[0]{icon}, ),
'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' => 'pubDate' =>
strftime( "%a,%e %b %Y %H:%M %p", localtime( ($data->{list}[$i]{dt}) - 3600 ) ), strftime( "%a,%e %b %Y %H:%M %p", localtime( ($data->{list}->[$i]->{dt}) - 3600 ) ),
} },
); );
$i++; $i++;