fix getDeviceList bug

This commit is contained in:
Marko Oldenburg 2020-01-09 22:02:41 +01:00
parent 6f71931524
commit 2b9989d491
2 changed files with 634 additions and 538 deletions

View File

@ -34,10 +34,8 @@
## ##
# #
################################ ################################
package main; package main;
use strict; use strict;
@ -115,11 +113,9 @@ if ($@) {
} }
} }
my $version = '0.7.15';
my $version = '0.7.11';
my $bridgeapi = '1.9'; my $bridgeapi = '1.9';
my %lockActionsSmartLock = ( my %lockActionsSmartLock = (
'unlock' => 1, 'unlock' => 1,
'lock' => 2, 'lock' => 2,
@ -136,7 +132,6 @@ my %lockActionsOpener = (
'deactivateContinuousMode' => 5 'deactivateContinuousMode' => 5
); );
# Declare functions # Declare functions
sub NUKIBridge_Initialize ($); sub NUKIBridge_Initialize ($);
sub NUKIBridge_Define ($$); sub NUKIBridge_Define ($$);
@ -159,8 +154,6 @@ sub NUKIBridge_getLogfile($);
sub NUKIBridge_getCallbackList($); sub NUKIBridge_getCallbackList($);
sub NUKIBridge_CallBlocking($@); sub NUKIBridge_CallBlocking($@);
sub NUKIBridge_Initialize($) { sub NUKIBridge_Initialize($) {
my ($hash) = @_; my ($hash) = @_;
@ -169,7 +162,8 @@ sub NUKIBridge_Initialize($) {
$hash->{Clients} = ':NUKIDevice:'; $hash->{Clients} = ':NUKIDevice:';
$hash->{MatchList} = { '1:NUKIDevice' => '^{.*}$' }; $hash->{MatchList} = { '1:NUKIDevice' => '^{.*}$' };
my $webhookFWinstance = join( ',', devspec2array('TYPE=FHEMWEB:FILTER=TEMPORARY!=1') ); my $webhookFWinstance =
join( ',', devspec2array('TYPE=FHEMWEB:FILTER=TEMPORARY!=1') );
# Consumer # Consumer
$hash->{SetFn} = 'NUKIBridge_Set'; $hash->{SetFn} = 'NUKIBridge_Set';
@ -177,10 +171,12 @@ sub NUKIBridge_Initialize($) {
$hash->{DefFn} = 'NUKIBridge_Define'; $hash->{DefFn} = 'NUKIBridge_Define';
$hash->{UndefFn} = 'NUKIBridge_Undef'; $hash->{UndefFn} = 'NUKIBridge_Undef';
$hash->{AttrFn} = 'NUKIBridge_Attr'; $hash->{AttrFn} = 'NUKIBridge_Attr';
$hash->{AttrList} = 'disable:1 '. $hash->{AttrList} =
'webhookFWinstance:' . $webhookFWinstance . ' '. 'disable:1 '
'webhookHttpHostname '. . 'webhookFWinstance:'
$readingFnAttributes; . $webhookFWinstance . ' '
. 'webhookHttpHostname '
. $readingFnAttributes;
foreach my $d ( sort keys %{ $modules{NUKIBridge}{defptr} } ) { foreach my $d ( sort keys %{ $modules{NUKIBridge}{defptr} } ) {
my $hash = $modules{NUKIBridge}{defptr}{$d}; my $hash = $modules{NUKIBridge}{defptr}{$d};
@ -196,7 +192,6 @@ sub NUKIBridge_Define($$) {
return ('too few parameters: define <name> NUKIBridge <HOST> <TOKEN>') return ('too few parameters: define <name> NUKIBridge <HOST> <TOKEN>')
if ( @a != 4 ); if ( @a != 4 );
my $name = $a[0]; my $name = $a[0];
my $host = $a[2]; my $host = $a[2];
my $token = $a[3]; my $token = $a[3];
@ -211,12 +206,19 @@ sub NUKIBridge_Define($$) {
$hash->{helper}->{iowrite} = 0; $hash->{helper}->{iowrite} = 0;
my $infix = 'NUKIBridge'; my $infix = 'NUKIBridge';
Log3($name, 3, "NUKIBridge ($name) - defined with host $host on port $port, Token $token"); Log3( $name, 3,
"NUKIBridge ($name) - defined with host $host on port $port, Token $token"
);
CommandAttr( undef, $name . ' room NUKI' ) CommandAttr( undef, $name . ' room NUKI' )
if ( AttrVal( $name, 'room', 'none' ) eq 'none' ); if ( AttrVal( $name, 'room', 'none' ) eq 'none' );
if ( NUKIBridge_addExtension( $name, 'NUKIBridge_CGI', $infix . "-" . $host ) ) { if (
NUKIBridge_addExtension(
$name, 'NUKIBridge_CGI', $infix . "-" . $host
)
)
{
$hash->{fhem}{infix} = $infix; $hash->{fhem}{infix} = $infix;
} }
@ -227,9 +229,12 @@ sub NUKIBridge_Define($$) {
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
if ($init_done) { if ($init_done) {
NUKIBridge_firstRun($hash) if ( ($hash->{HOST}) and ($hash->{TOKEN}) ); NUKIBridge_firstRun($hash)
} else { if ( ( $hash->{HOST} ) and ( $hash->{TOKEN} ) );
InternalTimer( gettimeofday()+15, 'NUKIBridge_firstRun', $hash) if ( ($hash->{HOST}) and ($hash->{TOKEN}) ); }
else {
InternalTimer( gettimeofday() + 15, 'NUKIBridge_firstRun', $hash )
if ( ( $hash->{HOST} ) and ( $hash->{TOKEN} ) );
} }
$modules{NUKIBridge}{defptr}{ $hash->{HOST} } = $hash; $modules{NUKIBridge}{defptr}{ $hash->{HOST} } = $hash;
@ -272,43 +277,76 @@ sub NUKIBridge_Attr(@) {
if ( $attrName eq 'disabledForIntervals' ) { if ( $attrName eq 'disabledForIntervals' ) {
if ( $cmd eq 'set' ) { if ( $cmd eq 'set' ) {
Log3($name, 3, "NUKIBridge ($name) - enable disabledForIntervals"); Log3( $name, 3,
"NUKIBridge ($name) - enable disabledForIntervals" );
readingsSingleUpdate( $hash, 'state', 'Unknown', 1 ); readingsSingleUpdate( $hash, 'state', 'Unknown', 1 );
} }
elsif ( $cmd eq 'del' ) { elsif ( $cmd eq 'del' ) {
readingsSingleUpdate( $hash, 'state', 'active', 1 ); readingsSingleUpdate( $hash, 'state', 'active', 1 );
Log3($name, 3, "NUKIBridge ($name) - delete disabledForIntervals"); Log3( $name, 3,
"NUKIBridge ($name) - delete disabledForIntervals" );
} }
} }
###################### ######################
#### webhook ######### #### webhook #########
return("Invalid value for attribute $attrName: can only by FQDN or IPv4 or IPv6 address") return (
if ( $attrVal and $attrName eq 'webhookHttpHostname' and $attrVal !~ /^([A-Za-z_.0-9]+\.[A-Za-z_.0-9]+)|[0-9:]+$/ ); "Invalid value for attribute $attrName: can only by FQDN or IPv4 or IPv6 address"
)
if ( $attrVal
and $attrName eq 'webhookHttpHostname'
and $attrVal !~ /^([A-Za-z_.0-9]+\.[A-Za-z_.0-9]+)|[0-9:]+$/ );
return("Invalid value for attribute $attrName: FHEMWEB instance $attrVal not existing") return (
if ( $attrVal and $attrName eq 'webhookFWinstance' and ( !defined( $defs{$attrVal} ) or $defs{$attrVal}{TYPE} ne 'FHEMWEB' ) ); "Invalid value for attribute $attrName: FHEMWEB instance $attrVal not existing"
)
if ( $attrVal
and $attrName eq 'webhookFWinstance'
and
( !defined( $defs{$attrVal} ) or $defs{$attrVal}{TYPE} ne 'FHEMWEB' ) );
return("Invalid value for attribute $attrName: needs to be an integer value") return (
"Invalid value for attribute $attrName: needs to be an integer value")
if ( $attrVal and $attrName eq 'webhookPort' and $attrVal !~ /^\d+$/ ); if ( $attrVal and $attrName eq 'webhookPort' and $attrVal !~ /^\d+$/ );
if ( $attrName =~ /^webhook.*/ ) { if ( $attrName =~ /^webhook.*/ ) {
my $webhookHttpHostname = ( $attrName eq 'webhookHttpHostname' ? $attrVal : AttrVal( $name, 'webhookHttpHostname', '' ) ); my $webhookHttpHostname =
my $webhookFWinstance = ( $attrName eq 'webhookFWinstance' ? $attrVal : AttrVal( $name, 'webhookFWinstance', '' ) ); ( $attrName eq 'webhookHttpHostname'
? $attrVal
: AttrVal( $name, 'webhookHttpHostname', '' ) );
my $webhookFWinstance =
( $attrName eq 'webhookFWinstance'
? $attrVal
: AttrVal( $name, 'webhookFWinstance', '' ) );
$hash->{WEBHOOK_URI} = '/' . AttrVal( $webhookFWinstance, 'webname', 'fhem' ) . '/NUKIBridge' . '-' . $hash->{HOST}; $hash->{WEBHOOK_URI} = '/'
$hash->{WEBHOOK_PORT} = ( $attrName eq 'webhookPort' ? $attrVal : AttrVal( $name, 'webhookPort', InternalVal( $webhookFWinstance, 'PORT', '' )) ); . AttrVal( $webhookFWinstance, 'webname', 'fhem' )
. '/NUKIBridge' . '-'
. $hash->{HOST};
$hash->{WEBHOOK_PORT} = (
$attrName eq 'webhookPort' ? $attrVal : AttrVal(
$name, 'webhookPort',
InternalVal( $webhookFWinstance, 'PORT', '' )
)
);
$hash->{WEBHOOK_URL} = ''; $hash->{WEBHOOK_URL} = '';
$hash->{WEBHOOK_COUNTER} = 0; $hash->{WEBHOOK_COUNTER} = 0;
if ( $webhookHttpHostname ne '' and $hash->{WEBHOOK_PORT} ne '' ) { if ( $webhookHttpHostname ne '' and $hash->{WEBHOOK_PORT} ne '' ) {
$hash->{WEBHOOK_URL} = 'http://' . $webhookHttpHostname . ':' . $hash->{WEBHOOK_PORT} . $hash->{WEBHOOK_URI}; $hash->{WEBHOOK_URL} =
my $url = 'http://' . $webhookHttpHostname . ':' . $hash->{WEBHOOK_PORT} . $hash->{WEBHOOK_URI}; 'http://'
. $webhookHttpHostname . ':'
. $hash->{WEBHOOK_PORT}
. $hash->{WEBHOOK_URI};
my $url =
'http://'
. $webhookHttpHostname . ':'
. $hash->{WEBHOOK_PORT}
. $hash->{WEBHOOK_URI};
Log3( $name, 3, "NUKIBridge ($name) - URL ist: $url" ); Log3( $name, 3, "NUKIBridge ($name) - URL ist: $url" );
NUKIBridge_Write( $hash, 'callback/add', $url, undef, undef ) NUKIBridge_Write( $hash, 'callback/add', $url, undef, undef )
@ -328,7 +366,9 @@ sub NUKIBridge_addExtension($$$) {
my $url = '/' . $link; my $url = '/' . $link;
Log3($name, 2, "NUKIBridge ($name) - Registering NUKIBridge for webhook URI $url ..."); Log3( $name, 2,
"NUKIBridge ($name) - Registering NUKIBridge for webhook URI $url ..."
);
$data{FWEXT}{$url}{deviceName} = $name; $data{FWEXT}{$url}{deviceName} = $name;
$data{FWEXT}{$url}{FUNC} = $func; $data{FWEXT}{$url}{FUNC} = $func;
@ -343,7 +383,9 @@ sub NUKIBridge_removeExtension($) {
my $url = '/' . $link; my $url = '/' . $link;
my $name = $data{FWEXT}{$url}{deviceName}; my $name = $data{FWEXT}{$url}{deviceName};
Log3($name, 2, "NUKIBridge ($name) - Unregistering NUKIBridge for webhook URL $url..."); Log3( $name, 2,
"NUKIBridge ($name) - Unregistering NUKIBridge for webhook URL $url..."
);
delete $data{FWEXT}{$url}; delete $data{FWEXT}{$url};
} }
@ -352,39 +394,45 @@ sub NUKIBridge_Set($@) {
my ( $arg, @params ) = @args; my ( $arg, @params ) = @args;
if ($cmd eq 'getdevicelist') { if ( lc($cmd) eq 'getdevicelist' ) {
return 'usage: getDeviceList' if ( @args != 0 ); return 'usage: getDeviceList' if ( @args != 0 );
NUKIBridge_Write($hash,"list",undef,undef,undef) if ( !IsDisabled($name) ); NUKIBridge_Write( $hash, "list", undef, undef, undef )
if ( !IsDisabled($name) );
return undef; return undef;
} }
elsif ( $cmd eq 'info' ) { elsif ( $cmd eq 'info' ) {
return 'usage: statusRequest' if ( @args != 0 ); return 'usage: statusRequest' if ( @args != 0 );
NUKIBridge_Write($hash,"info",undef,undef,undef) if ( !IsDisabled($name) ); NUKIBridge_Write( $hash, "info", undef, undef, undef )
if ( !IsDisabled($name) );
return undef; return undef;
} }
elsif ( lc($cmd) eq 'fwupdate' ) { elsif ( lc($cmd) eq 'fwupdate' ) {
return 'usage: fwUpdate' if ( @args != 0 ); return 'usage: fwUpdate' if ( @args != 0 );
NUKIBridge_CallBlocking($hash,"fwupdate",undef) if ( !IsDisabled($name) ); NUKIBridge_CallBlocking( $hash, "fwupdate", undef )
if ( !IsDisabled($name) );
return undef; return undef;
} }
elsif ( $cmd eq 'reboot' ) { elsif ( $cmd eq 'reboot' ) {
return 'usage: reboot' if ( @args != 0 ); return 'usage: reboot' if ( @args != 0 );
NUKIBridge_CallBlocking($hash,"reboot",undef) if ( !IsDisabled($name) ); NUKIBridge_CallBlocking( $hash, "reboot", undef )
if ( !IsDisabled($name) );
return undef; return undef;
} }
elsif ( lc($cmd) eq 'clearlog' ) { elsif ( lc($cmd) eq 'clearlog' ) {
return 'usage: clearLog' if ( @args != 0 ); return 'usage: clearLog' if ( @args != 0 );
NUKIBridge_CallBlocking($hash,"clearlog",undef) if ( !IsDisabled($name) ); NUKIBridge_CallBlocking( $hash, "clearlog", undef )
if ( !IsDisabled($name) );
} }
elsif ( lc($cmd) eq 'factoryreset' ) { elsif ( lc($cmd) eq 'factoryreset' ) {
return 'usage: clearLog' if ( @args != 0 ); return 'usage: clearLog' if ( @args != 0 );
NUKIBridge_CallBlocking($hash,"factoryReset",undef) if ( !IsDisabled($name) ); NUKIBridge_CallBlocking( $hash, "factoryReset", undef )
if ( !IsDisabled($name) );
} }
elsif ( lc($cmd) eq 'callbackremove' ) { elsif ( lc($cmd) eq 'callbackremove' ) {
return 'usage: callbackRemove' if ( @args > 1 ); return 'usage: callbackRemove' if ( @args > 1 );
@ -393,8 +441,11 @@ sub NUKIBridge_Set($@) {
my $resp = NUKIBridge_CallBlocking( $hash, 'callback/remove', $id ) my $resp = NUKIBridge_CallBlocking( $hash, 'callback/remove', $id )
if ( !IsDisabled($name) ); if ( !IsDisabled($name) );
if ( ( $resp->{success} eq 'true' if (
or $resp->{success} == 1) (
$resp->{success} eq 'true'
or $resp->{success} == 1
)
and !IsDisabled($name) and !IsDisabled($name)
) )
{ {
@ -455,9 +506,12 @@ sub NUKIBridge_GetCheckBridgeAlive($) {
Log3( $name, 4, "NUKIBridge ($name) - run NUKIBridge_Write" ); Log3( $name, 4, "NUKIBridge ($name) - run NUKIBridge_Write" );
} }
InternalTimer( gettimeofday()+15+int(rand(15)), 'NUKIBridge_GetCheckBridgeAlive', $hash); InternalTimer( gettimeofday() + 15 + int( rand(15) ),
'NUKIBridge_GetCheckBridgeAlive', $hash );
Log3($name, 4, "NUKIBridge ($name) - Call InternalTimer for NUKIBridge_GetCheckBridgeAlive"); Log3( $name, 4,
"NUKIBridge ($name) - Call InternalTimer for NUKIBridge_GetCheckBridgeAlive"
);
} }
sub NUKIBridge_firstRun($) { sub NUKIBridge_firstRun($) {
@ -466,8 +520,10 @@ sub NUKIBridge_firstRun($) {
my $name = $hash->{NAME}; my $name = $hash->{NAME};
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
NUKIBridge_Write($hash,'list',undef,undef,undef) if ( !IsDisabled($name) ); NUKIBridge_Write( $hash, 'list', undef, undef, undef )
InternalTimer( gettimeofday()+15, 'NUKIBridge_GetCheckBridgeAlive', $hash); if ( !IsDisabled($name) );
InternalTimer( gettimeofday() + 15,
'NUKIBridge_GetCheckBridgeAlive', $hash );
return undef; return undef;
} }
@ -550,26 +606,39 @@ sub NUKIBridge_Distribution($$$) {
} }
readingsBulkUpdate( $hash, 'lastError', $err ) readingsBulkUpdate( $hash, 'lastError', $err )
if ( ReadingsVal($name,'state','not connected') eq 'not connected' ); if ( ReadingsVal( $name, 'state', 'not connected' ) eq
'not connected' );
Log3($name, 4, "NUKIBridge ($name) - error while requesting: $err"); Log3( $name, 4,
"NUKIBridge ($name) - error while requesting: $err" );
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
return $err; return $err;
} }
} }
if ( ($json eq '' or $json =~ /Unavailable/i) and exists( $param->{code} ) and $param->{code} != 200 ) { if ( ( $json eq '' or $json =~ /Unavailable/i )
and exists( $param->{code} )
and $param->{code} != 200 )
{
if ( $param->{code} == 503 and $json eq 'HTTP 503 Unavailable' ) { if ( $param->{code} == 503 and $json eq 'HTTP 503 Unavailable' ) {
Log3($name, 4, "NUKIBridge ($name) - Response from Bridge: $param->{code}, $json"); Log3( $name, 4,
"NUKIBridge ($name) - Response from Bridge: $param->{code}, $json"
);
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
return; return;
} }
readingsBulkUpdate( $hash, 'lastError', 'Internal error, ' .$param->{code} ); readingsBulkUpdate( $hash, 'lastError',
Log3($name, 4, "NUKIBridge ($name) - received http code " .$param->{code}." without any data after requesting"); 'Internal error, ' . $param->{code} );
Log3( $name, 4,
"NUKIBridge ($name) - received http code "
. $param->{code}
. " without any data after requesting" );
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
return('received http code ' .$param->{code} . ' without any data after requesting'); 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} ) ) {
@ -598,7 +667,8 @@ sub NUKIBridge_Distribution($$$) {
else { else {
my $decode_json = eval { decode_json($json) }; my $decode_json = eval { decode_json($json) };
if ($@) { if ($@) {
Log3($name, 3, "NUKIBridge ($name) - JSON error while request: $@"); Log3( $name, 3,
"NUKIBridge ($name) - JSON error while request: $@" );
return; return;
} }
@ -622,10 +692,12 @@ sub NUKIBridge_ResponseProcessing($$$) {
if ( !$json ) { if ( !$json ) {
Log3( $name, 3, "NUKIBridge ($name) - empty answer received" ); Log3( $name, 3, "NUKIBridge ($name) - empty answer received" );
return undef; return undef;
} elsif ( $json =~ m'HTTP/1.1 200 OK' ) { }
elsif ( $json =~ m'HTTP/1.1 200 OK' ) {
Log3( $name, 4, "NUKIBridge ($name) - empty answer received" ); Log3( $name, 4, "NUKIBridge ($name) - empty answer received" );
return undef; return undef;
} elsif ( $json !~ m/^[\[{].*[}\]]$/ ) { }
elsif ( $json !~ m/^[\[{].*[}\]]$/ ) {
Log3( $name, 3, "NUKIBridge ($name) - invalid json detected: $json" ); Log3( $name, 3, "NUKIBridge ($name) - invalid json detected: $json" );
return ("NUKIBridge ($name) - invalid json detected: $json"); return ("NUKIBridge ($name) - invalid json detected: $json");
} }
@ -636,9 +708,15 @@ sub NUKIBridge_ResponseProcessing($$$) {
return; return;
} }
if ( (ref($decode_json) eq 'ARRAY' if ( $path eq 'list'
or $path eq 'info' )
{
if (
(
ref($decode_json) eq 'ARRAY'
and scalar( @{$decode_json} ) > 0 and scalar( @{$decode_json} ) > 0
and $path eq 'list') and $path 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 $path eq 'info' )
@ -653,7 +731,6 @@ sub NUKIBridge_ResponseProcessing($$$) {
my ( $json, $tail ) = NUKIBridge_ParseJSON( $hash, $buffer[1] ); my ( $json, $tail ) = NUKIBridge_ParseJSON( $hash, $buffer[1] );
while ($json) { while ($json) {
Log3( $name, 5, Log3( $name, 5,
"NUKIBridge ($name) - Decoding JSON message. Length: " "NUKIBridge ($name) - Decoding JSON message. Length: "
. length($json) . length($json)
@ -693,9 +770,12 @@ sub NUKIBridge_ResponseProcessing($$$) {
NUKIBridge_InfoProcessing( $hash, $decode_json ); NUKIBridge_InfoProcessing( $hash, $decode_json );
} }
}
else { else {
Log3($name, 5, "NUKIBridge ($name) - Rückgabe Path nicht korrekt: Log3(
$json"); $name, 5, "NUKIBridge ($name) - Rückgabe Path nicht korrekt:
$json"
);
return; return;
} }
@ -715,7 +795,6 @@ sub NUKIBridge_CGI() {
return ('NUKIBridge WEBHOOK - No IODev found') return ('NUKIBridge WEBHOOK - No IODev found')
unless ( defined($hash) and defined($name) ); unless ( defined($hash) and defined($name) );
my $json = ( split( '&', $request, 2 ) )[1]; my $json = ( split( '&', $request, 2 ) )[1];
if ( !$json ) { if ( !$json ) {
@ -727,18 +806,22 @@ sub NUKIBridge_CGI() {
return undef; return undef;
} }
elsif ( $json !~ m/^[\[{].*[}\]]$/ ) { elsif ( $json !~ m/^[\[{].*[}\]]$/ ) {
Log3($name, 3, "NUKIBridge WEBHOOK ($name) - invalid json detected: $json"); Log3( $name, 3,
"NUKIBridge WEBHOOK ($name) - invalid json detected: $json" );
return ("NUKIBridge WEBHOOK ($name) - invalid json detected: $json"); return ("NUKIBridge WEBHOOK ($name) - invalid json detected: $json");
} }
Log3($name, 5, "NUKIBridge WEBHOOK ($name) - Webhook received with JSON: $json"); Log3( $name, 5,
"NUKIBridge WEBHOOK ($name) - Webhook received with JSON: $json" );
if ( $json =~ m/^\{.*\}$/ ) { if ( $json =~ m/^\{.*\}$/ ) {
$hash->{WEBHOOK_COUNTER}++; $hash->{WEBHOOK_COUNTER}++;
$hash->{WEBHOOK_LAST} = TimeNow(); $hash->{WEBHOOK_LAST} = TimeNow();
Log3($name, 4, "NUKIBridge WEBHOOK ($name) - Received webhook for Log3(
matching NukiId at device $name"); $name, 4, "NUKIBridge WEBHOOK ($name) - Received webhook for
matching NukiId at device $name"
);
Dispatch( $hash, $json, undef ); Dispatch( $hash, $json, undef );
@ -747,7 +830,8 @@ matching NukiId at device $name");
# no data received # no data received
else { else {
Log3($name, 4, "NUKIBridge WEBHOOK - received malformed request\n$request"); Log3( $name, 4,
"NUKIBridge WEBHOOK - received malformed request\n$request" );
} }
return ( 'text/plain; charset=utf-8', 'Call failure: ' . $request ); return ( 'text/plain; charset=utf-8', 'Call failure: ' . $request );
@ -770,15 +854,20 @@ sub NUKIBridge_InfoProcessing($$) {
); );
readingsBeginUpdate($hash); readingsBeginUpdate($hash);
readingsBulkUpdate($hash,'appVersion',$decode_json->{versions}->{appVersion}); readingsBulkUpdate( $hash, 'appVersion',
readingsBulkUpdate($hash,'firmwareVersion',$decode_json->{versions}->{firmwareVersion}); $decode_json->{versions}->{appVersion} );
readingsBulkUpdate($hash,'wifiFirmwareVersion',$decode_json->{versions}->{wifiFirmwareVersion}); readingsBulkUpdate( $hash, 'firmwareVersion',
readingsBulkUpdate($hash,'bridgeType',$bridgeType{$decode_json->{bridgeType}}); $decode_json->{versions}->{firmwareVersion} );
readingsBulkUpdate( $hash, 'wifiFirmwareVersion',
$decode_json->{versions}->{wifiFirmwareVersion} );
readingsBulkUpdate( $hash, 'bridgeType',
$bridgeType{ $decode_json->{bridgeType} } );
readingsBulkUpdate( $hash, 'hardwareId', $decode_json->{ids}{hardwareId} ); readingsBulkUpdate( $hash, 'hardwareId', $decode_json->{ids}{hardwareId} );
readingsBulkUpdate( $hash, 'serverId', $decode_json->{ids}{serverId} ); readingsBulkUpdate( $hash, 'serverId', $decode_json->{ids}{serverId} );
readingsBulkUpdate( $hash, 'uptime', $decode_json->{uptime} ); readingsBulkUpdate( $hash, 'uptime', $decode_json->{uptime} );
readingsBulkUpdate( $hash, 'currentTime', $decode_json->{currentTime} ); readingsBulkUpdate( $hash, 'currentTime', $decode_json->{currentTime} );
readingsBulkUpdate($hash,'serverConnected',$decode_json->{serverConnected}); readingsBulkUpdate( $hash, 'serverConnected',
$decode_json->{serverConnected} );
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
} }
@ -788,7 +877,8 @@ sub NUKIBridge_getLogfile($) {
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $decode_json = NUKIBridge_CallBlocking( $hash, 'log', undef ); my $decode_json = NUKIBridge_CallBlocking( $hash, 'log', undef );
Log3($name, 4, "NUKIBridge ($name) - Log data are collected and processed"); Log3( $name, 4,
"NUKIBridge ($name) - Log data are collected and processed" );
if ( ref($decode_json) eq 'ARRAY' and scalar( @{$decode_json} ) > 0 ) { if ( ref($decode_json) eq 'ARRAY' and scalar( @{$decode_json} ) > 0 ) {
Log3( $name, 4, "NUKIBridge ($name) - created Table with log file" ); Log3( $name, 4, "NUKIBridge ($name) - created Table with log file" );
@ -839,11 +929,14 @@ sub NUKIBridge_getCallbackList($) {
return return
unless ( ref($decode_json) eq 'HASH' ); unless ( ref($decode_json) eq 'HASH' );
Log3($name, 4, "NUKIBridge ($name) - Callback data is collected and Log3(
processed"); $name, 4, "NUKIBridge ($name) - Callback data is collected and
processed"
);
if ( ref($decode_json->{callbacks}) eq 'ARRAY' and if ( ref( $decode_json->{callbacks} ) eq 'ARRAY'
scalar(@{$decode_json->{callbacks}}) > 0 ) { and scalar( @{ $decode_json->{callbacks} } ) > 0 )
{
Log3( $name, 4, "NUKIBridge ($name) - created Table with log file" ); Log3( $name, 4, "NUKIBridge ($name) - created Table with log file" );
my $ret = '<html><table width=100%><tr><td>'; my $ret = '<html><table width=100%><tr><td>';
@ -880,7 +973,6 @@ sub NUKIBridge_CallBlocking($@) {
my $port = $hash->{PORT}; my $port = $hash->{PORT};
my $token = $hash->{TOKEN}; my $token = $hash->{TOKEN};
my $url = 'http://' . $hash->{HOST} . ':' . $port; my $url = 'http://' . $hash->{HOST} . ':' . $port;
$url .= '/' . $path $url .= '/' . $path
if ( defined $path ); if ( defined $path );
@ -889,13 +981,14 @@ sub NUKIBridge_CallBlocking($@) {
$url .= '&' . $obj $url .= '&' . $obj
if ( defined($obj) ); if ( defined($obj) );
my($err,$data) = HttpUtils_BlockingGet({ my ( $err, $data ) = HttpUtils_BlockingGet(
{
url => $url, url => $url,
timeout => 3, timeout => 3,
method => "GET", method => "GET",
header => "Content-Type: application/json", header => "Content-Type: application/json",
}); }
);
if ( !$data ) { if ( !$data ) {
Log3( $name, 3, "NUKIDevice ($name) - empty answer received for $url" ); Log3( $name, 3, "NUKIDevice ($name) - empty answer received for $url" );
@ -906,7 +999,8 @@ sub NUKIBridge_CallBlocking($@) {
return undef; return undef;
} }
elsif ( $data !~ m/^[\[{].*[}\]]$/ and $path ne "log" ) { elsif ( $data !~ m/^[\[{].*[}\]]$/ and $path ne "log" ) {
Log3($name, 3, "NUKIDevice ($name) - invalid json detected for $url: $data"); Log3( $name, 3,
"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");
} }
@ -970,20 +1064,12 @@ sub NUKIBridge_ParseJSON($$) {
} }
} }
Log3($name, 5, Log3( $name, 5, "NUKIBridge ($name) - return msg: $msg and tail: $tail" );
"NUKIBridge ($name) - return msg: $msg and tail: $tail");
return ( $msg, $tail ); return ( $msg, $tail );
} }
1; 1;
=pod =pod
=item device =item device
=item summary Modul to control the Nuki Smartlock's over the Nuki Bridge. =item summary Modul to control the Nuki Smartlock's over the Nuki Bridge.

View File

@ -25,7 +25,6 @@
# #
############################################################################### ###############################################################################
package main; package main;
use strict; use strict;
@ -102,11 +101,7 @@ if ($@) {
} }
} }
my $version = '0.7.15';
my $version = '0.7.11';
# Declare functions # Declare functions
sub NUKIDevice_Initialize($); sub NUKIDevice_Initialize($);
@ -118,7 +113,6 @@ sub NUKIDevice_GetUpdate($);
sub NUKIDevice_Parse($$); sub NUKIDevice_Parse($$);
sub NUKIDevice_WriteReadings($$); sub NUKIDevice_WriteReadings($$);
my %deviceTypes = ( my %deviceTypes = (
0 => 'smartlock', 0 => 'smartlock',
2 => 'opener' 2 => 'opener'
@ -126,10 +120,6 @@ my %deviceTypes = (
my %deviceTypeIds = reverse(%deviceTypes); my %deviceTypeIds = reverse(%deviceTypes);
sub NUKIDevice_Initialize($) { sub NUKIDevice_Initialize($) {
my ($hash) = @_; my ($hash) = @_;
@ -141,11 +131,11 @@ sub NUKIDevice_Initialize($) {
$hash->{AttrFn} = 'NUKIDevice_Attr'; $hash->{AttrFn} = 'NUKIDevice_Attr';
$hash->{ParseFn} = 'NUKIDevice_Parse'; $hash->{ParseFn} = 'NUKIDevice_Parse';
$hash->{AttrList} = 'IODev '. $hash->{AttrList} =
'model:opener,smartlock '. 'IODev '
'disable:1 '. . 'model:opener,smartlock '
$readingFnAttributes; . 'disable:1 '
. $readingFnAttributes;
foreach my $d ( sort keys %{ $modules{NUKIDevice}{defptr} } ) { foreach my $d ( sort keys %{ $modules{NUKIDevice}{defptr} } ) {
my $hash = $modules{NUKIDevice}{defptr}{$d}; my $hash = $modules{NUKIDevice}{defptr}{$d};
@ -157,8 +147,8 @@ sub NUKIDevice_Define($$) {
my ( $hash, $def ) = @_; my ( $hash, $def ) = @_;
my @a = split( '[ \t][ \t]*', $def ); my @a = split( '[ \t][ \t]*', $def );
return 'too few parameters: define <name> NUKIDevice <nukiId> <deviceType>' if ( @a != 4 ); return 'too few parameters: define <name> NUKIDevice <nukiId> <deviceType>'
if ( @a != 4 );
my $name = $a[0]; my $name = $a[0];
my $nukiId = $a[2]; my $nukiId = $a[2];
@ -169,14 +159,13 @@ sub NUKIDevice_Define($$) {
$hash->{VERSION} = $version; $hash->{VERSION} = $version;
$hash->{STATE} = 'Initialized'; $hash->{STATE} = 'Initialized';
my $iodev = AttrVal( $name, 'IODev', 'none' ); my $iodev = AttrVal( $name, 'IODev', 'none' );
AssignIoPort( $hash, $iodev ) if ( !$hash->{IODev} ); AssignIoPort( $hash, $iodev ) if ( !$hash->{IODev} );
if ( defined( $hash->{IODev}->{NAME} ) ) { if ( defined( $hash->{IODev}->{NAME} ) ) {
Log3($name, 3, "NUKIDevice ($name) - I/O device is " Log3( $name, 3,
. $hash->{IODev}->{NAME}); "NUKIDevice ($name) - I/O device is " . $hash->{IODev}->{NAME} );
} }
else { else {
Log3( $name, 1, "NUKIDevice ($name) - no I/O device" ); Log3( $name, 1, "NUKIDevice ($name) - no I/O device" );
@ -187,12 +176,15 @@ sub NUKIDevice_Define($$) {
my $d = $modules{NUKIDevice}{defptr}{$nukiId}; my $d = $modules{NUKIDevice}{defptr}{$nukiId};
return return
'NUKIDevice device ' . $name . ' on NUKIBridge ' . $iodev . ' already defined.' 'NUKIDevice device '
. $name
. ' on NUKIBridge '
. $iodev
. ' already defined.'
if ( defined($d) if ( defined($d)
and $d->{IODev} == $hash->{IODev} and $d->{IODev} == $hash->{IODev}
and $d->{NAME} ne $name ); and $d->{NAME} ne $name );
Log3( $name, 3, "NUKIDevice ($name) - defined with NukiId: $nukiId" ); Log3( $name, 3, "NUKIDevice ($name) - defined with NukiId: $nukiId" );
CommandAttr( undef, $name . ' room NUKI' ) CommandAttr( undef, $name . ' room NUKI' )
@ -201,9 +193,12 @@ sub NUKIDevice_Define($$) {
if ( AttrVal( $name, 'model', 'none' ) eq 'none' ); if ( AttrVal( $name, 'model', 'none' ) eq 'none' );
if ($init_done) { if ($init_done) {
InternalTimer( gettimeofday()+int(rand(10)), "NUKIDevice_GetUpdate", $hash); InternalTimer( gettimeofday() + int( rand(10) ),
} else { "NUKIDevice_GetUpdate", $hash );
InternalTimer( gettimeofday()+15+int(rand(5)), "NUKIDevice_GetUpdate", $hash); }
else {
InternalTimer( gettimeofday() + 15 + int( rand(5) ),
"NUKIDevice_GetUpdate", $hash );
} }
$modules{NUKIDevice}{defptr}{$nukiId} = $hash; $modules{NUKIDevice}{defptr}{$nukiId} = $hash;
@ -244,13 +239,15 @@ sub NUKIDevice_Attr(@) {
} }
elsif ( $attrName eq 'disabledForIntervals' ) { elsif ( $attrName eq 'disabledForIntervals' ) {
if ( $cmd eq 'set' ) { if ( $cmd eq 'set' ) {
Log3($name, 3, "NUKIDevice ($name) - enable disabledForIntervals"); Log3( $name, 3,
"NUKIDevice ($name) - enable disabledForIntervals" );
readingsSingleUpdate( $hash, 'state', 'Unknown', 1 ); readingsSingleUpdate( $hash, 'state', 'Unknown', 1 );
} }
elsif ( $cmd eq 'del' ) { elsif ( $cmd eq 'del' ) {
readingsSingleUpdate( $hash, 'state', 'active', 1 ); readingsSingleUpdate( $hash, 'state', 'active', 1 );
Log3($name, 3, "NUKIDevice ($name) - delete disabledForIntervals"); Log3( $name, 3,
"NUKIDevice ($name) - delete disabledForIntervals" );
} }
} }
elsif ( $attrName eq 'model' ) { elsif ( $attrName eq 'model' ) {
@ -275,7 +272,8 @@ sub NUKIDevice_Set($$@) {
NUKIDevice_GetUpdate($hash); NUKIDevice_GetUpdate($hash);
return undef; return undef;
} elsif ( $cmd eq 'unpair' ) { }
elsif ( $cmd eq 'unpair' ) {
return ('usage: unpair') if ( @args != 0 ); return ('usage: unpair') if ( @args != 0 );
if ( !IsDisabled($name) ) { if ( !IsDisabled($name) ) {
@ -285,7 +283,8 @@ sub NUKIDevice_Set($$@) {
} }
return undef; return undef;
} elsif ( $cmd eq 'lock' }
elsif ($cmd eq 'lock'
or lc($cmd) eq 'deactivaterto' or lc($cmd) eq 'deactivaterto'
or $cmd eq 'unlock' or $cmd eq 'unlock'
or lc($cmd) eq 'activaterto' or lc($cmd) eq 'activaterto'
@ -294,18 +293,20 @@ 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' )
)
{ {
return ( 'usage: ' . $cmd ) return ( 'usage: ' . $cmd )
if ( @args != 0 ); if ( @args != 0 );
$lockAction = $cmd; $lockAction = $cmd;
} else { }
else {
my $list = ''; my $list = '';
$list= 'statusRequest:noArg unlock:noArg lock:noArg unlatch:noArg locknGo:noArg locknGoWithUnlatch:noArg unpair:noArg' $list =
'statusRequest:noArg unlock:noArg lock:noArg unlatch:noArg locknGo:noArg locknGoWithUnlatch:noArg unpair:noArg'
if ( $hash->{DEVICETYPE} == 0 ); if ( $hash->{DEVICETYPE} == 0 );
$list= 'statusRequest:noArg activateRto:noArg deactivateRto:noArg electricStrikeActuation:noArg activateContinuousMode:noArg deactivateContinuousMode:noArg unpair:noArg' $list =
'statusRequest:noArg activateRto:noArg deactivateRto:noArg electricStrikeActuation:noArg activateContinuousMode:noArg deactivateContinuousMode:noArg unpair:noArg'
if ( $hash->{DEVICETYPE} == 2 ); if ( $hash->{DEVICETYPE} == 2 );
return ( 'Unknown argument ' . $cmd . ', choose one of ' . $list ); return ( 'Unknown argument ' . $cmd . ', choose one of ' . $list );
@ -314,7 +315,8 @@ sub NUKIDevice_Set($$@) {
$hash->{helper}{lockAction} = $lockAction; $hash->{helper}{lockAction} = $lockAction;
$hash->{IODev}->{helper}->{iowrite} = 1 $hash->{IODev}->{helper}->{iowrite} = 1
if ( $hash->{IODev}->{helper}->{iowrite} == 0 ); if ( $hash->{IODev}->{helper}->{iowrite} == 0 );
IOWrite($hash,"lockAction",$lockAction,$hash->{NUKIID},$hash->{DEVICETYPE}); IOWrite( $hash, "lockAction", $lockAction, $hash->{NUKIID},
$hash->{DEVICETYPE} );
return undef; return undef;
} }
@ -348,7 +350,6 @@ sub NUKIDevice_Parse($$) {
return "NUKIDevice ($name) - invalid json detected: $json"; return "NUKIDevice ($name) - invalid json detected: $json";
} }
######################################### #########################################
#### verarbeiten des JSON Strings ####### #### verarbeiten des JSON Strings #######
my $decode_json = eval { decode_json($json) }; my $decode_json = eval { decode_json($json) };
@ -357,9 +358,10 @@ sub NUKIDevice_Parse($$) {
return; return;
} }
if ( ref($decode_json) ne 'HASH' ) { if ( ref($decode_json) ne 'HASH' ) {
Log3($name, 2, "NUKIDevice ($name) - got wrong status message for $name: $decode_json"); Log3( $name, 2,
"NUKIDevice ($name) - got wrong status message for $name: $decode_json"
);
return undef; return undef;
} }
@ -375,8 +377,15 @@ sub NUKIDevice_Parse($$) {
################## ##################
## Zwischenlösung so für die Umstellung, kann später gelöscht werden ## Zwischenlösung so für die Umstellung, kann später gelöscht werden
if ( AttrVal( $name, 'model', '' ) eq '' ) { if ( AttrVal( $name, 'model', '' ) eq '' ) {
CommandDefMod(undef,$name . ' NUKIDevice ' . $hash->{NUKIID} . ' ' . $decode_json->{deviceType}); CommandDefMod( undef,
CommandAttr(undef,$name . ' model ' . $deviceTypes{$decode_json->{deviceType}}); $name
. ' NUKIDevice '
. $hash->{NUKIID} . ' '
. $decode_json->{deviceType} );
CommandAttr( undef,
$name
. ' model '
. $deviceTypes{ $decode_json->{deviceType} } );
Log3( $name, 2, "NUKIDevice ($name) - redefined Defmod" ); Log3( $name, 2, "NUKIDevice ($name) - redefined Defmod" );
} }
@ -386,7 +395,8 @@ sub NUKIDevice_Parse($$) {
Log3( $name, 3, Log3( $name, 3,
"NUKIDevice ($name) - autocreate new device " "NUKIDevice ($name) - autocreate new device "
. makeDeviceName( $decode_json->{name} ) . makeDeviceName( $decode_json->{name} )
. " with nukiId $decode_json->{nukiId}, model $decode_json->{deviceType}"); . " with nukiId $decode_json->{nukiId}, model $decode_json->{deviceType}"
);
return return
'UNDEFINED ' 'UNDEFINED '
. makeDeviceName( $decode_json->{name} ) . makeDeviceName( $decode_json->{name} )
@ -403,7 +413,6 @@ sub NUKIDevice_WriteReadings($$) {
my $name = $hash->{NAME}; my $name = $hash->{NAME};
############################ ############################
#### Status des Smartlock #### Status des Smartlock
@ -414,7 +423,8 @@ sub NUKIDevice_WriteReadings($$) {
{ {
$battery = 'ok'; $battery = 'ok';
} elsif ( $decode_json->{batteryCritical} eq 'true' }
elsif ($decode_json->{batteryCritical} eq 'true'
or $decode_json->{batteryCritical} == 1 ) or $decode_json->{batteryCritical} == 1 )
{ {
$battery = "low"; $battery = "low";
@ -426,7 +436,8 @@ sub NUKIDevice_WriteReadings($$) {
if ( defined( $hash->{helper}{lockAction} ) ) { if ( defined( $hash->{helper}{lockAction} ) ) {
my ( $state, $lockState ); my ( $state, $lockState );
if ( defined($decode_json->{success}) if (
defined( $decode_json->{success} )
and ( $decode_json->{success} eq 'true' and ( $decode_json->{success} eq 'true'
or $decode_json->{success} == 1 ) or $decode_json->{success} == 1 )
) )
@ -435,16 +446,21 @@ sub NUKIDevice_WriteReadings($$) {
$lockState = $hash->{helper}{lockAction}; $lockState = $hash->{helper}{lockAction};
IOWrite( $hash, "lockState", undef, $hash->{NUKIID} ) IOWrite( $hash, "lockState", undef, $hash->{NUKIID} )
if ( ReadingsVal($hash->{IODev}->{NAME},'bridgeType','Software') eq 'Software' ); if (
ReadingsVal( $hash->{IODev}->{NAME}, 'bridgeType', 'Software' )
eq 'Software' );
} elsif ( defined($decode_json->{success}) }
elsif (
defined( $decode_json->{success} )
and ( $decode_json->{success} eq 'false' and ( $decode_json->{success} eq 'false'
or $decode_json->{success} == 0 ) or $decode_json->{success} == 0 )
) )
{ {
$state = $deviceTypes{ $hash->{DEVICETYPE} } . ' response error'; $state = $deviceTypes{ $hash->{DEVICETYPE} } . ' response error';
IOWrite($hash, 'lockState', undef, $hash->{NUKIID}, $hash->{DEVICETYPE} ); IOWrite( $hash, 'lockState', undef, $hash->{NUKIID},
$hash->{DEVICETYPE} );
} }
readingsBulkUpdate( $hash, 'state', $state ); readingsBulkUpdate( $hash, 'state', $state );
@ -452,10 +468,13 @@ sub NUKIDevice_WriteReadings($$) {
readingsBulkUpdate( $hash, 'success', $decode_json->{success} ); readingsBulkUpdate( $hash, 'success', $decode_json->{success} );
delete $hash->{helper}{lockAction}; delete $hash->{helper}{lockAction};
Log3($name, 5, "NUKIDevice ($name) - lockAction readings set for $name"); Log3( $name, 5,
"NUKIDevice ($name) - lockAction readings set for $name" );
} else { }
readingsBulkUpdate( $hash, 'batteryCritical', $decode_json->{batteryCritical} ); else {
readingsBulkUpdate( $hash, 'batteryCritical',
$decode_json->{batteryCritical} );
readingsBulkUpdate( $hash, 'lockState', $decode_json->{stateName} ); readingsBulkUpdate( $hash, 'lockState', $decode_json->{stateName} );
readingsBulkUpdate( $hash, 'state', $decode_json->{stateName} ); readingsBulkUpdate( $hash, 'state', $decode_json->{stateName} );
readingsBulkUpdate( $hash, 'batteryState', $battery ); readingsBulkUpdate( $hash, 'batteryState', $battery );
@ -473,17 +492,8 @@ sub NUKIDevice_WriteReadings($$) {
return undef; return undef;
} }
1; 1;
=pod =pod
=item device =item device
=item summary Modul to control the Nuki Smartlock's =item summary Modul to control the Nuki Smartlock's