publish forecast data in OpenWeatherMap, code change in 59_Weather, change response data structure for Weather Modul

This commit is contained in:
Marko Oldenburg 2019-01-10 09:30:26 +01:00
parent c49d955687
commit 5b95ea31e4
3 changed files with 86 additions and 93 deletions

View File

@ -364,7 +364,7 @@ sub Weather_WriteReadings($$) {
readingsBulkUpdate($hash, 'lastError', ''); readingsBulkUpdate($hash, 'lastError', '');
foreach my $r (keys %{$dataRef} ) { foreach my $r (keys %{$dataRef} ) {
readingsBulkUpdate($hash, $r, $dataRef->{$r}) readingsBulkUpdate($hash, $r, $dataRef->{$r})
if ( ref($dataRef->{$r}) ne 'HASH' ); if ( ref($dataRef->{$r}) ne 'HASH' and ref($dataRef->{$r}) ne 'ARRAY' );
} }
readingsBulkUpdate($hash, "validity", "up-to-date"); readingsBulkUpdate($hash, "validity", "up-to-date");
@ -373,7 +373,8 @@ sub Weather_WriteReadings($$) {
# 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)
if ( ref($dataRef->{$r}) ne 'HASH' and ref($dataRef->{$r}) ne 'ARRAY' );
} }
readingsBulkUpdate($hash, 'icon', $iconlist[$dataRef->{current}->{code}]); readingsBulkUpdate($hash, 'icon', $iconlist[$dataRef->{current}->{code}]);
@ -386,36 +387,27 @@ sub Weather_WriteReadings($$) {
} }
# forecast # forecast
if ( defined($dataRef->{forecast}) and ref( $dataRef->{forecast} ) eq 'HASH' ) { if ( defined($dataRef->{forecast}) and ref( $dataRef->{forecast} ) eq 'ARRAY'
## Forecast for hourly and scalar( @{ $dataRef->{forecast} } ) > 0 )
if ( defined($dataRef->{forecast}->{hourly}) and ref( $dataRef->{forecast}->{hourly} ) eq "ARRAY" {
and scalar( @{ $dataRef->{forecast}->{daily} } ) > 0 ) my $i= 0;
{ foreach my $fc (@{$dataRef->{forecast}}) {
$i++;
my $f= "fc" . $i ."_";
} while( my ($r,$v) = each %{$fc} ) {
readingsBulkUpdate($hash, $f.$r, $v)
## Forecast for Daily if ( ref($dataRef->{$r}) ne 'HASH' and ref($dataRef->{$r}) ne 'ARRAY' );
if ( defined($dataRef->{forecast}->{daily}) and ref( $dataRef->{forecast}->{daily} ) eq "ARRAY"
and scalar( @{ $dataRef->{forecast}->{daily} } ) > 0 )
{
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);
}
# 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}[$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})
and defined($dataRef->{forecast}[$i-1]{wind_speed}) )
{
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');
}
# readingsBulkUpdate($hash, $f . 'day_of_week', $wdays_txt_i18n{substr($dataRef->{forecast}[$i-1]{date},0,3)});
} }
} }
@ -470,7 +462,7 @@ sub Weather_Get($@) {
} else { } else {
my $rt= ""; my $rt= "";
if(defined($hash->{READINGS})) { if(defined($hash->{READINGS})) {
$rt= join(" ", sort keys %{$hash->{READINGS}}); $rt= join(":noArg ", sort keys %{$hash->{READINGS}});
} }
return "Unknown reading $reading, choose one of " . $rt; return "Unknown reading $reading, choose one of " . $rt;
} }
@ -490,7 +482,7 @@ sub Weather_Set($@) {
Weather_GetUpdate($hash); Weather_GetUpdate($hash);
return undef; return undef;
} else { } else {
return "Unknown argument $cmd, choose one of update"; return "Unknown argument $cmd, choose one of update:noArg";
} }
} }
@ -630,8 +622,7 @@ use constant ICONSCALE => 0.5;
##################################### #####################################
sub sub WeatherIconIMGTag($) {
WeatherIconIMGTag($) {
my $width= int(ICONSCALE*ICONWIDTH); my $width= int(ICONSCALE*ICONWIDTH);
my ($icon)= @_; my ($icon)= @_;
@ -643,8 +634,7 @@ WeatherIconIMGTag($) {
##################################### #####################################
sub sub WeatherAsHtmlV($;$)
WeatherAsHtmlV($;$)
{ {
my ($d,$items) = @_; my ($d,$items) = @_;
@ -676,20 +666,18 @@ WeatherAsHtmlV($;$)
return $ret; return $ret;
} }
sub sub WeatherAsHtml($;$)
WeatherAsHtml($;$)
{ {
my ($d,$i) = @_; my ($d,$i) = @_;
WeatherAsHtmlV($d,$i); WeatherAsHtmlV($d,$i);
} }
sub sub WeatherAsHtmlH($;$)
WeatherAsHtmlH($;$)
{ {
my ($d,$items) = @_; my ($d,$items) = @_;
$d = "<none>" if(!$d); $d = "<none>" if(!$d);
$items = 10 if( !$items ); $items = 9 if( !$items );
return "$d is not a Weather instance<br>" return "$d is not a Weather instance<br>"
if(!$defs{$d} || $defs{$d}->{TYPE} ne "Weather"); if(!$defs{$d} || $defs{$d}->{TYPE} ne "Weather");
@ -733,8 +721,7 @@ WeatherAsHtmlH($;$)
return $ret; return $ret;
} }
sub sub WeatherAsHtmlD($;$)
WeatherAsHtmlD($;$)
{ {
my ($d,$i) = @_; my ($d,$i) = @_;
if($FW_ss) { if($FW_ss) {

View File

@ -232,194 +232,197 @@ sub _ProcessingRetrieveData($$) {
if ( ref( $data->{daily}->{data} ) eq "ARRAY" if ( ref( $data->{daily}->{data} ) eq "ARRAY"
and scalar( @{ $data->{daily}->{data} } ) > 0 ) and scalar( @{ $data->{daily}->{data} } ) > 0 )
{ {
### löschen des alten Datensatzes
delete $self->{cached}->{forecast};
my $i = 0; my $i = 0;
foreach ( @{ $data->{daily}->{data} } ) { foreach ( @{ $data->{daily}->{data} } ) {
push( push(
@{ $self->{cached}->{forecast}->{daily} }, @{ $self->{cached}->{forecast} },
{ {
'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]->{temperatureLow} )
+ 0.5 + 0.5
), ),
'high_c' => int( 'high_c' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily}->{data}[$i]{temperatureHigh} $data->{daily}->{data}->[$i]->{temperatureHigh}
) + 0.5 ) + 0.5
), ),
'tempMin' => int( 'tempMin' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily}->{data}[$i]{temperatureMin} ) $data->{daily}->{data}->[$i]->{temperatureMin} )
+ 0.5 + 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]{temperatureMinTime} ->{data}->[$i]->{temperatureMinTime}
) )
), ),
'tempMax' => int( 'tempMax' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily}->{data}[$i]{temperatureMax} ) $data->{daily}->{data}->[$i]->{temperatureMax} )
+ 0.5 + 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]{temperatureMaxTime} ->{data}->[$i]->{temperatureMaxTime}
) )
), ),
'tempLow' => int( 'tempLow' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily}->{data}[$i]{temperatureLow} ) $data->{daily}->{data}->[$i]->{temperatureLow} )
+ 0.5 + 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]{temperatureLowTime} ->{data}->[$i]->{temperatureLowTime}
) )
), ),
'tempHigh' => int( 'tempHigh' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily}->{data}[$i]{temperatureHigh} $data->{daily}->{data}->[$i]->{temperatureHigh}
) + 0.5 ) + 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]{temperatureHighTime} ->{data}->[$i]->{temperatureHighTime}
) )
), ),
'apparentTempLow' => int( 'apparentTempLow' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily} $data->{daily}
->{data}[$i]{apparentTemperatureLow} ) + ->{data}->[$i]->{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]{apparentTemperatureLowTime} ->{data}->[$i]->{apparentTemperatureLowTime}
) )
), ),
'apparentTempHigh' => int( 'apparentTempHigh' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily} $data->{daily}
->{data}[$i]{apparentTemperatureHigh} ) + ->{data}->[$i]->{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]{apparentTemperatureHighTime} ->{data}->[$i]->{apparentTemperatureHighTime}
) )
), ),
'apparenttempMin' => int( 'apparenttempMin' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily} $data->{daily}
->{data}[$i]{apparentTemperatureMin} ) + ->{data}->[$i]->{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]{apparentTemperatureMinTime} ->{data}->[$i]->{apparentTemperatureMinTime}
) )
), ),
'apparenttempMax' => int( 'apparenttempMax' => int(
sprintf( "%.1f", sprintf( "%.1f",
$data->{daily} $data->{daily}
->{data}[$i]{apparentTemperatureMax} ) + ->{data}->[$i]->{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]{apparentTemperatureMaxTime} ->{data}->[$i]->{apparentTemperatureMaxTime}
) )
), ),
'code' => 'code' =>
$codes{ $data->{daily}->{data}[$i]{icon} }, $codes{ $data->{daily}->{data}->[$i]->{icon} },
'iconAPI' => $data->{daily}->{data}[$i]{icon}, 'iconAPI' => $data->{daily}->{data}->[$i]->{icon},
'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(
$data->{daily}->{data}[$i]{uvIndexTime} $data->{daily}->{data}->[$i]->{uvIndexTime}
) )
), ),
'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]{precipIntensityMaxTime} ->{data}->[$i]->{precipIntensityMaxTime}
) )
), ),
'dewPoint' => $data->{daily}->{data}[$i]{dewPoint}, 'dewPoint' => $data->{daily}->{data}->[$i]->{dewPoint},
'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},
'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' => $data->{daily}->{data}->[$i]->{windSpeed},
'wind_speed' => 'wind_speed' =>
$data->{daily}->{data}[$i]{windSpeed}, $data->{daily}->{data}->[$i]->{windSpeed},
'windGust' => $data->{daily}->{data}[$i]{windGust}, 'windGust' => $data->{daily}->{data}->[$i]->{windGust},
'windGustTime' => strftime( 'windGustTime' => strftime(
"%a,%e %b %Y %H:%M %p", "%a,%e %b %Y %H:%M %p",
localtime( localtime(
$data->{daily}->{data}[$i]{windGustTime} $data->{daily}->{data}->[$i]->{windGustTime}
) )
), ),
'moonPhase' => 'moonPhase' =>
$data->{daily}->{data}[$i]{moonPhase}, $data->{daily}->{data}->[$i]->{moonPhase},
'sunsetTime' => strftime( 'sunsetTime' => strftime(
"%a,%e %b %Y %H:%M %p", "%a,%e %b %Y %H:%M %p",
localtime( localtime(
$data->{daily}->{data}[$i]{sunsetTime} $data->{daily}->{data}->[$i]->{sunsetTime}
) )
), ),
'sunriseTime' => strftime( 'sunriseTime' => strftime(
"%a,%e %b %Y %H:%M %p", "%a,%e %b %Y %H:%M %p",
localtime( localtime(
$data->{daily}->{data}[$i]{sunriseTime} $data->{daily}->{data}->[$i]->{sunriseTime}
) )
), ),
'precipProbability' => 'precipProbability' =>
$data->{daily}->{data}[$i]{precipProbability}, $data->{daily}->{data}->[$i]->{precipProbability},
'pressure' => $data->{daily}->{data}[$i]{pressure}, 'pressure' => $data->{daily}->{data}->[$i]->{pressure},
'visibility' => 'visibility' =>
$data->{daily}->{data}[$i]{visibility}, $data->{daily}->{data}->[$i]->{visibility},
} }
); );

View File

@ -297,14 +297,17 @@ sub _ProcessingRetrieveData($$) {
}; };
} }
if ( $self->{endpoint} eq 'forecas' ) { if ( $self->{endpoint} eq 'forecast' ) {
my $i = 0;
if ( ref( $data->{list} ) eq "ARRAY" if ( ref( $data->{list} ) eq "ARRAY"
and scalar( @{ $data->{list} } ) > 0 ) and scalar( @{ $data->{list} } ) > 0 )
{ {
## löschen des alten Datensatzes
delete $self->{cached}->{forecast};
my $i = 0;
foreach ( @{ $data->{list} } ) { foreach ( @{ $data->{list} } ) {
push( push(
@{ $self->{cached}->{forecast}->{daily} }, @{ $self->{cached}->{forecast} },
{ {
'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
@ -340,7 +343,7 @@ sub _ProcessingRetrieveData($$) {
'iconAPI' => $data->{list}[$i]{weather}[0]{icon}, 'iconAPI' => $data->{list}[$i]{weather}[0]{icon},
'pubDate' => 'pubDate' =>
strftime( "%a,%e %b %Y %H:%M %p", localtime( $data->{list}[$i]{dt} ) ), strftime( "%a,%e %b %Y %H:%M %p", localtime( ($data->{list}[$i]{dt}) - 3600 ) ),
} }
); );