add actionQueury

This commit is contained in:
Marko Oldenburg 2020-01-11 12:08:35 +01:00
parent 835e1cbcfc
commit 6bb4df32d1
2 changed files with 98 additions and 58 deletions

View File

@ -113,7 +113,7 @@ if ($@) {
} }
} }
my $version = '0.7.22'; my $version = '0.7.31';
my $bridgeapi = '1.9'; my $bridgeapi = '1.9';
my %bridgeType = ( my %bridgeType = (
@ -150,6 +150,7 @@ sub NUKIBridge_Get($@);
sub NUKIBridge_GetCheckBridgeAlive($); sub NUKIBridge_GetCheckBridgeAlive($);
sub NUKIBridge_firstRun($); sub NUKIBridge_firstRun($);
sub NUKIBridge_Write($@); sub NUKIBridge_Write($@);
sub NUKIBridge_Call($);
sub NUKIBridge_Distribution($$$); sub NUKIBridge_Distribution($$$);
sub NUKIBridge_ResponseProcessing($$$); sub NUKIBridge_ResponseProcessing($$$);
sub NUKIBridge_CGI(); sub NUKIBridge_CGI();
@ -202,13 +203,13 @@ sub NUKIBridge_Define($$) {
my $token = $a[3]; my $token = $a[3];
my $port = 8080; my $port = 8080;
$hash->{HOST} = $host; $hash->{HOST} = $host;
$hash->{PORT} = $port; $hash->{PORT} = $port;
$hash->{TOKEN} = $token; $hash->{TOKEN} = $token;
$hash->{VERSION} = $version; $hash->{VERSION} = $version;
$hash->{BRIDGEAPI} = $bridgeapi; $hash->{BRIDGEAPI} = $bridgeapi;
$hash->{helper}->{aliveCount} = 0; $hash->{helper}->{aliveCount} = 0;
$hash->{helper}->{iowrite} = 0; $hash->{helper}->{actionQueue} = [];
my $infix = 'NUKIBridge'; my $infix = 'NUKIBridge';
Log3( $name, 3, Log3( $name, 3,
@ -504,9 +505,7 @@ sub NUKIBridge_GetCheckBridgeAlive($) {
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
Log3( $name, 4, "NUKIBridge ($name) - NUKIBridge_GetCheckBridgeAlive" ); Log3( $name, 4, "NUKIBridge ($name) - NUKIBridge_GetCheckBridgeAlive" );
if ( !IsDisabled($name) if ( !IsDisabled($name) ) {
and $hash->{helper}->{iowrite} == 0 )
{
NUKIBridge_Write( $hash, 'info', undef, undef, undef ); NUKIBridge_Write( $hash, 'info', undef, undef, undef );
@ -536,42 +535,77 @@ sub NUKIBridge_firstRun($) {
} }
sub NUKIBridge_Write($@) { sub NUKIBridge_Write($@) {
my ( $hash, $path, $obj, $nukiId, $deviceType ) = @_; my ( $hash, $endpoint, $param, $nukiId, $deviceType ) = @_;
my $name = $hash->{NAME}; my $obj = {
my $host = $hash->{HOST}; endpoint => $endpoint,
my $port = $hash->{PORT}; param => $param,
my $token = $hash->{TOKEN}; nukiId => $nukiId,
deviceType => $deviceType
};
my $uri = 'http://' . $hash->{HOST} . ':' . $port; $hash->{helper}->{lastDeviceAction} = $obj
$uri .= '/' . $path if ( defined $path ); if ( defined($param)
and $param );
unshift( @{ $hash->{helper}->{actionQueue} }, $obj );
NUKIBridge_Call($hash);
}
sub NUKIBridge_CreateUri($$) {
my ( $hash, $obj ) = @_;
my $host = $hash->{HOST};
my $port = $hash->{PORT};
my $token = $hash->{TOKEN};
my $endpoint = $obj->{endpoint};
my $param = $obj->{param};
my $nukiId = $obj->{nukiId};
my $deviceType = $obj->{deviceType};
my $uri = 'http://' . $host . ':' . $port;
$uri .= '/' . $endpoint if ( defined $endpoint );
$uri .= '?token=' . $token if ( defined($token) ); $uri .= '?token=' . $token if ( defined($token) );
$uri .= '&action=' . $lockActionsSmartLock{$obj} $uri .= '&action=' . $lockActionsSmartLock{$param}
if ( defined($obj) if ( defined($param)
and $path ne 'callback/add' and $param ne 'callback/add'
and $deviceType == 0 ); and $deviceType == 0 );
$uri .= '&action=' . $lockActionsOpener{$obj} $uri .= '&action=' . $lockActionsOpener{$param}
if ( defined($obj) if ( defined($param)
and $path ne 'callback/add' and $param ne 'callback/add'
and $deviceType == 2 ); and $deviceType == 2 );
$uri .= '&url=' . $obj $uri .= '&url=' . $param
if ( defined($obj) if ( defined($param)
and $path eq 'callback/add' ); and $param eq 'callback/add' );
$uri .= '&nukiId=' . $nukiId $uri .= '&nukiId=' . $nukiId
if ( defined($nukiId) ); if ( defined($nukiId) );
$uri .= '&deviceType=' . $deviceType $uri .= '&deviceType=' . $deviceType
if ( defined($deviceType) ); if ( defined($deviceType) );
return $uri;
}
sub NUKIBridge_Call($) {
my $hash = shift;
my $name = $hash->{NAME};
my $obj = pop( @{ $hash->{helper}->{actionQueue} } );
my $endpoint = $obj->{endpoint};
my $nukiId = $obj->{nukiId};
my $uri = NUKIBridge_CreateUri( $hash, $obj );
HttpUtils_NonblockingGet( HttpUtils_NonblockingGet(
{ {
url => $uri, url => $uri,
timeout => 60, timeout => 60,
hash => $hash, hash => $hash,
nukiId => $nukiId, nukiId => $nukiId,
endpoint => $path, endpoint => $endpoint,
header => 'Accept: application/json', header => 'Accept: application/json',
method => 'GET', method => 'GET',
callback => \&NUKIBridge_Distribution, callback => \&NUKIBridge_Distribution,
@ -632,6 +666,18 @@ sub NUKIBridge_Distribution($$$) {
"NUKIBridge ($name) - Response from Bridge: $param->{code}, $json" "NUKIBridge ($name) - Response from Bridge: $param->{code}, $json"
); );
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
if ( defined( $hash->{helper}->{lastDeviceAction} )
and $hash->{helper}->{lastDeviceAction} )
{
push(
@{ $hash->{helper}->{actionQueue} },
$hash->{helper}->{lastDeviceAction}
);
delete $hash->{helper}->{lastDeviceAction};
InternalTimer( gettimeofday() + 1, 'NUKIBridge_Call', $hash );
}
return; return;
} }
@ -666,6 +712,10 @@ sub NUKIBridge_Distribution($$$) {
return $param->{code}; return $param->{code};
} }
delete $hash->{helper}->{lastDeviceAction}
if ( defined( $hash->{helper}->{lastDeviceAction} )
and $hash->{helper}->{lastDeviceAction} );
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
if ( $hash == $dhash ) { if ( $hash == $dhash ) {
@ -684,14 +734,15 @@ sub NUKIBridge_Distribution($$$) {
Dispatch( $hash, $json, undef ); Dispatch( $hash, $json, undef );
} }
$hash->{helper}->{iowrite} = 0 InternalTimer( gettimeofday() + 1, 'NUKIBridge_Call', $hash )
if ( $hash->{helper}->{iowrite} == 1 ); if ( defined( $hash->{helper}->{actionQueue} )
and scalar( @{ $hash->{helper}->{actionQueue} } ) > 0 );
return undef; return undef;
} }
sub NUKIBridge_ResponseProcessing($$$) { sub NUKIBridge_ResponseProcessing($$$) {
my ( $hash, $json, $path ) = @_; my ( $hash, $json, $endpoint ) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $decode_json; my $decode_json;
@ -715,25 +766,25 @@ sub NUKIBridge_ResponseProcessing($$$) {
return; return;
} }
if ( $path eq 'list' if ( $endpoint eq 'list'
or $path eq 'info' ) or $endpoint eq 'info' )
{ {
if ( if (
( (
ref($decode_json) eq 'ARRAY' ref($decode_json) eq 'ARRAY'
and scalar( @{$decode_json} ) > 0 and scalar( @{$decode_json} ) > 0
and $path eq 'list' and $endpoint eq 'list'
) )
or ( ref( $decode_json->{scanResults} ) eq 'ARRAY' or ( ref( $decode_json->{scanResults} ) eq 'ARRAY'
and scalar( @{ $decode_json->{scanResults} } ) > 0 and scalar( @{ $decode_json->{scanResults} } ) > 0
and $path eq 'info' ) and $endpoint eq 'info' )
) )
{ {
my @buffer; my @buffer;
@buffer = split( '\[', $json ) @buffer = split( '\[', $json )
if ( $path eq 'list' ); if ( $endpoint eq 'list' );
@buffer = split( '"scanResults": \[', $json ) @buffer = split( '"scanResults": \[', $json )
if ( $path eq 'info' ); if ( $endpoint eq 'info' );
my ( $json, $tail ) = NUKIBridge_ParseJSON( $hash, $buffer[1] ); my ( $json, $tail ) = NUKIBridge_ParseJSON( $hash, $buffer[1] );
@ -768,7 +819,7 @@ sub NUKIBridge_ResponseProcessing($$$) {
} }
NUKIBridge_InfoProcessing( $hash, $decode_json ) NUKIBridge_InfoProcessing( $hash, $decode_json )
if ( $path eq 'info' ); if ( $endpoint eq 'info' );
readingsSingleUpdate( $hash, 'state', 'connected', 1 ); readingsSingleUpdate( $hash, 'state', 'connected', 1 );
Log3( $name, 5, "NUKIBridge ($name) - Bridge ist online" ); Log3( $name, 5, "NUKIBridge ($name) - Bridge ist online" );
@ -965,7 +1016,7 @@ processed"
} }
sub NUKIBridge_CallBlocking($@) { sub NUKIBridge_CallBlocking($@) {
my ( $hash, $path, $obj ) = @_; my ( $hash, $endpoint, $obj ) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $host = $hash->{HOST}; my $host = $hash->{HOST};
@ -973,8 +1024,8 @@ sub NUKIBridge_CallBlocking($@) {
my $token = $hash->{TOKEN}; my $token = $hash->{TOKEN};
my $url = 'http://' . $hash->{HOST} . ':' . $port; my $url = 'http://' . $hash->{HOST} . ':' . $port;
$url .= '/' . $path $url .= '/' . $endpoint
if ( defined $path ); if ( defined $endpoint );
$url .= '?token=' . $token $url .= '?token=' . $token
if ( defined($token) ); if ( defined($token) );
$url .= '&' . $obj $url .= '&' . $obj
@ -997,7 +1048,7 @@ sub NUKIBridge_CallBlocking($@) {
Log3( $name, 4, "NUKIDevice ($name) - empty answer received for $url" ); Log3( $name, 4, "NUKIDevice ($name) - empty answer received for $url" );
return undef; return undef;
} }
elsif ( $data !~ m/^[\[{].*[}\]]$/ and $path ne "log" ) { elsif ( $data !~ m/^[\[{].*[}\]]$/ and $endpoint ne "log" ) {
Log3( $name, 3, Log3( $name, 3,
"NUKIDevice ($name) - invalid json detected for $url: $data" ); "NUKIDevice ($name) - invalid json detected for $url: $data" );
return ("NUKIDevice ($name) - invalid json detected for $url: $data"); return ("NUKIDevice ($name) - invalid json detected for $url: $data");

View File

@ -329,18 +329,6 @@ sub NUKIDevice_Set($$@) {
NUKIDevice_GetUpdate($hash); NUKIDevice_GetUpdate($hash);
return undef; return undef;
}
elsif ( $cmd eq 'unpair' ) {
return ('usage: unpair') if ( @args != 0 );
if ( !IsDisabled($name) ) {
$hash->{IODev}->{helper}->{iowrite} = 1
if ( $hash->{IODev}->{helper}->{iowrite} == 0 );
IOWrite( $hash, $cmd, undef, $hash->{NUKIID}, $hash->{DEVICETYPE} );
}
return undef;
} }
elsif ($cmd eq 'lock' elsif ($cmd eq 'lock'
or lc($cmd) eq 'deactivaterto' or lc($cmd) eq 'deactivaterto'
@ -351,7 +339,8 @@ sub NUKIDevice_Set($$@) {
or lc($cmd) eq 'lockngo' or lc($cmd) eq 'lockngo'
or lc($cmd) eq 'activatecontinuousmode' or lc($cmd) eq 'activatecontinuousmode'
or lc($cmd) eq 'lockngowithunlatch' or lc($cmd) eq 'lockngowithunlatch'
or lc($cmd) eq 'deactivatecontinuousmode' ) or lc($cmd) eq 'deactivatecontinuousmode'
or $cmd eq 'unpair' )
{ {
return ( 'usage: ' . $cmd ) return ( 'usage: ' . $cmd )
if ( @args != 0 ); if ( @args != 0 );
@ -371,8 +360,7 @@ sub NUKIDevice_Set($$@) {
} }
$hash->{helper}{lockAction} = $lockAction; $hash->{helper}{lockAction} = $lockAction;
$hash->{IODev}->{helper}->{iowrite} = 1
if ( $hash->{IODev}->{helper}->{iowrite} == 0 );
IOWrite( $hash, "lockAction", $lockAction, $hash->{NUKIID}, IOWrite( $hash, "lockAction", $lockAction, $hash->{NUKIID},
$hash->{DEVICETYPE} ); $hash->{DEVICETYPE} );
@ -529,7 +517,8 @@ sub NUKIDevice_WriteReadings($$) {
or $t eq 'paired' or $t eq 'paired'
or $t eq 'batteryCritical' or $t eq 'batteryCritical'
or $t eq 'timestamp' ); or $t eq 'timestamp' );
readingsBulkUpdate( $hash, $t, ($v =~ m/^[0-9]$/ ? $lockStates{$v}{ $hash->{DEVICETYPE} } : $v) ) readingsBulkUpdate( $hash, $t,
( $v =~ m/^[0-9]$/ ? $lockStates{$v}{ $hash->{DEVICETYPE} } : $v ) )
if ( $t eq 'state' ); if ( $t eq 'state' );
readingsBulkUpdate( $hash, $t, $modes{$v}{ $hash->{DEVICETYPE} } ) readingsBulkUpdate( $hash, $t, $modes{$v}{ $hash->{DEVICETYPE} } )
if ( $t eq 'mode' ); if ( $t eq 'mode' );