From e424250cc1c85714465fc1caf42f4afdcdd5cec8 Mon Sep 17 00:00:00 2001 From: Marko Oldenburg Date: Sat, 17 Dec 2016 20:47:58 +0100 Subject: [PATCH] =?UTF-8?q?Commandref=20f=C3=BCr=20Callback=20und=20neue?= =?UTF-8?q?=20Readings=20der=20Bridge=20an=20gepasst?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 73_NUKIBridge.pm | 144 ++++++++++++++++++++++++++--------------------- 74_NUKIDevice.pm | 86 ++++++++++++---------------- 2 files changed, 118 insertions(+), 112 deletions(-) diff --git a/73_NUKIBridge.pm b/73_NUKIBridge.pm index 4956524..c983974 100644 --- a/73_NUKIBridge.pm +++ b/73_NUKIBridge.pm @@ -72,16 +72,16 @@ sub NUKIBridge_Initialize($) { # Consumer $hash->{SetFn} = "NUKIBridge_Set"; $hash->{GetFn} = "NUKIBridge_Get"; - $hash->{DefFn} = "NUKIBridge_Define"; - $hash->{UndefFn} = "NUKIBridge_Undef"; - $hash->{AttrFn} = "NUKIBridge_Attr"; - $hash->{AttrList} = "disable:1 ". + $hash->{DefFn} = "NUKIBridge_Define"; + $hash->{UndefFn} = "NUKIBridge_Undef"; + $hash->{AttrFn} = "NUKIBridge_Attr"; + $hash->{AttrList} = "disable:1 ". $readingFnAttributes; foreach my $d(sort keys %{$modules{NUKIBridge}{defptr}}) { - my $hash = $modules{NUKIBridge}{defptr}{$d}; - $hash->{VERSION} = $version; + my $hash = $modules{NUKIBridge}{defptr}{$d}; + $hash->{VERSION} = $version; } } @@ -103,15 +103,16 @@ sub NUKIBridge_Define($$) { - my $name = $a[0]; - my $host = $a[2]; + my $name = $a[0]; + my $host = $a[2]; my $token = $a[3]; - my $port = 8080; + my $port = 8080; - $hash->{HOST} = $host; - $hash->{PORT} = $port; - $hash->{TOKEN} = $token; - $hash->{VERSION} = $version; + $hash->{HOST} = $host; + $hash->{PORT} = $port; + $hash->{TOKEN} = $token; + $hash->{VERSION} = $version; + $hash->{helper}{aliveCount} = 0; @@ -157,23 +158,23 @@ sub NUKIBridge_Attr(@) { if( $attrName eq "disable" ) { if( $cmd eq "set" and $attrVal eq "1" ) { - readingsSingleUpdate ( $hash, "state", "disabled", 1 ); + readingsSingleUpdate ( $hash, "state", "disabled", 1 ); Log3 $name, 3, "NUKIBridge ($name) - disabled"; - } - - elsif( $cmd eq "del" ) { + } + + elsif( $cmd eq "del" ) { readingsSingleUpdate ( $hash, "state", "active", 1 ); Log3 $name, 3, "NUKIBridge ($name) - enabled"; } } if( $attrName eq "disabledForIntervals" ) { - if( $cmd eq "set" ) { + if( $cmd eq "set" ) { Log3 $name, 3, "NUKIBridge ($name) - enable disabledForIntervals"; readingsSingleUpdate ( $hash, "state", "Unknown", 1 ); - } - - elsif( $cmd eq "del" ) { + } + + elsif( $cmd eq "del" ) { readingsSingleUpdate ( $hash, "state", "active", 1 ); Log3 $name, 3, "NUKIBridge ($name) - delete disabledForIntervals"; } @@ -205,28 +206,28 @@ sub NUKIBridge_Set($@) { } elsif($cmd eq 'fwUpdate') { return "usage: fwUpdate" if( @args != 0 ); - NUKIBridge_CallBlocking($hash,"fwupdate",undef); + NUKIBridge_CallBlocking($hash,"fwupdate",undef) if( !IsDisabled($name) ); return undef; } elsif($cmd eq 'reboot') { return "usage: reboot" if( @args != 0 ); - NUKIBridge_CallBlocking($hash,"reboot",undef); + NUKIBridge_CallBlocking($hash,"reboot",undef) if( !IsDisabled($name) ); return undef; } elsif($cmd eq 'clearLog') { return "usage: clearLog" if( @args != 0 ); - NUKIBridge_CallBlocking($hash,"clearlog",undef); + 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( $resp->{success} eq "true" ) { + 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"; @@ -247,12 +248,12 @@ sub NUKIBridge_Get($@) { if($cmd eq 'logFile') { return "usage: logFile" if( @args != 0 ); - NUKIBridge_getLogfile($hash); + NUKIBridge_getLogfile($hash) if( !IsDisabled($name) ); } elsif($cmd eq 'callbackList') { return "usage: callbackList" if( @args != 0 ); - NUKIBridge_getCallbackList($hash); + NUKIBridge_getCallbackList($hash) if( !IsDisabled($name) ); } else { my $list = "logFile:noArg callbackList:noArg"; @@ -273,7 +274,7 @@ sub NUKIBridge_GetCheckBridgeAlive($) { NUKIBridge_Call($hash,$hash,"alive",undef,undef); - InternalTimer( gettimeofday()+30+int(rand(13)), "NUKIBridge_GetCheckBridgeAlive", $hash, 1 ); + InternalTimer( gettimeofday()+17+int(rand(15)), "NUKIBridge_GetCheckBridgeAlive", $hash, 1 ); Log3 $name, 4, "NUKIBridge ($name) - Call InternalTimer for NUKIBridge_GetCheckBridgeAlive"; } } @@ -300,7 +301,6 @@ sub NUKIBridge_Call($$$$$) { my $port = $hash->{PORT}; my $token = $hash->{TOKEN}; - return "Only single call to Bridge, please try again later" if( $hash->{helper}{BRIDGE_CALL} ); my $uri = "http://" . $hash->{HOST} . ":" . $port; $uri .= "/" . $path if( defined $path); @@ -309,19 +309,18 @@ sub NUKIBridge_Call($$$$$) { $uri .= "&url=" . $lockAction if( defined($lockAction) and $path eq "callback/add" ); $uri .= "&nukiId=" . $nukiId if( defined($nukiId) ); - $hash->{helper}{BRIDGE_CALL} = 1; HttpUtils_NonblockingGet( - { - url => $uri, - timeout => 30, - hash => $hash, - chash => $chash, - endpoint => $path, - header => "Accept: application/json", - method => "GET", - callback => \&NUKIBridge_Distribution, - } + { + url => $uri, + timeout => 30, + hash => $hash, + chash => $chash, + endpoint => $path, + header => "Accept: application/json", + method => "GET", + callback => \&NUKIBridge_Distribution, + } ); Log3 $name, 4, "NUKIBridge ($name) - Send HTTP POST with URL $uri"; @@ -335,22 +334,21 @@ sub NUKIBridge_Distribution($$$) { my $name = $hash->{NAME}; my $host = $hash->{HOST}; - delete $hash->{helper}{BRIDGE_CALL}; readingsBeginUpdate($hash); if( defined( $err ) ) { - - if ( $err ne "" ) { + if ( $err ne "" ) { if ($param->{endpoint} eq "alive") { - readingsBulkUpdate( $hash, "state", "not connected"); + 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; - } + } } if( $json eq "" and exists( $param->{code} ) && $param->{code} ne 200 ) { @@ -360,14 +358,15 @@ sub NUKIBridge_Distribution($$$) { 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"; + 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"; + readingsEndUpdate( $hash, 1 ); + return "received http code ".$param->{code}." without any data after requesting"; } if( ( $json =~ /Error/i ) and exists( $param->{code} ) ) { @@ -381,13 +380,13 @@ sub NUKIBridge_Distribution($$$) { 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 ) { + 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}'; @@ -400,7 +399,7 @@ sub NUKIBridge_Distribution($$$) { readingsEndUpdate( $hash, 1 ); - return $param->{code}; + return $param->{code}; } if( $hash == $param->{chash} ) { @@ -557,8 +556,9 @@ sub NUKIBridge_getLogfile($) { $ret .= "Type"; $ret .= ''; + foreach my $logs (@{$decode_json}) { - + $ret .= "$logs->{timestamp}"; $ret .= " "; $ret .= "$logs->{type}"; @@ -620,14 +620,12 @@ sub NUKIBridge_CallBlocking($$$) { my $port = $hash->{PORT}; my $token = $hash->{TOKEN}; - return "Only single call to Bridge, please try again later" if( $hash->{helper}{BRIDGE_CALL} ); my $url = "http://" . $hash->{HOST} . ":" . $port; $url .= "/" . $path if( defined $path); $url .= "?token=" . $token if( defined($token) ); $url .= "&" . $obj if( defined($obj) ); - $hash->{helper}{BRIDGE_CALL} = 1; my($err,$data) = HttpUtils_BlockingGet({ url => $url, @@ -635,8 +633,7 @@ sub NUKIBridge_CallBlocking($$$) { method => "GET", header => "Content-Type: application/json", }); - - delete $hash->{helper}{BRIDGE_CALL}; + if( !$data ) { Log3 $name, 3, "NUKIDevice ($name) - empty answer received for $url"; @@ -753,6 +750,15 @@ sub NUKIBridge_CallBlocking($$$) {
  • 0_name - Name des ersten gefunden Nuki Smartlocks
  • smartlockCount - Anzahl aller gefundenen Smartlock
  • bridgeAPI - API Version der Bridge
  • +
  • bridgeType - Hardware oder Software/App Bridge
  • +
  • currentTime - aktuelle Zeit auf der Bridge zum zeitpunkt des Info holens
  • +
  • firmwareVersion - aktuell auf der Bridge verwendete Firmwareversion
  • +
  • hardwareId - ID der Hardware Bridge
  • +
  • lastError - gibt die letzte HTTP Errormeldung wieder
  • +
  • serverConnected - true/false gibt an ob die Hardwarebridge Verbindung zur Nuki-Cloude hat.
  • +
  • serverId - gibt die ID des Cloudeservers wieder
  • +
  • uptime - Uptime der Bridge in Sekunden
  • +
  • wifiFirmwareVersion- Firmwareversion des Wifi Modules der Bridge

  • Die vorangestellte Zahl ist forlaufend und gibt beginnend bei 0 die Eigenschaften Eines Smartlocks wieder. @@ -761,7 +767,19 @@ sub NUKIBridge_CallBlocking($$$) { Set +

    + + Get +

    diff --git a/74_NUKIDevice.pm b/74_NUKIDevice.pm index 9e719f3..6f2a9b4 100644 --- a/74_NUKIDevice.pm +++ b/74_NUKIDevice.pm @@ -42,25 +42,24 @@ sub NUKIDevice_Initialize($) { my ($hash) = @_; - $hash->{SetFn} = "NUKIDevice_Set"; - $hash->{DefFn} = "NUKIDevice_Define"; - $hash->{UndefFn} = "NUKIDevice_Undef"; - $hash->{AttrFn} = "NUKIDevice_Attr"; + $hash->{SetFn} = "NUKIDevice_Set"; + $hash->{DefFn} = "NUKIDevice_Define"; + $hash->{UndefFn} = "NUKIDevice_Undef"; + $hash->{AttrFn} = "NUKIDevice_Attr"; - my $webhookFWinstance = join( ",", devspec2array('TYPE=FHEMWEB:FILTER=TEMPORARY!=1') ); + my $webhookFWinstance = join( ",", devspec2array('TYPE=FHEMWEB:FILTER=TEMPORARY!=1') ); - $hash->{AttrList} = "IODev ". + $hash->{AttrList} = "IODev ". "disable:1 ". "webhookFWinstance:$webhookFWinstance ". "webhookHttpHostname ". - "webhookPort ". $readingFnAttributes; foreach my $d(sort keys %{$modules{NUKIDevice}{defptr}}) { - my $hash = $modules{NUKIDevice}{defptr}{$d}; - $hash->{VERSION} = $version; + my $hash = $modules{NUKIDevice}{defptr}{$d}; + $hash->{VERSION} = $version; } } @@ -87,8 +86,8 @@ sub NUKIDevice_Define($$) { my ($name,$nukiId) = @a; - $hash->{NUKIID} = $nukiId; - $hash->{VERSION} = $version; + $hash->{NUKIID} = $nukiId; + $hash->{VERSION} = $version; $hash->{STATE} = 'Initialized'; my $infix = "NUKIDevice"; @@ -167,24 +166,26 @@ sub NUKIDevice_Attr(@) { my $token = $hash->{IODev}->{TOKEN}; if( $attrName eq "disable" ) { - if( $cmd eq "set" ) { - if( $attrVal eq "0" ) { - RemoveInternalTimer( $hash ); - InternalTimer( gettimeofday()+2, "NUKIDevice_GetUpdateInternalTimer", $hash, 0 ); - readingsSingleUpdate ( $hash, "state", "Initialized", 1 ); - Log3 $name, 3, "NUKIDevice ($name) - enabled"; - } else { - readingsSingleUpdate ( $hash, "state", "disabled", 1 ); - RemoveInternalTimer( $hash ); - Log3 $name, 3, "NUKIDevice ($name) - disabled"; - } - - } else { - - RemoveInternalTimer( $hash ); - InternalTimer( gettimeofday()+2, "NUKIDevice_GetUpdateInternalTimer", $hash, 0 ); - readingsSingleUpdate ( $hash, "state", "Initialized", 1 ); - Log3 $name, 3, "NUKIDevice ($name) - enabled"; + if( $cmd eq "set" and $attrVal eq "1" ) { + 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"; + } + } + + if( $attrName eq "disabledForIntervals" ) { + if( $cmd eq "set" ) { + 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"; } } @@ -251,7 +252,7 @@ sub NUKIDevice_removeExtension($) { my ($link) = @_; - my $url = "?/$link"; + my $url = "/$link"; my $name = $data{FWEXT}{$url}{deviceName}; Log3 $name, 2, "NUKIDevice ($name) - Unregistering NUKIDevice for webhook URL $url..."; @@ -294,28 +295,11 @@ sub NUKIDevice_Set($$@) { } $hash->{helper}{lockAction} = $lockAction; - NUKIDevice_ReadFromNUKIBridge($hash,"lockAction",$lockAction,$hash->{NUKIID} ); + NUKIDevice_ReadFromNUKIBridge($hash,"lockAction",$lockAction,$hash->{NUKIID} ) if( !IsDisabled($name) ); return undef; } -sub NUKIDevice_GetUpdateTimer($) { - - my ($hash) = @_; - my $name = $hash->{NAME}; - - - RemoveInternalTimer($hash); - - if( !IsDisabled($name) ) { - NUKIDevice_ReadFromNUKIBridge($hash, "lockState", undef, $hash->{NUKIID} ); - Log3 $name, 5, "NUKIDevice ($name) - NUKIDevice_GetUpdateTimer Call NUKIDevice_ReadFromNUKIBridge"; - InternalTimer( gettimeofday()+12+int(rand(18)), "NUKIDevice_GetUpdateTimer", $hash, 1 ); - } - - return undef; -} - sub NUKIDevice_GetUpdate($) { my ($hash) = @_; @@ -469,7 +453,9 @@ sub NUKIDevice_CGI() { my $name; my $nukiId; - # data received Testaufruf: wget --post-data '{"nukiId": 102844, "state": 1,"stateName": "locked", "batteryCritical": false}' http://10.6.6.20:8083/fhem/NUKIDevice-102844 + # data received + # Testaufruf: 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); printf "\n\nHTTP Header: $header\n\n"; @@ -576,6 +562,8 @@ sub NUKIDevice_CGI() { Attributes