erweitertes Error Handling, 2 Stufen Modulkommunikation verbessert

This commit is contained in:
Marko Oldenburg 2016-09-28 13:57:32 +02:00
parent be870d67c4
commit 030d0b77a0
2 changed files with 100 additions and 68 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.35"; my $version = "0.1.40";
@ -76,8 +76,8 @@ sub NUKIBridge_Initialize($) {
sub NUKIBridge_Read($@) { sub NUKIBridge_Read($@) {
my ($hash,$chash,$name,$path,$lockAction,$nukiId)= @_; my ($hash,$chash,$name,$path,$lockAction,$nukiId)= @_;
NUKIBridge_Call($hash,$chash,$path,$lockAction,$nukiId );
return NUKIBridge_Call($hash,$chash,$path,$lockAction,$nukiId );
} }
sub NUKIBridge_Define($$) { sub NUKIBridge_Define($$) {
@ -113,9 +113,10 @@ sub NUKIBridge_Define($$) {
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
if( $init_done ) { if( $init_done ) {
NUKIDevice_GetUpdate($hash); NUKIBridge_Get($hash) if( ($hash->{HOST}) and ($hash->{TOKEN}) );
NUKIBridge_GetCheckBridgeAlive($hash) if( ($hash->{HOST}) and ($hash->{TOKEN}) );
} else { } else {
InternalTimer( gettimeofday()+15, "NUKIBridge_GetUpdate", $hash, 0 ) if( ($hash->{HOST}) and ($hash->{TOKEN}) ); InternalTimer( gettimeofday()+15, "NUKIBridge_Get", $hash, 0 ) if( ($hash->{HOST}) and ($hash->{TOKEN}) );
} }
$modules{NUKIBridge}{defptr}{$hash->{HOST}} = $hash; $modules{NUKIBridge}{defptr}{$hash->{HOST}} = $hash;
@ -148,7 +149,7 @@ sub NUKIBridge_Attr(@) {
if( $cmd eq "set" ) { if( $cmd eq "set" ) {
if( $attrVal eq "0" ) { if( $attrVal eq "0" ) {
RemoveInternalTimer( $hash ); RemoveInternalTimer( $hash );
InternalTimer( gettimeofday()+2, "NUKIBridge_GetUpdate", $hash, 0 ); InternalTimer( gettimeofday()+2, "NUKIBridge_GetCheckBridgeAlive", $hash, 0 );
readingsSingleUpdate($hash, 'state', 'Initialized', 1 ); readingsSingleUpdate($hash, 'state', 'Initialized', 1 );
Log3 $name, 3, "NUKIBridge ($name) - enabled"; Log3 $name, 3, "NUKIBridge ($name) - enabled";
} else { } else {
@ -159,7 +160,7 @@ sub NUKIBridge_Attr(@) {
} else { } else {
RemoveInternalTimer( $hash ); RemoveInternalTimer( $hash );
InternalTimer( gettimeofday()+2, "NUKIBridge_GetUpdate", $hash, 0 ); InternalTimer( gettimeofday()+2, "NUKIBridge_GetCheckBridgeAlive", $hash, 0 );
readingsSingleUpdate($hash, 'state', 'Initialized', 1 ); readingsSingleUpdate($hash, 'state', 'Initialized', 1 );
Log3 $name, 3, "NUKIBridge ($name) - enabled"; Log3 $name, 3, "NUKIBridge ($name) - enabled";
} }
@ -196,20 +197,23 @@ sub NUKIBridge_Set($@) {
my ($arg, @params) = @args; my ($arg, @params) = @args;
if($cmd eq 'statusRequest') { if($cmd eq 'autocreate') {
return "usage: statusRequest" if( @args != 0 ); return "usage: autocreate" if( @args != 0 );
$hash->{LOCAL} = 1;
NUKIBridge_Get($hash); NUKIBridge_Get($hash);
delete $hash->{LOCAL};
return undef; return undef;
} elsif($cmd eq 'other') { } elsif($cmd eq 'statusRequest') {
NUKIBridge_GetCheckBridgeAlive($hash);
return undef;
} elsif($cmd eq 'other2') { } elsif($cmd eq 'other2') {
} else { } else {
my $list = "statusRequest:noArg"; my $list = "statusRequest:noArg autocreate:noArg";
return "Unknown argument $cmd, choose one of $list"; return "Unknown argument $cmd, choose one of $list";
} }
@ -220,69 +224,74 @@ sub NUKIBridge_Get($) {
my ($hash) = @_; my ($hash) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
RemoveInternalTimer($hash);
NUKIBridge_Call($hash,$hash,"list",undef,undef) if( !IsDisabled($name) ); NUKIBridge_Call($hash,$hash,"list",undef,undef) if( !IsDisabled($name) );
NUKIBridge_GetCheckBridgeAlive($hash);
Log3 $name, 3, "NUKIBridge ($name) - Call NUKIBridge_Get" if( !IsDisabled($name) ); Log3 $name, 3, "NUKIBridge ($name) - Call NUKIBridge_Get" if( !IsDisabled($name) );
return 1; return 1;
} }
sub NUKIBridge_GetUpdate($) { sub NUKIBridge_GetCheckBridgeAlive($) {
my ($hash) = @_; my ($hash) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
RemoveInternalTimer($hash);
if( !IsDisabled($name) ) { if( !IsDisabled($name) ) {
#RemoveInternalTimer($hash); NUKIBridge_Call($hash,$hash,"list",undef,undef,1);
NUKIBridge_Call($hash,$hash,"list",undef,undef);
#InternalTimer( gettimeofday()+$hash->{INTERVAL}, "NUKIBridge_GetUpdate", $hash, 1 ); InternalTimer( gettimeofday()+$hash->{INTERVAL}, "NUKIBridge_GetCheckBridgeAlive", $hash, 1 );
Log3 $name, 3, "NUKIBridge ($name) - Call NUKIBridge_GetUpdate"; Log3 $name, 3, "NUKIBridge ($name) - Call InternalTimer for NUKIBridge_GetCheckBridgeAlive";
} }
return 1; return 1;
} }
sub NUKIBridge_Call($$$$$;$) { sub NUKIBridge_Call($$$$$;$) {
my ($hash,$chash,$path,$lockAction,$nukiId,$method) = @_; my ($hash,$chash,$path,$lockAction,$nukiId,$alive) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $host = $hash->{HOST}; my $host = $hash->{HOST};
my $port = $hash->{PORT}; my $port = $hash->{PORT};
my $token = $hash->{TOKEN}; my $token = $hash->{TOKEN};
$alive = 0 if( !defined($alive) );
my $uri = "http://" . $hash->{HOST} . ":" . $port; my $uri = "http://" . $hash->{HOST} . ":" . $port;
$uri .= "/" . $path if( defined $path); $uri .= "/" . $path if( defined $path);
$uri .= "?token=" . $token if( defined($token) ); $uri .= "?token=" . $token if( defined($token) );
$uri .= "&action=" . $lockActions{$lockAction} if( defined($lockAction) ); $uri .= "&action=" . $lockActions{$lockAction} if( defined($lockAction) );
$uri .= "&nukiId=" . $nukiId if( $path ne "list" and defined($nukiId) ); $uri .= "&nukiId=" . $nukiId if( defined($nukiId) );
$method = 'GET' if( !$method );
HttpUtils_NonblockingGet( HttpUtils_NonblockingGet(
{ {
url => $uri, url => $uri,
timeout => 10, timeout => 10,
hash => $hash, hash => $hash,
chash => $chash, chash => $chash,
method => $method, endpoint => $path,
header => "Content-Type: application/json", alive => $alive,
doTrigger => 1, method => "GET",
noshutdown => 1, doTrigger => 1,
callback => \&NUKIBridge_dispatch, noshutdown => 1,
callback => \&NUKIBridge_Dispatch,
} }
); );
Log3 $name, 3, "NUKIBridge ($name) - Send HTTP POST with URL $uri"; Log3 $name, 3, "NUKIBridge ($name) - Send HTTP POST with URL $uri";
#readingsSingleUpdate( $hash, "state", $state, 1 ); #return undef; # beim Aufruf aus dem logischen Modul kam immer erst ein Fehler, deshalb auskommentiert
return undef;
} }
sub NUKIBridge_dispatch($$$) { sub NUKIBridge_Dispatch($$$) {
my ( $param, $err, $json ) = @_; my ( $param, $err, $json ) = @_;
my $hash = $param->{hash}; my $hash = $param->{hash};
@ -291,44 +300,73 @@ sub NUKIBridge_dispatch($$$) {
my $host = $hash->{HOST}; my $host = $hash->{HOST};
readingsBeginUpdate($hash);
if( defined( $err ) ) { if( defined( $err ) ) {
if( $err ne "" ) {
Log3 $name, 3, "NUKIBridge ($name) - error while requesting: $err"; if( $err ne "" and $param->{endpoint} eq "list" and $param->{alive} eq 1 ) {
return; readingsBulkUpdate( $hash, "state", "not connected");
readingsBulkUpdate( $hash, "lastError", $err );
Log3 $name, 3, "NUKIBridge ($name) - Bridge ist offline";
readingsEndUpdate( $hash, 1 );
return;
}
elsif ( $err ne "" ) {
readingsBulkUpdate( $hash, "lastError", $err );
Log3 $name, 3, "NUKIBridge ($name) - error while requesting: $err";
readingsEndUpdate( $hash, 1 );
return $err;
} }
} }
if( $json eq "" and exists( $param->{code} ) && $param->{code} ne 200 ) { if( $json eq "" and exists( $param->{code} ) && $param->{code} ne 200 ) {
Log3 $name, 3, "NUKIBridge ($name) - statusRequestERROR: received http code ".$param->{code}." without any data after requesting"; readingsBulkUpdate( $hash, "lastError", "Internal error, " .$param->{code} );
Log3 $name, 3, "NUKIBridge ($name) - received http code " .$param->{code}." without any data after requesting";
return; readingsEndUpdate( $hash, 1 );
return "received http code ".$param->{code}." without any data after requesting";
} }
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", "nukiId is not known" ) if( $param->{code} eq 404 );
readingsBulkUpdate( $hash, "lastError", "action is undefined" ) if( $param->{code} eq 400 );
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 );
######### Zum testen da ich kein Nuki Smartkey 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"; # Log3 $name, 3, "NUKIBridge ($name) - Test JSON String";
# $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 );
return $param->{code};
}
return; if( $param->{code} eq 200 and $param->{endpoint} eq "list" and $param->{alive} eq 1 ) {
readingsBulkUpdate( $hash, "state", "connected" );
Log3 $name, 3, "NUKIBridge ($name) - Bridge ist online";
readingsEndUpdate( $hash, 1 );
return;
} }
if( $hash == $param->{chash} ) { if( $hash == $param->{chash} ) {
#$json = '[{"nukiId": 1, "name": "Home"}, {"nukiId": 2, "name": "Grandma"}]'; # zum testen da ich kein Nuki Smartkey habe #$json = '[{"nukiId": 1, "name": "Home"}, {"nukiId": 2, "name": "Grandma"}]'; # zum testen da ich kein Nuki Smartlock habe
NUKIBridge_ResponseProcessing($hash,$json); NUKIBridge_ResponseProcessing($hash,$json);
@ -336,6 +374,9 @@ sub NUKIBridge_dispatch($$$) {
NUKIDevice_Parse($param->{chash},$json); NUKIDevice_Parse($param->{chash},$json);
} }
readingsEndUpdate( $hash, 1 );
return undef;
} }
sub NUKIBridge_ResponseProcessing($$) { sub NUKIBridge_ResponseProcessing($$) {
@ -355,6 +396,7 @@ sub NUKIBridge_ResponseProcessing($$) {
return $json; return $json;
} }
return undef;
} }
sub NUKIBridge_Autocreate($$;$) { sub NUKIBridge_Autocreate($$;$) {

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.37"; my $version = "0.1.40";
@ -155,8 +155,8 @@ sub NUKIDevice_Attr(@) {
if( $cmd eq "set" ) { if( $cmd eq "set" ) {
if( $attrVal eq "0" ) { if( $attrVal eq "0" ) {
RemoveInternalTimer( $hash ); RemoveInternalTimer( $hash );
InternalTimer( gettimeofday()+2, "NUKIDevice_GetUpdate", $hash, 0 ) if( ReadingsVal( $hash->{NAME}, "state", 0 ) eq "disabled" ); InternalTimer( gettimeofday()+2, "NUKIDevice_GetUpdateInternalTimer", $hash, 0 );
readingsSingleUpdate ( $hash, "state", "initialized", 1 ); readingsSingleUpdate ( $hash, "state", "Initialized", 1 );
Log3 $name, 3, "NUKIDevice ($name) - enabled"; Log3 $name, 3, "NUKIDevice ($name) - enabled";
} else { } else {
readingsSingleUpdate ( $hash, "state", "disabled", 1 ); readingsSingleUpdate ( $hash, "state", "disabled", 1 );
@ -167,8 +167,8 @@ sub NUKIDevice_Attr(@) {
} else { } else {
RemoveInternalTimer( $hash ); RemoveInternalTimer( $hash );
InternalTimer( gettimeofday()+2, "NUKIDevice_GetUpdate", $hash, 0 ) if( ReadingsVal( $hash->{NAME}, "state", 0 ) eq "disabled" ); InternalTimer( gettimeofday()+2, "NUKIDevice_GetUpdateInternalTimer", $hash, 0 );
readingsSingleUpdate ( $hash, "state", "initialized", 1 ); readingsSingleUpdate ( $hash, "state", "Initialized", 1 );
Log3 $name, 3, "NUKIDevice ($name) - enabled"; Log3 $name, 3, "NUKIDevice ($name) - enabled";
} }
} }
@ -228,6 +228,7 @@ sub NUKIDevice_Set($$@) {
return "Unknown argument $cmd, choose one of $list"; return "Unknown argument $cmd, choose one of $list";
} }
my $result = NUKIDevice_ReadFromNUKIBridge($hash,"lockAction",$lockAction,$hash->{NUKIID} ); my $result = NUKIDevice_ReadFromNUKIBridge($hash,"lockAction",$lockAction,$hash->{NUKIID} );
if( !defined($result) ) { if( !defined($result) ) {
@ -249,21 +250,10 @@ sub NUKIDevice_GetUpdate($) {
my $name = $hash->{NAME}; my $name = $hash->{NAME};
Log3 $name, 3, "NUKIBridge ($name) - Call NUKIBridge_GetUpdate"; NUKIDevice_ReadFromNUKIBridge($hash, "lockState", undef, $hash->{NUKIID} );
Log3 $name, 3, "NUKIDevice ($name) - NUKIDevice_GetUpdate Call NUKIDevice_ReadFromNUKIBridge";
my $result = NUKIDevice_ReadFromNUKIBridge($hash, "lockState", undef, $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_GetUpdateInternalTimer($) { sub NUKIDevice_GetUpdateInternalTimer($) {