2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-04 05:16:45 +00:00

00_MQTT2_CLIENT.pm: fix connect message, add onConnect attribtute (Forum #92946)

git-svn-id: https://svn.fhem.de/fhem/trunk@17725 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2018-11-10 17:43:12 +00:00
parent 200d9e0b13
commit 35b8482891

View File

@ -40,6 +40,7 @@ MQTT2_CLIENT_Initialize($)
lwt lwt
lwtRetain lwtRetain
mqttVersion:3.1.1,3.1 mqttVersion:3.1.1,3.1
onConnect
rawEvents rawEvents
subscriptions subscriptions
SSL SSL
@ -285,14 +286,16 @@ MQTT2_CLIENT_Read($@)
#################################### ####################################
if($cpt eq "CONNACK") { if($cpt eq "CONNACK") {
my $rc = ord(substr($fb,1,1)); my $rc = ord(substr($pl,1,1));
if($rc == 0) { if($rc == 0) {
my $onc = AttrVal($name, "onConnect", "");
MQTT2_CLIENT_doPublish($hash, split(" ", $onc, 2)) if($onc);
MQTT2_CLIENT_doinit($hash); MQTT2_CLIENT_doinit($hash);
} else { } else {
my @txt = ("Accepted", "bad proto", "bad id", "server unavailable", my @txt = ("Accepted", "bad proto", "bad id", "server unavailable",
"bad user name or password", "not authorized"); "bad user name or password", "not authorized");
Log3 1, $name, "$name: Connection refused, ". Log3 $name, 1, "$name: Connection refused, ".
($rc <= int(@txt) ? $txt[$rc] : "unknown error $rc"); ($rc <= int(@txt) ? $txt[$rc] : "unknown error $rc");
MQTT2_CLIENT_Disco($hash); MQTT2_CLIENT_Disco($hash);
return; return;
@ -314,13 +317,15 @@ MQTT2_CLIENT_Read($@)
$val = substr($pl, $off); $val = substr($pl, $off);
addToWritebuffer($hash, pack("CCnC*", 0x40, 2, $pid)) if($qos); # PUBACK addToWritebuffer($hash, pack("CCnC*", 0x40, 2, $pid)) if($qos); # PUBACK
$val = "" if(!defined($val)); if(!IsDisabled($name)) {
my $ac = AttrVal($name, "autocreate", undef) ? "autocreate:":""; $val = "" if(!defined($val));
my $cid = $hash->{clientId}; my $ac = AttrVal($name, "autocreate", undef) ? "autocreate:":"";
Dispatch($hash, "$ac$cid:$tp:$val", undef, !$ac); my $cid = $hash->{clientId};
Dispatch($hash, "$ac$cid:$tp:$val", undef, !$ac);
my $re = AttrVal($name, "rawEvents", undef); my $re = AttrVal($name, "rawEvents", undef);
DoTrigger($name, "$tp:$val") if($re && $tp =~ m/$re/); DoTrigger($name, "$tp:$val") if($re && $tp =~ m/$re/);
}
} else { } else {
Log 1, "M2: Unhandled packet $cpt, disconneting $name"; Log 1, "M2: Unhandled packet $cpt, disconneting $name";
@ -483,6 +488,11 @@ MQTT2_CLIENT_getStr($$)
set the MQTT protocol version in the CONNECT header, default is 3.1 set the MQTT protocol version in the CONNECT header, default is 3.1
</li></br> </li></br>
<a name="onConnect"></a>
<li>onConnect topic message<br>
publish the topic after each connect or reconnect.
</li></br>
<a name="rawEvents"></a> <a name="rawEvents"></a>
<li>rawEvents &lt;topic-regexp&gt;<br> <li>rawEvents &lt;topic-regexp&gt;<br>
send all messages as events attributed to this MQTT2_CLIENT instance. send all messages as events attributed to this MQTT2_CLIENT instance.