Commandref für Callback und neue Readings der Bridge an gepasst

This commit is contained in:
Marko Oldenburg 2016-12-17 20:47:58 +01:00
parent 35d8a87efc
commit e424250cc1
2 changed files with 118 additions and 112 deletions

View File

@ -112,6 +112,7 @@ sub NUKIBridge_Define($$) {
$hash->{PORT} = $port;
$hash->{TOKEN} = $token;
$hash->{VERSION} = $version;
$hash->{helper}{aliveCount} = 0;
@ -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,7 +309,6 @@ 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(
{
@ -335,15 +334,14 @@ 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 ($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" );
@ -360,6 +358,7 @@ sub NUKIBridge_Distribution($$$) {
Log3 $name, 5, "NUKIBridge ($name) - Bridge ist online";
readingsEndUpdate( $hash, 1 );
$hash->{helper}{aliveCount} = 0;
return;
}
@ -557,6 +556,7 @@ sub NUKIBridge_getLogfile($) {
$ret .= "<td><b>Type</b></td>";
$ret .= '</tr>';
foreach my $logs (@{$decode_json}) {
$ret .= "<td>$logs->{timestamp}</td>";
@ -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,
@ -636,7 +634,6 @@ sub NUKIBridge_CallBlocking($$$) {
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($$$) {
<li>0_name - Name des ersten gefunden Nuki Smartlocks</li>
<li>smartlockCount - Anzahl aller gefundenen Smartlock</li>
<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>
<br>
Die vorangestellte Zahl ist forlaufend und gibt beginnend bei 0 die Eigenschaften <b>Eines</b> Smartlocks wieder.
</ul>
@ -761,7 +767,19 @@ sub NUKIBridge_CallBlocking($$$) {
<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>statusRequest - startet einen checkAlive der Bridge, es wird festgestellt ob die Bridge noch online ist</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>
<br>
</ul>
<br><br>

View File

@ -53,7 +53,6 @@ sub NUKIDevice_Initialize($) {
"disable:1 ".
"webhookFWinstance:$webhookFWinstance ".
"webhookHttpHostname ".
"webhookPort ".
$readingFnAttributes;
@ -167,27 +166,29 @@ 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 {
if( $cmd eq "set" and $attrVal eq "1" ) {
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 );
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";
}
}
######################
#### webhook #########
@ -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,24 +295,7 @@ sub NUKIDevice_Set($$@) {
}
$hash->{helper}{lockAction} = $lockAction;
NUKIDevice_ReadFromNUKIBridge($hash,"lockAction",$lockAction,$hash->{NUKIID} );
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 );
}
NUKIDevice_ReadFromNUKIBridge($hash,"lockAction",$lockAction,$hash->{NUKIID} ) if( !IsDisabled($name) );
return undef;
}
@ -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() {
<b>Attributes</b>
<ul>
<li>disable - disables the 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>