besseres Error Handling für Bridge und Device, set lockAction mit direkter Rückmeldung

This commit is contained in:
Marko Oldenburg 2016-09-28 22:01:55 +02:00
parent 030d0b77a0
commit 0928638b33
2 changed files with 80 additions and 35 deletions

View File

@ -34,7 +34,7 @@ use JSON;
use Time::HiRes qw(gettimeofday); use Time::HiRes qw(gettimeofday);
use HttpUtils; 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} ) ) { if( ( $json =~ /Error/i ) and exists( $param->{code} ) ) {
readingsBulkUpdate( $hash, "lastError", "invalid API token" ) if( $param->{code} eq 401 ); 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 and $hash == $param->{chash} );
readingsBulkUpdate( $hash, "lastError", "action is undefined" ) if( $param->{code} eq 400 );
###### 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) - 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) - 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 ############ ######### Zum testen da ich kein Nuki Smartlock habe ############
#if ( $param->{code} eq 404 ) { #if ( $param->{code} eq 404 ) {
# Log3 $name, 3, "NUKIBridge ($name) - Test JSON String"; # 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"}'; # $json = '{"state": 1, "stateName": "locked", "batteryCritical": false, "success": "true"}';
# }
# NUKIDevice_Parse($param->{chash},$json); # NUKIDevice_Parse($param->{chash},$json);
#} #}
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
return $param->{code}; return $param->{code};
} }

View File

@ -33,7 +33,7 @@ use warnings;
use JSON; use JSON;
use Time::HiRes qw(gettimeofday); use Time::HiRes qw(gettimeofday);
my $version = "0.1.40"; my $version = "0.1.42";
@ -228,20 +228,10 @@ sub NUKIDevice_Set($$@) {
return "Unknown argument $cmd, choose one of $list"; 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} ); return undef;
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";
}
} }
sub NUKIDevice_GetUpdate($) { sub NUKIDevice_GetUpdate($) {
@ -306,6 +296,27 @@ sub NUKIDevice_Parse($$) {
my $name = $hash->{NAME}; 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); my $decode_json = decode_json($result);
if( ref($decode_json) ne "HASH" ) { if( ref($decode_json) ne "HASH" ) {
@ -317,7 +328,27 @@ sub NUKIDevice_Parse($$) {
############################ ############################
#### Status des Smartkey #### Status des Smartlock
readingsBeginUpdate($hash);
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; my $battery;
if( $decode_json->{batteryCritical} eq "false" ) { if( $decode_json->{batteryCritical} eq "false" ) {
$battery = "ok"; $battery = "ok";
@ -325,17 +356,20 @@ sub NUKIDevice_Parse($$) {
$battery = "low"; $battery = "low";
} }
readingsBulkUpdate( $hash, "batteryCritical", $decode_json->{batteryCritical} );
readingsBeginUpdate($hash); readingsBulkUpdate( $hash, "lockState", $decode_json->{stateName} );
readingsBulkUpdate( $hash, "state", $decode_json->{stateName} ); readingsBulkUpdate( $hash, "state", $decode_json->{stateName} );
readingsBulkUpdate( $hash, "battery", $battery ); readingsBulkUpdate( $hash, "battery", $battery );
readingsBulkUpdate( $hash, "success", $decode_json->{success} ); readingsBulkUpdate( $hash, "success", $decode_json->{success} );
readingsEndUpdate( $hash, 1 );
Log3 $name, 3, "readings set for $name"; Log3 $name, 3, "readings set for $name";
} }
readingsEndUpdate( $hash, 1 );
return undef;
}
1; 1;