diff --git a/73_NUKIBridge.pm b/73_NUKIBridge.pm index 265a0eb..276ac94 100644 --- a/73_NUKIBridge.pm +++ b/73_NUKIBridge.pm @@ -34,7 +34,7 @@ use JSON; use Time::HiRes qw(gettimeofday); use HttpUtils; -my $version = "0.1.40"; +my $version = "0.1.42"; @@ -335,21 +335,32 @@ sub NUKIBridge_Dispatch($$$) { if( ( $json =~ /Error/i ) and exists( $param->{code} ) ) { readingsBulkUpdate( $hash, "lastError", "invalid API token" ) if( $param->{code} eq 401 ); - readingsBulkUpdate( $hash, "lastError", "nukiId is not known" ) if( $param->{code} eq 404 ); - readingsBulkUpdate( $hash, "lastError", "action is undefined" ) if( $param->{code} eq 400 ); + readingsBulkUpdate( $hash, "lastError", "action is undefined" ) if( $param->{code} eq 400 and $hash == $param->{chash} ); + + + ###### Fehler bei Antwort auf Anfrage eines logischen Devices ###### + NUKIDevice_Parse($param->{chash},$param->{code}) if( $param->{code} eq 404 ); + NUKIDevice_Parse($param->{chash},$param->{code}) if( $param->{code} eq 400 and $hash != $param->{chash} ); + Log3 $name, 3, "NUKIBridge ($name) - invalid API token" if( $param->{code} eq 401 ); Log3 $name, 3, "NUKIBridge ($name) - nukiId is not known" if( $param->{code} eq 404 ); - Log3 $name, 3, "NUKIBridge ($name) - action is undefined" if( $param->{code} eq 400 ); + Log3 $name, 3, "NUKIBridge ($name) - action is undefined" if( $param->{code} eq 400 and $hash == $param->{chash} ); ######### Zum testen da ich kein Nuki Smartlock habe ############ #if ( $param->{code} eq 404 ) { - # Log3 $name, 3, "NUKIBridge ($name) - Test JSON String"; - # $json = '{"state": 1, "stateName": "locked", "batteryCritical": false, "success": "true"}'; + # if( defined($param->{chash}->{helper}{lockAction}) ) { + # Log3 $name, 3, "NUKIBridge ($name) - Test JSON String for lockAction"; + # $json = '{"success": true, "batteryCritical": false}'; + # } else { + # Log3 $name, 3, "NUKIBridge ($name) - Test JSON String for lockState"; + # $json = '{"state": 1, "stateName": "locked", "batteryCritical": false, "success": "true"}'; + # } # NUKIDevice_Parse($param->{chash},$json); #} - + + readingsEndUpdate( $hash, 1 ); return $param->{code}; } diff --git a/74_NUKIDevice.pm b/74_NUKIDevice.pm index 16e67cb..36eecc4 100644 --- a/74_NUKIDevice.pm +++ b/74_NUKIDevice.pm @@ -33,7 +33,7 @@ use warnings; use JSON; use Time::HiRes qw(gettimeofday); -my $version = "0.1.40"; +my $version = "0.1.42"; @@ -209,7 +209,7 @@ sub NUKIDevice_Set($$@) { } elsif( $cmd eq 'lock' ) { $lockAction = $cmd; - + } elsif( $cmd eq 'unlock' ) { $lockAction = $cmd; @@ -228,20 +228,10 @@ sub NUKIDevice_Set($$@) { return "Unknown argument $cmd, choose one of $list"; } + $hash->{helper}{lockAction} = $lockAction; + NUKIDevice_ReadFromNUKIBridge($hash,"lockAction",$lockAction,$hash->{NUKIID} ); - my $result = NUKIDevice_ReadFromNUKIBridge($hash,"lockAction",$lockAction,$hash->{NUKIID} ); - - if( !defined($result) ) { - - $hash->{STATE} = "unknown"; - Log3 $name, 3, "NUKIDevice ($name) - unknown result to ReadFromNUKIBridge"; - return; - - } else { - - NUKIDevice_Parse($hash,$result); - Log3 $name, 3, "NUKIDevice ($name) - Call NUKIDevice_Parse"; - } + return undef; } sub NUKIDevice_GetUpdate($) { @@ -306,6 +296,27 @@ sub NUKIDevice_Parse($$) { my $name = $hash->{NAME}; + ######################################### + ####### Errorhandling ############# + + if( $result =~ /\d{3}/ ) { + if( $result eq 400 ) { + readingsSingleUpdate( $hash, "state", "action is undefined", 1 ); + Log3 $name, 3, "NUKIDevice ($name) - action is undefined"; + return; + } + + if( $result eq 404 ) { + readingsSingleUpdate( $hash, "state", "nukiId is not known", 1 ); + Log3 $name, 3, "NUKIDevice ($name) - nukiId is not known"; + return; + } + } + + + ######################################### + #### verarbeiten des JSON Strings ####### + my $decode_json = decode_json($result); if( ref($decode_json) ne "HASH" ) { @@ -317,23 +328,46 @@ sub NUKIDevice_Parse($$) { ############################ - #### Status des Smartkey - my $battery; - if( $decode_json->{batteryCritical} eq "false" ) { - $battery = "ok"; - } else { - $battery = "low"; - } - + #### Status des Smartlock readingsBeginUpdate($hash); - readingsBulkUpdate( $hash, "state", $decode_json->{stateName} ); - readingsBulkUpdate( $hash, "battery", $battery ); - readingsBulkUpdate( $hash, "success", $decode_json->{success} ); + + if( defined($hash->{helper}{lockAction}) ) { + + my ($state,$lockState); + + $state = $hash->{helper}{lockAction} if( $decode_json->{success} eq "true" ); + $state = "error" if( $decode_json->{success} eq "false" ); + $lockState = $hash->{helper}{lockAction} if( $decode_json->{success} eq "true" ); + + + readingsBulkUpdate( $hash, "state", $state ); + readingsBulkUpdate( $hash, "lockState", $lockState ); + readingsBulkUpdate( $hash, "success", $decode_json->{success} ); + + delete $hash->{helper}{lockAction}; + + } else { + + my $battery; + if( $decode_json->{batteryCritical} eq "false" ) { + $battery = "ok"; + } else { + $battery = "low"; + } + + readingsBulkUpdate( $hash, "batteryCritical", $decode_json->{batteryCritical} ); + readingsBulkUpdate( $hash, "lockState", $decode_json->{stateName} ); + readingsBulkUpdate( $hash, "state", $decode_json->{stateName} ); + readingsBulkUpdate( $hash, "battery", $battery ); + readingsBulkUpdate( $hash, "success", $decode_json->{success} ); + + Log3 $name, 3, "readings set for $name"; + } + readingsEndUpdate( $hash, 1 ); - - Log3 $name, 3, "readings set for $name"; + return undef; }