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 FHEM::Meta;
my $version = '1.6.4.1';
my $version = '1.6.5';
sub GardenaSmartBridge_Initialize($) {
@ -94,7 +93,7 @@ sub GardenaSmartBridge_Initialize($) {
my $hash = $modules{GardenaSmartBridge}{defptr}{$d};
$hash->{VERSION} = $version;
}
return FHEM::Meta::InitMod( __FILE__, $hash );
}
@ -154,12 +153,17 @@ sub Define($$) {
return 'too few parameters: define <NAME> GardenaSmartBridge'
if ( @a != 2 );
return
'Cannot define Gardena Bridge device. Perl modul ' . ${missingModul} . ' is missing.'
'Cannot define Gardena Bridge device. Perl modul '
. ${missingModul}
. ' is missing.'
if ($missingModul);
my $name = $a[0];
$hash->{BRIDGE} = 1;
$hash->{URL} = AttrVal($name,'gardenaBaseURL','https://sg-api.dss.husqvarnagroup.net') . '/sg-1';
$hash->{BRIDGE} = 1;
$hash->{URL} =
AttrVal( $name, 'gardenaBaseURL',
'https://sg-api.dss.husqvarnagroup.net' )
. '/sg-1';
$hash->{VERSION} = $version;
$hash->{INTERVAL} = 60;
$hash->{NOTIFYDEV} = "global,$name";
@ -392,8 +396,10 @@ sub Write($@) {
}
);
Log3($name, 4,
"GardenaSmartBridge ($name) - Send with URL: $hash->{URL}$uri, HEADER: secret!, DATA: secret!, METHOD: $method");
Log3( $name, 4,
"GardenaSmartBridge ($name) - Send with URL: $hash->{URL}$uri, HEADER: secret!, DATA: secret!, METHOD: $method"
);
# Log3($name, 3,
# "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'} ) );
my $dname = $dhash->{NAME};
my $decode_json = eval { decode_json($data) };
if ($@) {
Log3 $name, 3,
"GardenaSmartBridge ($name) - JSON error while request";
Log3 $name, 3, "GardenaSmartBridge ($name) - JSON error while request";
}
if ( defined($err) ) {
@ -487,7 +492,11 @@ sub ErrorHandling($$$) {
readingsBulkUpdate( $dhash, "state", "the command is processed",
1 );
InternalTimer( gettimeofday() + 5, "FHEM::GardenaSmartBridge::getDevices", $hash, 1 );
InternalTimer(
gettimeofday() + 5,
"FHEM::GardenaSmartBridge::getDevices",
$hash, 1
);
}
elsif ( $param->{code} != 200 ) {
@ -509,10 +518,11 @@ sub ErrorHandling($$$) {
return;
}
if ( $data =~ /Error/
or ( defined( $decode_json )
and ref($decode_json) eq 'HASH'
and defined($decode_json->{errors}) )
if (
$data =~ /Error/
or ( defined($decode_json)
and ref($decode_json) eq 'HASH'
and defined( $decode_json->{errors} ) )
)
{
readingsBeginUpdate($dhash);
@ -522,7 +532,7 @@ sub ErrorHandling($$$) {
readingsBulkUpdate( $dhash, "lastRequestState", "request_error", 1 );
if ( $param->{code} == 400 ) {
if ( $decode_json ) {
if ($decode_json) {
if ( ref( $decode_json->{errors} ) eq "ARRAY"
and defined( $decode_json->{errors} ) )
{
@ -603,7 +613,7 @@ sub ErrorHandling($$$) {
readingsSingleUpdate( $hash, 'state', 'connected to cloud', 1 )
if ( defined( $hash->{helper}{locations_id} ) );
ResponseProcessing( $hash, $data )
if ( ref($decode_json) eq 'HASH' );
if ( ref($decode_json) eq 'HASH' );
}
sub ResponseProcessing($$) {
@ -692,8 +702,8 @@ sub ResponseProcessing($$) {
Dispatch( $hash, $json, undef )
unless ( $decode_json->{category} eq 'gateway' );
WriteReadings($hash,$decode_json)
if ( defined($decode_json->{category})
WriteReadings( $hash, $decode_json )
if ( defined( $decode_json->{category} )
and $decode_json->{category} eq 'gateway' );
}
@ -726,51 +736,85 @@ sub WriteReadings($$) {
{
readingsBeginUpdate($hash);
if ( $decode_json->{id} eq $hash->{helper}{locations_id} ) {
readingsBulkUpdateIfChanged( $hash, 'name', $decode_json->{name} );
readingsBulkUpdateIfChanged( $hash, 'authorized_user_ids',
scalar( @{ $decode_json->{authorized_user_ids} } ) );
readingsBulkUpdateIfChanged( $hash, '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);
readingsBulkUpdateIfChanged( $hash, $t, $v );
}
readingsBulkUpdateIfChanged( $hash, 'zones',
scalar( @{ $decode_json->{zones} } ) );
}
elsif ( $decode_json->{id} ne $hash->{helper}{locations_id} and ref($decode_json->{abilities}) eq 'ARRAY' ) {
my $properties = scalar( @{ $decode_json->{abilities}[0]{properties} } );
do {
while ( ( my ( $t, $v ) ) = 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' );
}
elsif ( $decode_json->{id} ne $hash->{helper}{locations_id}
and ref( $decode_json->{abilities} ) eq 'ARRAY' )
{
my $properties =
scalar( @{ $decode_json->{abilities}[0]{properties} } );
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'
do {
while ( ( my ( $t, $v ) ) =
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' ) {
readingsBulkUpdateIfChanged( $hash, '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} );
if ( $decode_json->{abilities}[0]{properties}
[$properties]{name} eq 'ethernet_status' )
{
readingsBulkUpdateIfChanged( $hash,
'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') {
readingsBulkUpdateIfChanged( $hash, 'wifi_status-ssid', $v->{ssid} );
readingsBulkUpdateIfChanged( $hash, '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} );
elsif ( $decode_json->{abilities}[0]{properties}
[$properties]{name} eq 'wifi_status' )
{
readingsBulkUpdateIfChanged( $hash,
'wifi_status-ssid', $v->{ssid} );
readingsBulkUpdateIfChanged( $hash,
'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 FHEM::Meta;
my $version = "1.6.2";
my $version = "1.6.3";
sub GardenaSmartDevice_Initialize($) {
@ -84,7 +84,7 @@ sub GardenaSmartDevice_Initialize($) {
my $hash = $modules{GardenaSmartDevice}{defptr}{$d};
$hash->{VERSION} = $version;
}
return FHEM::Meta::InitMod( __FILE__, $hash );
}
@ -249,7 +249,8 @@ sub Set($@) {
my $duration = join( " ", @args );
$payload = '"name":"pump_manual_watering_timer","parameters":{"duration":'
$payload =
'"name":"pump_manual_watering_timer","parameters":{"duration":'
. $duration . '}';
}
### watering_computer
@ -264,13 +265,11 @@ sub Set($@) {
$payload = '"name":"cancel_override"';
}
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 );
$payload =
'"properties":{"value":"' . $val . '"}';
my $val = ( defined( $args[0] ) ? join( " ", @args ) * 60 : lc $cmd );
$payload = '"properties":{"value":"' . $val . '"}';
}
### Watering ic24
elsif ( $cmd =~ /manualDurationValve/ ) {
@ -320,8 +319,8 @@ sub Set($@) {
$list .= 'manualOverride:slider,0,1,59 cancelOverride:noArg'
if ( AttrVal( $name, 'model', 'unknown' ) eq 'watering_computer' );
$list .= 'pumpTimer:slider,0,1,59'
if ( AttrVal( $name, 'model', 'unknown' ) eq 'electronic_pressure_pump' );
# $list .= 'pumpTimer:slider,0,1,59'
# if ( AttrVal( $name, 'model', 'unknown' ) eq 'electronic_pressure_pump' );
$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'
@ -426,9 +425,7 @@ sub WriteReadings($$) {
$hash,
$decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name},
RigRadingsValue(
$hash, $propertie->{value}
)
RigRadingsValue( $hash, $propertie->{value} )
)
if ( defined( $propertie->{value} )
and $decode_json->{abilities}[$abilities]{name} . '-'
@ -451,9 +448,7 @@ sub WriteReadings($$) {
$hash,
$decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name},
RigRadingsValue(
$hash, $propertie->{value}
)
RigRadingsValue( $hash, $propertie->{value} )
)
if (
defined( $propertie->{value} )
@ -578,14 +573,10 @@ sub WriteReadings($$) {
)
)
) if ( AttrVal( $name, 'model', 'unknown' ) eq 'ic24' );
readingsBulkUpdate(
$hash, 'state',
ReadingsVal(
$name, 'power-power_timer',
'no info from power-timer'
)
) if ( AttrVal( $name, 'model', 'unknown' ) eq 'power' );
readingsBulkUpdate( $hash, 'state',
ReadingsVal( $name, 'power-power_timer', 'no info from power-timer' ) )
if ( AttrVal( $name, 'model', 'unknown' ) eq 'power' );
readingsEndUpdate( $hash, 1 );
@ -725,8 +716,7 @@ sub RigRadingsValue($$) {
$rigReadingValue = Zulu2LocalString($readingValue);
}
else {
$rigReadingValue =
ReadingLangGerman( $hash, $readingValue );
$rigReadingValue = ReadingLangGerman( $hash, $readingValue );
}
return $rigReadingValue;