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', '');
foreach my $r (keys %{$dataRef} ) {
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");
@ -373,7 +373,8 @@ sub Weather_WriteReadings($$) {
# current
if ( defined($dataRef->{current}) and ref( $dataRef->{current} ) eq 'HASH' ) {
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}]);
@ -386,36 +387,27 @@ sub Weather_WriteReadings($$) {
}
# forecast
if ( defined($dataRef->{forecast}) and ref( $dataRef->{forecast} ) eq 'HASH' ) {
## Forecast for hourly
if ( defined($dataRef->{forecast}->{hourly}) and ref( $dataRef->{forecast}->{hourly} ) eq "ARRAY"
and scalar( @{ $dataRef->{forecast}->{daily} } ) > 0 )
{
}
## Forecast for Daily
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)});
if ( defined($dataRef->{forecast}) and ref( $dataRef->{forecast} ) eq 'ARRAY'
and scalar( @{ $dataRef->{forecast} } ) > 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)
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}[$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 {
my $rt= "";
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;
}
@ -490,7 +482,7 @@ sub Weather_Set($@) {
Weather_GetUpdate($hash);
return undef;
} 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
WeatherIconIMGTag($) {
sub WeatherIconIMGTag($) {
my $width= int(ICONSCALE*ICONWIDTH);
my ($icon)= @_;
@ -643,8 +634,7 @@ WeatherIconIMGTag($) {
#####################################
sub
WeatherAsHtmlV($;$)
sub WeatherAsHtmlV($;$)
{
my ($d,$items) = @_;
@ -676,20 +666,18 @@ WeatherAsHtmlV($;$)
return $ret;
}
sub
WeatherAsHtml($;$)
sub WeatherAsHtml($;$)
{
my ($d,$i) = @_;
WeatherAsHtmlV($d,$i);
}
sub
WeatherAsHtmlH($;$)
sub WeatherAsHtmlH($;$)
{
my ($d,$items) = @_;
$d = "<none>" if(!$d);
$items = 10 if( !$items );
$items = 9 if( !$items );
return "$d is not a Weather instance<br>"
if(!$defs{$d} || $defs{$d}->{TYPE} ne "Weather");
@ -733,8 +721,7 @@ WeatherAsHtmlH($;$)
return $ret;
}
sub
WeatherAsHtmlD($;$)
sub WeatherAsHtmlD($;$)
{
my ($d,$i) = @_;
if($FW_ss) {

View File

@ -232,194 +232,197 @@ sub _ProcessingRetrieveData($$) {
if ( ref( $data->{daily}->{data} ) eq "ARRAY"
and scalar( @{ $data->{daily}->{data} } ) > 0 )
{
### löschen des alten Datensatzes
delete $self->{cached}->{forecast};
my $i = 0;
foreach ( @{ $data->{daily}->{data} } ) {
push(
@{ $self->{cached}->{forecast}->{daily} },
@{ $self->{cached}->{forecast} },
{
'date' => strftime(
"%a, %d.%m.%Y",
localtime( $data->{daily}->{data}[$i]{'time'} )
localtime( $data->{daily}->{data}->[$i]->{'time'} )
),
'day_of_week' => strftime(
"%a",
localtime( $data->{daily}->{data}[$i]{'time'} )
localtime( $data->{daily}->{data}->[$i]->{'time'} )
),
'low_c' => int(
sprintf( "%.1f",
$data->{daily}->{data}[$i]{temperatureLow} )
$data->{daily}->{data}->[$i]->{temperatureLow} )
+ 0.5
),
'high_c' => int(
sprintf( "%.1f",
$data->{daily}->{data}[$i]{temperatureHigh}
$data->{daily}->{data}->[$i]->{temperatureHigh}
) + 0.5
),
'tempMin' => int(
sprintf( "%.1f",
$data->{daily}->{data}[$i]{temperatureMin} )
$data->{daily}->{data}->[$i]->{temperatureMin} )
+ 0.5
),
'tempMinTime' => strftime(
"%a,%e %b %Y %H:%M %p",
localtime(
$data->{daily}
->{data}[$i]{temperatureMinTime}
->{data}->[$i]->{temperatureMinTime}
)
),
'tempMax' => int(
sprintf( "%.1f",
$data->{daily}->{data}[$i]{temperatureMax} )
$data->{daily}->{data}->[$i]->{temperatureMax} )
+ 0.5
),
'tempMaxTime' => strftime(
"%a,%e %b %Y %H:%M %p",
localtime(
$data->{daily}
->{data}[$i]{temperatureMaxTime}
->{data}->[$i]->{temperatureMaxTime}
)
),
'tempLow' => int(
sprintf( "%.1f",
$data->{daily}->{data}[$i]{temperatureLow} )
$data->{daily}->{data}->[$i]->{temperatureLow} )
+ 0.5
),
'tempLowTime' => strftime(
"%a,%e %b %Y %H:%M %p",
localtime(
$data->{daily}
->{data}[$i]{temperatureLowTime}
->{data}->[$i]->{temperatureLowTime}
)
),
'tempHigh' => int(
sprintf( "%.1f",
$data->{daily}->{data}[$i]{temperatureHigh}
$data->{daily}->{data}->[$i]->{temperatureHigh}
) + 0.5
),
'tempHighTime' => strftime(
"%a,%e %b %Y %H:%M %p",
localtime(
$data->{daily}
->{data}[$i]{temperatureHighTime}
->{data}->[$i]->{temperatureHighTime}
)
),
'apparentTempLow' => int(
sprintf( "%.1f",
$data->{daily}
->{data}[$i]{apparentTemperatureLow} ) +
->{data}->[$i]->{apparentTemperatureLow} ) +
0.5
),
'apparentTempLowTime' => strftime(
"%a,%e %b %Y %H:%M %p",
localtime(
$data->{daily}
->{data}[$i]{apparentTemperatureLowTime}
->{data}->[$i]->{apparentTemperatureLowTime}
)
),
'apparentTempHigh' => int(
sprintf( "%.1f",
$data->{daily}
->{data}[$i]{apparentTemperatureHigh} ) +
->{data}->[$i]->{apparentTemperatureHigh} ) +
0.5
),
'apparentTempHighTime' => strftime(
"%a,%e %b %Y %H:%M %p",
localtime(
$data->{daily}
->{data}[$i]{apparentTemperatureHighTime}
->{data}->[$i]->{apparentTemperatureHighTime}
)
),
'apparenttempMin' => int(
sprintf( "%.1f",
$data->{daily}
->{data}[$i]{apparentTemperatureMin} ) +
->{data}->[$i]->{apparentTemperatureMin} ) +
0.5
),
'apparenttempMinTime' => strftime(
"%a,%e %b %Y %H:%M %p",
localtime(
$data->{daily}
->{data}[$i]{apparentTemperatureMinTime}
->{data}->[$i]->{apparentTemperatureMinTime}
)
),
'apparenttempMax' => int(
sprintf( "%.1f",
$data->{daily}
->{data}[$i]{apparentTemperatureMax} ) +
->{data}->[$i]->{apparentTemperatureMax} ) +
0.5
),
'apparenttempMaxTime' => strftime(
"%a,%e %b %Y %H:%M %p",
localtime(
$data->{daily}
->{data}[$i]{apparentTemperatureMaxTime}
->{data}->[$i]->{apparentTemperatureMaxTime}
)
),
'code' =>
$codes{ $data->{daily}->{data}[$i]{icon} },
'iconAPI' => $data->{daily}->{data}[$i]{icon},
$codes{ $data->{daily}->{data}->[$i]->{icon} },
'iconAPI' => $data->{daily}->{data}->[$i]->{icon},
'condition' => encode_utf8(
$data->{daily}->{data}[$i]{summary}
$data->{daily}->{data}->[$i]->{summary}
),
'ozone' => $data->{daily}->{data}[$i]{ozone},
'uvIndex' => $data->{daily}->{data}[$i]{uvIndex},
'ozone' => $data->{daily}->{data}->[$i]->{ozone},
'uvIndex' => $data->{daily}->{data}->[$i]->{uvIndex},
'uvIndexTime' => strftime(
"%a,%e %b %Y %H:%M %p",
localtime(
$data->{daily}->{data}[$i]{uvIndexTime}
$data->{daily}->{data}->[$i]->{uvIndexTime}
)
),
'precipIntensity' =>
$data->{daily}->{data}[$i]{precipIntensity},
$data->{daily}->{data}->[$i]->{precipIntensity},
'precipIntensityMax' =>
$data->{daily}->{data}[$i]{precipIntensityMax},
$data->{daily}->{data}->[$i]->{precipIntensityMax},
'precipIntensityMaxTime' => strftime(
"%a,%e %b %Y %H:%M %p",
localtime(
$data->{daily}
->{data}[$i]{precipIntensityMaxTime}
->{data}->[$i]->{precipIntensityMaxTime}
)
),
'dewPoint' => $data->{daily}->{data}[$i]{dewPoint},
'humidity' => $data->{daily}->{data}[$i]{humidity}
'dewPoint' => $data->{daily}->{data}->[$i]->{dewPoint},
'humidity' => $data->{daily}->{data}->[$i]->{humidity}
* 100,
'cloudCover' =>
$data->{daily}->{data}[$i]{cloudCover},
$data->{daily}->{data}->[$i]->{cloudCover},
'precipType' =>
$data->{daily}->{data}[$i]{precipType},
$data->{daily}->{data}->[$i]->{precipType},
'wind_direction' =>
$data->{daily}->{data}[$i]{windBearing},
'wind' => $data->{daily}->{data}[$i]{windSpeed},
$data->{daily}->{data}->[$i]->{windBearing},
'wind' => $data->{daily}->{data}->[$i]->{windSpeed},
'wind_speed' =>
$data->{daily}->{data}[$i]{windSpeed},
'windGust' => $data->{daily}->{data}[$i]{windGust},
$data->{daily}->{data}->[$i]->{windSpeed},
'windGust' => $data->{daily}->{data}->[$i]->{windGust},
'windGustTime' => strftime(
"%a,%e %b %Y %H:%M %p",
localtime(
$data->{daily}->{data}[$i]{windGustTime}
$data->{daily}->{data}->[$i]->{windGustTime}
)
),
'moonPhase' =>
$data->{daily}->{data}[$i]{moonPhase},
$data->{daily}->{data}->[$i]->{moonPhase},
'sunsetTime' => strftime(
"%a,%e %b %Y %H:%M %p",
localtime(
$data->{daily}->{data}[$i]{sunsetTime}
$data->{daily}->{data}->[$i]->{sunsetTime}
)
),
'sunriseTime' => strftime(
"%a,%e %b %Y %H:%M %p",
localtime(
$data->{daily}->{data}[$i]{sunriseTime}
$data->{daily}->{data}->[$i]->{sunriseTime}
)
),
'precipProbability' =>
$data->{daily}->{data}[$i]{precipProbability},
'pressure' => $data->{daily}->{data}[$i]{pressure},
$data->{daily}->{data}->[$i]->{precipProbability},
'pressure' => $data->{daily}->{data}->[$i]->{pressure},
'visibility' =>
$data->{daily}->{data}[$i]{visibility},
$data->{daily}->{data}->[$i]->{visibility},
}
);

View File

@ -297,14 +297,17 @@ sub _ProcessingRetrieveData($$) {
};
}
if ( $self->{endpoint} eq 'forecas' ) {
my $i = 0;
if ( $self->{endpoint} eq 'forecast' ) {
if ( ref( $data->{list} ) eq "ARRAY"
and scalar( @{ $data->{list} } ) > 0 )
{
## löschen des alten Datensatzes
delete $self->{cached}->{forecast};
my $i = 0;
foreach ( @{ $data->{list} } ) {
push(
@{ $self->{cached}->{forecast}->{daily} },
@{ $self->{cached}->{forecast} },
{
'temperature' => int(
sprintf( "%.1f", ( $data->{list}[$i]{main}->{temp} - 273.15 ) ) + 0.5
@ -340,7 +343,7 @@ sub _ProcessingRetrieveData($$) {
'iconAPI' => $data->{list}[$i]{weather}[0]{icon},
'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 ) ),
}
);