code style

This commit is contained in:
Marko Oldenburg 2019-06-04 12:46:40 +02:00
parent 640a2fa8fd
commit fc264fbf75
2 changed files with 109 additions and 75 deletions

View File

@ -59,8 +59,7 @@ use strict;
use warnings; use warnings;
use FHEM::Meta; use FHEM::Meta;
my $version = '1.6.4.1'; my $version = '1.6.5';
sub GardenaSmartBridge_Initialize($) { sub GardenaSmartBridge_Initialize($) {
@ -94,7 +93,7 @@ sub GardenaSmartBridge_Initialize($) {
my $hash = $modules{GardenaSmartBridge}{defptr}{$d}; my $hash = $modules{GardenaSmartBridge}{defptr}{$d};
$hash->{VERSION} = $version; $hash->{VERSION} = $version;
} }
return FHEM::Meta::InitMod( __FILE__, $hash ); return FHEM::Meta::InitMod( __FILE__, $hash );
} }
@ -154,12 +153,17 @@ sub Define($$) {
return 'too few parameters: define <NAME> GardenaSmartBridge' return 'too few parameters: define <NAME> GardenaSmartBridge'
if ( @a != 2 ); if ( @a != 2 );
return return
'Cannot define Gardena Bridge device. Perl modul ' . ${missingModul} . ' is missing.' 'Cannot define Gardena Bridge device. Perl modul '
. ${missingModul}
. ' is missing.'
if ($missingModul); if ($missingModul);
my $name = $a[0]; my $name = $a[0];
$hash->{BRIDGE} = 1; $hash->{BRIDGE} = 1;
$hash->{URL} = AttrVal($name,'gardenaBaseURL','https://sg-api.dss.husqvarnagroup.net') . '/sg-1'; $hash->{URL} =
AttrVal( $name, 'gardenaBaseURL',
'https://sg-api.dss.husqvarnagroup.net' )
. '/sg-1';
$hash->{VERSION} = $version; $hash->{VERSION} = $version;
$hash->{INTERVAL} = 60; $hash->{INTERVAL} = 60;
$hash->{NOTIFYDEV} = "global,$name"; $hash->{NOTIFYDEV} = "global,$name";
@ -392,8 +396,10 @@ sub Write($@) {
} }
); );
Log3($name, 4, Log3( $name, 4,
"GardenaSmartBridge ($name) - Send with URL: $hash->{URL}$uri, HEADER: secret!, DATA: secret!, METHOD: $method"); "GardenaSmartBridge ($name) - Send with URL: $hash->{URL}$uri, HEADER: secret!, DATA: secret!, METHOD: $method"
);
# Log3($name, 3, # Log3($name, 3,
# "GardenaSmartBridge ($name) - Send with URL: $hash->{URL}$uri, HEADER: $header, DATA: $payload, METHOD: $method"); # "GardenaSmartBridge ($name) - Send with URL: $hash->{URL}$uri, HEADER: $header, DATA: $payload, METHOD: $method");
} }
@ -410,11 +416,10 @@ sub ErrorHandling($$$) {
unless ( not defined( $param->{'device_id'} ) ); unless ( not defined( $param->{'device_id'} ) );
my $dname = $dhash->{NAME}; my $dname = $dhash->{NAME};
my $decode_json = eval { decode_json($data) }; my $decode_json = eval { decode_json($data) };
if ($@) { if ($@) {
Log3 $name, 3, Log3 $name, 3, "GardenaSmartBridge ($name) - JSON error while request";
"GardenaSmartBridge ($name) - JSON error while request";
} }
if ( defined($err) ) { if ( defined($err) ) {
@ -487,7 +492,11 @@ sub ErrorHandling($$$) {
readingsBulkUpdate( $dhash, "state", "the command is processed", readingsBulkUpdate( $dhash, "state", "the command is processed",
1 ); 1 );
InternalTimer( gettimeofday() + 5, "FHEM::GardenaSmartBridge::getDevices", $hash, 1 ); InternalTimer(
gettimeofday() + 5,
"FHEM::GardenaSmartBridge::getDevices",
$hash, 1
);
} }
elsif ( $param->{code} != 200 ) { elsif ( $param->{code} != 200 ) {
@ -509,10 +518,11 @@ sub ErrorHandling($$$) {
return; return;
} }
if ( $data =~ /Error/ if (
or ( defined( $decode_json ) $data =~ /Error/
and ref($decode_json) eq 'HASH' or ( defined($decode_json)
and defined($decode_json->{errors}) ) and ref($decode_json) eq 'HASH'
and defined( $decode_json->{errors} ) )
) )
{ {
readingsBeginUpdate($dhash); readingsBeginUpdate($dhash);
@ -522,7 +532,7 @@ sub ErrorHandling($$$) {
readingsBulkUpdate( $dhash, "lastRequestState", "request_error", 1 ); readingsBulkUpdate( $dhash, "lastRequestState", "request_error", 1 );
if ( $param->{code} == 400 ) { if ( $param->{code} == 400 ) {
if ( $decode_json ) { if ($decode_json) {
if ( ref( $decode_json->{errors} ) eq "ARRAY" if ( ref( $decode_json->{errors} ) eq "ARRAY"
and defined( $decode_json->{errors} ) ) and defined( $decode_json->{errors} ) )
{ {
@ -603,7 +613,7 @@ sub ErrorHandling($$$) {
readingsSingleUpdate( $hash, 'state', 'connected to cloud', 1 ) readingsSingleUpdate( $hash, 'state', 'connected to cloud', 1 )
if ( defined( $hash->{helper}{locations_id} ) ); if ( defined( $hash->{helper}{locations_id} ) );
ResponseProcessing( $hash, $data ) ResponseProcessing( $hash, $data )
if ( ref($decode_json) eq 'HASH' ); if ( ref($decode_json) eq 'HASH' );
} }
sub ResponseProcessing($$) { sub ResponseProcessing($$) {
@ -692,8 +702,8 @@ sub ResponseProcessing($$) {
Dispatch( $hash, $json, undef ) Dispatch( $hash, $json, undef )
unless ( $decode_json->{category} eq 'gateway' ); unless ( $decode_json->{category} eq 'gateway' );
WriteReadings($hash,$decode_json) WriteReadings( $hash, $decode_json )
if ( defined($decode_json->{category}) if ( defined( $decode_json->{category} )
and $decode_json->{category} eq 'gateway' ); and $decode_json->{category} eq 'gateway' );
} }
@ -726,51 +736,85 @@ sub WriteReadings($$) {
{ {
readingsBeginUpdate($hash); readingsBeginUpdate($hash);
if ( $decode_json->{id} eq $hash->{helper}{locations_id} ) { if ( $decode_json->{id} eq $hash->{helper}{locations_id} ) {
readingsBulkUpdateIfChanged( $hash, 'name', $decode_json->{name} ); readingsBulkUpdateIfChanged( $hash, 'name', $decode_json->{name} );
readingsBulkUpdateIfChanged( $hash, 'authorized_user_ids', readingsBulkUpdateIfChanged( $hash, 'authorized_user_ids',
scalar( @{ $decode_json->{authorized_user_ids} } ) ); scalar( @{ $decode_json->{authorized_user_ids} } ) );
readingsBulkUpdateIfChanged( $hash, 'devices', readingsBulkUpdateIfChanged( $hash, 'devices',
scalar( @{ $decode_json->{devices} } ) ); scalar( @{ $decode_json->{devices} } ) );
while ( ( my ( $t, $v ) ) = each %{ $decode_json->{geo_position} } ) { while ( ( my ( $t, $v ) ) = each %{ $decode_json->{geo_position} } )
{
$v = encode_utf8($v); $v = encode_utf8($v);
readingsBulkUpdateIfChanged( $hash, $t, $v ); readingsBulkUpdateIfChanged( $hash, $t, $v );
} }
readingsBulkUpdateIfChanged( $hash, 'zones', readingsBulkUpdateIfChanged( $hash, 'zones',
scalar( @{ $decode_json->{zones} } ) ); scalar( @{ $decode_json->{zones} } ) );
} }
elsif ( $decode_json->{id} ne $hash->{helper}{locations_id} and ref($decode_json->{abilities}) eq 'ARRAY' ) { elsif ( $decode_json->{id} ne $hash->{helper}{locations_id}
my $properties = scalar( @{ $decode_json->{abilities}[0]{properties} } ); and ref( $decode_json->{abilities} ) eq 'ARRAY' )
{
do { my $properties =
while ( ( my ( $t, $v ) ) = each %{ $decode_json->{abilities}[0]{properties}[$properties] } ) { scalar( @{ $decode_json->{abilities}[0]{properties} } );
next
if ( ref($v) eq 'ARRAY');
#$v = encode_utf8($v);
readingsBulkUpdateIfChanged( $hash, $decode_json->{abilities}[0]{properties}[$properties]{name} . '-' . $t, $v )
unless ( $decode_json->{abilities}[0]{properties}[$properties]{name} eq 'ethernet_status'
or $decode_json->{abilities}[0]{properties}[$properties]{name} eq 'wifi_status' );
if ( ( $decode_json->{abilities}[0]{properties}[$properties]{name} eq 'ethernet_status' do {
or $decode_json->{abilities}[0]{properties}[$properties]{name} eq 'wifi_status') while ( ( my ( $t, $v ) ) =
and ref($v) eq 'HASH' each
%{ $decode_json->{abilities}[0]{properties}[$properties] } )
{
next
if ( ref($v) eq 'ARRAY' );
#$v = encode_utf8($v);
readingsBulkUpdateIfChanged(
$hash,
$decode_json->{abilities}[0]{properties}[$properties]
{name} . '-' . $t,
$v
)
unless (
$decode_json->{abilities}[0]{properties}[$properties]
{name} eq 'ethernet_status'
or $decode_json->{abilities}[0]{properties}[$properties]
{name} eq 'wifi_status' );
if (
(
$decode_json->{abilities}[0]{properties}
[$properties]{name} eq 'ethernet_status'
or $decode_json->{abilities}[0]{properties}
[$properties]{name} eq 'wifi_status'
)
and ref($v) eq 'HASH'
) )
{ {
if ( $decode_json->{abilities}[0]{properties}[$properties]{name} eq 'ethernet_status' ) { if ( $decode_json->{abilities}[0]{properties}
readingsBulkUpdateIfChanged( $hash, 'ethernet_status-mac', $v->{mac} ); [$properties]{name} eq 'ethernet_status' )
readingsBulkUpdateIfChanged( $hash, 'ethernet_status-ip', $v->{ip} ) {
if ( ref($v->{ip}) ne 'HASH' ); readingsBulkUpdateIfChanged( $hash,
readingsBulkUpdateIfChanged( $hash, 'ethernet_status-isconnected', $v->{isconnected} ); 'ethernet_status-mac', $v->{mac} );
readingsBulkUpdateIfChanged( $hash,
'ethernet_status-ip', $v->{ip} )
if ( ref( $v->{ip} ) ne 'HASH' );
readingsBulkUpdateIfChanged( $hash,
'ethernet_status-isconnected',
$v->{isconnected} );
} }
elsif ( $decode_json->{abilities}[0]{properties}[$properties]{name} eq 'wifi_status') { elsif ( $decode_json->{abilities}[0]{properties}
readingsBulkUpdateIfChanged( $hash, 'wifi_status-ssid', $v->{ssid} ); [$properties]{name} eq 'wifi_status' )
readingsBulkUpdateIfChanged( $hash, 'wifi_status-mac', $v->{mac} ); {
readingsBulkUpdateIfChanged( $hash, 'wifi_status-ip', $v->{ip} ) readingsBulkUpdateIfChanged( $hash,
if ( ref($v->{ip}) ne 'HASH' ); 'wifi_status-ssid', $v->{ssid} );
readingsBulkUpdateIfChanged( $hash, 'wifi_status-isconnected', $v->{isconnected} ); readingsBulkUpdateIfChanged( $hash,
readingsBulkUpdateIfChanged( $hash, 'wifi_status-signal', $v->{signal} ); 'wifi_status-mac', $v->{mac} );
readingsBulkUpdateIfChanged( $hash,
'wifi_status-ip', $v->{ip} )
if ( ref( $v->{ip} ) ne 'HASH' );
readingsBulkUpdateIfChanged( $hash,
'wifi_status-isconnected', $v->{isconnected} );
readingsBulkUpdateIfChanged( $hash,
'wifi_status-signal', $v->{signal} );
} }
} }
} }

View File

@ -59,7 +59,7 @@ use strict;
use warnings; use warnings;
use FHEM::Meta; use FHEM::Meta;
my $version = "1.6.2"; my $version = "1.6.3";
sub GardenaSmartDevice_Initialize($) { sub GardenaSmartDevice_Initialize($) {
@ -84,7 +84,7 @@ sub GardenaSmartDevice_Initialize($) {
my $hash = $modules{GardenaSmartDevice}{defptr}{$d}; my $hash = $modules{GardenaSmartDevice}{defptr}{$d};
$hash->{VERSION} = $version; $hash->{VERSION} = $version;
} }
return FHEM::Meta::InitMod( __FILE__, $hash ); return FHEM::Meta::InitMod( __FILE__, $hash );
} }
@ -249,7 +249,8 @@ sub Set($@) {
my $duration = join( " ", @args ); my $duration = join( " ", @args );
$payload = '"name":"pump_manual_watering_timer","parameters":{"duration":' $payload =
'"name":"pump_manual_watering_timer","parameters":{"duration":'
. $duration . '}'; . $duration . '}';
} }
### watering_computer ### watering_computer
@ -264,13 +265,11 @@ sub Set($@) {
$payload = '"name":"cancel_override"'; $payload = '"name":"cancel_override"';
} }
elsif ( lc $cmd eq 'on' or lc $cmd eq 'off' or lc $cmd eq 'on-for-timer' ) { elsif ( lc $cmd eq 'on' or lc $cmd eq 'off' or lc $cmd eq 'on-for-timer' ) {
my $val = ( defined($args[0]) ? join(" ", @args)*60 : lc $cmd ); my $val = ( defined( $args[0] ) ? join( " ", @args ) * 60 : lc $cmd );
$payload = $payload = '"properties":{"value":"' . $val . '"}';
'"properties":{"value":"' . $val . '"}';
} }
### Watering ic24 ### Watering ic24
elsif ( $cmd =~ /manualDurationValve/ ) { elsif ( $cmd =~ /manualDurationValve/ ) {
@ -320,8 +319,8 @@ sub Set($@) {
$list .= 'manualOverride:slider,0,1,59 cancelOverride:noArg' $list .= 'manualOverride:slider,0,1,59 cancelOverride:noArg'
if ( AttrVal( $name, 'model', 'unknown' ) eq 'watering_computer' ); if ( AttrVal( $name, 'model', 'unknown' ) eq 'watering_computer' );
$list .= 'pumpTimer:slider,0,1,59' # $list .= 'pumpTimer:slider,0,1,59'
if ( AttrVal( $name, 'model', 'unknown' ) eq 'electronic_pressure_pump' ); # if ( AttrVal( $name, 'model', 'unknown' ) eq 'electronic_pressure_pump' );
$list .= $list .=
'manualDurationValve1:slider,1,1,59 manualDurationValve2:slider,1,1,59 manualDurationValve3:slider,1,1,59 manualDurationValve4:slider,1,1,59 manualDurationValve5:slider,1,1,59 manualDurationValve6:slider,1,1,59' 'manualDurationValve1:slider,1,1,59 manualDurationValve2:slider,1,1,59 manualDurationValve3:slider,1,1,59 manualDurationValve4:slider,1,1,59 manualDurationValve5:slider,1,1,59 manualDurationValve6:slider,1,1,59'
@ -426,9 +425,7 @@ sub WriteReadings($$) {
$hash, $hash,
$decode_json->{abilities}[$abilities]{name} . '-' $decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name}, . $propertie->{name},
RigRadingsValue( RigRadingsValue( $hash, $propertie->{value} )
$hash, $propertie->{value}
)
) )
if ( defined( $propertie->{value} ) if ( defined( $propertie->{value} )
and $decode_json->{abilities}[$abilities]{name} . '-' and $decode_json->{abilities}[$abilities]{name} . '-'
@ -451,9 +448,7 @@ sub WriteReadings($$) {
$hash, $hash,
$decode_json->{abilities}[$abilities]{name} . '-' $decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name}, . $propertie->{name},
RigRadingsValue( RigRadingsValue( $hash, $propertie->{value} )
$hash, $propertie->{value}
)
) )
if ( if (
defined( $propertie->{value} ) defined( $propertie->{value} )
@ -578,14 +573,10 @@ sub WriteReadings($$) {
) )
) )
) if ( AttrVal( $name, 'model', 'unknown' ) eq 'ic24' ); ) if ( AttrVal( $name, 'model', 'unknown' ) eq 'ic24' );
readingsBulkUpdate( readingsBulkUpdate( $hash, 'state',
$hash, 'state', ReadingsVal( $name, 'power-power_timer', 'no info from power-timer' ) )
ReadingsVal( if ( AttrVal( $name, 'model', 'unknown' ) eq 'power' );
$name, 'power-power_timer',
'no info from power-timer'
)
) if ( AttrVal( $name, 'model', 'unknown' ) eq 'power' );
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
@ -725,8 +716,7 @@ sub RigRadingsValue($$) {
$rigReadingValue = Zulu2LocalString($readingValue); $rigReadingValue = Zulu2LocalString($readingValue);
} }
else { else {
$rigReadingValue = $rigReadingValue = ReadingLangGerman( $hash, $readingValue );
ReadingLangGerman( $hash, $readingValue );
} }
return $rigReadingValue; return $rigReadingValue;