Merge pull request #4 from Thyraz/Opener

Opener
This commit is contained in:
Leon Gaultier 2020-01-04 08:17:22 +01:00 committed by GitHub
commit 98f4cce479
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 218 additions and 198 deletions

208
73_NUKIBridge.pm Normal file → Executable file
View File

@ -46,17 +46,25 @@ use JSON;
use HttpUtils;
my $version = "0.6.4";
my $bridgeapi = "1.6";
my $version = "0.6.5";
my $bridgeapi = "1.9";
my %lockActions = (
'unlock' => 1,
'lock' => 2,
'unlatch' => 3,
'locknGo' => 4,
'locknGoWithUnlatch' => 5
my %lockActionsSmartLock = (
'unlock' => 1,
'lock' => 2,
'unlatch' => 3,
'locknGo' => 4,
'locknGoWithUnlatch' => 5
);
my %lockActionsOpener = (
'activateRto' => 1,
'deactivateRto' => 2,
'electricStrikeActuation' => 3,
'activateContinuousMode' => 4,
'deactivateContinuousMode' => 5
);
@ -66,13 +74,16 @@ sub NUKIBridge_Define ($$);
sub NUKIBridge_Undef ($$);
sub NUKIBridge_Read($@);
sub NUKIBridge_Attr(@);
sub NUKIBridge_addExtension($$$);
sub NUKIBridge_removeExtension($);
sub NUKIBridge_Set($@);
sub NUKIBridge_Get($@);
sub NUKIBridge_GetCheckBridgeAlive($);
sub NUKIBridge_firstRun($);
sub NUKIBridge_Call($$$$$);
sub NUKIBridge_Call($$$$$$);
sub NUKIBridge_Distribution($$$);
sub NUKIBridge_ResponseProcessing($$$);
sub NUKIBridge_CGI();
sub NUKIBridge_Autocreate($$;$);
sub NUKIBridge_InfoProcessing($$);
sub NUKIBridge_getLogfile($);
@ -92,6 +103,7 @@ sub NUKIBridge_Initialize($) {
$hash->{WriteFn} = "NUKIBridge_Read";
$hash->{Clients} = ":NUKIDevice:";
my $webhookFWinstance = join( ",", devspec2array('TYPE=FHEMWEB:FILTER=TEMPORARY!=1') );
# Consumer
$hash->{SetFn} = "NUKIBridge_Set";
@ -100,6 +112,8 @@ sub NUKIBridge_Initialize($) {
$hash->{UndefFn} = "NUKIBridge_Undef";
$hash->{AttrFn} = "NUKIBridge_Attr";
$hash->{AttrList} = "disable:1 ".
"webhookFWinstance:$webhookFWinstance ".
"webhookHttpHostname ".
$readingFnAttributes;
@ -111,8 +125,8 @@ sub NUKIBridge_Initialize($) {
sub NUKIBridge_Read($@) {
my ($hash,$chash,$name,$path,$lockAction,$nukiId)= @_;
NUKIBridge_Call($hash,$chash,$path,$lockAction,$nukiId );
my ($hash,$chash,$name,$path,$lockAction,$nukiId,$deviceType)= @_;
NUKIBridge_Call($hash,$chash,$path,$lockAction,$nukiId,$deviceType );
}
@ -138,12 +152,20 @@ sub NUKIBridge_Define($$) {
$hash->{VERSION} = $version;
$hash->{BRIDGEAPI} = $bridgeapi;
$hash->{helper}{aliveCount} = 0;
my $infix = "NUKIBridge";
Log3 $name, 3, "NUKIBridge ($name) - defined with host $host on port $port, Token $token";
$attr{$name}{room} = "NUKI" if( !defined( $attr{$name}{room} ) );
if ( NUKIBridge_addExtension( $name, "NUKIBridge_CGI", $infix . "-" . $host ) ) {
$hash->{fhem}{infix} = $infix;
}
$hash->{WEBHOOK_REGISTER} = "unregistered";
readingsSingleUpdate($hash, 'state', 'Initialized', 1 );
RemoveInternalTimer($hash);
@ -166,6 +188,10 @@ sub NUKIBridge_Undef($$) {
my $host = $hash->{HOST};
my $name = $hash->{NAME};
if ( defined( $hash->{fhem}{infix} ) ) {
NUKIBridge_removeExtension( $hash->{fhem}{infix} );
}
RemoveInternalTimer( $hash );
delete $modules{NUKIBridge}{defptr}{$hash->{HOST}};
@ -204,10 +230,72 @@ sub NUKIBridge_Attr(@) {
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 && $attrName eq "webhookHttpHostname" && $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 && $attrName eq "webhookFWinstance" && ( !defined( $defs{$attrVal} ) || $defs{$attrVal}{TYPE} ne "FHEMWEB" ) );
return "Invalid value for attribute $attrName: needs to be an integer value" if ( $attrVal && $attrName eq "webhookPort" && $attrVal !~ /^\d+$/ );
if ( $attrName =~ /^webhook.*/ ) {
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_URL} = "";
$hash->{WEBHOOK_COUNTER} = "0";
if ( $webhookHttpHostname ne "" && $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};
Log3 $name, 3, "NUKIBridge ($name) - URL ist: $url";
NUKIBridge_Call($hash,$hash,"callback/add",$url,undef,undef ) if( $init_done );
$hash->{WEBHOOK_REGISTER} = "sent";
} else {
$hash->{WEBHOOK_REGISTER} = "incomplete_attributes";
}
}
return undef;
}
sub NUKIBridge_addExtension($$$) {
my ( $name, $func, $link ) = @_;
my $url = "/$link";
Log3 $name, 2, "NUKIBridge ($name) - Registering NUKIBridge for webhook URI $url ...";
$data{FWEXT}{$url}{deviceName} = $name;
$data{FWEXT}{$url}{FUNC} = $func;
$data{FWEXT}{$url}{LINK} = $link;
return 1;
}
sub NUKIBridge_removeExtension($) {
my ($link) = @_;
my $url = "/$link";
my $name = $data{FWEXT}{$url}{deviceName};
Log3 $name, 2, "NUKIBridge ($name) - Unregistering NUKIBridge for webhook URL $url...";
delete $data{FWEXT}{$url};
}
sub NUKIBridge_Set($@) {
my ($hash, $name, $cmd, @args) = @_;
@ -217,14 +305,14 @@ sub NUKIBridge_Set($@) {
if($cmd eq 'autocreate') {
return "usage: autocreate" if( @args != 0 );
NUKIBridge_Call($hash,$hash,"list",undef,undef) if( !IsDisabled($name) );
NUKIBridge_Call($hash,$hash,"list",undef,undef,undef) if( !IsDisabled($name) );
return undef;
} elsif($cmd eq 'info') {
return "usage: statusRequest" if( @args != 0 );
NUKIBridge_Call($hash,$hash,"info",undef,undef) if( !IsDisabled($name) );
NUKIBridge_Call($hash,$hash,"info",undef,undef,undef) if( !IsDisabled($name) );
return undef;
@ -257,7 +345,7 @@ sub NUKIBridge_Set($@) {
my $id = "id=" . join( " ", @args );
my $resp = NUKIBridge_CallBlocking($hash,"callback/remove",$id) if( !IsDisabled($name) );
if( $resp->{success} eq "true" and !IsDisabled($name) ) {
if( ($resp->{success} eq "true" or $resp->{success} == 1) and !IsDisabled($name) ) {
return "Success Callback $id removed";
} else {
return "remove Callback failed";
@ -304,7 +392,7 @@ sub NUKIBridge_GetCheckBridgeAlive($) {
if( !IsDisabled($name) ) {
NUKIBridge_Call($hash,$hash,'info',undef,undef);
NUKIBridge_Call($hash,$hash,'info',undef,undef,undef);
Log3 $name, 4, "NUKIBridge ($name) - run NUKIBridge_Call";
}
@ -321,15 +409,15 @@ sub NUKIBridge_firstRun($) {
RemoveInternalTimer($hash);
NUKIBridge_Call($hash,$hash,'list',undef,undef) if( !IsDisabled($name) );
NUKIBridge_Call($hash,$hash,'list',undef,undef,undef) if( !IsDisabled($name) );
InternalTimer( gettimeofday()+15, 'NUKIBridge_GetCheckBridgeAlive', $hash, 1 );
return undef;
}
sub NUKIBridge_Call($$$$$) {
sub NUKIBridge_Call($$$$$$) {
my ($hash,$chash,$path,$lockAction,$nukiId) = @_;
my ($hash,$chash,$path,$lockAction,$nukiId,$deviceType) = @_;
my $name = $hash->{NAME};
my $host = $hash->{HOST};
@ -340,9 +428,11 @@ sub NUKIBridge_Call($$$$$) {
my $uri = "http://" . $hash->{HOST} . ":" . $port;
$uri .= "/" . $path if( defined $path);
$uri .= "?token=" . $token if( defined($token) );
$uri .= "&action=" . $lockActions{$lockAction} if( defined($lockAction) and $path ne "callback/add" );
$uri .= "&action=" . $lockActionsSmartLock{$lockAction} if( defined($lockAction) and $path ne "callback/add" and $chash->{DEVICETYPE} == 0 );
$uri .= "&action=" . $lockActionsOpener{$lockAction} if( defined($lockAction) and $path ne "callback/add" and $chash->{DEVICETYPE} == 2 );
$uri .= "&url=" . $lockAction if( defined($lockAction) and $path eq "callback/add" );
$uri .= "&nukiId=" . $nukiId if( defined($nukiId) );
$uri .= "&deviceType=" . $deviceType if( defined($deviceType) );
HttpUtils_NonblockingGet(
@ -486,7 +576,7 @@ sub NUKIBridge_ResponseProcessing($$$) {
if( ref($decode_json) eq "ARRAY" and scalar(@{$decode_json}) > 0 and $path eq "list" ) {
NUKIBridge_Autocreate($hash,$decode_json);
NUKIBridge_Call($hash,$hash,"info",undef,undef) if( !IsDisabled($name) );
NUKIBridge_Call($hash,$hash,"info",undef,undef,undef) if( !IsDisabled($name) );
}
elsif( $path eq "info" ) {
@ -507,6 +597,75 @@ sub NUKIBridge_ResponseProcessing($$$) {
return undef;
}
sub NUKIBridge_CGI() {
my ($request) = @_;
my $hash;
my $name;
my $nukiId;
# data received
# Testaufruf:
# curl --data '{"nukiId": 123456, "state": 1,"stateName": "locked", "batteryCritical": false}' http://10.6.6.20:8083/fhem/NUKIDevice-123456
# wget --post-data '{"nukiId": 123456, "state": 1,"stateName": "locked", "batteryCritical": false}' http://10.6.6.20:8083/fhem/NUKIDevice-123456
my $header = join("\n", @FW_httpheader);
my ($first,$json) = split("&",$request,2);
if( !$json ) {
Log3 $name, 3, "NUKIBridge ($name) - empty message received";
return undef;
} 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";
}
Log3 $name, 5, "NUKIBridge ($name) - Webhook received with JSON: $json";
my $decode_json = eval{decode_json($json)};
if($@){
Log3 $name, 3, "NUKIBridge ($name) - JSON error while request: $@";
return;
}
if( ref($decode_json) eq "HASH" ) {
$hash->{WEBHOOK_COUNTER}++;
$hash->{WEBHOOK_LAST} = TimeNow();
if ( defined( $modules{NUKIDevice}{defptr} ) ) {
while ( my ( $key, $value ) = each %{ $modules{NUKIDevice}{defptr} } ) {
$hash = $modules{NUKIDevice}{defptr}{$key};
$name = $hash->{NAME};
$nukiId = InternalVal( $name, "NUKIID", undef );
next if ( !$nukiId or $nukiId ne $decode_json->{nukiId} );
Log3 $name, 4, "NUKIBridge ($name) - Received webhook for matching NukiId at device $name";
NUKIDevice_Parse($hash,$json);
}
}
return ( undef, undef );
}
# no data received
else {
Log3 undef, 4, "NUKIBridge - received malformed request\n$request";
}
return ( "text/plain; charset=utf-8", "Call failure: " . $request );
}
sub NUKIBridge_Autocreate($$;$) {
my ($hash,$decode_json,$force)= @_;
@ -522,6 +681,7 @@ sub NUKIBridge_Autocreate($$;$) {
my $autocreated = 0;
my $nukiSmartlock;
my $nukiId;
my $nukiType;
my $nukiName;
readingsBeginUpdate($hash);
@ -529,6 +689,7 @@ sub NUKIBridge_Autocreate($$;$) {
foreach $nukiSmartlock (@{$decode_json}) {
$nukiId = $nukiSmartlock->{nukiId};
$nukiType = $nukiSmartlock->{deviceType};
$nukiName = $nukiSmartlock->{name};
@ -539,7 +700,8 @@ sub NUKIBridge_Autocreate($$;$) {
}
my $devname = "NUKIDevice" . $nukiId;
my $define= "$devname NUKIDevice $nukiId IODev=$name";
my $define= "$devname NUKIDevice $nukiId IODev=$name $nukiType";
Log3 $name, 3, "NUKIDevice ($name) - create new device '$devname' for address '$nukiId'";
my $cmdret= CommandDefine(undef,$define);
@ -846,6 +1008,8 @@ sub NUKIBridge_CallBlocking($$$) {
<b>Attributes</b>
<ul>
<li>disable - disables the Nuki Bridge</li>
<li>webhookFWinstance - Webinstanz of the Callback</li>
<li>webhookHttpHostname - IP or FQDN of the FHEM Server Callback</li>
<br>
</ul>
</ul>
@ -918,6 +1082,8 @@ sub NUKIBridge_CallBlocking($$$) {
<b>Attribute</b>
<ul>
<li>disable - deaktiviert die Nuki Bridge</li>
<li>webhookFWinstance - zu verwendene Webinstanz für den Callbackaufruf</li>
<li>webhookHttpHostname - IP oder FQDN vom FHEM Server für den Callbackaufruf</li>
<br>
</ul>
</ul>

208
74_NUKIDevice.pm Normal file → Executable file
View File

@ -33,7 +33,7 @@ use warnings;
use JSON;
my $version = "0.6.4";
my $version = "0.6.5";
@ -43,14 +43,11 @@ sub NUKIDevice_Initialize($);
sub NUKIDevice_Define($$);
sub NUKIDevice_Undef($$);
sub NUKIDevice_Attr(@);
sub NUKIDevice_addExtension($$$);
sub NUKIDevice_removeExtension($);
sub NUKIDevice_Set($$@);
sub NUKIDevice_GetUpdate($);
sub NUKIDevice_ReadFromNUKIBridge($@);
sub NUKIDevice_Parse($$);
sub NUKIDevice_WriteReadings($$);
sub NUKIDevice_CGI();
@ -65,12 +62,8 @@ sub NUKIDevice_Initialize($) {
$hash->{UndefFn} = "NUKIDevice_Undef";
$hash->{AttrFn} = "NUKIDevice_Attr";
my $webhookFWinstance = join( ",", devspec2array('TYPE=FHEMWEB:FILTER=TEMPORARY!=1') );
$hash->{AttrList} = "IODev ".
"disable:1 ".
"webhookFWinstance:$webhookFWinstance ".
"webhookHttpHostname ".
$readingFnAttributes;
@ -93,22 +86,21 @@ sub NUKIDevice_Define($$) {
foreach my $param ( @a ) {
if( $param =~ m/IODev=([^\s]*)/ ) {
$iodev = $1;
splice( @a, $i, 3 );
splice( @a, $i, 1 );
last;
}
$i++;
}
return "too few parameters: define <name> NUKIDevice <nukiId>" if( @a < 2 );
return "too few parameters: define <name> NUKIDevice <nukiId> <deviceType>" if( @a < 2 );
my ($name,$nukiId) = @a;
my ($name,$nukiId,$deviceType) = @a;
$hash->{NUKIID} = $nukiId;
$hash->{DEVICETYPE} = (defined $deviceType) ? $deviceType : 0;
$hash->{VERSION} = $version;
$hash->{STATE} = 'Initialized';
my $infix = "NUKIDevice";
AssignIoPort($hash,$iodev) if( !$hash->{IODev} );
@ -139,14 +131,6 @@ sub NUKIDevice_Define($$) {
$attr{$name}{room} = "NUKI" if( !defined( $attr{$name}{room} ) );
if ( NUKIDevice_addExtension( $name, "NUKIDevice_CGI", $infix ) ) {
$hash->{fhem}{infix} = $infix;
}
$hash->{WEBHOOK_REGISTER} = "unregistered";
if( $init_done ) {
InternalTimer( gettimeofday()+int(rand(10)), "NUKIDevice_GetUpdate", $hash, 0 );
} else {
@ -163,11 +147,6 @@ sub NUKIDevice_Undef($$) {
my $nukiId = $hash->{NUKIID};
my $name = $hash->{NAME};
if ( defined( $hash->{fhem}{infix} ) ) {
NUKIDevice_removeExtension( $hash->{fhem}{infix} );
}
RemoveInternalTimer($hash);
my $code = $hash->{NUKIID};
@ -208,74 +187,9 @@ sub NUKIDevice_Attr(@) {
}
}
######################
#### webhook #########
return "Invalid value for attribute $attrName: can only by FQDN or IPv4 or IPv6 address" if ( $attrVal && $attrName eq "webhookHttpHostname" && $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 && $attrName eq "webhookFWinstance" && ( !defined( $defs{$attrVal} ) || $defs{$attrVal}{TYPE} ne "FHEMWEB" ) );
return "Invalid value for attribute $attrName: needs to be an integer value" if ( $attrVal && $attrName eq "webhookPort" && $attrVal !~ /^\d+$/ );
if ( $attrName =~ /^webhook.*/ ) {
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" ) . "/NUKIDevice";
$hash->{WEBHOOK_PORT} = ( $attrName eq "webhookPort" ? $attrVal : AttrVal( $name, "webhookPort", InternalVal( $webhookFWinstance, "PORT", "" )) );
$hash->{WEBHOOK_URL} = "";
$hash->{WEBHOOK_COUNTER} = "0";
if ( $webhookHttpHostname ne "" && $hash->{WEBHOOK_PORT} ne "" ) {
$hash->{WEBHOOK_URL} = "http://" . $webhookHttpHostname . ":" . $hash->{WEBHOOK_PORT} . $hash->{WEBHOOK_URI} . "-" . $hash->{NUKIID};
my $url = "http://$webhookHttpHostname" . ":" . $hash->{WEBHOOK_PORT} . $hash->{WEBHOOK_URI} . "-" . $hash->{NUKIID};
Log3 $name, 3, "NUKIDevice ($name) - URL ist: $url";
NUKIDevice_ReadFromNUKIBridge($hash,"callback/add",$url,undef ) if( $init_done );
$hash->{WEBHOOK_REGISTER} = "sent";
} else {
$hash->{WEBHOOK_REGISTER} = "incomplete_attributes";
}
}
return undef;
}
sub NUKIDevice_addExtension($$$) {
my ( $name, $func, $link ) = @_;
my $url = "/$link";
return 0 if ( defined( $data{FWEXT}{$url} ) && $data{FWEXT}{$url}{deviceName} ne $name );
Log3 $name, 2, "NUKIDevice ($name) - Registering NUKIDevice for webhook URI $url ...";
$data{FWEXT}{$url}{deviceName} = $name;
$data{FWEXT}{$url}{FUNC} = $func;
$data{FWEXT}{$url}{LINK} = $link;
return 1;
}
sub NUKIDevice_removeExtension($) {
my ($link) = @_;
my $url = "/$link";
my $name = $data{FWEXT}{$url}{deviceName};
Log3 $name, 2, "NUKIDevice ($name) - Unregistering NUKIDevice for webhook URL $url...";
delete $data{FWEXT}{$url};
}
sub NUKIDevice_Set($$@) {
my ($hash, $name, @aa) = @_;
@ -290,33 +204,40 @@ sub NUKIDevice_Set($$@) {
NUKIDevice_GetUpdate($hash);
return undef;
} elsif( $cmd eq 'lock' ) {
} elsif( $cmd eq 'lock' or $cmd eq 'deactivateRto' ) {
$lockAction = $cmd;
} elsif( $cmd eq 'unlock' ) {
} elsif( $cmd eq 'unlock' or $cmd eq 'activateRto' ) {
$lockAction = $cmd;
} elsif( $cmd eq 'unlatch' ) {
} elsif( $cmd eq 'unlatch' or $cmd eq 'electricStrikeActuation' ) {
$lockAction = $cmd;
} elsif( $cmd eq 'locknGo' ) {
} elsif( $cmd eq 'locknGo' or $cmd eq 'activateContinuousMode' ) {
$lockAction = $cmd;
} elsif( $cmd eq 'locknGoWithUnlatch' ) {
} elsif( $cmd eq 'locknGoWithUnlatch' or $cmd eq 'deactivateContinuousMode' ) {
$lockAction = $cmd;
} elsif( $cmd eq 'unpair' ) {
NUKIDevice_ReadFromNUKIBridge($hash,"$cmd",undef,$hash->{NUKIID} ) if( !IsDisabled($name) );
NUKIDevice_ReadFromNUKIBridge($hash,"$cmd",undef,$hash->{NUKIID},$hash->{DEVICETYPE} ) if( !IsDisabled($name) );
return undef;
} else {
my $list = "statusRequest:noArg unlock:noArg lock:noArg unlatch:noArg locknGo:noArg locknGoWithUnlatch:noArg unpair:noArg";
my $list = '';
if ( $hash->{DEVICETYPE} == 0 ) {
$list= "statusRequest:noArg unlock:noArg lock:noArg unlatch:noArg locknGo:noArg locknGoWithUnlatch:noArg unpair:noArg";
} elsif ( $hash->{DEVICETYPE} == 2 ) {
$list= "statusRequest:noArg activateRto:noArg deactivateRto:noArg electricStrikeActuation:noArg activateContinuousMode:noArg deactivateContinuousMode:noArg unpair:noArg";
}
return "Unknown argument $cmd, choose one of $list";
}
$hash->{helper}{lockAction} = $lockAction;
NUKIDevice_ReadFromNUKIBridge($hash,"lockAction",$lockAction,$hash->{NUKIID} ) if( !IsDisabled($name) );
NUKIDevice_ReadFromNUKIBridge($hash,"lockAction",$lockAction,$hash->{NUKIID},$hash->{DEVICETYPE} ) if( !IsDisabled($name) );
return undef;
}
@ -328,7 +249,7 @@ sub NUKIDevice_GetUpdate($) {
RemoveInternalTimer($hash);
NUKIDevice_ReadFromNUKIBridge($hash, "lockState", undef, $hash->{NUKIID} ) if( !IsDisabled($name) );
NUKIDevice_ReadFromNUKIBridge($hash, "lockState", undef, $hash->{NUKIID}, $hash->{DEVICETYPE} ) if( !IsDisabled($name) );
Log3 $name, 5, "NUKIDevice ($name) - NUKIDevice_GetUpdate Call NUKIDevice_ReadFromNUKIBridge" if( !IsDisabled($name) );
return undef;
@ -499,73 +420,6 @@ sub NUKIDevice_WriteReadings($$) {
return undef;
}
sub NUKIDevice_CGI() {
my ($request) = @_;
my $hash;
my $name;
my $nukiId;
# data received
# Testaufruf:
# curl --data '{"nukiId": 123456, "state": 1,"stateName": "locked", "batteryCritical": false}' http://10.6.6.20:8083/fhem/NUKIDevice-123456
# wget --post-data '{"nukiId": 123456, "state": 1,"stateName": "locked", "batteryCritical": false}' http://10.6.6.20:8083/fhem/NUKIDevice-123456
my $header = join("\n", @FW_httpheader);
my ($first,$json) = split("&",$request,2);
if( !$json ) {
Log3 $name, 3, "NUKIDevice ($name) - empty answer received";
return undef;
} elsif( $json =~ m'HTTP/1.1 200 OK' ) {
Log3 $name, 4, "NUKIDevice ($name) - empty answer received";
return undef;
} elsif( $json !~ m/^[\[{].*[}\]]$/ ) {
Log3 $name, 3, "NUKIDevice ($name) - invalid json detected: $json";
return "NUKIDevice ($name) - invalid json detected: $json";
}
my $decode_json = eval{decode_json($json)};
if($@){
Log3 $name, 3, "NUKIDevice ($name) - JSON error while request: $@";
return;
}
if( ref($decode_json) eq "HASH" ) {
if ( defined( $modules{NUKIDevice}{defptr} ) ) {
while ( my ( $key, $value ) = each %{ $modules{NUKIDevice}{defptr} } ) {
$hash = $modules{NUKIDevice}{defptr}{$key};
$name = $hash->{NAME};
$nukiId = InternalVal( $name, "NUKIID", undef );
next if ( !$nukiId or $nukiId ne $decode_json->{nukiId} );
$hash->{WEBHOOK_COUNTER}++;
$hash->{WEBHOOK_LAST} = TimeNow();
Log3 $name, 4, "NUKIDevice ($name) - Received webhook for matching NukiId at device $name";
NUKIDevice_Parse($hash,$json);
}
}
return ( undef, undef );
}
# no data received
else {
Log3 undef, 4, "NUKIDevice - received malformed request\n$request";
}
return ( "text/plain; charset=utf-8", "Call failure: " . $request );
}
@ -589,17 +443,19 @@ sub NUKIDevice_CGI() {
<ul>
<u><b>NUKIDevice - Controls the Nuki Smartlock</b></u>
<br>
The Nuki module connects FHEM over the Nuki Bridge with a Nuki Smartlock. After that, it´s possible to lock and unlock the Smartlock.<br>
The Nuki module connects FHEM over the Nuki Bridge with a Nuki Smartlock or Nuki Opener. After that, it´s possible to lock and unlock the Smartlock.<br>
Normally the Nuki devices are automatically created by the bridge module.
<br><br>
<a name="NUKIDevicedefine"></a>
<b>Define</b>
<ul><br>
<code>define &lt;name&gt; NUKIDevice &lt;Nuki-Id&gt; &lt;IODev-Device&gt;</code>
<code>define &lt;name&gt; NUKIDevice &lt;Nuki-Id&gt; &lt;IODev-Device&gt; &lt;Device-Type&gt;</code>
<br><br>
Device-Type is 0 for the Smartlock and 2 for the Opener.
<br><br>
Example:
<ul><br>
<code>define Frontdoor NUKIDevice 1 NBridge1</code><br>
<code>define Frontdoor NUKIDevice 1 NBridge1 0</code><br>
</ul>
<br>
This statement creates a NUKIDevice with the name Frontdoor, the NukiId 1 and the IODev device NBridge1.<br>
@ -636,8 +492,6 @@ sub NUKIDevice_CGI() {
<b>Attributes</b>
<ul>
<li>disable - disables the Nuki device</li>
<li>webhookFWinstance - Webinstanz of the Callback</li>
<li>webhookHttpHostname - IP or FQDN of the FHEM Server Callback</li>
<br>
</ul>
</ul>
@ -650,17 +504,19 @@ sub NUKIDevice_CGI() {
<ul>
<u><b>NUKIDevice - Steuert das Nuki Smartlock</b></u>
<br>
Das Nuki Modul verbindet FHEM über die Nuki Bridge mit einem Nuki Smartlock. Es ist dann m&ouml;glich das Schloss zu ver- und entriegeln.<br>
Das Nuki Modul verbindet FHEM über die Nuki Bridge mit einem Nuki Smartlock oder Nuki Opener. Es ist dann m&ouml;glich das Schloss zu ver- und entriegeln.<br>
In der Regel werden die Nuki Devices automatisch durch das Bridgemodul angelegt.
<br><br>
<a name="NUKIDevicedefine"></a>
<b>Define</b>
<ul><br>
<code>define &lt;name&gt; NUKIDevice &lt;Nuki-Id&gt; &lt;IODev-Device&gt;</code>
<code>define &lt;name&gt; NUKIDevice &lt;Nuki-Id&gt; &lt;IODev-Device&gt; &lt;Device-Type&gt;</code>
<br><br>
Device-Type ist 0 f&uuml;r das Smartlock und 2 f&üuml;r den Opener.
<br><br>
Beispiel:
<ul><br>
<code>define Haust&uuml;r NUKIDevice 1 NBridge1</code><br>
<code>define Haust&uuml;r NUKIDevice 1 NBridge1 0</code><br>
</ul>
<br>
Diese Anweisung erstellt ein NUKIDevice mit Namen Haust&uuml;r, der NukiId 1 sowie dem IODev Device NBridge1.<br>
@ -697,8 +553,6 @@ sub NUKIDevice_CGI() {
<b>Attribute</b>
<ul>
<li>disable - deaktiviert das Nuki Device</li>
<li>webhookFWinstance - zu verwendene Webinstanz für den Callbackaufruf</li>
<li>webhookHttpHostname - IP oder FQDN vom FHEM Server für den Callbackaufruf</li>
<br>
</ul>
</ul>

Binary file not shown.

BIN
Bridge-API-v1.9.pdf Normal file

Binary file not shown.