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;
use strict;
@ -115,11 +113,9 @@ if ($@) {
}
}
my $version = '0.7.11';
my $version = '0.7.15';
my $bridgeapi = '1.9';
my %lockActionsSmartLock = (
'unlock' => 1,
'lock' => 2,
@ -136,7 +132,6 @@ my %lockActionsOpener = (
'deactivateContinuousMode' => 5
);
# Declare functions
sub NUKIBridge_Initialize ($);
sub NUKIBridge_Define ($$);
@ -159,8 +154,6 @@ sub NUKIBridge_getLogfile($);
sub NUKIBridge_getCallbackList($);
sub NUKIBridge_CallBlocking($@);
sub NUKIBridge_Initialize($) {
my ($hash) = @_;
@ -169,7 +162,8 @@ sub NUKIBridge_Initialize($) {
$hash->{Clients} = ':NUKIDevice:';
$hash->{MatchList} = { '1:NUKIDevice' => '^{.*}$' };
my $webhookFWinstance = join( ',', devspec2array('TYPE=FHEMWEB:FILTER=TEMPORARY!=1') );
my $webhookFWinstance =
join( ',', devspec2array('TYPE=FHEMWEB:FILTER=TEMPORARY!=1') );
# Consumer
$hash->{SetFn} = 'NUKIBridge_Set';
@ -177,12 +171,14 @@ sub NUKIBridge_Initialize($) {
$hash->{DefFn} = 'NUKIBridge_Define';
$hash->{UndefFn} = 'NUKIBridge_Undef';
$hash->{AttrFn} = 'NUKIBridge_Attr';
$hash->{AttrList} = 'disable:1 '.
'webhookFWinstance:' . $webhookFWinstance . ' '.
'webhookHttpHostname '.
$readingFnAttributes;
$hash->{AttrList} =
'disable:1 '
. 'webhookFWinstance:'
. $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};
$hash->{VERSION} = $version;
}
@ -193,10 +189,9 @@ sub NUKIBridge_Define($$) {
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 );
my $name = $a[0];
my $host = $a[2];
my $token = $a[3];
@ -211,28 +206,38 @@ sub NUKIBridge_Define($$) {
$hash->{helper}->{iowrite} = 0;
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')
if ( AttrVal($name,'room','none') eq 'none' );
CommandAttr( undef, $name . ' room NUKI' )
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->{WEBHOOK_REGISTER} = "unregistered";
readingsSingleUpdate($hash, 'state', 'Initialized', 1 );
readingsSingleUpdate( $hash, 'state', 'Initialized', 1 );
RemoveInternalTimer($hash);
if ( $init_done ) {
NUKIBridge_firstRun($hash) if ( ($hash->{HOST}) and ($hash->{TOKEN}) );
} else {
InternalTimer( gettimeofday()+15, 'NUKIBridge_firstRun', $hash) if ( ($hash->{HOST}) and ($hash->{TOKEN}) );
if ($init_done) {
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;
}
@ -247,8 +252,8 @@ sub NUKIBridge_Undef($$) {
NUKIBridge_removeExtension( $hash->{fhem}{infix} );
}
RemoveInternalTimer( $hash );
delete $modules{NUKIBridge}{defptr}{$hash->{HOST}};
RemoveInternalTimer($hash);
delete $modules{NUKIBridge}{defptr}{ $hash->{HOST} };
return undef;
}
@ -261,58 +266,91 @@ sub NUKIBridge_Attr(@) {
if ( $attrName eq 'disable' ) {
if ( $cmd eq 'set' and $attrVal == 1 ) {
readingsSingleUpdate ( $hash, 'state', 'disabled', 1 );
Log3($name, 3, "NUKIBridge ($name) - disabled");
readingsSingleUpdate( $hash, 'state', 'disabled', 1 );
Log3( $name, 3, "NUKIBridge ($name) - disabled" );
}
elsif ( $cmd eq 'del' ) {
readingsSingleUpdate ( $hash, 'state', 'active', 1 );
Log3($name, 3, "NUKIBridge ($name) - enabled");
readingsSingleUpdate( $hash, 'state', 'active', 1 );
Log3( $name, 3, "NUKIBridge ($name) - enabled" );
}
}
if ( $attrName eq 'disabledForIntervals' ) {
if ( $cmd eq 'set' ) {
Log3($name, 3, "NUKIBridge ($name) - enable disabledForIntervals");
readingsSingleUpdate ( $hash, 'state', 'Unknown', 1 );
Log3( $name, 3,
"NUKIBridge ($name) - enable disabledForIntervals" );
readingsSingleUpdate( $hash, 'state', 'Unknown', 1 );
}
elsif ( $cmd eq 'del' ) {
readingsSingleUpdate ( $hash, 'state', 'active', 1 );
Log3($name, 3, "NUKIBridge ($name) - delete disabledForIntervals");
readingsSingleUpdate( $hash, 'state', 'active', 1 );
Log3( $name, 3,
"NUKIBridge ($name) - delete disabledForIntervals" );
}
}
######################
#### webhook #########
return("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: 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")
if ( $attrVal and $attrName eq 'webhookFWinstance' and ( !defined( $defs{$attrVal} ) or $defs{$attrVal}{TYPE} ne 'FHEMWEB' ) );
return (
"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 ( $attrName =~ /^webhook.*/ ) {
my $webhookHttpHostname = ( $attrName eq 'webhookHttpHostname' ? $attrVal : AttrVal( $name, 'webhookHttpHostname', '' ) );
my $webhookFWinstance = ( $attrName eq 'webhookFWinstance' ? $attrVal : AttrVal( $name, 'webhookFWinstance', '' ) );
my $webhookHttpHostname =
( $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_PORT} = ( $attrName eq 'webhookPort' ? $attrVal : AttrVal( $name, 'webhookPort', InternalVal( $webhookFWinstance, 'PORT', '' )) );
$hash->{WEBHOOK_URI} = '/'
. 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_COUNTER} = 0;
if ( $webhookHttpHostname ne '' and $hash->{WEBHOOK_PORT} ne '' ) {
$hash->{WEBHOOK_URL} = 'http://' . $webhookHttpHostname . ':' . $hash->{WEBHOOK_PORT} . $hash->{WEBHOOK_URI};
my $url = 'http://' . $webhookHttpHostname . ':' . $hash->{WEBHOOK_PORT} . $hash->{WEBHOOK_URI};
$hash->{WEBHOOK_URL} =
'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");
NUKIBridge_Write($hash,'callback/add',$url,undef,undef )
if ( $init_done );
Log3( $name, 3, "NUKIBridge ($name) - URL ist: $url" );
NUKIBridge_Write( $hash, 'callback/add', $url, undef, undef )
if ($init_done);
$hash->{WEBHOOK_REGISTER} = 'sent';
}
else {
@ -328,7 +366,9 @@ sub NUKIBridge_addExtension($$$) {
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}{FUNC} = $func;
@ -343,87 +383,98 @@ sub NUKIBridge_removeExtension($) {
my $url = '/' . $link;
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};
}
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 );
NUKIBridge_Write($hash,"list",undef,undef,undef) if ( !IsDisabled($name) );
NUKIBridge_Write( $hash, "list", undef, undef, undef )
if ( !IsDisabled($name) );
return undef;
}
elsif ($cmd eq 'info') {
elsif ( $cmd eq 'info' ) {
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;
}
elsif (lc($cmd) eq 'fwupdate') {
elsif ( lc($cmd) eq 'fwupdate' ) {
return 'usage: fwUpdate' if ( @args != 0 );
NUKIBridge_CallBlocking($hash,"fwupdate",undef) if ( !IsDisabled($name) );
NUKIBridge_CallBlocking( $hash, "fwupdate", undef )
if ( !IsDisabled($name) );
return undef;
}
elsif ($cmd eq 'reboot') {
elsif ( $cmd eq 'reboot' ) {
return 'usage: reboot' if ( @args != 0 );
NUKIBridge_CallBlocking($hash,"reboot",undef) if ( !IsDisabled($name) );
NUKIBridge_CallBlocking( $hash, "reboot", undef )
if ( !IsDisabled($name) );
return undef;
}
elsif (lc($cmd) eq 'clearlog') {
elsif ( lc($cmd) eq 'clearlog' ) {
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 );
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 );
my $id = "id=" . (@args > 0 ? join( ' ', @args ) : 0);
my $resp = NUKIBridge_CallBlocking($hash,'callback/remove',$id)
my $id = "id=" . ( @args > 0 ? join( ' ', @args ) : 0 );
my $resp = NUKIBridge_CallBlocking( $hash, 'callback/remove', $id )
if ( !IsDisabled($name) );
if ( ( $resp->{success} eq 'true'
or $resp->{success} == 1)
if (
(
$resp->{success} eq 'true'
or $resp->{success} == 1
)
and !IsDisabled($name)
)
{
return('Success Callback ' . $id . ' removed');
return ( 'Success Callback ' . $id . ' removed' );
}
else {
return('remove Callback failed');
return ('remove Callback failed');
}
}
else {
my $list = '';
$list .= 'info:noArg getDeviceList:noArg callbackRemove:noArg ';
$list .= 'clearLog:noArg fwUpdate:noArg reboot:noArg factoryReset:noArg'
if ( ReadingsVal($name,'bridgeType','Software') eq 'Hardware' );
return('Unknown argument ' . $cmd . ', choose one of ' . $list);
if ( ReadingsVal( $name, 'bridgeType', 'Software' ) eq 'Hardware' );
return ( 'Unknown argument ' . $cmd . ', choose one of ' . $list );
}
}
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 );
NUKIBridge_getLogfile($hash) if ( !IsDisabled($name) );
}
elsif (lc($cmd) eq 'callbacklist') {
elsif ( lc($cmd) eq 'callbacklist' ) {
return 'usage: callbackList' if ( @args != 0 );
NUKIBridge_getCallbackList($hash) if ( !IsDisabled($name) );
@ -432,7 +483,7 @@ sub NUKIBridge_Get($@) {
my $list = '';
$list .= 'callbackList: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;
}
@ -444,20 +495,23 @@ sub NUKIBridge_GetCheckBridgeAlive($) {
my $name = $hash->{NAME};
RemoveInternalTimer($hash);
Log3($name, 4, "NUKIBridge ($name) - NUKIBridge_GetCheckBridgeAlive");
Log3( $name, 4, "NUKIBridge ($name) - NUKIBridge_GetCheckBridgeAlive" );
if ( !IsDisabled($name)
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($) {
@ -466,14 +520,16 @@ sub NUKIBridge_firstRun($) {
my $name = $hash->{NAME};
RemoveInternalTimer($hash);
NUKIBridge_Write($hash,'list',undef,undef,undef) if ( !IsDisabled($name) );
InternalTimer( gettimeofday()+15, 'NUKIBridge_GetCheckBridgeAlive', $hash);
NUKIBridge_Write( $hash, 'list', undef, undef, undef )
if ( !IsDisabled($name) );
InternalTimer( gettimeofday() + 15,
'NUKIBridge_GetCheckBridgeAlive', $hash );
return undef;
}
sub NUKIBridge_Write($@) {
my ($hash,$path,$lockAction,$nukiId,$deviceType) = @_;
my ( $hash, $path, $lockAction, $nukiId, $deviceType ) = @_;
my $name = $hash->{NAME};
my $host = $hash->{HOST};
@ -481,7 +537,7 @@ sub NUKIBridge_Write($@) {
my $token = $hash->{TOKEN};
my $uri = 'http://' . $hash->{HOST} . ':' . $port;
$uri .= '/' . $path if ( defined $path);
$uri .= '/' . $path if ( defined $path );
$uri .= '?token=' . $token if ( defined($token) );
$uri .= '&action=' . $lockActionsSmartLock{$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($$$) {
@ -533,43 +589,56 @@ sub NUKIBridge_Distribution($$$) {
my $dname = $dhash->{NAME};
Log3($name, 5, "NUKIBridge ($name) - Response JSON: $json");
Log3($name, 5, "NUKIBridge ($name) - Response ERROR: $err");
Log3($name, 5, "NUKIBridge ($name) - Response CODE: $param->{code}")
if ( defined($param->{code}) and ($param->{code}) );
Log3( $name, 5, "NUKIBridge ($name) - Response JSON: $json" );
Log3( $name, 5, "NUKIBridge ($name) - Response ERROR: $err" );
Log3( $name, 5, "NUKIBridge ($name) - Response CODE: $param->{code}" )
if ( defined( $param->{code} ) and ( $param->{code} ) );
readingsBeginUpdate($hash);
if ( defined( $err ) ) {
if ( defined($err) ) {
if ( $err ne '' ) {
if ($param->{endpoint} eq 'info') {
readingsBulkUpdate( $hash, 'state', 'not connected')
if ( $param->{endpoint} eq 'info' ) {
readingsBulkUpdate( $hash, 'state', 'not connected' )
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;
}
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 );
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' ) {
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 );
return;
}
readingsBulkUpdate( $hash, 'lastError', 'Internal error, ' .$param->{code} );
Log3($name, 4, "NUKIBridge ($name) - received http code " .$param->{code}." without any data after requesting");
readingsBulkUpdate( $hash, 'lastError',
'Internal error, ' . $param->{code} );
Log3( $name, 4,
"NUKIBridge ($name) - received http code "
. $param->{code}
. " without any data after requesting" );
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} ) ) {
@ -579,11 +648,11 @@ sub NUKIBridge_Distribution($$$) {
readingsBulkUpdate( $hash, 'lastError', 'action is undefined' )
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 );
Log3($name, 4, "NUKIBridge ($name) - nukiId is not known")
Log3( $name, 4, "NUKIBridge ($name) - nukiId is not known" )
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 );
readingsEndUpdate( $hash, 1 );
@ -593,18 +662,19 @@ sub NUKIBridge_Distribution($$$) {
readingsEndUpdate( $hash, 1 );
if ( $hash == $dhash ) {
NUKIBridge_ResponseProcessing($hash,$json,$param->{endpoint});
NUKIBridge_ResponseProcessing( $hash, $json, $param->{endpoint} );
}
else {
my $decode_json = eval{decode_json($json)};
if ($@){
Log3($name, 3, "NUKIBridge ($name) - JSON error while request: $@");
my $decode_json = eval { decode_json($json) };
if ($@) {
Log3( $name, 3,
"NUKIBridge ($name) - JSON error while request: $@" );
return;
}
$decode_json->{nukiId} = $param->{nukiId};
$json = encode_json($decode_json);
Dispatch($hash,$json,undef);
Dispatch( $hash, $json, undef );
}
$hash->{helper}->{iowrite} = 0
@ -614,34 +684,42 @@ sub NUKIBridge_Distribution($$$) {
}
sub NUKIBridge_ResponseProcessing($$$) {
my ($hash,$json,$path) = @_;
my ( $hash, $json, $path ) = @_;
my $name = $hash->{NAME};
my $decode_json;
if ( !$json ) {
Log3($name, 3, "NUKIBridge ($name) - empty answer received");
Log3( $name, 3, "NUKIBridge ($name) - empty answer received" );
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;
} elsif ( $json !~ m/^[\[{].*[}\]]$/ ) {
Log3($name, 3, "NUKIBridge ($name) - invalid json detected: $json");
return("NUKIBridge ($name) - invalid json detected: $json");
}
elsif ( $json !~ m/^[\[{].*[}\]]$/ ) {
Log3( $name, 3, "NUKIBridge ($name) - invalid json detected: $json" );
return ("NUKIBridge ($name) - invalid json detected: $json");
}
$decode_json = eval{decode_json($json)};
if ($@){
Log3($name, 3, "NUKIBridge ($name) - JSON error while request: $@");
$decode_json = eval { decode_json($json) };
if ($@) {
Log3( $name, 3, "NUKIBridge ($name) - JSON error while request: $@" );
return;
}
if ( (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')
if ( $path eq 'list'
or $path eq 'info' )
{
if (
(
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;
@ -653,49 +731,51 @@ sub NUKIBridge_ResponseProcessing($$$) {
my ( $json, $tail ) = NUKIBridge_ParseJSON( $hash, $buffer[1] );
while ($json) {
Log3($name, 5,
Log3( $name, 5,
"NUKIBridge ($name) - Decoding JSON message. Length: "
. length($json)
. " Content: "
. $json);
. $json );
Log3($name, 5,
Log3( $name, 5,
"NUKIBridge ($name) - Vor Sub: Laenge JSON: "
. length($json)
. " Content: "
. $json
. " Tail: "
. $tail);
. $tail );
Dispatch( $hash, $json, undef )
unless ( not defined($tail) and not($tail) );
( $json, $tail ) = NUKIBridge_ParseJSON( $hash, $tail );
Log3($name, 5,
Log3( $name, 5,
"NUKIBridge ($name) - Nach Sub: Laenge JSON: "
. length($json)
. " Content: "
. $json
. " Tail: "
. $tail);
. $tail );
}
}
if ( $path eq 'info' ) {
readingsBeginUpdate( $hash );
readingsBeginUpdate($hash);
readingsBulkUpdate( $hash, 'state', 'connected' );
Log3($name, 5, "NUKIBridge ($name) - Bridge ist online");
Log3( $name, 5, "NUKIBridge ($name) - Bridge ist online" );
readingsEndUpdate( $hash, 1 );
$hash->{helper}{aliveCount} = 0;
NUKIBridge_InfoProcessing($hash,$decode_json);
NUKIBridge_InfoProcessing( $hash, $decode_json );
}
}
else {
Log3($name, 5, "NUKIBridge ($name) - Rückgabe Path nicht korrekt:
$json");
Log3(
$name, 5, "NUKIBridge ($name) - Rückgabe Path nicht korrekt:
$json"
);
return;
}
@ -712,49 +792,53 @@ sub NUKIBridge_CGI() {
$name = $hash->{NAME};
}
return('NUKIBridge WEBHOOK - No IODev found')
unless (defined($hash) and defined($name) );
return ('NUKIBridge WEBHOOK - No IODev found')
unless ( defined($hash) and defined($name) );
my $json = (split('&',$request,2))[1];
my $json = ( split( '&', $request, 2 ) )[1];
if ( !$json ) {
Log3($name, 3, "NUKIBridge WEBHOOK ($name) - empty message received");
Log3( $name, 3, "NUKIBridge WEBHOOK ($name) - empty message received" );
return undef;
}
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;
}
elsif ( $json !~ m/^[\[{].*[}\]]$/ ) {
Log3($name, 3, "NUKIBridge WEBHOOK ($name) - invalid json detected: $json");
return("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");
}
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/^\{.*\}$/ ) {
$hash->{WEBHOOK_COUNTER}++;
$hash->{WEBHOOK_LAST} = TimeNow();
Log3($name, 4, "NUKIBridge WEBHOOK ($name) - Received webhook for
matching NukiId at device $name");
Log3(
$name, 4, "NUKIBridge WEBHOOK ($name) - Received webhook for
matching NukiId at device $name"
);
Dispatch($hash,$json,undef);
Dispatch( $hash, $json, undef );
return ( undef, undef );
}
# no data received
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($$) {
my ($hash,$decode_json) = @_;
my ( $hash, $decode_json ) = @_;
my $name = $hash->{NAME};
@ -770,48 +854,54 @@ sub NUKIBridge_InfoProcessing($$) {
);
readingsBeginUpdate($hash);
readingsBulkUpdate($hash,'appVersion',$decode_json->{versions}->{appVersion});
readingsBulkUpdate($hash,'firmwareVersion',$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,'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);
readingsBulkUpdate( $hash, 'appVersion',
$decode_json->{versions}->{appVersion} );
readingsBulkUpdate( $hash, 'firmwareVersion',
$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, '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($) {
my ($hash) = @_;
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 ) {
Log3($name, 4, "NUKIBridge ($name) - created Table with log file");
if ( ref($decode_json) eq 'ARRAY' and scalar( @{$decode_json} ) > 0 ) {
Log3( $name, 4, "NUKIBridge ($name) - created Table with log file" );
my $ret = '<html><table width=100%><tr><td>';
$ret .= '<table class="block wide">';
foreach my $logs (@{$decode_json}) {
foreach my $logs ( @{$decode_json} ) {
$ret .= '<tr class="odd">';
if ($logs->{timestamp}) {
if ( $logs->{timestamp} ) {
$ret .= '<td><b>timestamp:</b> </td>';
$ret .= '<td>' . $logs->{timestamp} . '</td>';
$ret .= '<td> </td>';
}
if ($logs->{type}) {
if ( $logs->{type} ) {
$ret .= '<td><b>type:</b> </td>';
$ret .= '<td>' . $logs->{type} . '</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 'timestamp' );
@ -834,17 +924,20 @@ sub NUKIBridge_getCallbackList($) {
my ($hash) = @_;
my $name = $hash->{NAME};
my $decode_json = NUKIBridge_CallBlocking($hash,'callback/list',undef);
my $decode_json = NUKIBridge_CallBlocking( $hash, 'callback/list', undef );
return
unless ( ref($decode_json) eq 'HASH' );
Log3($name, 4, "NUKIBridge ($name) - Callback data is collected and
processed");
Log3(
$name, 4, "NUKIBridge ($name) - Callback data is collected and
processed"
);
if ( ref($decode_json->{callbacks}) eq 'ARRAY' and
scalar(@{$decode_json->{callbacks}}) > 0 ) {
Log3($name, 4, "NUKIBridge ($name) - created Table with log file");
if ( ref( $decode_json->{callbacks} ) eq 'ARRAY'
and scalar( @{ $decode_json->{callbacks} } ) > 0 )
{
Log3( $name, 4, "NUKIBridge ($name) - created Table with log file" );
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 .= '</tr>';
foreach my $cb (@{$decode_json->{callbacks}}) {
foreach my $cb ( @{ $decode_json->{callbacks} } ) {
$ret .= '<td>' . $cb->{id} . '</td>';
$ret .= '<td> </td>';
@ -873,53 +966,54 @@ scalar(@{$decode_json->{callbacks}}) > 0 ) {
}
sub NUKIBridge_CallBlocking($@) {
my ($hash,$path,$obj) = @_;
my ( $hash, $path, $obj ) = @_;
my $name = $hash->{NAME};
my $host = $hash->{HOST};
my $port = $hash->{PORT};
my $token = $hash->{TOKEN};
my $url = 'http://' . $hash->{HOST} . ':' . $port;
$url .= '/' . $path
if ( defined $path);
if ( defined $path );
$url .= '?token=' . $token
if ( defined($token) );
$url .= '&' . $obj
if ( defined($obj) );
my($err,$data) = HttpUtils_BlockingGet({
my ( $err, $data ) = HttpUtils_BlockingGet(
{
url => $url,
timeout => 3,
method => "GET",
header => "Content-Type: application/json",
});
}
);
if ( !$data ) {
Log3($name, 3, "NUKIDevice ($name) - empty answer received for $url");
Log3( $name, 3, "NUKIDevice ($name) - empty answer received for $url" );
return undef;
}
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;
}
elsif ( $data !~ m/^[\[{].*[}\]]$/ and $path ne "log" ) {
Log3($name, 3, "NUKIDevice ($name) - invalid json detected for $url: $data");
return("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");
}
my $decode_json = eval{decode_json($data)};
if ($@){
Log3($name, 3, "NUKIBridge ($name) - JSON error while request: $@");
my $decode_json = eval { decode_json($data) };
if ($@) {
Log3( $name, 3, "NUKIBridge ($name) - JSON error while request: $@" );
return;
}
return undef if ( !$decode_json );
Log3($name, 5, "NUKIBridge ($name) - Data: $data");
Log3($name, 4, "NUKIBridge ($name) - Blocking HTTP Query finished");
Log3( $name, 5, "NUKIBridge ($name) - Data: $data" );
Log3( $name, 4, "NUKIBridge ($name) - Blocking HTTP Query finished" );
return ($decode_json);
}
@ -936,15 +1030,15 @@ sub NUKIBridge_ParseJSON($$) {
foreach my $c ( split //, $buffer ) {
if ( $open == $close and $open > 0 ) {
$tail .= $c;
Log3($name, 5,
"NUKIBridge ($name) - $open == $close and $open > 0");
Log3( $name, 5,
"NUKIBridge ($name) - $open == $close and $open > 0" );
}
elsif ( ( $open == $close ) and ( $c ne '{' ) ) {
Log3($name, 5,
"NUKIBridge ($name) - Garbage character before message: "
. $c);
Log3( $name, 5,
"NUKIBridge ($name) - Garbage character before message: "
. $c );
}
else {
@ -970,20 +1064,12 @@ sub NUKIBridge_ParseJSON($$) {
}
}
Log3($name, 5,
"NUKIBridge ($name) - return msg: $msg and tail: $tail");
Log3( $name, 5, "NUKIBridge ($name) - return msg: $msg and tail: $tail" );
return ( $msg, $tail );
}
1;
=pod
=item device
=item summary Modul to control the Nuki Smartlock's over the Nuki Bridge.

View File

@ -25,7 +25,6 @@
#
###############################################################################
package main;
use strict;
@ -102,11 +101,7 @@ if ($@) {
}
}
my $version = '0.7.11';
my $version = '0.7.15';
# Declare functions
sub NUKIDevice_Initialize($);
@ -118,7 +113,6 @@ sub NUKIDevice_GetUpdate($);
sub NUKIDevice_Parse($$);
sub NUKIDevice_WriteReadings($$);
my %deviceTypes = (
0 => 'smartlock',
2 => 'opener'
@ -126,10 +120,6 @@ my %deviceTypes = (
my %deviceTypeIds = reverse(%deviceTypes);
sub NUKIDevice_Initialize($) {
my ($hash) = @_;
@ -141,13 +131,13 @@ sub NUKIDevice_Initialize($) {
$hash->{AttrFn} = 'NUKIDevice_Attr';
$hash->{ParseFn} = 'NUKIDevice_Parse';
$hash->{AttrList} = 'IODev '.
'model:opener,smartlock '.
'disable:1 '.
$readingFnAttributes;
$hash->{AttrList} =
'IODev '
. 'model:opener,smartlock '
. '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};
$hash->{VERSION} = $version;
}
@ -157,29 +147,28 @@ sub NUKIDevice_Define($$) {
my ( $hash, $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 $nukiId = $a[2];
my $deviceType = (defined $a[3]) ? $a[3] : 0;
my $deviceType = ( defined $a[3] ) ? $a[3] : 0;
$hash->{NUKIID} = $nukiId;
$hash->{DEVICETYPE} = (defined $deviceType) ? $deviceType : 0;
$hash->{DEVICETYPE} = ( defined $deviceType ) ? $deviceType : 0;
$hash->{VERSION} = $version;
$hash->{STATE} = 'Initialized';
my $iodev = AttrVal( $name, 'IODev', 'none' );
AssignIoPort( $hash, $iodev ) if ( !$hash->{IODev} );
if ( defined( $hash->{IODev}->{NAME} ) ) {
Log3($name, 3, "NUKIDevice ($name) - I/O device is "
. $hash->{IODev}->{NAME});
Log3( $name, 3,
"NUKIDevice ($name) - I/O device is " . $hash->{IODev}->{NAME} );
}
else {
Log3($name, 1, "NUKIDevice ($name) - no I/O device");
Log3( $name, 1, "NUKIDevice ($name) - no I/O device" );
}
$iodev = $hash->{IODev}->{NAME};
@ -187,23 +176,29 @@ sub NUKIDevice_Define($$) {
my $d = $modules{NUKIDevice}{defptr}{$nukiId};
return
'NUKIDevice device ' . $name . ' on NUKIBridge ' . $iodev . ' already defined.'
'NUKIDevice device '
. $name
. ' on NUKIBridge '
. $iodev
. ' already defined.'
if ( defined($d)
and $d->{IODev} == $hash->{IODev}
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 ( AttrVal($name,'room','none') eq 'none');
CommandAttr(undef,$name . ' model ' . $deviceTypes{$deviceType})
if ( AttrVal($name,'model','none') eq 'none');
if ( $init_done ) {
InternalTimer( gettimeofday()+int(rand(10)), "NUKIDevice_GetUpdate", $hash);
} else {
InternalTimer( gettimeofday()+15+int(rand(5)), "NUKIDevice_GetUpdate", $hash);
if ($init_done) {
InternalTimer( gettimeofday() + int( rand(10) ),
"NUKIDevice_GetUpdate", $hash );
}
else {
InternalTimer( gettimeofday() + 15 + int( rand(5) ),
"NUKIDevice_GetUpdate", $hash );
}
$modules{NUKIDevice}{defptr}{$nukiId} = $hash;
@ -219,8 +214,8 @@ sub NUKIDevice_Undef($$) {
RemoveInternalTimer($hash);
Log3($name, 3, "NUKIDevice ($name) - undefined with NukiId: $nukiId");
delete($modules{NUKIDevice}{defptr}{$nukiId});
Log3( $name, 3, "NUKIDevice ($name) - undefined with NukiId: $nukiId" );
delete( $modules{NUKIDevice}{defptr}{$nukiId} );
return undef;
}
@ -233,29 +228,31 @@ sub NUKIDevice_Attr(@) {
if ( $attrName eq 'disable' ) {
if ( $cmd eq 'set' and $attrVal == 1 ) {
readingsSingleUpdate ( $hash, 'state', 'disabled', 1 );
Log3($name, 3, "NUKIDevice ($name) - disabled");
readingsSingleUpdate( $hash, 'state', 'disabled', 1 );
Log3( $name, 3, "NUKIDevice ($name) - disabled" );
}
elsif ( $cmd eq 'del' ) {
readingsSingleUpdate ( $hash, 'state', 'active', 1 );
Log3($name, 3, "NUKIDevice ($name) - enabled");
readingsSingleUpdate( $hash, 'state', 'active', 1 );
Log3( $name, 3, "NUKIDevice ($name) - enabled" );
}
}
elsif ( $attrName eq 'disabledForIntervals' ) {
if ( $cmd eq 'set' ) {
Log3($name, 3, "NUKIDevice ($name) - enable disabledForIntervals");
readingsSingleUpdate ( $hash, 'state', 'Unknown', 1 );
Log3( $name, 3,
"NUKIDevice ($name) - enable disabledForIntervals" );
readingsSingleUpdate( $hash, 'state', 'Unknown', 1 );
}
elsif ( $cmd eq 'del' ) {
readingsSingleUpdate ( $hash, 'state', 'active', 1 );
Log3($name, 3, "NUKIDevice ($name) - delete disabledForIntervals");
readingsSingleUpdate( $hash, 'state', 'active', 1 );
Log3( $name, 3,
"NUKIDevice ($name) - delete disabledForIntervals" );
}
}
elsif ( $attrName eq 'model' ) {
if ( $cmd eq 'set' ) {
Log3($name, 3, "NUKIDevice ($name) - change model");
Log3( $name, 3, "NUKIDevice ($name) - change model" );
$hash->{DEVICETYPE} = $deviceTypeIds{$attrVal};
}
}
@ -264,28 +261,30 @@ sub NUKIDevice_Attr(@) {
}
sub NUKIDevice_Set($$@) {
my ($hash, $name, @aa) = @_;
my ( $hash, $name, @aa ) = @_;
my ($cmd, @args) = @aa;
my ( $cmd, @args ) = @aa;
my $lockAction;
if ( lc($cmd) eq 'statusrequest' ) {
return('usage: statusRequest') if ( @args != 0 );
return ('usage: statusRequest') if ( @args != 0 );
NUKIDevice_GetUpdate($hash);
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) ) {
$hash->{IODev}->{helper}->{iowrite} = 1
if ( $hash->{IODev}->{helper}->{iowrite} == 0 );
IOWrite($hash,$cmd,undef,$hash->{NUKIID},$hash->{DEVICETYPE});
IOWrite( $hash, $cmd, undef, $hash->{NUKIID}, $hash->{DEVICETYPE} );
}
return undef;
} elsif ( $cmd eq 'lock'
}
elsif ($cmd eq 'lock'
or lc($cmd) eq 'deactivaterto'
or $cmd eq 'unlock'
or lc($cmd) eq 'activaterto'
@ -294,27 +293,30 @@ sub NUKIDevice_Set($$@) {
or lc($cmd) eq 'lockngo'
or lc($cmd) eq 'activatecontinuousmode'
or lc($cmd) eq 'lockngowithunlatch'
or lc($cmd) eq 'deactivatecontinuousmode'
)
or lc($cmd) eq 'deactivatecontinuousmode' )
{
return('usage: ' . $cmd)
return ( 'usage: ' . $cmd )
if ( @args != 0 );
$lockAction = $cmd;
} else {
}
else {
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 );
$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 );
return('Unknown argument ' . $cmd . ', choose one of ' . $list);
return ( 'Unknown argument ' . $cmd . ', choose one of ' . $list );
}
$hash->{helper}{lockAction} = $lockAction;
$hash->{IODev}->{helper}->{iowrite} = 1
if ( $hash->{IODev}->{helper}->{iowrite} == 0 );
IOWrite($hash,"lockAction",$lockAction,$hash->{NUKIID},$hash->{DEVICETYPE});
IOWrite( $hash, "lockAction", $lockAction, $hash->{NUKIID},
$hash->{DEVICETYPE} );
return undef;
}
@ -326,40 +328,40 @@ sub NUKIDevice_GetUpdate($) {
RemoveInternalTimer($hash);
IOWrite($hash, 'lockState', undef, $hash->{NUKIID}, $hash->{DEVICETYPE} )
IOWrite( $hash, 'lockState', undef, $hash->{NUKIID}, $hash->{DEVICETYPE} )
if ( !IsDisabled($name) );
Log3($name, 5, "NUKIDevice ($name) - NUKIDevice_GetUpdate Call IOWrite")
Log3( $name, 5, "NUKIDevice ($name) - NUKIDevice_GetUpdate Call IOWrite" )
if ( !IsDisabled($name) );
return undef;
}
sub NUKIDevice_Parse($$) {
my($hash,$json) = @_;
my ( $hash, $json ) = @_;
my $name = $hash->{NAME};
Log3($name, 5, "NUKIDevice ($name) - Parse with result: $json");
Log3( $name, 5, "NUKIDevice ($name) - Parse with result: $json" );
#########################################
####### Errorhandling #############
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";
}
#########################################
#### verarbeiten des JSON Strings #######
my $decode_json = eval{ decode_json($json) };
if ($@){
Log3($name, 3, "NUKIDevice ($name) - JSON error while request: $@");
my $decode_json = eval { decode_json($json) };
if ($@) {
Log3( $name, 3, "NUKIDevice ($name) - JSON error while request: $@" );
return;
}
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;
}
@ -369,52 +371,60 @@ sub NUKIDevice_Parse($$) {
my $name = $hash->{NAME};
NUKIDevice_WriteReadings( $hash, $decode_json );
Log3($name, 4,
"NUKIDevice ($name) - find logical device: $hash->{NAME}");
Log3( $name, 4,
"NUKIDevice ($name) - find logical device: $hash->{NAME}" );
##################
## Zwischenlösung so für die Umstellung, kann später gelöscht werden
if ( AttrVal($name,'model','') eq '' ) {
CommandDefMod(undef,$name . ' NUKIDevice ' . $hash->{NUKIID} . ' ' . $decode_json->{deviceType});
CommandAttr(undef,$name . ' model ' . $deviceTypes{$decode_json->{deviceType}});
Log3($name, 2, "NUKIDevice ($name) - redefined Defmod");
if ( AttrVal( $name, 'model', '' ) eq '' ) {
CommandDefMod( undef,
$name
. ' NUKIDevice '
. $hash->{NUKIID} . ' '
. $decode_json->{deviceType} );
CommandAttr( undef,
$name
. ' model '
. $deviceTypes{ $decode_json->{deviceType} } );
Log3( $name, 2, "NUKIDevice ($name) - redefined Defmod" );
}
return $hash->{NAME};
}
else {
Log3($name, 3,
Log3( $name, 3,
"NUKIDevice ($name) - autocreate new device "
. makeDeviceName( $decode_json->{name} )
. " with nukiId $decode_json->{nukiId}, model $decode_json->{deviceType}");
. " with nukiId $decode_json->{nukiId}, model $decode_json->{deviceType}"
);
return
'UNDEFINED '
. makeDeviceName( $decode_json->{name} )
. " 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($$) {
my ($hash,$decode_json) = @_;
my ( $hash, $decode_json ) = @_;
my $name = $hash->{NAME};
############################
#### Status des Smartlock
my $battery;
if ( defined($decode_json->{batteryCritical}) ) {
if ( defined( $decode_json->{batteryCritical} ) ) {
if ( $decode_json->{batteryCritical} eq 'false'
or $decode_json->{batteryCritical} == 0 )
{
$battery = 'ok';
} elsif ( $decode_json->{batteryCritical} eq 'true'
}
elsif ($decode_json->{batteryCritical} eq 'true'
or $decode_json->{batteryCritical} == 1 )
{
$battery = "low";
@ -423,28 +433,34 @@ sub NUKIDevice_WriteReadings($$) {
readingsBeginUpdate($hash);
if ( defined($hash->{helper}{lockAction}) ) {
my ($state,$lockState);
if ( defined( $hash->{helper}{lockAction} ) ) {
my ( $state, $lockState );
if ( defined($decode_json->{success})
and ($decode_json->{success} eq 'true'
or $decode_json->{success} == 1)
if (
defined( $decode_json->{success} )
and ( $decode_json->{success} eq 'true'
or $decode_json->{success} == 1 )
)
{
$state = $hash->{helper}{lockAction};
$lockState = $hash->{helper}{lockAction};
IOWrite($hash, "lockState", undef, $hash->{NUKIID} )
if ( ReadingsVal($hash->{IODev}->{NAME},'bridgeType','Software') eq 'Software' );
IOWrite( $hash, "lockState", undef, $hash->{NUKIID} )
if (
ReadingsVal( $hash->{IODev}->{NAME}, 'bridgeType', 'Software' )
eq 'Software' );
} elsif ( defined($decode_json->{success})
and ($decode_json->{success} eq 'false'
or $decode_json->{success} == 0)
}
elsif (
defined( $decode_json->{success} )
and ( $decode_json->{success} eq 'false'
or $decode_json->{success} == 0 )
)
{
$state = $deviceTypes{$hash->{DEVICETYPE}} . ' response error';
IOWrite($hash, 'lockState', undef, $hash->{NUKIID}, $hash->{DEVICETYPE} );
$state = $deviceTypes{ $hash->{DEVICETYPE} } . ' response error';
IOWrite( $hash, 'lockState', undef, $hash->{NUKIID},
$hash->{DEVICETYPE} );
}
readingsBulkUpdate( $hash, 'state', $state );
@ -452,10 +468,13 @@ sub NUKIDevice_WriteReadings($$) {
readingsBulkUpdate( $hash, 'success', $decode_json->{success} );
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, 'state', $decode_json->{stateName} );
readingsBulkUpdate( $hash, 'batteryState', $battery );
@ -465,7 +484,7 @@ sub NUKIDevice_WriteReadings($$) {
readingsBulkUpdate( $hash, 'rssi', $decode_json->{rssi} );
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 );
@ -473,17 +492,8 @@ sub NUKIDevice_WriteReadings($$) {
return undef;
}
1;
=pod
=item device
=item summary Modul to control the Nuki Smartlock's