2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-20 19:36:02 +00:00

59_Wunderground.pm: implement Unit.pm support

git-svn-id: https://svn.fhem.de/fhem/trunk@12514 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
jpawlowski 2016-11-06 17:45:41 +00:00
parent 4c8e85b7a5
commit 27060b0478

View File

@ -32,7 +32,7 @@ use warnings;
use vars qw(%data); use vars qw(%data);
use HttpUtils; use HttpUtils;
use Encode; use Encode;
use UConv; use Unit;
use Data::Dumper; use Data::Dumper;
sub Wunderground_Hash2Readings($$;$); sub Wunderground_Hash2Readings($$;$);
@ -50,7 +50,7 @@ sub Wunderground_Initialize($) {
$hash->{DefFn} = "Wunderground_Define"; $hash->{DefFn} = "Wunderground_Define";
$hash->{AttrFn} = "Wunderground_Attr"; $hash->{AttrFn} = "Wunderground_Attr";
$hash->{UndefFn} = "Wunderground_Undefine"; $hash->{UndefFn} = "Wunderground_Undefine";
$hash->{DbLog_splitFn} = "UConv::DbLog_split"; $hash->{DbLog_splitFn} = "Unit_DbLog_split";
$hash->{parseParams} = 1; $hash->{parseParams} = 1;
$hash->{AttrList} = $hash->{AttrList} =
@ -239,7 +239,7 @@ sub Wunderground_ReceiveCommand($$$) {
Log3 $name, 5, Log3 $name, 5,
"Wunderground $name: called function Wunderground_ReceiveCommand()"; "Wunderground $name: called function Wunderground_ReceiveCommand()";
readingsBeginUpdate($hash); readingsUnitBeginUpdate($hash);
# service not reachable # service not reachable
if ($err) { if ($err) {
@ -284,31 +284,18 @@ sub Wunderground_ReceiveCommand($$$) {
} }
# state # state
my @stateReadings = split( /\s+/, AttrVal( $name, "stateReadings", "" ) ); my $stateReadings = AttrVal( $name, "stateReadings", "" );
my $stateReadingsLang = AttrVal( $name, "stateReadingsLang", "en" );
my $stateReadingsFormat = AttrVal( $name, "stateReadingsFormat", "0" ); my $stateReadingsFormat = AttrVal( $name, "stateReadingsFormat", "0" );
my $stateReadingsLang = AttrVal( $name, "wu_lang", "en" );
foreach (@stateReadings) {
$_ =~ /^(\w+):?(\w+)?$/;
my $r = $1;
my $v = ReadingsVal( $name, $r, undef );
my $u = UConv::rname2unitDetails( $r, $stateReadingsLang, $v );
my $n = ( $2 ? $2 : ( $u->{"short"} ? $u->{"short"} : $1 ) );
my $v2 = (
$stateReadingsFormat eq "2"
? $u->{"value_unit_long"}
: ( $stateReadingsFormat eq "1" ? $u->{"value_unit"} : $v )
);
if ( defined($v2) ) { $state =
$state .= " " if ( $state ne "Initialized" ); getMultiValStatus( $name, $stateReadings,
$state = "" if ( $state eq "Initialized" ); $stateReadingsLang, $stateReadingsFormat );
$state .= "$n: $v2";
}
}
readingsBulkUpdate( $hash, "state", $state ); readingsUnitBulkUpdate( $hash, "state", $state );
readingsBulkUpdateIfChanged( $hash, "lastQueryResult", $lastQueryResult ); readingsUnitBulkUpdateIfChanged( $hash, "lastQueryResult",
readingsEndUpdate( $hash, 1 ); $lastQueryResult );
readingsUnitEndUpdate( $hash, 1 );
return; return;
} }
@ -395,16 +382,16 @@ sub Wunderground_Hash2Readings($$;$) {
$moonset =~ s/^(\d):(\d\d)$/0$1:$2/; $moonset =~ s/^(\d):(\d\d)$/0$1:$2/;
$moonset =~ s/^(\d\d):(\d)$/$1:0$2/; $moonset =~ s/^(\d\d):(\d)$/$1:0$2/;
readingsBulkUpdate( $hash, "sunrise", $sunrise ); readingsUnitBulkUpdate( $hash, "sunrise", $sunrise );
readingsBulkUpdate( $hash, "sunset", $sunset ); readingsUnitBulkUpdate( $hash, "sunset", $sunset );
readingsBulkUpdate( $hash, "moonrise", $moonrise ); readingsUnitBulkUpdate( $hash, "moonrise", $moonrise );
readingsBulkUpdate( $hash, "moonset", $moonset ); readingsUnitBulkUpdate( $hash, "moonset", $moonset );
readingsBulkUpdate( $hash, "moon_age", readingsUnitBulkUpdate( $hash, "moon_age",
$h->{moon_phase}{ageOfMoon} ); $h->{moon_phase}{ageOfMoon} );
readingsBulkUpdate( $hash, "moon_pct", readingsUnitBulkUpdate( $hash, "moon_pct",
$h->{moon_phase}{percentIlluminated} ); $h->{moon_phase}{percentIlluminated} );
readingsBulkUpdate( $hash, "moon_phase", readingsUnitBulkUpdate( $hash, "moon_phase",
$h->{moon_phase}{phaseofMoon} ); $h->{moon_phase}{phaseofMoon} );
} }
@ -415,104 +402,104 @@ sub Wunderground_Hash2Readings($$;$) {
my $period = $h->{period} - 1; my $period = $h->{period} - 1;
$reading = "fc" . $period . "_"; $reading = "fc" . $period . "_";
readingsBulkUpdate( $hash, $reading . "condition", readingsUnitBulkUpdate( $hash, $reading . "condition",
$h->{conditions} ); $h->{conditions} );
readingsBulkUpdate( readingsUnitBulkUpdate(
$hash, $hash,
$reading . "high_c", $reading . "high_c",
$h->{high}{celsius} $h->{high}{celsius}
); );
readingsBulkUpdate( readingsUnitBulkUpdate(
$hash, $hash,
$reading . "high_f", $reading . "high_f",
$h->{high}{fahrenheit} $h->{high}{fahrenheit}
); );
readingsBulkUpdate( $hash, $reading . "humidity", readingsUnitBulkUpdate( $hash, $reading . "humidity",
$h->{avehumidity} ); $h->{avehumidity} );
readingsBulkUpdate( $hash, $reading . "humidity_min", readingsUnitBulkUpdate( $hash, $reading . "humidity_min",
$h->{minhumidity} ); $h->{minhumidity} );
readingsBulkUpdate( $hash, $reading . "humidity_max", readingsUnitBulkUpdate( $hash, $reading . "humidity_max",
$h->{maxhumidity} ); $h->{maxhumidity} );
readingsBulkUpdate( $hash, $reading . "icon", $h->{icon} ); readingsUnitBulkUpdate( $hash, $reading . "icon", $h->{icon} );
readingsBulkUpdate( $hash, $reading . "icon_url", readingsUnitBulkUpdate( $hash, $reading . "icon_url",
$h->{icon_url} ); $h->{icon_url} );
readingsBulkUpdate( readingsUnitBulkUpdate(
$hash, $hash,
$reading . "low_c", $reading . "low_c",
$h->{low}{celsius} $h->{low}{celsius}
); );
readingsBulkUpdate( readingsUnitBulkUpdate(
$hash, $hash,
$reading . "low_f", $reading . "low_f",
$h->{low}{fahrenheit} $h->{low}{fahrenheit}
); );
readingsBulkUpdate( $hash, $reading . "pop", $h->{pop} ); readingsUnitBulkUpdate( $hash, $reading . "pop", $h->{pop} );
readingsBulkUpdate( readingsUnitBulkUpdate(
$hash, $hash,
$reading . "rain_day", $reading . "rain_day",
$h->{qpf_allday}{mm} $h->{qpf_allday}{mm}
); );
readingsBulkUpdate( readingsUnitBulkUpdate(
$hash, $hash,
$reading . "rain_day_in", $reading . "rain_day_in",
$h->{qpf_allday}{in} $h->{qpf_allday}{in}
); );
readingsBulkUpdate( readingsUnitBulkUpdate(
$hash, $hash,
$reading . "rain_night", $reading . "rain_night",
$h->{qpf_night}{mm} $h->{qpf_night}{mm}
); );
readingsBulkUpdate( readingsUnitBulkUpdate(
$hash, $hash,
$reading . "rain_night_in", $reading . "rain_night_in",
$h->{qpf_night}{in} $h->{qpf_night}{in}
); );
readingsBulkUpdate( readingsUnitBulkUpdate(
$hash, $hash,
$reading . "snow_day", $reading . "snow_day",
$h->{snow_allday}{cm} $h->{snow_allday}{cm}
); );
readingsBulkUpdate( readingsUnitBulkUpdate(
$hash, $hash,
$reading . "snow_day_in", $reading . "snow_day_in",
$h->{snow_allday}{in} $h->{snow_allday}{in}
); );
readingsBulkUpdate( readingsUnitBulkUpdate(
$hash, $hash,
$reading . "snow_night", $reading . "snow_night",
$h->{snow_night}{cm} $h->{snow_night}{cm}
); );
readingsBulkUpdate( readingsUnitBulkUpdate(
$hash, $hash,
$reading . "snow_night_in", $reading . "snow_night_in",
$h->{snow_night}{in} $h->{snow_night}{in}
); );
readingsBulkUpdate( readingsUnitBulkUpdate(
$hash, $hash,
$reading . "wind_direction", $reading . "wind_direction",
$h->{avewind}{degrees} $h->{avewind}{degrees}
); );
readingsBulkUpdate( readingsUnitBulkUpdate(
$hash, $hash,
$reading . "wind_direction_max", $reading . "wind_direction_max",
$h->{maxwind}{degrees} $h->{maxwind}{degrees}
); );
readingsBulkUpdate( readingsUnitBulkUpdate(
$hash, $hash,
$reading . "wind_speed", $reading . "wind_speed",
$h->{avewind}{kph} $h->{avewind}{kph}
); );
readingsBulkUpdate( readingsUnitBulkUpdate(
$hash, $hash,
$reading . "wind_speed_mph", $reading . "wind_speed_mph",
$h->{avewind}{mph} $h->{avewind}{mph}
); );
readingsBulkUpdate( readingsUnitBulkUpdate(
$hash, $hash,
$reading . "wind_speed_max", $reading . "wind_speed_max",
$h->{maxwind}{kph} $h->{maxwind}{kph}
); );
readingsBulkUpdate( readingsUnitBulkUpdate(
$hash, $hash,
$reading . "wind_speed_max_mph", $reading . "wind_speed_max_mph",
$h->{maxwind}{mph} $h->{maxwind}{mph}
@ -545,16 +532,17 @@ sub Wunderground_Hash2Readings($$;$) {
$reading = "fc" . $period . "_"; $reading = "fc" . $period . "_";
readingsBulkUpdate( $hash, $reading . "icon$night", readingsUnitBulkUpdate( $hash, $reading . "icon$night",
$h->{icon} ); $h->{icon} );
readingsBulkUpdate( $hash, $reading . "icon_url$night", readingsUnitBulkUpdate( $hash, $reading . "icon_url$night",
$h->{icon_url} ); $h->{icon_url} );
readingsBulkUpdate( $hash, $reading . "pop$night", $h->{pop} ); readingsUnitBulkUpdate( $hash, $reading . "pop$night",
readingsBulkUpdate( $hash, $reading . "text$night", $h->{pop} );
readingsUnitBulkUpdate( $hash, $reading . "text$night",
$h->{fcttext_metric} ); $h->{fcttext_metric} );
readingsBulkUpdate( $hash, $reading . "text_f$night", readingsUnitBulkUpdate( $hash, $reading . "text_f$night",
$h->{fcttext} ); $h->{fcttext} );
readingsBulkUpdate( $hash, $reading . "title$night", readingsUnitBulkUpdate( $hash, $reading . "title$night",
$h->{title} ); $h->{title} );
} }
@ -577,7 +565,7 @@ sub Wunderground_Hash2Readings($$;$) {
$value =~ s/^(\d+)%$/$1/; $value =~ s/^(\d+)%$/$1/;
readingsBulkUpdate( $hash, $reading, $value ); readingsUnitBulkUpdate( $hash, $reading, $value );
} }
} }
} }
@ -589,7 +577,7 @@ sub Wunderground_Hash2Readings($$;$) {
Wunderground_Hash2Readings( $hash, $_, $r . $i ); Wunderground_Hash2Readings( $hash, $_, $r . $i );
} }
else { else {
readingsBulkUpdate( $hash, $r . $i, $_ ); readingsUnitBulkUpdate( $hash, $r . $i, $_ );
} }
$i++; $i++;