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,12 +171,14 @@ 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};
$hash->{VERSION} = $version; $hash->{VERSION} = $version;
} }
@ -193,10 +189,9 @@ sub NUKIBridge_Define($$) {
my @a = split( "[ \t][ \t]*", $def ); my @a = split( "[ \t][ \t]*", $def );
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,28 +206,38 @@ 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;
} }
$hash->{WEBHOOK_REGISTER} = "unregistered"; $hash->{WEBHOOK_REGISTER} = "unregistered";
readingsSingleUpdate($hash, 'state', 'Initialized', 1 ); readingsSingleUpdate( $hash, 'state', 'Initialized', 1 );
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;
return undef; return undef;
} }
@ -247,8 +252,8 @@ sub NUKIBridge_Undef($$) {
NUKIBridge_removeExtension( $hash->{fhem}{infix} ); NUKIBridge_removeExtension( $hash->{fhem}{infix} );
} }
RemoveInternalTimer( $hash ); RemoveInternalTimer($hash);
delete $modules{NUKIBridge}{defptr}{$hash->{HOST}}; delete $modules{NUKIBridge}{defptr}{ $hash->{HOST} };
return undef; return undef;
} }
@ -261,58 +266,91 @@ sub NUKIBridge_Attr(@) {
if ( $attrName eq 'disable' ) { if ( $attrName eq 'disable' ) {
if ( $cmd eq 'set' and $attrVal == 1 ) { if ( $cmd eq 'set' and $attrVal == 1 ) {
readingsSingleUpdate ( $hash, 'state', 'disabled', 1 ); readingsSingleUpdate( $hash, 'state', 'disabled', 1 );
Log3($name, 3, "NUKIBridge ($name) - disabled"); Log3( $name, 3, "NUKIBridge ($name) - disabled" );
} }
elsif ( $cmd eq 'del' ) { elsif ( $cmd eq 'del' ) {
readingsSingleUpdate ( $hash, 'state', 'active', 1 ); readingsSingleUpdate( $hash, 'state', 'active', 1 );
Log3($name, 3, "NUKIBridge ($name) - enabled"); Log3( $name, 3, "NUKIBridge ($name) - enabled" );
} }
} }
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,
readingsSingleUpdate ( $hash, 'state', 'Unknown', 1 ); "NUKIBridge ($name) - enable disabledForIntervals" );
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 )
if ( $init_done ); if ($init_done);
$hash->{WEBHOOK_REGISTER} = 'sent'; $hash->{WEBHOOK_REGISTER} = 'sent';
} }
else { else {
@ -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,87 +383,98 @@ 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};
} }
sub NUKIBridge_Set($@) { sub NUKIBridge_Set($@) {
my ($hash, $name, $cmd, @args) = @_; my ( $hash, $name, $cmd, @args ) = @_;
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 );
my $id = "id=" . (@args > 0 ? join( ' ', @args ) : 0); my $id = "id=" . ( @args > 0 ? join( ' ', @args ) : 0 );
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)
) )
{ {
return('Success Callback ' . $id . ' removed'); return ( 'Success Callback ' . $id . ' removed' );
} }
else { else {
return('remove Callback failed'); return ('remove Callback failed');
} }
} }
else { else {
my $list = ''; my $list = '';
$list .= 'info:noArg getDeviceList:noArg callbackRemove:noArg '; $list .= 'info:noArg getDeviceList:noArg callbackRemove:noArg ';
$list .= 'clearLog:noArg fwUpdate:noArg reboot:noArg factoryReset:noArg' $list .= 'clearLog:noArg fwUpdate:noArg reboot:noArg factoryReset:noArg'
if ( ReadingsVal($name,'bridgeType','Software') eq 'Hardware' ); if ( ReadingsVal( $name, 'bridgeType', 'Software' ) eq 'Hardware' );
return('Unknown argument ' . $cmd . ', choose one of ' . $list); return ( 'Unknown argument ' . $cmd . ', choose one of ' . $list );
} }
} }
sub NUKIBridge_Get($@) { sub NUKIBridge_Get($@) {
my ($hash, $name, $cmd, @args) = @_; my ( $hash, $name, $cmd, @args ) = @_;
my ($arg, @params) = @args; my ( $arg, @params ) = @args;
if (lc($cmd) eq 'logfile') { if ( lc($cmd) eq 'logfile' ) {
return 'usage: logFile' if ( @args != 0 ); return 'usage: logFile' if ( @args != 0 );
NUKIBridge_getLogfile($hash) if ( !IsDisabled($name) ); NUKIBridge_getLogfile($hash) if ( !IsDisabled($name) );
} }
elsif (lc($cmd) eq 'callbacklist') { elsif ( lc($cmd) eq 'callbacklist' ) {
return 'usage: callbackList' if ( @args != 0 ); return 'usage: callbackList' if ( @args != 0 );
NUKIBridge_getCallbackList($hash) if ( !IsDisabled($name) ); NUKIBridge_getCallbackList($hash) if ( !IsDisabled($name) );
@ -432,7 +483,7 @@ sub NUKIBridge_Get($@) {
my $list = ''; my $list = '';
$list .= 'callbackList:noArg '; $list .= 'callbackList:noArg ';
$list .= 'logFile:noArg' $list .= 'logFile:noArg'
if ( ReadingsVal($name,'bridgeType','Software') eq 'Hardware' ); if ( ReadingsVal( $name, 'bridgeType', 'Software' ) eq 'Hardware' );
return 'Unknown argument ' . $cmd . ', choose one of ' . $list; return 'Unknown argument ' . $cmd . ', choose one of ' . $list;
} }
@ -444,20 +495,23 @@ sub NUKIBridge_GetCheckBridgeAlive($) {
my $name = $hash->{NAME}; my $name = $hash->{NAME};
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 ) and $hash->{helper}->{iowrite} == 0 )
{ {
NUKIBridge_Write($hash,'info',undef,undef,undef); NUKIBridge_Write( $hash, 'info', undef, undef, undef );
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,14 +520,16 @@ 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;
} }
sub NUKIBridge_Write($@) { sub NUKIBridge_Write($@) {
my ($hash,$path,$lockAction,$nukiId,$deviceType) = @_; my ( $hash, $path, $lockAction, $nukiId, $deviceType ) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $host = $hash->{HOST}; my $host = $hash->{HOST};
@ -481,7 +537,7 @@ sub NUKIBridge_Write($@) {
my $token = $hash->{TOKEN}; my $token = $hash->{TOKEN};
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=' . $lockActionsSmartLock{$lockAction} $uri .= '&action=' . $lockActionsSmartLock{$lockAction}
if ( defined($lockAction) if ( defined($lockAction)
@ -515,7 +571,7 @@ sub NUKIBridge_Write($@) {
} }
); );
Log3($name, 4, "NUKIBridge ($name) - Send HTTP POST with URL $uri"); Log3( $name, 4, "NUKIBridge ($name) - Send HTTP POST with URL $uri" );
} }
sub NUKIBridge_Distribution($$$) { sub NUKIBridge_Distribution($$$) {
@ -533,43 +589,56 @@ sub NUKIBridge_Distribution($$$) {
my $dname = $dhash->{NAME}; my $dname = $dhash->{NAME};
Log3($name, 5, "NUKIBridge ($name) - Response JSON: $json"); Log3( $name, 5, "NUKIBridge ($name) - Response JSON: $json" );
Log3($name, 5, "NUKIBridge ($name) - Response ERROR: $err"); Log3( $name, 5, "NUKIBridge ($name) - Response ERROR: $err" );
Log3($name, 5, "NUKIBridge ($name) - Response CODE: $param->{code}") Log3( $name, 5, "NUKIBridge ($name) - Response CODE: $param->{code}" )
if ( defined($param->{code}) and ($param->{code}) ); if ( defined( $param->{code} ) and ( $param->{code} ) );
readingsBeginUpdate($hash); readingsBeginUpdate($hash);
if ( defined( $err ) ) { if ( defined($err) ) {
if ( $err ne '' ) { if ( $err ne '' ) {
if ($param->{endpoint} eq 'info') { if ( $param->{endpoint} eq 'info' ) {
readingsBulkUpdate( $hash, 'state', 'not connected') readingsBulkUpdate( $hash, 'state', 'not connected' )
if ( $hash->{helper}{aliveCount} > 1 ); if ( $hash->{helper}{aliveCount} > 1 );
Log3($name, 5, "NUKIBridge ($name) - Bridge ist offline"); Log3( $name, 5, "NUKIBridge ($name) - Bridge ist offline" );
$hash->{helper}{aliveCount} = $hash->{helper}{aliveCount} + 1; $hash->{helper}{aliveCount} = $hash->{helper}{aliveCount} + 1;
} }
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} ) ) {
@ -579,11 +648,11 @@ sub NUKIBridge_Distribution($$$) {
readingsBulkUpdate( $hash, 'lastError', 'action is undefined' ) readingsBulkUpdate( $hash, 'lastError', 'action is undefined' )
if ( $param->{code} == 400 and $hash == $dhash ); if ( $param->{code} == 400 and $hash == $dhash );
Log3($name, 4, "NUKIBridge ($name) - invalid API token") Log3( $name, 4, "NUKIBridge ($name) - invalid API token" )
if ( $param->{code} == 401 ); if ( $param->{code} == 401 );
Log3($name, 4, "NUKIBridge ($name) - nukiId is not known") Log3( $name, 4, "NUKIBridge ($name) - nukiId is not known" )
if ( $param->{code} == 404 ); if ( $param->{code} == 404 );
Log3($name, 4, "NUKIBridge ($name) - action is undefined") Log3( $name, 4, "NUKIBridge ($name) - action is undefined" )
if ( $param->{code} == 400 and $hash == $dhash ); if ( $param->{code} == 400 and $hash == $dhash );
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
@ -593,18 +662,19 @@ sub NUKIBridge_Distribution($$$) {
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
if ( $hash == $dhash ) { if ( $hash == $dhash ) {
NUKIBridge_ResponseProcessing($hash,$json,$param->{endpoint}); NUKIBridge_ResponseProcessing( $hash, $json, $param->{endpoint} );
} }
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;
} }
$decode_json->{nukiId} = $param->{nukiId}; $decode_json->{nukiId} = $param->{nukiId};
$json = encode_json($decode_json); $json = encode_json($decode_json);
Dispatch($hash,$json,undef); Dispatch( $hash, $json, undef );
} }
$hash->{helper}->{iowrite} = 0 $hash->{helper}->{iowrite} = 0
@ -614,34 +684,42 @@ sub NUKIBridge_Distribution($$$) {
} }
sub NUKIBridge_ResponseProcessing($$$) { sub NUKIBridge_ResponseProcessing($$$) {
my ($hash,$json,$path) = @_; my ( $hash, $json, $path ) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $decode_json; my $decode_json;
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' ) { }
Log3($name, 4, "NUKIBridge ($name) - empty answer received"); elsif ( $json =~ m'HTTP/1.1 200 OK' ) {
Log3( $name, 4, "NUKIBridge ($name) - empty answer received" );
return undef; return undef;
} elsif ( $json !~ m/^[\[{].*[}\]]$/ ) { }
Log3($name, 3, "NUKIBridge ($name) - invalid json detected: $json"); elsif ( $json !~ m/^[\[{].*[}\]]$/ ) {
return("NUKIBridge ($name) - invalid json detected: $json"); Log3( $name, 3, "NUKIBridge ($name) - invalid json detected: $json" );
return ("NUKIBridge ($name) - invalid json detected: $json");
} }
$decode_json = eval{decode_json($json)}; $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;
} }
if ( (ref($decode_json) eq 'ARRAY' if ( $path eq 'list'
and scalar(@{$decode_json}) > 0 or $path eq 'info' )
and $path eq 'list') {
or ( ref($decode_json->{scanResults}) eq 'ARRAY' if (
and scalar(@{$decode_json->{scanResults}}) > 0 (
and $path eq 'info') ref($decode_json) eq 'ARRAY'
and scalar( @{$decode_json} ) > 0
and $path eq 'list'
)
or ( ref( $decode_json->{scanResults} ) eq 'ARRAY'
and scalar( @{ $decode_json->{scanResults} } ) > 0
and $path eq 'info' )
) )
{ {
my @buffer; my @buffer;
@ -653,49 +731,51 @@ 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)
. " Content: " . " Content: "
. $json); . $json );
Log3($name, 5, Log3( $name, 5,
"NUKIBridge ($name) - Vor Sub: Laenge JSON: " "NUKIBridge ($name) - Vor Sub: Laenge JSON: "
. length($json) . length($json)
. " Content: " . " Content: "
. $json . $json
. " Tail: " . " Tail: "
. $tail); . $tail );
Dispatch( $hash, $json, undef ) Dispatch( $hash, $json, undef )
unless ( not defined($tail) and not($tail) ); unless ( not defined($tail) and not($tail) );
( $json, $tail ) = NUKIBridge_ParseJSON( $hash, $tail ); ( $json, $tail ) = NUKIBridge_ParseJSON( $hash, $tail );
Log3($name, 5, Log3( $name, 5,
"NUKIBridge ($name) - Nach Sub: Laenge JSON: " "NUKIBridge ($name) - Nach Sub: Laenge JSON: "
. length($json) . length($json)
. " Content: " . " Content: "
. $json . $json
. " Tail: " . " Tail: "
. $tail); . $tail );
} }
} }
if ( $path eq 'info' ) { if ( $path eq 'info' ) {
readingsBeginUpdate( $hash ); readingsBeginUpdate($hash);
readingsBulkUpdate( $hash, 'state', 'connected' ); readingsBulkUpdate( $hash, 'state', 'connected' );
Log3($name, 5, "NUKIBridge ($name) - Bridge ist online"); Log3( $name, 5, "NUKIBridge ($name) - Bridge ist online" );
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
$hash->{helper}{aliveCount} = 0; $hash->{helper}{aliveCount} = 0;
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;
} }
@ -712,49 +792,53 @@ sub NUKIBridge_CGI() {
$name = $hash->{NAME}; $name = $hash->{NAME};
} }
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 ) {
Log3($name, 3, "NUKIBridge WEBHOOK ($name) - empty message received"); Log3( $name, 3, "NUKIBridge WEBHOOK ($name) - empty message 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 WEBHOOK ($name) - empty answer received"); Log3( $name, 4, "NUKIBridge WEBHOOK ($name) - empty answer received" );
return undef; return undef;
} }
elsif ( $json !~ m/^[\[{].*[}\]]$/ ) { elsif ( $json !~ m/^[\[{].*[}\]]$/ ) {
Log3($name, 3, "NUKIBridge WEBHOOK ($name) - invalid json detected: $json"); Log3( $name, 3,
return("NUKIBridge WEBHOOK ($name) - invalid json detected: $json"); "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 );
return ( undef, undef ); return ( undef, undef );
} }
# 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 );
} }
sub NUKIBridge_InfoProcessing($$) { sub NUKIBridge_InfoProcessing($$) {
my ($hash,$decode_json) = @_; my ( $hash, $decode_json ) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
@ -770,48 +854,54 @@ 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,'hardwareId',$decode_json->{ids}{hardwareId}); readingsBulkUpdate( $hash, 'wifiFirmwareVersion',
readingsBulkUpdate($hash,'serverId',$decode_json->{ids}{serverId}); $decode_json->{versions}->{wifiFirmwareVersion} );
readingsBulkUpdate($hash,'uptime',$decode_json->{uptime}); readingsBulkUpdate( $hash, 'bridgeType',
readingsBulkUpdate($hash,'currentTime',$decode_json->{currentTime}); $bridgeType{ $decode_json->{bridgeType} } );
readingsBulkUpdate($hash,'serverConnected',$decode_json->{serverConnected}); readingsBulkUpdate( $hash, 'hardwareId', $decode_json->{ids}{hardwareId} );
readingsEndUpdate($hash,1); readingsBulkUpdate( $hash, 'serverId', $decode_json->{ids}{serverId} );
readingsBulkUpdate( $hash, 'uptime', $decode_json->{uptime} );
readingsBulkUpdate( $hash, 'currentTime', $decode_json->{currentTime} );
readingsBulkUpdate( $hash, 'serverConnected',
$decode_json->{serverConnected} );
readingsEndUpdate( $hash, 1 );
} }
sub NUKIBridge_getLogfile($) { sub NUKIBridge_getLogfile($) {
my ($hash) = @_; my ($hash) = @_;
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" );
my $ret = '<html><table width=100%><tr><td>'; my $ret = '<html><table width=100%><tr><td>';
$ret .= '<table class="block wide">'; $ret .= '<table class="block wide">';
foreach my $logs (@{$decode_json}) { foreach my $logs ( @{$decode_json} ) {
$ret .= '<tr class="odd">'; $ret .= '<tr class="odd">';
if ($logs->{timestamp}) { if ( $logs->{timestamp} ) {
$ret .= '<td><b>timestamp:</b> </td>'; $ret .= '<td><b>timestamp:</b> </td>';
$ret .= '<td>' . $logs->{timestamp} . '</td>'; $ret .= '<td>' . $logs->{timestamp} . '</td>';
$ret .= '<td> </td>'; $ret .= '<td> </td>';
} }
if ($logs->{type}) { if ( $logs->{type} ) {
$ret .= '<td><b>type:</b> </td>'; $ret .= '<td><b>type:</b> </td>';
$ret .= '<td>' . $logs->{type} . '</td>'; $ret .= '<td>' . $logs->{type} . '</td>';
$ret .= '<td> </td>'; $ret .= '<td> </td>';
} }
foreach my $d (reverse sort keys %{$logs}) { foreach my $d ( reverse sort keys %{$logs} ) {
next if ( $d eq 'type' ); next if ( $d eq 'type' );
next if ( $d eq 'timestamp' ); next if ( $d eq 'timestamp' );
@ -834,17 +924,20 @@ sub NUKIBridge_getCallbackList($) {
my ($hash) = @_; my ($hash) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $decode_json = NUKIBridge_CallBlocking($hash,'callback/list',undef); my $decode_json = NUKIBridge_CallBlocking( $hash, 'callback/list', undef );
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>';
@ -856,7 +949,7 @@ scalar(@{$decode_json->{callbacks}}) > 0 ) {
$ret .= '<td><b>Callback-URL</b></td>'; $ret .= '<td><b>Callback-URL</b></td>';
$ret .= '</tr>'; $ret .= '</tr>';
foreach my $cb (@{$decode_json->{callbacks}}) { foreach my $cb ( @{ $decode_json->{callbacks} } ) {
$ret .= '<td>' . $cb->{id} . '</td>'; $ret .= '<td>' . $cb->{id} . '</td>';
$ret .= '<td> </td>'; $ret .= '<td> </td>';
@ -873,53 +966,54 @@ scalar(@{$decode_json->{callbacks}}) > 0 ) {
} }
sub NUKIBridge_CallBlocking($@) { sub NUKIBridge_CallBlocking($@) {
my ($hash,$path,$obj) = @_; my ( $hash, $path, $obj ) = @_;
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};
my $url = 'http://' . $hash->{HOST} . ':' . $port; my $url = 'http://' . $hash->{HOST} . ':' . $port;
$url .= '/' . $path $url .= '/' . $path
if ( defined $path); if ( defined $path );
$url .= '?token=' . $token $url .= '?token=' . $token
if ( defined($token) ); if ( defined($token) );
$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" );
return undef; return undef;
} }
elsif ( $data =~ m'HTTP/1.1 200 OK' ) { elsif ( $data =~ m'HTTP/1.1 200 OK' ) {
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 $path ne "log" ) {
Log3($name, 3, "NUKIDevice ($name) - invalid json detected for $url: $data"); Log3( $name, 3,
return("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");
} }
my $decode_json = eval{decode_json($data)}; my $decode_json = eval { decode_json($data) };
if ($@){ if ($@) {
Log3($name, 3, "NUKIBridge ($name) - JSON error while request: $@"); Log3( $name, 3, "NUKIBridge ($name) - JSON error while request: $@" );
return; return;
} }
return undef if ( !$decode_json ); return undef if ( !$decode_json );
Log3($name, 5, "NUKIBridge ($name) - Data: $data"); Log3( $name, 5, "NUKIBridge ($name) - Data: $data" );
Log3($name, 4, "NUKIBridge ($name) - Blocking HTTP Query finished"); Log3( $name, 4, "NUKIBridge ($name) - Blocking HTTP Query finished" );
return ($decode_json); return ($decode_json);
} }
@ -936,15 +1030,15 @@ sub NUKIBridge_ParseJSON($$) {
foreach my $c ( split //, $buffer ) { foreach my $c ( split //, $buffer ) {
if ( $open == $close and $open > 0 ) { if ( $open == $close and $open > 0 ) {
$tail .= $c; $tail .= $c;
Log3($name, 5, Log3( $name, 5,
"NUKIBridge ($name) - $open == $close and $open > 0"); "NUKIBridge ($name) - $open == $close and $open > 0" );
} }
elsif ( ( $open == $close ) and ( $c ne '{' ) ) { elsif ( ( $open == $close ) and ( $c ne '{' ) ) {
Log3($name, 5, Log3( $name, 5,
"NUKIBridge ($name) - Garbage character before message: " "NUKIBridge ($name) - Garbage character before message: "
. $c); . $c );
} }
else { else {
@ -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,13 +131,13 @@ 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};
$hash->{VERSION} = $version; $hash->{VERSION} = $version;
} }
@ -157,29 +147,28 @@ 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];
my $deviceType = (defined $a[3]) ? $a[3] : 0; my $deviceType = ( defined $a[3] ) ? $a[3] : 0;
$hash->{NUKIID} = $nukiId; $hash->{NUKIID} = $nukiId;
$hash->{DEVICETYPE} = (defined $deviceType) ? $deviceType : 0; $hash->{DEVICETYPE} = ( defined $deviceType ) ? $deviceType : 0;
$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" );
} }
$iodev = $hash->{IODev}->{NAME}; $iodev = $hash->{IODev}->{NAME};
@ -187,23 +176,29 @@ 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' )
if ( AttrVal( $name, 'room', 'none' ) eq 'none' );
CommandAttr( undef, $name . ' model ' . $deviceTypes{$deviceType} )
if ( AttrVal( $name, 'model', 'none' ) eq 'none' );
CommandAttr(undef,$name . ' room NUKI') if ($init_done) {
if ( AttrVal($name,'room','none') eq 'none'); InternalTimer( gettimeofday() + int( rand(10) ),
CommandAttr(undef,$name . ' model ' . $deviceTypes{$deviceType}) "NUKIDevice_GetUpdate", $hash );
if ( AttrVal($name,'model','none') eq 'none'); }
else {
if ( $init_done ) { InternalTimer( gettimeofday() + 15 + int( rand(5) ),
InternalTimer( gettimeofday()+int(rand(10)), "NUKIDevice_GetUpdate", $hash); "NUKIDevice_GetUpdate", $hash );
} else {
InternalTimer( gettimeofday()+15+int(rand(5)), "NUKIDevice_GetUpdate", $hash);
} }
$modules{NUKIDevice}{defptr}{$nukiId} = $hash; $modules{NUKIDevice}{defptr}{$nukiId} = $hash;
@ -219,8 +214,8 @@ sub NUKIDevice_Undef($$) {
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
Log3($name, 3, "NUKIDevice ($name) - undefined with NukiId: $nukiId"); Log3( $name, 3, "NUKIDevice ($name) - undefined with NukiId: $nukiId" );
delete($modules{NUKIDevice}{defptr}{$nukiId}); delete( $modules{NUKIDevice}{defptr}{$nukiId} );
return undef; return undef;
} }
@ -233,29 +228,31 @@ sub NUKIDevice_Attr(@) {
if ( $attrName eq 'disable' ) { if ( $attrName eq 'disable' ) {
if ( $cmd eq 'set' and $attrVal == 1 ) { if ( $cmd eq 'set' and $attrVal == 1 ) {
readingsSingleUpdate ( $hash, 'state', 'disabled', 1 ); readingsSingleUpdate( $hash, 'state', 'disabled', 1 );
Log3($name, 3, "NUKIDevice ($name) - disabled"); Log3( $name, 3, "NUKIDevice ($name) - disabled" );
} }
elsif ( $cmd eq 'del' ) { elsif ( $cmd eq 'del' ) {
readingsSingleUpdate ( $hash, 'state', 'active', 1 ); readingsSingleUpdate( $hash, 'state', 'active', 1 );
Log3($name, 3, "NUKIDevice ($name) - enabled"); Log3( $name, 3, "NUKIDevice ($name) - enabled" );
} }
} }
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,
readingsSingleUpdate ( $hash, 'state', 'Unknown', 1 ); "NUKIDevice ($name) - enable disabledForIntervals" );
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' ) {
if ( $cmd eq 'set' ) { if ( $cmd eq 'set' ) {
Log3($name, 3, "NUKIDevice ($name) - change model"); Log3( $name, 3, "NUKIDevice ($name) - change model" );
$hash->{DEVICETYPE} = $deviceTypeIds{$attrVal}; $hash->{DEVICETYPE} = $deviceTypeIds{$attrVal};
} }
} }
@ -264,28 +261,30 @@ sub NUKIDevice_Attr(@) {
} }
sub NUKIDevice_Set($$@) { sub NUKIDevice_Set($$@) {
my ($hash, $name, @aa) = @_; my ( $hash, $name, @aa ) = @_;
my ($cmd, @args) = @aa; my ( $cmd, @args ) = @aa;
my $lockAction; my $lockAction;
if ( lc($cmd) eq 'statusrequest' ) { if ( lc($cmd) eq 'statusrequest' ) {
return('usage: statusRequest') if ( @args != 0 ); return ('usage: statusRequest') if ( @args != 0 );
NUKIDevice_GetUpdate($hash); NUKIDevice_GetUpdate($hash);
return undef; return undef;
} elsif ( $cmd eq 'unpair' ) { }
return('usage: unpair') if ( @args != 0 ); elsif ( $cmd eq 'unpair' ) {
return ('usage: unpair') if ( @args != 0 );
if ( !IsDisabled($name) ) { if ( !IsDisabled($name) ) {
$hash->{IODev}->{helper}->{iowrite} = 1 $hash->{IODev}->{helper}->{iowrite} = 1
if ( $hash->{IODev}->{helper}->{iowrite} == 0 ); if ( $hash->{IODev}->{helper}->{iowrite} == 0 );
IOWrite($hash,$cmd,undef,$hash->{NUKIID},$hash->{DEVICETYPE}); IOWrite( $hash, $cmd, undef, $hash->{NUKIID}, $hash->{DEVICETYPE} );
} }
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,27 +293,30 @@ 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 );
} }
$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;
} }
@ -326,40 +328,40 @@ sub NUKIDevice_GetUpdate($) {
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
IOWrite($hash, 'lockState', undef, $hash->{NUKIID}, $hash->{DEVICETYPE} ) IOWrite( $hash, 'lockState', undef, $hash->{NUKIID}, $hash->{DEVICETYPE} )
if ( !IsDisabled($name) ); if ( !IsDisabled($name) );
Log3($name, 5, "NUKIDevice ($name) - NUKIDevice_GetUpdate Call IOWrite") Log3( $name, 5, "NUKIDevice ($name) - NUKIDevice_GetUpdate Call IOWrite" )
if ( !IsDisabled($name) ); if ( !IsDisabled($name) );
return undef; return undef;
} }
sub NUKIDevice_Parse($$) { sub NUKIDevice_Parse($$) {
my($hash,$json) = @_; my ( $hash, $json ) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
Log3($name, 5, "NUKIDevice ($name) - Parse with result: $json"); Log3( $name, 5, "NUKIDevice ($name) - Parse with result: $json" );
######################################### #########################################
####### Errorhandling ############# ####### Errorhandling #############
if ( $json !~ m/^[\[{].*[}\]]$/ ) { if ( $json !~ m/^[\[{].*[}\]]$/ ) {
Log3($name, 3, "NUKIDevice ($name) - invalid json detected: $json"); Log3( $name, 3, "NUKIDevice ($name) - invalid json detected: $json" );
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) };
if ($@){ if ($@) {
Log3($name, 3, "NUKIDevice ($name) - JSON error while request: $@"); Log3( $name, 3, "NUKIDevice ($name) - JSON error while request: $@" );
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;
} }
@ -369,52 +371,60 @@ sub NUKIDevice_Parse($$) {
my $name = $hash->{NAME}; my $name = $hash->{NAME};
NUKIDevice_WriteReadings( $hash, $decode_json ); NUKIDevice_WriteReadings( $hash, $decode_json );
Log3($name, 4, Log3( $name, 4,
"NUKIDevice ($name) - find logical device: $hash->{NAME}"); "NUKIDevice ($name) - find logical device: $hash->{NAME}" );
################## ##################
## 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
Log3($name, 2, "NUKIDevice ($name) - redefined Defmod"); . ' NUKIDevice '
. $hash->{NUKIID} . ' '
. $decode_json->{deviceType} );
CommandAttr( undef,
$name
. ' model '
. $deviceTypes{ $decode_json->{deviceType} } );
Log3( $name, 2, "NUKIDevice ($name) - redefined Defmod" );
} }
return $hash->{NAME}; return $hash->{NAME};
} }
else { else {
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} )
. " NUKIDevice $decode_json->{nukiId} $decode_json->{deviceType}"; . " NUKIDevice $decode_json->{nukiId} $decode_json->{deviceType}";
} }
Log3($name, 5, "NUKIDevice ($name) - parse status message for $name"); Log3( $name, 5, "NUKIDevice ($name) - parse status message for $name" );
NUKIDevice_WriteReadings($hash,$decode_json); NUKIDevice_WriteReadings( $hash, $decode_json );
} }
sub NUKIDevice_WriteReadings($$) { sub NUKIDevice_WriteReadings($$) {
my ($hash,$decode_json) = @_; my ( $hash, $decode_json ) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
############################ ############################
#### Status des Smartlock #### Status des Smartlock
my $battery; my $battery;
if ( defined($decode_json->{batteryCritical}) ) { if ( defined( $decode_json->{batteryCritical} ) ) {
if ( $decode_json->{batteryCritical} eq 'false' if ( $decode_json->{batteryCritical} eq 'false'
or $decode_json->{batteryCritical} == 0 ) or $decode_json->{batteryCritical} == 0 )
{ {
$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";
@ -423,28 +433,34 @@ sub NUKIDevice_WriteReadings($$) {
readingsBeginUpdate($hash); readingsBeginUpdate($hash);
if ( defined($hash->{helper}{lockAction}) ) { if ( defined( $hash->{helper}{lockAction} ) ) {
my ($state,$lockState); my ( $state, $lockState );
if ( defined($decode_json->{success}) if (
and ($decode_json->{success} eq 'true' defined( $decode_json->{success} )
or $decode_json->{success} == 1) and ( $decode_json->{success} eq 'true'
or $decode_json->{success} == 1 )
) )
{ {
$state = $hash->{helper}{lockAction}; $state = $hash->{helper}{lockAction};
$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}) }
and ($decode_json->{success} eq 'false' elsif (
or $decode_json->{success} == 0) defined( $decode_json->{success} )
and ( $decode_json->{success} eq 'false'
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 );
@ -465,7 +484,7 @@ sub NUKIDevice_WriteReadings($$) {
readingsBulkUpdate( $hash, 'rssi', $decode_json->{rssi} ); readingsBulkUpdate( $hash, 'rssi', $decode_json->{rssi} );
readingsBulkUpdate( $hash, 'paired', $decode_json->{paired} ); readingsBulkUpdate( $hash, 'paired', $decode_json->{paired} );
Log3($name, 5, "NUKIDevice ($name) - readings set for $name"); Log3( $name, 5, "NUKIDevice ($name) - readings set for $name" );
} }
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
@ -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