2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-28 17:12:32 +00:00

59_WUup: some code cleanup

git-svn-id: https://svn.fhem.de/fhem/trunk@21672 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
mahowi 2020-04-14 12:30:32 +00:00
parent 0675991faa
commit 90d2fe29d8

View File

@ -35,7 +35,7 @@ use HttpUtils;
use UConv; use UConv;
use FHEM::Meta; use FHEM::Meta;
my $version = "0.9.16"; my $version = q(0.9.17);
################################################################################ ################################################################################
# #
@ -46,21 +46,22 @@ my $version = "0.9.16";
sub WUup_Initialize { sub WUup_Initialize {
my ($hash) = @_; my ($hash) = @_;
$hash->{DefFn} = "WUup_Define"; $hash->{DefFn} = 'WUup_Define';
$hash->{UndefFn} = "WUup_Undef"; $hash->{UndefFn} = 'WUup_Undef';
$hash->{SetFn} = "WUup_Set"; $hash->{SetFn} = 'WUup_Set';
$hash->{AttrFn} = "WUup_Attr"; $hash->{AttrFn} = 'WUup_Attr';
$hash->{AttrList} = $hash->{AttrList} =
"disable:1,0 " 'disable:1,0 '
. "disabledForIntervals " . 'disabledForIntervals '
. "interval " . 'interval '
. "unit_windspeed:km/h,m/s " . 'unit_windspeed:km/h,m/s '
. "unit_solarradiation:W/m²,lux " . 'unit_solarradiation:W/m²,lux '
. "round " . 'round '
. "wubaromin wudailyrainin wudewptf wuhumidity wurainin wusoilmoisture " . 'wubaromin wudailyrainin wudewptf wuhumidity wurainin '
. "wusoiltempf wusolarradiation wutempf wuUV wuwinddir wuwinddir_avg2m " . 'wusoilmoisture wusoiltempf wusolarradiation wutempf wuUV '
. "wuwindgustdir wuwindgustdir_10m wuwindgustmph wuwindgustmph_10m " . 'wuwinddir wuwinddir_avg2m wuwindgustdir wuwindgustdir_10m '
. "wuwindspdmph_avg2m wuwindspeedmph wuAqPM2.5 wuAqPM10 " . 'wuwindgustmph wuwindgustmph_10m wuwindspdmph_avg2m wuwindspeedmph '
. 'wuAqPM2.5 wuAqPM10 '
. $readingFnAttributes; . $readingFnAttributes;
$hash->{VERSION} = $version; $hash->{VERSION} = $version;
@ -75,7 +76,7 @@ sub WUup_Define {
my @param = split( "[ \t][ \t]*", $def ); my @param = split( "[ \t][ \t]*", $def );
return "syntax: define <name> WUup <stationID> <password>" return q{syntax: define <name> WUup <stationID> <password>}
if ( int(@param) != 4 ); if ( int(@param) != 4 );
my $name = $hash->{NAME}; my $name = $hash->{NAME};
@ -87,11 +88,11 @@ sub WUup_Define {
$hash->{helper}{password} = $param[3]; $hash->{helper}{password} = $param[3];
$hash->{helper}{softwaretype} = 'FHEM'; $hash->{helper}{softwaretype} = 'FHEM';
$hash->{helper}{url} = $hash->{helper}{url} =
"https://weatherstation.wunderground.com/weatherstation/updateweatherstation.php"; 'https://weatherstation.wunderground.com/weatherstation/updateweatherstation.php';
$hash->{helper}{url_rf} = $hash->{helper}{url_rf} =
"https://rtupdate.wunderground.com/weatherstation/updateweatherstation.php"; 'https://rtupdate.wunderground.com/weatherstation/updateweatherstation.php';
readingsSingleUpdate( $hash, "state", "defined", 1 ); readingsSingleUpdate( $hash, 'state', 'defined', 1 );
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
@ -99,10 +100,10 @@ sub WUup_Define {
WUup_stateRequestTimer($hash); WUup_stateRequestTimer($hash);
} }
else { else {
InternalTimer( gettimeofday(), "WUup_stateRequestTimer", $hash, 0 ); InternalTimer( gettimeofday(), 'WUup_stateRequestTimer', $hash, 0 );
} }
Log3( $name, 3, "WUup ($name): defined" ); Log3( $name, 3, qq{WUup ($name): defined} );
return; return;
} }
@ -116,10 +117,10 @@ sub WUup_Undef {
sub WUup_Set { sub WUup_Set {
my $hash = shift; my $hash = shift;
my $name = shift; my $name = shift;
my $cmd = shift // return qq{"set $name needs at least one argument}; my $cmd = shift // return qq{set $name needs at least one argument};
return WUup_stateRequestTimer($hash) if ( $cmd eq "update" ); return WUup_stateRequestTimer($hash) if ( $cmd eq 'update' );
return "Unknown argument $cmd, choose one of update:noArg"; return qq{Unknown argument $cmd, choose one of update:noArg};
} }
sub WUup_Attr { sub WUup_Attr {
@ -129,48 +130,48 @@ sub WUup_Attr {
my $attrVal = shift; my $attrVal = shift;
my $hash = $defs{$name}; my $hash = $defs{$name};
if ( $attrName eq "disable" ) { if ( $attrName eq 'disable' ) {
if ( $cmd eq "set" and $attrVal eq "1" ) { if ( $cmd eq 'set' and $attrVal eq '1' ) {
readingsSingleUpdate( $hash, "state", "disabled", 1 ); readingsSingleUpdate( $hash, 'state', 'disabled', 1 );
Log3( $name, 3, "WUup ($name) - disabled" ); Log3( $name, 3, qq{WUup ($name) - disabled} );
} }
elsif ( $cmd eq "del" ) { elsif ( $cmd eq 'del' ) {
readingsSingleUpdate( $hash, "state", "active", 1 ); readingsSingleUpdate( $hash, 'state', 'active', 1 );
Log3( $name, 3, "WUup ($name) - enabled" ); Log3( $name, 3, qq{WUup ($name) - enabled} );
} }
} }
if ( $attrName eq "disabledForIntervals" ) { if ( $attrName eq 'disabledForIntervals' ) {
if ( $cmd eq "set" ) { if ( $cmd eq 'set' ) {
readingsSingleUpdate( $hash, "state", "unknown", 1 ); readingsSingleUpdate( $hash, 'state', 'unknown', 1 );
Log3( $name, 3, "WUup ($name) - disabledForIntervals" ); Log3( $name, 3, qq{WUup ($name) - disabledForIntervals} );
} }
elsif ( $cmd eq "del" ) { elsif ( $cmd eq 'del' ) {
readingsSingleUpdate( $hash, "state", "active", 1 ); readingsSingleUpdate( $hash, 'state', 'active', 1 );
Log3( $name, 3, "WUup ($name) - enabled" ); Log3( $name, 3, qq{WUup ($name) - enabled} );
} }
} }
if ( $attrName eq "interval" ) { if ( $attrName eq 'interval' ) {
if ( $cmd eq "set" ) { if ( $cmd eq 'set' ) {
if ( $attrVal < 3 ) { if ( $attrVal < 3 ) {
Log3( $name, 1, Log3( $name, 1,
"WUup ($name) - interval too small, please use something >= 3 (sec), default is 300 (sec)." qq{WUup ($name) - interval too small, please use something >= 3 (sec), default is 300 (sec).}
); );
return return
"interval too small, please use something >= 3 (sec), default is 300 (sec)"; qq{interval too small, please use something >= 3 (sec), default is 300 (sec)};
} }
else { else {
$hash->{INTERVAL} = $attrVal; $hash->{INTERVAL} = $attrVal;
Log3( $name, 4, "WUup ($name) - set interval to $attrVal" ); Log3( $name, 4, qq{WUup ($name) - set interval to $attrVal} );
} }
} }
elsif ( $cmd eq "del" ) { elsif ( $cmd eq 'del' ) {
$hash->{INTERVAL} = 300; $hash->{INTERVAL} = 300;
Log3( $name, 4, "WUup ($name) - set interval to default" ); Log3( $name, 4, qq{WUup ($name) - set interval to default} );
} }
} }
@ -182,11 +183,11 @@ sub WUup_stateRequestTimer {
my $name = $hash->{NAME}; my $name = $hash->{NAME};
if ( !IsDisabled($name) ) { if ( !IsDisabled($name) ) {
readingsSingleUpdate( $hash, "state", "active", 1 ) readingsSingleUpdate( $hash, 'state', 'active', 1 )
if ( if (
( ReadingsVal( $name, "state", 0 ) eq "defined" ( ReadingsVal( $name, 'state', 0 ) eq 'defined'
or ReadingsVal( $name, "state", 0 ) eq "disabled" or ReadingsVal( $name, 'state', 0 ) eq 'disabled'
or ReadingsVal( $name, "state", 0 ) eq "Unknown" or ReadingsVal( $name, 'state', 0 ) eq 'Unknown'
) )
); );
@ -194,14 +195,14 @@ sub WUup_stateRequestTimer {
} }
else { else {
readingsSingleUpdate( $hash, "state", "disabled", 1 ); readingsSingleUpdate( $hash, 'state', 'disabled', 1 );
} }
InternalTimer( gettimeofday() + $hash->{INTERVAL}, InternalTimer( gettimeofday() + $hash->{INTERVAL},
"WUup_stateRequestTimer", $hash, 1 ); 'WUup_stateRequestTimer', $hash, 1 );
Log3( $name, 5, Log3( $name, 5,
"Sub WUup_stateRequestTimer ($name) - Request Timer is called" ); qq{Sub WUup_stateRequestTimer ($name) - Request Timer is called} );
return; return;
} }
@ -217,14 +218,14 @@ sub WUup_send {
else { else {
$url = $hash->{helper}{url}; $url = $hash->{helper}{url};
} }
$url .= "?ID=" . $hash->{helper}{stationid}; $url .= "?ID=$hash->{helper}{stationid}";
$url .= "&PASSWORD=" . $hash->{helper}{password}; $url .= "&PASSWORD=$hash->{helper}{password}";
my $datestring = strftime "%F+%T", gmtime; my $datestring = strftime "%F+%T", gmtime;
$datestring =~ s{:} $datestring =~ s{:}
{%3A}gxms; {%3A}gxms;
$url .= "&dateutc=" . $datestring; $url .= "&dateutc=$datestring";
my ( $data, $d, $r, $o ); my ( $data, $d, $r, $o );
my $a = $attr{$name}; my $a = $attr{$name};
@ -242,34 +243,35 @@ sub WUup_send {
if ( $key =~ m{\w+f \z}xms ) { if ( $key =~ m{\w+f \z}xms ) {
$value = UConv::c2f( $value, $rnd ); $value = UConv::c2f( $value, $rnd );
} }
elsif ( $key =~ m{\w+mph [^\n]*}xms ) { if ( $key =~ m{\w+mph [^\n]*}xms ) {
if ( $unit_windspeed eq "m/s" ) { if ( $unit_windspeed eq 'm/s' ) {
Log3( $name, 5, "WUup ($name) - windspeed unit is m/s" ); Log3( $name, 5, qq{WUup ($name) - windspeed unit is m/s} );
$value = $value =
UConv::kph2mph( ( UConv::mps2kph( $value, $rnd ) ), UConv::kph2mph( ( UConv::mps2kph( $value, $rnd ) ),
$rnd ); $rnd );
} }
else { else {
Log3( $name, 5, "WUup ($name) - windspeed unit is km/h" ); Log3( $name, 5, qq{WUup ($name) - windspeed unit is km/h} );
$value = UConv::kph2mph( $value, $rnd ); $value = UConv::kph2mph( $value, $rnd );
} }
} }
elsif ( $key eq "baromin" ) { if ( $key eq 'baromin' ) {
$value = UConv::hpa2inhg( $value, $rnd ); $value = UConv::hpa2inhg( $value, $rnd );
} }
elsif ( $key =~ m{rainin \z}xms ) { if ( $key =~ m{rainin \z}xms ) {
$value = UConv::mm2in( $value, $rnd ); $value = UConv::mm2in( $value, $rnd );
} }
elsif ( $key eq "solarradiation" ) { if ( $key eq 'solarradiation' ) {
if ( $unit_solarradiation eq "lux" ) { if ( $unit_solarradiation eq 'lux' ) {
Log3( $name, 5, "WUup ($name) - solarradiation unit is lux" ); Log3( $name, 5,
qq{WUup ($name) - solarradiation unit is lux} );
$value = UConv::lux2wpsm( $value, $rnd ); $value = UConv::lux2wpsm( $value, $rnd );
} }
else { else {
Log3( $name, 5, Log3( $name, 5,
"WUup ($name) - solarradiation unit is W/m²" ); qq{WUup ($name) - solarradiation unit is W/m²} );
} }
} }
$data .= "&$key=$value"; $data .= "&$key=$value";
@ -277,32 +279,32 @@ sub WUup_send {
readingsBeginUpdate($hash); readingsBeginUpdate($hash);
if ( defined($data) ) { if ( defined($data) ) {
readingsBulkUpdate( $hash, "data", $data ); readingsBulkUpdate( $hash, 'data', $data );
Log3( $name, 4, "WUup ($name) - data sent: $data" ); Log3( $name, 4, qq{WUup ($name) - data sent: $data} );
$url .= $data; $url .= $data;
$url .= "&softwaretype=" . $hash->{helper}{softwaretype}; $url .= "&softwaretype=$hash->{helper}{softwaretype}";
$url .= "&action=updateraw"; $url .= '&action=updateraw';
if ( $hash->{INTERVAL} < 300 ) { if ( $hash->{INTERVAL} < 300 ) {
$url .= "&realtime=1&rtfreq=" . $hash->{INTERVAL}; $url .= "&realtime=1&rtfreq=$hash->{INTERVAL}";
} }
my $param = { my $param = {
url => $url, url => $url,
timeout => 6, timeout => 6,
hash => $hash, hash => $hash,
method => "GET", method => 'GET',
header => "agent: FHEM-WUup/$ver\r\nUser-Agent: FHEM-WUup/$ver", header => "agent: FHEM-WUup/$ver\r\nUser-Agent: FHEM-WUup/$ver",
callback => \&WUup_receive callback => \&WUup_receive
}; };
Log3( $name, 5, "WUup ($name) - full URL: $url" ); Log3( $name, 5, qq{WUup ($name) - full URL: $url} );
HttpUtils_NonblockingGet($param); HttpUtils_NonblockingGet($param);
} }
else { else {
CommandDeleteReading( undef, "$name data" ); CommandDeleteReading( undef, "$name data" );
CommandDeleteReading( undef, "$name response" ); CommandDeleteReading( undef, "$name response" );
Log3( $name, 3, "WUup ($name) - no data" ); Log3( $name, 3, qq{WUup ($name) - no data} );
readingsBulkUpdate( $hash, "state", "defined" ); readingsBulkUpdate( $hash, 'state', 'defined' );
} }
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
@ -319,16 +321,14 @@ sub WUup_receive {
if ( $err ne q{} ) { if ( $err ne q{} ) {
Log3( $name, 3, Log3( $name, 3,
"WUup ($name) - error while requesting " qq{WUup ($name) - error while requesting $param->{url} - $err} );
. $param->{url} readingsSingleUpdate( $hash, 'state', 'ERROR', undef );
. " - $err" ); readingsSingleUpdate( $hash, 'response', $err, undef );
readingsSingleUpdate( $hash, "state", "ERROR", undef );
readingsSingleUpdate( $hash, "response", $err, undef );
} }
elsif ( $data ne q{} ) { elsif ( $data ne q{} ) {
Log3( $name, 4, "WUup ($name) - server response: $data" ); Log3( $name, 4, qq{WUup ($name) - server response: $data} );
readingsSingleUpdate( $hash, "state", "active", undef ); readingsSingleUpdate( $hash, 'state', 'active', undef );
readingsSingleUpdate( $hash, "response", $data, undef ); readingsSingleUpdate( $hash, 'response', $data, undef );
} }
return; return;
} }
@ -602,7 +602,7 @@ sub WUup_receive {
"license": [ "license": [
"gpl_2" "gpl_2"
], ],
"version": "v0.9.16", "version": "v0.9.17",
"release_status": "stable", "release_status": "stable",
"author": [ "author": [
"Manfred Winter <mahowi@gmail.com>" "Manfred Winter <mahowi@gmail.com>"