diff --git a/fhem/FHEM/00_MQTT2_CLIENT.pm b/fhem/FHEM/00_MQTT2_CLIENT.pm index dd5a0179c..19f9a5078 100644 --- a/fhem/FHEM/00_MQTT2_CLIENT.pm +++ b/fhem/FHEM/00_MQTT2_CLIENT.pm @@ -41,6 +41,7 @@ MQTT2_CLIENT_Initialize($) clientId disable:1,0 disabledForIntervals + disconnectAfter ignoreRegexp lwt lwtRetain @@ -164,6 +165,12 @@ MQTT2_CLIENT_doinit($) MQTT2_CLIENT_doPublish($hash, $r->[0], $r->[1], $r->[2], 0); } } + if($hash->{sendHash}) { + map { MQTT2_CLIENT_doPublish($hash,$_->[1],$_->[2],$_->[3]) } + @{$hash->{sendHash}}; + delete($hash->{sendHash}); + } + MQTT2_CLIENT_updateDisconnectTimer($hash); } @@ -211,8 +218,32 @@ MQTT2_CLIENT_Disco($;$$) } $isUndef ? DevIo_CloseDev($hash) : DevIo_Disconnected($hash); delete($hash->{BUF}); + + if($hash->{disconnectTimerHash}) { + readingsSingleUpdate($hash, "state", "disconnected", 1); + RemoveInternalTimer($hash->{disconnectTimerHash}); + delete($hash->{disconnectTimerHash}); + } } +sub +MQTT2_CLIENT_updateDisconnectTimer($) +{ + my ($hash) = @_; + return if(!$hash->{FD} || $hash->{connecting}); + if($hash->{disconnectTimerHash}) { + RemoveInternalTimer($hash->{disconnectTimerHash}); + delete($hash->{disconnectTimerHash}); + delete($hash->{disconnectAt}); + } + my $to = AttrVal($hash->{NAME}, "disconnectAfter", 0); + return if(!$to); + $to += time(); + $hash->{disconnectAt} = FmtDateTime($to); + $hash->{disconnectTimerHash} = { h=>$hash }; + InternalTimer($to, sub{ MQTT2_CLIENT_Disco($_[0]->{h},1) }, + $hash->{disconnectTimerHash}, 0); +} sub MQTT2_CLIENT_Delete($@) @@ -270,6 +301,16 @@ MQTT2_CLIENT_Attr(@) } } + if($attrName eq "disconnectAfter") { + $hash->{devioLoglevel} = ($type eq "set" ? 5 : 0); + return undef if(!$init_done); + InternalTimer(0, sub { + MQTT2_CLIENT_updateDisconnectTimer($hash); + MQTT2_CLIENT_connect($hash) + if(!$hash->{FD} && ($type ne "set" || $param[0] eq "0")); + }, undef, 0); + } + return undef; } @@ -403,6 +444,7 @@ MQTT2_CLIENT_Read($@) } $val = substr($pl, $off); MQTT2_CLIENT_send($hash, pack("CCnC*", 0x40, 2, $pid)) if($qos); # PUBACK + MQTT2_CLIENT_updateDisconnectTimer($hash); if(!IsDisabled($name)) { $val = "" if(!defined($val)); @@ -442,6 +484,15 @@ MQTT2_CLIENT_doPublish($@) return if(IsDisabled($name)); $val = "" if(!defined($val)); + if((!$hash->{FD} || $hash->{connecting}) && + AttrVal($name, "disconnectAfter", undef)) { + $hash->{sendHash} = [] if(!defined($hash->{sendHash})); + push(@{$hash->{sendHash}}, \@_); + MQTT2_CLIENT_connect($hash) if(!$hash->{connecting}); + return; + } + MQTT2_CLIENT_updateDisconnectTimer($hash); + my $hdr = 0x30; my $pi = ""; $hdr += 1 if($retain); @@ -629,6 +680,13 @@ MQTT2_CLIENT_getStr($$) disable dispatching of messages.
+ +
  • disconnectAfter <seconds>
    + if set, the connection will be closed after <seconds> of inactivity + on the connection, and will be automatically reopened when sending a + command. +
  • +
  • ignoreRegexp
    if $topic:$message matches ignoreRegexp, then it will be silently ignored.