Merge branch 'patch-Errorhandling' into devel

This commit is contained in:
Marko Oldenburg 2019-05-21 15:41:48 +02:00
commit dc7a1f9467
2 changed files with 79 additions and 33 deletions

View File

@ -59,7 +59,7 @@ use strict;
use warnings; use warnings;
use FHEM::Meta; use FHEM::Meta;
my $version = "1.6.1"; my $version = "1.6.3";
sub GardenaSmartBridge_Initialize($) { sub GardenaSmartBridge_Initialize($) {
@ -404,6 +404,12 @@ 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) };
if ($@) {
Log3 $name, 3,
"GardenaSmartBridge ($name) - JSON error while request";
}
if ( defined($err) ) { if ( defined($err) ) {
if ( $err ne "" ) { if ( $err ne "" ) {
@ -497,13 +503,10 @@ sub ErrorHandling($$$) {
return; return;
} }
if ( if ( $data =~ /Error/
( or ( defined( $decode_json )
( $data =~ /Error/ ) and ref($decode_json) eq 'HASH'
or defined( eval { decode_json($data) }->{errors} ) and defined($decode_json->{errors}) )
)
and ref($param->{code}) eq 'HASH'
and exists( $param->{code} )
) )
{ {
readingsBeginUpdate($dhash); readingsBeginUpdate($dhash);
@ -513,28 +516,28 @@ sub ErrorHandling($$$) {
readingsBulkUpdate( $dhash, "lastRequestState", "request_error", 1 ); readingsBulkUpdate( $dhash, "lastRequestState", "request_error", 1 );
if ( $param->{code} == 400 ) { if ( $param->{code} == 400 ) {
if ( eval { decode_json($data) } ) { if ( $decode_json ) {
if ( ref( eval { decode_json($data) }->{errors} ) eq "ARRAY" if ( ref( $decode_json->{errors} ) eq "ARRAY"
and defined( eval { decode_json($data) }->{errors} ) ) and defined( $decode_json->{errors} ) )
{ {
readingsBulkUpdate( readingsBulkUpdate(
$dhash, $dhash,
"state", "state",
eval { decode_json($data) }->{errors}[0]{error} . ' ' $decode_json->{errors}[0]{error} . ' '
. eval { decode_json($data) }->{errors}[0]{attribute}, . $decode_json->{errors}[0]{attribute},
1 1
); );
readingsBulkUpdate( readingsBulkUpdate(
$dhash, $dhash,
"lastRequestState", "lastRequestState",
eval { decode_json($data) }->{errors}[0]{error} . ' ' $decode_json->{errors}[0]{error} . ' '
. eval { decode_json($data) }->{errors}[0]{attribute}, . $decode_json->{errors}[0]{attribute},
1 1
); );
Log3 $dname, 5, Log3 $dname, 5,
"GardenaSmartBridge ($dname) - RequestERROR: " "GardenaSmartBridge ($dname) - RequestERROR: "
. eval { decode_json($data) }->{errors}[0]{error} . " " . $decode_json->{errors}[0]{error} . " "
. eval { decode_json($data) }->{errors}[0]{attribute}; . $decode_json->{errors}[0]{attribute};
} }
} }
else { else {
@ -593,7 +596,8 @@ 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' );
} }
sub ResponseProcessing($$) { sub ResponseProcessing($$) {
@ -682,6 +686,9 @@ 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)
if ( defined($decode_json->{category})
and $decode_json->{category} eq 'gateway' );
} }
( $json, $tail ) = ParseJSON( $hash, $tail ); ( $json, $tail ) = ParseJSON( $hash, $tail );
@ -711,25 +718,64 @@ sub WriteReadings($$) {
and defined( $decode_json->{name} ) and defined( $decode_json->{name} )
and $decode_json->{name} ) and $decode_json->{name} )
{ {
readingsBeginUpdate($hash); readingsBeginUpdate($hash);
readingsBulkUpdateIfChanged( $hash, 'name', $decode_json->{name} ); if ( $decode_json->{id} eq $hash->{helper}{locations_id} ) {
readingsBulkUpdateIfChanged( $hash, 'authorized_user_ids',
scalar( @{ $decode_json->{authorized_user_ids} } ) ); readingsBulkUpdateIfChanged( $hash, 'name', $decode_json->{name} );
readingsBulkUpdateIfChanged( $hash, 'devices', readingsBulkUpdateIfChanged( $hash, 'authorized_user_ids',
scalar( @{ $decode_json->{devices} } ) ); 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); $v = encode_utf8($v);
readingsBulkUpdateIfChanged( $hash, $t, $v ); readingsBulkUpdateIfChanged( $hash, $t, $v );
}
readingsBulkUpdateIfChanged( $hash, 'zones',
scalar( @{ $decode_json->{zones} } ) );
}
elsif ( $decode_json->{id} ne $hash->{helper}{locations_id} ) {
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' );
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} );
}
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} );
}
}
}
$properties--;
} while ( $properties >= 0 );
} }
readingsBulkUpdateIfChanged( $hash, 'zones',
scalar( @{ $decode_json->{zones} } ) );
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
} }
Log3 $name, 3, "GardenaSmartBridge ($name) - readings would be written"; Log3 $name, 4, "GardenaSmartBridge ($name) - readings would be written";
} }
#################################### ####################################

View File

@ -59,7 +59,7 @@ use strict;
use warnings; use warnings;
use FHEM::Meta; use FHEM::Meta;
my $version = "1.6.1"; my $version = "1.6.2";
sub GardenaSmartDevice_Initialize($) { sub GardenaSmartDevice_Initialize($) {
@ -367,7 +367,7 @@ sub Parse($$) {
my $decode_json = eval { decode_json($json) }; my $decode_json = eval { decode_json($json) };
if ($@) { if ($@) {
Log3 $name, 3, Log3 $name, 3,
"GardenaSmartBridge ($name) - JSON error while request: $@"; "GardenaSmartDevice ($name) - JSON error while request: $@";
} }
Log3 $name, 4, "GardenaSmartDevice ($name) - ParseFn was called"; Log3 $name, 4, "GardenaSmartDevice ($name) - ParseFn was called";