Kompletter Umbau auf serielle Anfragen an die Bridge

This commit is contained in:
Marko Oldenburg 2016-11-16 11:33:03 +01:00
parent 135d1dc99a
commit 355d57c2b1
2 changed files with 40 additions and 120 deletions

View File

@ -46,7 +46,7 @@ use JSON;
use HttpUtils; use HttpUtils;
my $version = "0.3.15"; my $version = "0.3.22";
@ -77,8 +77,7 @@ sub NUKIBridge_Initialize($) {
$hash->{DefFn} = "NUKIBridge_Define"; $hash->{DefFn} = "NUKIBridge_Define";
$hash->{UndefFn} = "NUKIBridge_Undef"; $hash->{UndefFn} = "NUKIBridge_Undef";
$hash->{AttrFn} = "NUKIBridge_Attr"; $hash->{AttrFn} = "NUKIBridge_Attr";
$hash->{AttrList} = "interval ". $hash->{AttrList} = "disable:1 ".
"disable:1 ".
"webhookFWinstance:$webhookFWinstance ". "webhookFWinstance:$webhookFWinstance ".
$readingFnAttributes; $readingFnAttributes;
@ -111,12 +110,10 @@ sub NUKIBridge_Define($$) {
my $host = $a[2]; my $host = $a[2];
my $token = $a[3]; my $token = $a[3];
my $port = 8080; my $port = 8080;
my $interval = 60;
$hash->{HOST} = $host; $hash->{HOST} = $host;
$hash->{PORT} = $port; $hash->{PORT} = $port;
$hash->{TOKEN} = $token; $hash->{TOKEN} = $token;
$hash->{INTERVAL} = $interval;
$hash->{VERSION} = $version; $hash->{VERSION} = $version;
@ -164,7 +161,7 @@ sub NUKIBridge_Attr(@) {
if( $cmd eq "set" ) { if( $cmd eq "set" ) {
if( $attrVal eq "0" ) { if( $attrVal eq "0" ) {
RemoveInternalTimer( $hash ); RemoveInternalTimer( $hash );
InternalTimer( gettimeofday()+2, "NUKIBridge_GetCheckBridgeAlive", $hash, 0 ); InternalTimer( gettimeofday()+2, "NUKIBridge_GetSmartlocksState", $hash, 0 );
readingsSingleUpdate($hash, 'state', 'Initialized', 1 ); readingsSingleUpdate($hash, 'state', 'Initialized', 1 );
Log3 $name, 3, "NUKIBridge ($name) - enabled"; Log3 $name, 3, "NUKIBridge ($name) - enabled";
} else { } else {
@ -175,34 +172,12 @@ sub NUKIBridge_Attr(@) {
} else { } else {
RemoveInternalTimer( $hash ); RemoveInternalTimer( $hash );
InternalTimer( gettimeofday()+2, "NUKIBridge_GetCheckBridgeAlive", $hash, 0 ); InternalTimer( gettimeofday()+2, "NUKIBridge_GetSmartlocksState", $hash, 0 );
readingsSingleUpdate($hash, 'state', 'Initialized', 1 ); readingsSingleUpdate($hash, 'state', 'Initialized', 1 );
Log3 $name, 3, "NUKIBridge ($name) - enabled"; Log3 $name, 3, "NUKIBridge ($name) - enabled";
} }
} }
if( $attrName eq "interval" ) {
if( $cmd eq "set" ) {
if( $attrVal < 30 ) {
Log3 $name, 3, "NUKIBridge ($name) - interval too small, please use something > 30 (sec), default is 60 (sec)";
return "interval too small, please use something > 30 (sec), default is 60 (sec)";
} else {
$hash->{INTERVAL} = $attrVal;
Log3 $name, 3, "NUKIBridge ($name) - set interval to $attrVal";
}
}
elsif( $cmd eq "del" ) {
$hash->{INTERVAL} = 60;
Log3 $name, 3, "NUKIBridge ($name) - set interval to default";
} else {
if( $cmd eq "set" ) {
$attr{$name}{$attrName} = $attrVal;
Log3 $name, 3, "NUKIBridge ($name) - $attrName : $attrVal";
}
}
}
# webhook* # webhook*
if ( $attrName =~ /^webhook.*/ ) { if ( $attrName =~ /^webhook.*/ ) {
my $webhookHttpHostname = ( my $webhookHttpHostname = (
@ -266,7 +241,7 @@ sub NUKIBridge_Set($@) {
return undef; return undef;
} elsif($cmd eq 'statusRequest') { } elsif($cmd eq 'info') {
return "usage: statusRequest" if( @args != 0 ); return "usage: statusRequest" if( @args != 0 );
NUKIBridge_Call($hash,$hash,"info",undef,undef) if( !IsDisabled($name) ); NUKIBridge_Call($hash,$hash,"info",undef,undef) if( !IsDisabled($name) );
@ -293,7 +268,7 @@ sub NUKIBridge_Set($@) {
NUKIBridge_CallBlocking($hash,"clearlog",undef); NUKIBridge_CallBlocking($hash,"clearlog",undef);
} else { } else {
my $list = "statusRequest:noArg autocreate:noArg clearLog:noArg fwUpdate:noArg reboot:noArg"; my $list = "info:noArg autocreate:noArg clearLog:noArg fwUpdate:noArg reboot:noArg";
return "Unknown argument $cmd, choose one of $list"; return "Unknown argument $cmd, choose one of $list";
} }
@ -324,34 +299,13 @@ sub NUKIBridge_firstRun($) {
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
NUKIBridge_Call($hash,$hash,"list",undef,undef) if( !IsDisabled($name) ); NUKIBridge_Call($hash,$hash,"list",undef,undef) if( !IsDisabled($name) );
InternalTimer( gettimeofday()+3, "NUKIBridge_GetCheckBridgeAlive", $hash, 0 );
Log3 $name, 4, "NUKIBridge ($name) - Call NUKIBridge_Get" if( !IsDisabled($name) );
return 1; return undef;
}
sub NUKIBridge_GetCheckBridgeAlive($) {
my ($hash) = @_;
my $name = $hash->{NAME};
RemoveInternalTimer($hash);
if( !IsDisabled($name) ) {
NUKIBridge_Call($hash,$hash,"info",undef,undef);
InternalTimer( gettimeofday()+$hash->{INTERVAL}, "NUKIBridge_GetCheckBridgeAlive", $hash, 1 );
Log3 $name, 4, "NUKIBridge ($name) - Call InternalTimer for NUKIBridge_GetCheckBridgeAlive";
}
return 1;
} }
sub NUKIBridge_Call($$$$$) { sub NUKIBridge_Call($$$$$) {
my ($hash,$chash,$path,$lockAction,$nukiId,) = @_; my ($hash,$chash,$path,$lockAction,$nukiId) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $host = $hash->{HOST}; my $host = $hash->{HOST};
@ -369,14 +323,14 @@ sub NUKIBridge_Call($$$$$) {
HttpUtils_NonblockingGet( HttpUtils_NonblockingGet(
{ {
url => $uri, url => $uri,
timeout => 15, timeout => 30,
hash => $hash, hash => $hash,
chash => $chash, chash => $chash,
endpoint => $path, endpoint => $path,
header => "Accept: application/json", header => "Accept: application/json",
method => "GET", method => "GET",
callback => \&NUKIBridge_Distribution, callback => \&NUKIBridge_Distribution,
} }
); );
@ -386,13 +340,13 @@ sub NUKIBridge_Call($$$$$) {
sub NUKIBridge_Distribution($$$) { sub NUKIBridge_Distribution($$$) {
my ( $param, $err, $json ) = @_; my ( $param, $err, $json ) = @_;
my $hash = $param->{hash}; my $hash = $param->{hash};
my $doTrigger = $param->{doTrigger}; my $doTrigger = $param->{doTrigger};
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $host = $hash->{HOST}; my $host = $hash->{HOST};
Log3 $name, 3, "NUKIBridge ($name) - Param Alive: $param->{alive}"; Log3 $name, 3, "NUKIBridge ($name) - Param Alive: $param->{alive}";
Log3 $name, 3, "NUKIBridge ($name) - Param Code: $param->{code}"; Log3 $name, 3, "NUKIBridge ($name) - Param Code: $param->{code}";
Log3 $name, 3, "NUKIBridge ($name) - Error: $err"; Log3 $name, 3, "NUKIBridge ($name) - Error: $err";
@ -401,7 +355,6 @@ sub NUKIBridge_Distribution($$$) {
readingsBeginUpdate($hash); readingsBeginUpdate($hash);
if( defined( $err ) ) { if( defined( $err ) ) {
@ -465,7 +418,7 @@ sub NUKIBridge_Distribution($$$) {
} else { } else {
NUKIDevice_Parse($param->{chash},$json,$param->{endpoint}); NUKIDevice_Parse($param->{chash},$json);
} }
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
@ -644,7 +597,7 @@ sub NUKIBridge_CallBlocking($$$) {
my($err,$data) = HttpUtils_BlockingGet({ my($err,$data) = HttpUtils_BlockingGet({
url => $url, url => $url,
timeout => 3, timeout => 5,
method => "GET", method => "GET",
header => "Content-Type: application/json", header => "Content-Type: application/json",
}); });
@ -779,7 +732,6 @@ sub NUKIBridge_CGI() {
<b>Attributes</b> <b>Attributes</b>
<ul> <ul>
<li>disable - disables the Nuki Bridge</li> <li>disable - disables the Nuki Bridge</li>
<li>interval - changes the interval for the CheckAlive</li>
<br> <br>
</ul> </ul>
</ul> </ul>
@ -831,7 +783,6 @@ sub NUKIBridge_CGI() {
<b>Attribute</b> <b>Attribute</b>
<ul> <ul>
<li>disable - deaktiviert die Nuki Bridge</li> <li>disable - deaktiviert die Nuki Bridge</li>
<li>interval - verändert den Interval für den CheckAlive</li>
<br> <br>
</ul> </ul>
</ul> </ul>

View File

@ -33,7 +33,7 @@ use warnings;
use JSON; use JSON;
#use Time::HiRes qw(gettimeofday); #use Time::HiRes qw(gettimeofday);
my $version = "0.3.15"; my $version = "0.3.22";
@ -49,7 +49,6 @@ sub NUKIDevice_Initialize($) {
$hash->{AttrList} = "IODev ". $hash->{AttrList} = "IODev ".
"disable:1 ". "disable:1 ".
"interval ".
$readingFnAttributes; $readingFnAttributes;
@ -86,7 +85,6 @@ sub NUKIDevice_Define($$) {
$hash->{NUKIID} = $nukiId; $hash->{NUKIID} = $nukiId;
$hash->{VERSION} = $version; $hash->{VERSION} = $version;
$hash->{STATE} = 'Initialized'; $hash->{STATE} = 'Initialized';
$hash->{INTERVAL} = 20;
AssignIoPort($hash,$iodev) if( !$hash->{IODev} ); AssignIoPort($hash,$iodev) if( !$hash->{IODev} );
@ -116,15 +114,6 @@ sub NUKIDevice_Define($$) {
Log3 $name, 3, "NUKIDevice ($name) - defined with Code: $code"; Log3 $name, 3, "NUKIDevice ($name) - defined with Code: $code";
$attr{$name}{room} = "NUKI" if( !defined( $attr{$name}{room} ) ); $attr{$name}{room} = "NUKI" if( !defined( $attr{$name}{room} ) );
RemoveInternalTimer($hash);
if( $init_done ) {
NUKIDevice_GetUpdateInternalTimer($hash);
} else {
InternalTimer(gettimeofday()+20, "NUKIDevice_GetUpdateInternalTimer", $hash, 0);
}
return undef; return undef;
} }
@ -174,23 +163,6 @@ sub NUKIDevice_Attr(@) {
} }
} }
if( $attrName eq "interval" ) {
if( $cmd eq "set" ) {
if( $attrVal < 10 ) {
Log3 $name, 3, "NUKIDevice ($name) - interval too small, please use something > 10 (sec), default is 20 (sec)";
return "interval too small, please use something > 10 (sec), default is 60 (sec)";
} else {
$hash->{INTERVAL} = $attrVal;
Log3 $name, 3, "NUKIDevice ($name) - set interval to $attrVal";
}
} else {
$hash->{INTERVAL} = 20;
Log3 $name, 3, "NUKIDevice ($name) - set interval to default";
}
}
return undef; return undef;
} }
@ -247,20 +219,6 @@ sub NUKIDevice_GetUpdate($) {
return undef; return undef;
} }
sub NUKIDevice_GetUpdateInternalTimer($) {
my ($hash) = @_;
my $name = $hash->{NAME};
NUKIDevice_GetUpdate($hash);
Log3 $name, 5, "NUKIDevice ($name) - Call NUKIDevice_GetUpdate";
RemoveInternalTimer($hash);
InternalTimer(gettimeofday()+$hash->{INTERVAL}, "NUKIDevice_GetUpdateInternalTimer", $hash, 1) if( $hash->{INTERVAL} );
Log3 $name, 5, "NUKIDevice ($name) - Call InternalTimer";
}
sub NUKIDevice_ReadFromNUKIBridge($@) { sub NUKIDevice_ReadFromNUKIBridge($@) {
my ($hash,@a) = @_; my ($hash,@a) = @_;
@ -291,9 +249,9 @@ sub NUKIDevice_ReadFromNUKIBridge($@) {
return $ret; return $ret;
} }
sub NUKIDevice_Parse($$$) { sub NUKIDevice_Parse($$) {
my($hash,$result,$path) = @_; my($hash,$result) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
@ -317,9 +275,9 @@ sub NUKIDevice_Parse($$$) {
######################################### #########################################
#### verarbeiten des JSON Strings ####### #### verarbeiten des JSON Strings #######
my $decode_json = decode_json($result); my $decode_json = decode_json($result);
if( ref($decode_json) ne "HASH" ) { if( ref($decode_json) ne "HASH" ) {
Log3 $name, 2, "$name: got wrong status message for $name: $decode_json"; Log3 $name, 2, "$name: got wrong status message for $name: $decode_json";
return undef; return undef;
@ -327,6 +285,14 @@ sub NUKIDevice_Parse($$$) {
Log3 $name, 5, "parse status message for $name"; Log3 $name, 5, "parse status message for $name";
NUKIDevice_WriteReadings($hash,$decode_json);
}
sub NUKIDevice_WriteReadings($$) {
my ($hash,$decode_json) = @_;
my $name = $hash->{NAME};
############################ ############################
#### Status des Smartlock #### Status des Smartlock
@ -359,6 +325,7 @@ sub NUKIDevice_Parse($$$) {
readingsBulkUpdate( $hash, "battery", $battery ); readingsBulkUpdate( $hash, "battery", $battery );
delete $hash->{helper}{lockAction}; delete $hash->{helper}{lockAction};
Log3 $name, 5, "readings set for $name";
} else { } else {
@ -378,6 +345,10 @@ sub NUKIDevice_Parse($$$) {
1; 1;
@ -438,7 +409,6 @@ sub NUKIDevice_Parse($$$) {
<b>Attributes</b> <b>Attributes</b>
<ul> <ul>
<li>disable - disables the Nuki device</li> <li>disable - disables the Nuki device</li>
<li>interval - changes the interval for the statusRequest</li>
<br> <br>
</ul> </ul>
</ul> </ul>
@ -494,7 +464,6 @@ sub NUKIDevice_Parse($$$) {
<b>Attribute</b> <b>Attribute</b>
<ul> <ul>
<li>disable - deaktiviert das Nuki Device</li> <li>disable - deaktiviert das Nuki Device</li>
<li>interval - verändert den Interval für den statusRequest</li>
<br> <br>
</ul> </ul>
</ul> </ul>