mod-NUKI/73_NUKIBridge.pm

828 lines
26 KiB
Perl
Raw Normal View History

2016-09-27 11:37:19 +00:00
###############################################################################
#
# Developed with Kate
#
# (c) 2016 Copyright: Marko Oldenburg (leongaultier at gmail dot com)
2016-09-27 11:37:19 +00:00
# All rights reserved
#
# This script is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
#
# The GNU General Public License can be found at
# http://www.gnu.org/copyleft/gpl.html.
# A copy is found in the textfile GPL.txt and important notices to the license
# from the author is found in LICENSE.txt distributed with these scripts.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#
# $Id$
#
###############################################################################
2016-10-30 12:45:03 +00:00
#################################
######### Wichtige Hinweise und Links #################
## Beispiel für Logausgabe
# https://forum.fhem.de/index.php/topic,55756.msg508412.html#msg508412
##
#
################################
2016-09-27 11:37:19 +00:00
package main;
use strict;
use warnings;
use JSON;
2016-11-03 16:58:01 +00:00
2016-09-27 11:37:19 +00:00
use HttpUtils;
2016-12-18 20:05:14 +00:00
my $version = "0.4.0";
2016-09-27 11:37:19 +00:00
my %lockActions = (
'unlock' => 1,
'lock' => 2,
'unlatch' => 3,
'locknGo' => 4,
'locknGoWithUnlatch' => 5
2016-09-27 11:37:19 +00:00
);
sub NUKIBridge_Initialize($) {
my ($hash) = @_;
# Provider
$hash->{ReadFn} = "NUKIBridge_Read";
$hash->{WriteFn} = "NUKIBridge_Read";
$hash->{Clients} = ":NUKIDevice:";
2016-11-03 16:58:01 +00:00
2016-09-27 11:37:19 +00:00
# Consumer
$hash->{SetFn} = "NUKIBridge_Set";
2016-10-30 21:24:52 +00:00
$hash->{GetFn} = "NUKIBridge_Get";
$hash->{DefFn} = "NUKIBridge_Define";
$hash->{UndefFn} = "NUKIBridge_Undef";
$hash->{AttrFn} = "NUKIBridge_Attr";
$hash->{AttrList} = "disable:1 ".
2016-09-27 11:37:19 +00:00
$readingFnAttributes;
foreach my $d(sort keys %{$modules{NUKIBridge}{defptr}}) {
my $hash = $modules{NUKIBridge}{defptr}{$d};
$hash->{VERSION} = $version;
2016-09-27 11:37:19 +00:00
}
}
sub NUKIBridge_Read($@) {
my ($hash,$chash,$name,$path,$lockAction,$nukiId)= @_;
NUKIBridge_Call($hash,$chash,$path,$lockAction,$nukiId );
2016-09-27 11:37:19 +00:00
}
sub NUKIBridge_Define($$) {
my ( $hash, $def ) = @_;
my @a = split( "[ \t][ \t]*", $def );
return "too few parameters: define <name> NUKIBridge <HOST> <TOKEN>" if( @a != 4 );
my $name = $a[0];
my $host = $a[2];
2016-09-27 11:37:19 +00:00
my $token = $a[3];
my $port = 8080;
2016-09-27 11:37:19 +00:00
$hash->{HOST} = $host;
$hash->{PORT} = $port;
$hash->{TOKEN} = $token;
$hash->{VERSION} = $version;
$hash->{helper}{aliveCount} = 0;
2016-09-27 11:37:19 +00:00
Log3 $name, 3, "NUKIBridge ($name) - defined with host $host on port $port, Token $token";
$attr{$name}{room} = "NUKI" if( !defined( $attr{$name}{room} ) );
readingsSingleUpdate($hash, 'state', 'Initialized', 1 );
2016-09-27 11:37:19 +00:00
RemoveInternalTimer($hash);
2016-11-13 09:44:02 +00:00
if( $init_done ) {
NUKIBridge_firstRun($hash) if( ($hash->{HOST}) and ($hash->{TOKEN}) );
} else {
InternalTimer( gettimeofday()+15, "NUKIBridge_firstRun", $hash, 0 ) if( ($hash->{HOST}) and ($hash->{TOKEN}) );
}
2016-09-27 11:37:19 +00:00
$modules{NUKIBridge}{defptr}{$hash->{HOST}} = $hash;
2016-09-27 11:37:19 +00:00
return undef;
}
sub NUKIBridge_Undef($$) {
my ( $hash, $arg ) = @_;
my $host = $hash->{HOST};
my $name = $hash->{NAME};
RemoveInternalTimer( $hash );
delete $modules{NUKIBridge}{defptr}{$hash->{HOST}};
2016-09-27 11:37:19 +00:00
return undef;
}
sub NUKIBridge_Attr(@) {
my ( $cmd, $name, $attrName, $attrVal ) = @_;
my $hash = $defs{$name};
my $orig = $attrVal;
2016-09-27 11:37:19 +00:00
if( $attrName eq "disable" ) {
if( $cmd eq "set" and $attrVal eq "1" ) {
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";
2016-09-27 11:37:19 +00:00
}
}
if( $attrName eq "disabledForIntervals" ) {
if( $cmd eq "set" ) {
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";
2016-11-03 16:58:01 +00:00
}
}
2016-09-27 11:37:19 +00:00
return undef;
}
sub NUKIBridge_Set($@) {
my ($hash, $name, $cmd, @args) = @_;
my ($arg, @params) = @args;
if($cmd eq 'autocreate') {
return "usage: autocreate" if( @args != 0 );
2016-09-27 11:37:19 +00:00
NUKIBridge_Call($hash,$hash,"list",undef,undef) if( !IsDisabled($name) );
2016-09-27 11:37:19 +00:00
return undef;
} elsif($cmd eq 'info') {
2016-11-03 16:58:01 +00:00
return "usage: statusRequest" if( @args != 0 );
2016-11-03 16:58:01 +00:00
NUKIBridge_Call($hash,$hash,"info",undef,undef) if( !IsDisabled($name) );
return undef;
2016-09-27 11:37:19 +00:00
2016-11-03 16:58:01 +00:00
} elsif($cmd eq 'fwUpdate') {
return "usage: fwUpdate" if( @args != 0 );
NUKIBridge_CallBlocking($hash,"fwupdate",undef) if( !IsDisabled($name) );
2016-11-03 16:58:01 +00:00
return undef;
} elsif($cmd eq 'reboot') {
return "usage: reboot" if( @args != 0 );
NUKIBridge_CallBlocking($hash,"reboot",undef) if( !IsDisabled($name) );
2016-11-03 16:58:01 +00:00
return undef;
} elsif($cmd eq 'clearLog') {
return "usage: clearLog" if( @args != 0 );
NUKIBridge_CallBlocking($hash,"clearlog",undef) if( !IsDisabled($name) );
} elsif($cmd eq 'callbackRemove') {
return "usage: callbackRemove" if( @args != 1 );
my $id = "id=" . join( " ", @args );
my $resp = NUKIBridge_CallBlocking($hash,"callback/remove",$id) if( !IsDisabled($name) );
if( $resp->{success} eq "true" and !IsDisabled($name) ) {
return "Success Callback $id removed";
} else {
return "remove Callback failed";
}
2016-09-27 11:37:19 +00:00
} else {
my $list = "info:noArg autocreate:noArg clearLog:noArg fwUpdate:noArg reboot:noArg callbackRemove:0,1,2";
2016-09-27 11:37:19 +00:00
return "Unknown argument $cmd, choose one of $list";
}
}
2016-10-30 21:24:52 +00:00
sub NUKIBridge_Get($@) {
my ($hash, $name, $cmd, @args) = @_;
my ($arg, @params) = @args;
if($cmd eq 'logFile') {
return "usage: logFile" if( @args != 0 );
NUKIBridge_getLogfile($hash) if( !IsDisabled($name) );
2016-10-30 21:24:52 +00:00
} elsif($cmd eq 'callbackList') {
return "usage: callbackList" if( @args != 0 );
NUKIBridge_getCallbackList($hash) if( !IsDisabled($name) );
2016-10-30 21:24:52 +00:00
} else {
my $list = "logFile:noArg callbackList:noArg";
2016-10-30 21:24:52 +00:00
return "Unknown argument $cmd, choose one of $list";
}
}
sub NUKIBridge_GetCheckBridgeAlive($) {
2016-09-27 11:37:19 +00:00
my ($hash) = @_;
my $name = $hash->{NAME};
RemoveInternalTimer($hash);
2016-12-17 09:06:10 +00:00
Log3 $name, 4, "NUKIBridge ($name) - NUKIBridge_GetCheckBridgeAlive";
if( !IsDisabled($name) ) {
NUKIBridge_Call($hash,$hash,"alive",undef,undef);
InternalTimer( gettimeofday()+17+int(rand(15)), "NUKIBridge_GetCheckBridgeAlive", $hash, 1 );
Log3 $name, 4, "NUKIBridge ($name) - Call InternalTimer for NUKIBridge_GetCheckBridgeAlive";
}
}
sub NUKIBridge_firstRun($) {
my ($hash) = @_;
my $name = $hash->{NAME};
RemoveInternalTimer($hash);
NUKIBridge_Call($hash,$hash,"list",undef,undef) if( !IsDisabled($name) );
NUKIBridge_GetCheckBridgeAlive($hash);
return undef;
2016-09-27 11:37:19 +00:00
}
2016-11-03 16:58:01 +00:00
sub NUKIBridge_Call($$$$$) {
2016-09-27 11:37:19 +00:00
my ($hash,$chash,$path,$lockAction,$nukiId) = @_;
2016-09-27 11:37:19 +00:00
my $name = $hash->{NAME};
my $host = $hash->{HOST};
my $port = $hash->{PORT};
my $token = $hash->{TOKEN};
2016-09-27 11:37:19 +00:00
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 .= "&url=" . $lockAction if( defined($lockAction) and $path eq "callback/add" );
$uri .= "&nukiId=" . $nukiId if( defined($nukiId) );
2016-09-27 11:37:19 +00:00
HttpUtils_NonblockingGet(
{
url => $uri,
timeout => 30,
hash => $hash,
chash => $chash,
endpoint => $path,
header => "Accept: application/json",
method => "GET",
callback => \&NUKIBridge_Distribution,
}
2016-09-27 11:37:19 +00:00
);
Log3 $name, 4, "NUKIBridge ($name) - Send HTTP POST with URL $uri";
2016-09-27 11:37:19 +00:00
}
2016-11-01 12:39:13 +00:00
sub NUKIBridge_Distribution($$$) {
2016-09-27 11:37:19 +00:00
my ( $param, $err, $json ) = @_;
my $hash = $param->{hash};
my $doTrigger = $param->{doTrigger};
my $name = $hash->{NAME};
my $host = $hash->{HOST};
2016-11-01 12:39:13 +00:00
readingsBeginUpdate($hash);
2016-09-27 11:37:19 +00:00
if( defined( $err ) ) {
if ( $err ne "" ) {
if ($param->{endpoint} eq "alive") {
readingsBulkUpdate( $hash, "state", "not connected") if( $hash->{helper}{aliveCount} > 1 );
$hash->{helper}{aliveCount} = $hash->{helper}{aliveCount} + 1;
}
readingsBulkUpdate( $hash, "lastError", $err ) if( ReadingsVal($name,"state","not connected") eq "not connected" );
Log3 $name, 4, "NUKIBridge ($name) - error while requesting: $err";
readingsEndUpdate( $hash, 1 );
return $err;
}
2016-09-27 11:37:19 +00:00
}
if( $json eq "" and exists( $param->{code} ) && $param->{code} ne 200 ) {
if( $param->{endpoint} eq "alive") {
2016-09-27 11:37:19 +00:00
readingsBulkUpdate( $hash, "state", "connected" );
Log3 $name, 5, "NUKIBridge ($name) - Bridge ist online";
readingsEndUpdate( $hash, 1 );
$hash->{helper}{aliveCount} = 0;
return;
}
readingsBulkUpdate( $hash, "lastError", "Internal error, " .$param->{code} );
Log3 $name, 4, "NUKIBridge ($name) - received http code " .$param->{code}." without any data after requesting";
2016-09-27 11:37:19 +00:00
readingsEndUpdate( $hash, 1 );
return "received http code ".$param->{code}." without any data after requesting";
2016-09-27 11:37:19 +00:00
}
if( ( $json =~ /Error/i ) and exists( $param->{code} ) ) {
readingsBulkUpdate( $hash, "lastError", "invalid API token" ) if( $param->{code} eq 401 );
readingsBulkUpdate( $hash, "lastError", "action is undefined" ) if( $param->{code} eq 400 and $hash == $param->{chash} );
###### Fehler bei Antwort auf Anfrage eines logischen Devices ######
NUKIDevice_Parse($param->{chash},$param->{code}) if( $param->{code} eq 404 );
NUKIDevice_Parse($param->{chash},$param->{code}) if( $param->{code} eq 400 and $hash != $param->{chash} );
Log3 $name, 4, "NUKIBridge ($name) - invalid API token" if( $param->{code} eq 401 );
Log3 $name, 4, "NUKIBridge ($name) - nukiId is not known" if( $param->{code} eq 404 );
Log3 $name, 4, "NUKIBridge ($name) - action is undefined" if( $param->{code} eq 400 and $hash == $param->{chash} );
######### Zum testen da ich kein Nuki Smartlock habe ############
#if ( $param->{code} eq 404 ) {
# if( defined($param->{chash}->{helper}{lockAction}) ) {
# Log3 $name, 3, "NUKIBridge ($name) - Test JSON String for lockAction";
# $json = '{"success": true, "batteryCritical": false}';
# } else {
# Log3 $name, 3, "NUKIBridge ($name) - Test JSON String for lockState";
# $json = '{"state": 1, "stateName": "locked", "batteryCritical": false, "success": "true"}';
# }
2016-09-27 11:43:10 +00:00
# NUKIDevice_Parse($param->{chash},$json);
#}
readingsEndUpdate( $hash, 1 );
return $param->{code};
}
2016-09-27 11:37:19 +00:00
if( $hash == $param->{chash} ) {
2016-09-27 11:43:10 +00:00
2016-11-03 16:58:01 +00:00
#$json = '[{"nukiId": 1,"name": "Home","lastKnownState": {"state": 1,"stateName": "locked","batteryCritical": false,"timestamp": "2016-10-03T06:49:00+00:00"}},{"nukiId": 2,"name": "Grandma","lastKnownState": {"state": 3,"stateName": "unlocked","batteryCritical": false,"timestamp": "2016-10-03T06:49:00+00:00"}}]' if( $param->{endpoint} eq "list" ); # zum testen da ich kein Nuki Smartlock habe
2016-09-27 11:43:10 +00:00
2016-11-03 16:58:01 +00:00
NUKIBridge_ResponseProcessing($hash,$json,$param->{endpoint});
2016-09-27 11:37:19 +00:00
} else {
2016-09-27 11:43:10 +00:00
NUKIDevice_Parse($param->{chash},$json);
2016-09-27 11:37:19 +00:00
}
readingsEndUpdate( $hash, 1 );
return undef;
2016-09-27 11:37:19 +00:00
}
2016-11-03 16:58:01 +00:00
sub NUKIBridge_ResponseProcessing($$$) {
2016-09-27 11:37:19 +00:00
2016-11-03 16:58:01 +00:00
my ($hash,$json,$path) = @_;
2016-09-27 11:37:19 +00:00
my $name = $hash->{NAME};
my $decode_json;
$decode_json = decode_json($json);
2016-11-03 16:58:01 +00:00
if( ref($decode_json) eq "ARRAY" and scalar(@{$decode_json}) > 0 and $path eq "list" ) {
2016-09-27 11:37:19 +00:00
NUKIBridge_Autocreate($hash,$decode_json);
NUKIBridge_Call($hash,$hash,"info",undef,undef) if( !IsDisabled($name) );
2016-11-03 16:58:01 +00:00
}
elsif( $path eq "info" ) {
NUKIBridge_InfoProcessing($hash,$decode_json);
2016-09-27 11:37:19 +00:00
} else {
2016-11-03 16:58:01 +00:00
Log3 $name, 5, "NUKIDevice ($name) - Rückgabe Path nicht korrekt: $json";
return;
2016-09-27 11:37:19 +00:00
}
return undef;
2016-09-27 11:37:19 +00:00
}
sub NUKIBridge_Autocreate($$;$) {
my ($hash,$decode_json,$force)= @_;
my $name = $hash->{NAME};
if( !$force ) {
foreach my $d (keys %defs) {
next if($defs{$d}{TYPE} ne "autocreate");
return undef if(AttrVal($defs{$d}{NAME},"disable",undef));
}
}
my $autocreated = 0;
my $nukiSmartlock;
my $nukiId;
my $nukiName;
readingsBeginUpdate($hash);
foreach $nukiSmartlock (@{$decode_json}) {
$nukiId = $nukiSmartlock->{nukiId};
$nukiName = $nukiSmartlock->{name};
my $code = $name ."-".$nukiId;
if( defined($modules{NUKIDevice}{defptr}{$code}) ) {
Log3 $name, 3, "NUKIDevice ($name) - NukiId '$nukiId' already defined as '$modules{NUKIDevice}{defptr}{$code}->{NAME}'";
2016-09-27 11:37:19 +00:00
next;
}
my $devname = "NUKIDevice" . $nukiId;
my $define= "$devname NUKIDevice $nukiId IODev=$name";
Log3 $name, 3, "NUKIDevice ($name) - create new device '$devname' for address '$nukiId'";
2016-09-27 11:37:19 +00:00
my $cmdret= CommandDefine(undef,$define);
if($cmdret) {
Log3 $name, 3, "NUKIDevice ($name) - Autocreate: An error occurred while creating device for nukiId '$nukiId': $cmdret";
2016-09-27 11:37:19 +00:00
} else {
$cmdret= CommandAttr(undef,"$devname alias $nukiName");
$cmdret= CommandAttr(undef,"$devname room NUKI");
$cmdret= CommandAttr(undef,"$devname IODev $name");
}
$defs{$devname}{helper}{fromAutocreate} = 1 ;
readingsBulkUpdate( $hash, "${autocreated}_nukiId", $nukiId );
readingsBulkUpdate( $hash, "${autocreated}_name", $nukiName );
$autocreated++;
readingsBulkUpdate( $hash, "smartlockCount", $autocreated );
2016-09-27 11:37:19 +00:00
}
readingsEndUpdate( $hash, 1 );
if( $autocreated ) {
2016-11-03 16:58:01 +00:00
Log3 $name, 2, "NUKIDevice ($name) - autocreated $autocreated devices";
2016-09-27 11:37:19 +00:00
CommandSave(undef,undef) if( AttrVal( "autocreate", "autosave", 1 ) );
}
return "created $autocreated devices";
}
2016-11-03 16:58:01 +00:00
sub NUKIBridge_InfoProcessing($$) {
my ($hash,$decode_json) = @_;
my $name = $hash->{NAME};
my %bridgeType = (
'1' => 'Hardware',
'2' => 'Software'
);
readingsBeginUpdate($hash);
2016-12-07 10:57:33 +00:00
readingsBulkUpdate($hash,"appVersion",$decode_json->{versions}->{appVersion});
readingsBulkUpdate($hash,"firmwareVersion",$decode_json->{versions}->{firmwareVersion});
readingsBulkUpdate($hash,"wifiFirmwareVersion",$decode_json->{versions}->{wifiFirmwareVersion});
2016-11-03 16:58:01 +00:00
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);
}
2016-10-30 21:24:52 +00:00
sub NUKIBridge_getLogfile($) {
my ($hash) = @_;
my $name = $hash->{NAME};
2016-11-03 16:58:01 +00:00
my $decode_json = NUKIBridge_CallBlocking($hash,"log",undef);
2016-10-30 21:24:52 +00:00
Log3 $name, 4, "NUKIBridge ($name) - Log Daten werden geholt und aufbereitet";
2016-10-30 21:24:52 +00:00
if( ref($decode_json) eq "ARRAY" and scalar(@{$decode_json}) > 0 ) {
Log3 $name, 4, "NUKIBridge ($name) - Tabelle mit Logdaten wird aufgebaut";
2016-10-30 21:24:52 +00:00
my $ret = '<html><table width=100%><tr><td>';
$ret .= '<table class="block wide">';
2016-10-30 21:24:52 +00:00
foreach my $logs (@{$decode_json}) {
$ret .= '<tr class="odd">';
2016-12-18 20:05:14 +00:00
if($logs->{timestamp}) {
$ret .= "<td><b>timestamp:</b> </td>";
$ret .= "<td>$logs->{timestamp}</td>";
$ret .= '<td> </td>';
}
if($logs->{type}) {
$ret .= "<td><b>type:</b> </td>";
$ret .= "<td>$logs->{type}</td>";
$ret .= '<td> </td>';
}
foreach my $d (reverse sort keys %{$logs}) {
2016-12-18 20:05:14 +00:00
next if( $d eq "type" );
next if( $d eq "timestamp" );
$ret .= "<td><b>$d:</b> </td>";
$ret .= "<td>$logs->{$d}</td>";
$ret .= '<td> </td>';
}
2016-10-30 21:24:52 +00:00
$ret .= '</tr>';
}
$ret .= '</table></td></tr>';
$ret .= '</table></html>';
return $ret;
}
}
sub NUKIBridge_getCallbackList($) {
my ($hash) = @_;
my $name = $hash->{NAME};
my $decode_json = NUKIBridge_CallBlocking($hash,"callback/list",undef);
Log3 $name, 4, "NUKIBridge ($name) - Callback Daten werden geholt und aufbereitet";
if( ref($decode_json->{callbacks}) eq "ARRAY" and scalar(@{$decode_json->{callbacks}}) > 0 ) {
Log3 $name, 4, "NUKIBridge ($name) - Tabelle mit Logdaten wird aufgebaut";
my $ret = '<html><table width=100%><tr><td>';
$ret .= '<table class="block wide">';
$ret .= '<tr class="odd">';
$ret .= "<td><b>Callback-ID</b></td>";
$ret .= "<td> </td>";
$ret .= "<td><b>Callback-URL</b></td>";
$ret .= '</tr>';
foreach my $cb (@{$decode_json->{callbacks}}) {
$ret .= "<td>$cb->{id}</td>";
$ret .= "<td> </td>";
$ret .= "<td>$cb->{url}</td>";
$ret .= '</tr>';
}
$ret .= '</table></td></tr>';
$ret .= '</table></html>';
return $ret;
}
return "Keine Callback Daten vorhanden oder Fehler bei der Verarbeitung";
}
2016-11-03 16:58:01 +00:00
sub NUKIBridge_CallBlocking($$$) {
2016-10-30 21:24:52 +00:00
2016-11-03 16:58:01 +00:00
my ($hash,$path,$obj) = @_;
2016-10-30 21:24:52 +00:00
my $name = $hash->{NAME};
my $host = $hash->{HOST};
my $port = $hash->{PORT};
my $token = $hash->{TOKEN};
2016-10-30 21:24:52 +00:00
my $url = "http://" . $hash->{HOST} . ":" . $port;
$url .= "/" . $path if( defined $path);
$url .= "?token=" . $token if( defined($token) );
$url .= "&" . $obj if( defined($obj) );
2016-10-30 21:24:52 +00:00
my($err,$data) = HttpUtils_BlockingGet({
url => $url,
timeout => 3,
2016-10-30 21:24:52 +00:00
method => "GET",
header => "Content-Type: application/json",
});
2016-10-30 21:24:52 +00:00
if( !$data ) {
Log3 $name, 3, "NUKIDevice ($name) - empty answer received for $url";
return undef;
2016-10-30 21:24:52 +00:00
} elsif( $data =~ m'HTTP/1.1 200 OK' ) {
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";
2016-10-30 21:24:52 +00:00
}
2016-10-30 21:32:09 +00:00
2016-10-30 21:24:52 +00:00
my $decode_json = decode_json($data);
2016-10-30 21:24:52 +00:00
return undef if( !$decode_json );
Log3 $name, 5, "NUKIBridge ($name) - Data: $data";
Log3 $name, 4, "NUKIBridge ($name) - Blocking HTTP Abfrage beendet";
2016-10-30 21:24:52 +00:00
return ($decode_json);
}
2016-09-27 11:37:19 +00:00
1;
=pod
2016-10-10 10:14:16 +00:00
=item device
2016-10-22 15:00:29 +00:00
=item summary Modul to control the Nuki Smartlock's over the Nuki Bridge.
2016-10-10 10:14:16 +00:00
=item summary_DE Modul zur Steuerung des Nuki Smartlock über die Nuki Bridge.
2016-09-27 11:37:19 +00:00
=begin html
2016-12-18 20:17:33 +00:00
2016-10-10 10:14:16 +00:00
<a name="NUKIBridge"></a>
<h3>NUKIBridge</h3>
2016-09-27 11:37:19 +00:00
<ul>
2016-10-11 07:11:43 +00:00
<u><b>NUKIBridge - controls the Nuki Smartlock over the Nuki Bridge</b></u>
<br>
The Nuki Bridge module connects FHEM to the Nuki Bridge and then reads all the smartlocks available on the bridge. Furthermore, the detected Smartlocks are automatically created as independent devices.
<br><br>
<a name="NUKIBridgedefine"></a>
<b>Define</b>
<ul><br>
<code>define &lt;name&gt; NUKIBridge &lt;HOST&gt; &lt;API-TOKEN&gt;</code>
<br><br>
Example:
<ul><br>
<code>define NBridge1 NUKIBridge 192.168.0.23 F34HK6</code><br>
</ul>
<br>
This statement creates a NUKIBridge device with the name NBridge1 and the IP 192.168.0.23 as well as the token F34HK6.<br>
After the bridge device is created, all available Smartlocks are automatically placed in FHEM.
</ul>
<br><br>
<a name="NUKIBridgereadings"></a>
<b>Readings</b>
<ul>
<li>0_nukiId - ID of the first found Nuki Smartlock</li>
<li>0_name - Name of the first found Nuki Smartlock</li>
<li>smartlockCount - number of all found Smartlocks</li>
<li>bridgeAPI - API Version of bridge</li>
<li>bridgeType - Hardware bridge / Software bridge</li>
<li>currentTime - Current timestamp</li>
<li>firmwareVersion - Version of the bridge firmware</li>
<li>hardwareId - Hardware ID</li>
<li>lastError - Last connected error</li>
<li>serverConnected - Flag indicating whether or not the bridge is connected to the Nuki server</li>
<li>serverId - Server ID</li>
<li>uptime - Uptime of the bridge in seconds</li>
<li>wifiFirmwareVersion- Version of the WiFi modules firmware</li>
2016-10-11 07:11:43 +00:00
<br>
The preceding number is continuous, starts with 0 und returns the properties of <b>one</b> Smartlock.
</ul>
<br><br>
<a name="NUKIBridgeset"></a>
<b>Set</b>
<ul>
<li>autocreate - Prompts to re-read all Smartlocks from the bridge and if not already present in FHEM, create the autimatic.</li>
<li>callbackRemove - Removes a previously added callback</li>
<li>clearLog - Clears the log of the Bridge</li>
<li>fwUpdate - Immediately checks for a new firmware update and installs it</li>
<li>info - Returns all Smart Locks in range and some device information of the bridge itself</li>
<li>reboot - reboots the bridge</li>
<br>
</ul>
<br><br>
<a name="NUKIBridgeget"></a>
<b>Get</b>
<ul>
<li>callbackList - List of register url callbacks. The Bridge register up to 3 url callbacks.</li>
<li>logFile - Retrieves the log of the Bridge</li>
2016-10-11 07:11:43 +00:00
<br>
</ul>
<br><br>
<a name="NUKIBridgeattribut"></a>
<b>Attributes</b>
<ul>
<li>disable - disables the Nuki Bridge</li>
<br>
</ul>
2016-09-27 11:37:19 +00:00
</ul>
=end html
=begin html_DE
2016-10-10 10:14:16 +00:00
<a name="NUKIBridge"></a>
<h3>NUKIBridge</h3>
2016-09-27 11:37:19 +00:00
<ul>
2016-10-10 10:14:16 +00:00
<u><b>NUKIBridge - Steuert das Nuki Smartlock über die Nuki Bridge</b></u>
<br>
Das Nuki Bridge Modul verbindet FHEM mit der Nuki Bridge und liest dann alle auf der Bridge verfügbaren Smartlocks ein. Desweiteren werden automatisch die erkannten Smartlocks als eigenst&auml;ndige Devices an gelegt.
<br><br>
<a name="NUKIBridgedefine"></a>
<b>Define</b>
<ul><br>
<code>define &lt;name&gt; NUKIBridge &lt;HOST&gt; &lt;API-TOKEN&gt;</code>
<br><br>
Beispiel:
<ul><br>
<code>define NBridge1 NUKIBridge 192.168.0.23 F34HK6</code><br>
</ul>
<br>
Diese Anweisung erstellt ein NUKIBridge Device mit Namen NBridge1 und der IP 192.168.0.23 sowie dem Token F34HK6.<br>
Nach dem anlegen des Bridge Devices werden alle zur verf&uuml;gung stehende Smartlock automatisch in FHEM an gelegt.
</ul>
<br><br>
<a name="NUKIBridgereadings"></a>
<b>Readings</b>
<ul>
<li>0_nukiId - ID des ersten gefundenen Nuki Smartlocks</li>
<li>0_name - Name des ersten gefunden Nuki Smartlocks</li>
<li>smartlockCount - Anzahl aller gefundenen Smartlock</li>
2016-10-11 07:11:43 +00:00
<li>bridgeAPI - API Version der Bridge</li>
<li>bridgeType - Hardware oder Software/App Bridge</li>
<li>currentTime - aktuelle Zeit auf der Bridge zum zeitpunkt des Info holens</li>
<li>firmwareVersion - aktuell auf der Bridge verwendete Firmwareversion</li>
<li>hardwareId - ID der Hardware Bridge</li>
<li>lastError - gibt die letzte HTTP Errormeldung wieder</li>
<li>serverConnected - true/false gibt an ob die Hardwarebridge Verbindung zur Nuki-Cloude hat.</li>
<li>serverId - gibt die ID des Cloudeservers wieder</li>
<li>uptime - Uptime der Bridge in Sekunden</li>
<li>wifiFirmwareVersion- Firmwareversion des Wifi Modules der Bridge</li>
2016-10-10 10:14:16 +00:00
<br>
Die vorangestellte Zahl ist forlaufend und gibt beginnend bei 0 die Eigenschaften <b>Eines</b> Smartlocks wieder.
</ul>
<br><br>
<a name="NUKIBridgeset"></a>
<b>Set</b>
<ul>
<li>autocreate - Veranlasst ein erneutes Einlesen aller Smartlocks von der Bridge und falls noch nicht in FHEM vorhanden das autimatische anlegen.</li>
<li>callbackRemove - Löschen einer Callback Instanz auf der Bridge. Die Instanz ID kann mittels get callbackList ermittelt werden</li>
<li>clearLog - löscht das Logfile auf der Bridge</li>
<li>fwUpdate - schaut nach einer neueren Firmware und installiert diese sofern vorhanden</li>
<li>info - holt aktuellen Informationen über die Bridge</li>
<li>reboot - veranlässt ein reboot der Bridge</li>
<br>
</ul>
<br><br>
<a name="NUKIBridgeget"></a>
<b>Get</b>
<ul>
<li>callbackList - Gibt die Liste der eingetragenen Callback URL's wieder. Die Bridge nimmt maximal 3 auf.</li>
<li>logFile - Zeigt das Logfile der Bridge an</li>
2016-10-10 10:14:16 +00:00
<br>
</ul>
<br><br>
<a name="NUKIBridgeattribut"></a>
<b>Attribute</b>
<ul>
<li>disable - deaktiviert die Nuki Bridge</li>
<br>
</ul>
2016-09-27 11:37:19 +00:00
</ul>
=end html_DE
=cut