MQTT2*: change autocreate to simple/complex (Forum #98130)

git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@18794 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2019-03-05 10:56:08 +00:00
parent f579a39d80
commit 68b1a74a34
3 changed files with 43 additions and 22 deletions

View File

@ -36,7 +36,7 @@ MQTT2_CLIENT_Initialize($)
no warnings 'qw'; no warnings 'qw';
my @attrList = qw( my @attrList = qw(
autocreate:1,0 autocreate:no,simple,complex
clientId clientId
disable:1,0 disable:1,0
disabledForIntervals disabledForIntervals
@ -341,10 +341,12 @@ MQTT2_CLIENT_Read($@)
if(!IsDisabled($name)) { if(!IsDisabled($name)) {
$val = "" if(!defined($val)); $val = "" if(!defined($val));
my $ac = AttrVal($name, "autocreate", undef) ? "autocreate\0":""; my $ac = AttrVal($name, "autocreate", "no");
$ac = $ac eq "1" ? "simple" : ($ac eq "0" ? "no" : $ac); # backward comp.
my $cid = $hash->{clientId}; my $cid = $hash->{clientId};
$tp =~ s/:/_/g; # 96608 $tp =~ s/:/_/g; # 96608
Dispatch($hash, "$ac$cid\0$tp\0$val", undef, !$ac); Dispatch($hash, "autocreate=$ac\0$cid\0$tp\0$val", undef, $ac eq "no");
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/);
@ -516,12 +518,19 @@ MQTT2_CLIENT_getStr($$)
<ul> <ul>
<a name="autocreate"></a> <a name="autocreate"></a>
<li>autocreate<br> <li>autocreate [no|simple|complex]<br>
if set, at least one MQTT2_DEVICE will be created, and its readingsList if set to simple/complex, at least one MQTT2_DEVICE will be created, and
will be expanded upon reception of published messages. Note: this is its readingsList will be expanded upon reception of published messages.
slightly different from MQTT2_SERVER, where each connection has its own Note: this is slightly different from MQTT2_SERVER, where each connection
clientId. This parameter is sadly not transferred via the MQTT protocol, has its own clientId. This parameter is sadly not transferred via the
so the clientId of this MQTT2_CLIENT instance will be used. MQTT protocol, so the clientId of this MQTT2_CLIENT instance will be
used.<br>
With simple the one-argument version of json2nameValue is added:
json2nameValue($EVENT), with complex the full version:
json2nameValue($EVENT, 'SENSOR_', $JSONMAP). Which one is better depends
on the attached devices and on the personal taste, and it is only
relevant for json payload. For non-json payload there is no difference
between simple and complex.
</li></br> </li></br>
<a name="clientId"></a> <a name="clientId"></a>

View File

@ -40,7 +40,7 @@ MQTT2_SERVER_Initialize($)
no warnings 'qw'; no warnings 'qw';
my @attrList = qw( my @attrList = qw(
SSL:0,1 SSL:0,1
autocreate:0,1 autocreate:no,simple,complex
disable:0,1 disable:0,1
disabledForIntervals disabledForIntervals
keepaliveFactor keepaliveFactor
@ -427,8 +427,10 @@ MQTT2_SERVER_doPublish($$$$;$)
AttrVal($serverName, "rePublish", undef)) { AttrVal($serverName, "rePublish", undef)) {
$cid = $src->{NAME} if(!defined($cid)); $cid = $src->{NAME} if(!defined($cid));
$cid =~ s,[^a-z0-9._],_,gi; $cid =~ s,[^a-z0-9._],_,gi;
my $ac = AttrVal($serverName, "autocreate", 1) ? "autocreate\0":""; my $ac = AttrVal($serverName, "autocreate", "simple");
Dispatch($server, "$ac$cid\0$tp\0$val", undef, !$ac); $ac = $ac eq "1" ? "simple" : ($ac eq "0" ? "no" : $ac); # backward comp.
Dispatch($server, "autocreate=$ac\0$cid\0$tp\0$val", undef, $ac eq "no");
my $re = AttrVal($serverName, "rawEvents", undef); my $re = AttrVal($serverName, "rawEvents", undef);
DoTrigger($server->{NAME}, "$tp:$val") if($re && $tp =~ m/$re/); DoTrigger($server->{NAME}, "$tp:$val") if($re && $tp =~ m/$re/);
} }
@ -642,9 +644,16 @@ MQTT2_SERVER_ReadDebug($$)
</li><br> </li><br>
<a name="autocreate"></a> <a name="autocreate"></a>
<li>autocreate<br> <li>autocreate [no|simple|complex]<br>
MQTT2_DEVICES will be automatically created upon receiving an MQTT2_DEVICES will be automatically created upon receiving an
unknown message. Set this value to 0 to disable autocreating. unknown message. Set this value to no to disable autocreating, the
default is simple.<br>
With simple the one-argument version of json2nameValue is added:
json2nameValue($EVENT), with complex the full version:
json2nameValue($EVENT, 'SENSOR_', $JSONMAP). Which one is better depends
on the attached devices and on the personal taste, and it is only
relevant for json payload. For non-json payload there is no difference
between simple and complex.
</li><br> </li><br>
</ul> </ul>

View File

@ -95,10 +95,10 @@ MQTT2_DEVICE_Parse($$)
} }
} }
my $autocreate; my $autocreate = "no";
if($msg =~ m/^autocreate\0(.*)$/s) { if($msg =~ m/^autocreate=([^\0]+)\0(.*)$/s) {
$msg = $1; $autocreate = $1;
$autocreate = 1; $msg = $2;
} }
my ($cid, $topic, $value) = split("\0", $msg, 3); my ($cid, $topic, $value) = split("\0", $msg, 3);
@ -150,8 +150,8 @@ MQTT2_DEVICE_Parse($$)
} }
################################################# #################################################
# autocreate and/or expand readingList # IODevs autocreate and/or expand readingList
if($autocreate && !%fnd) { if($autocreate ne "no" && !%fnd) {
return "" if($cid && $cid =~ m/mosqpub.*/); return "" if($cid && $cid =~ m/mosqpub.*/);
################## bridge stuff ################## bridge stuff
@ -180,7 +180,10 @@ MQTT2_DEVICE_Parse($$)
my $ret = json2nameValue($value); my $ret = json2nameValue($value);
if(keys %{$ret}) { if(keys %{$ret}) {
$topic =~ m,.*/([^/]+),; $topic =~ m,.*/([^/]+),;
$add = "{ json2nameValue(\$EVENT) }"; my $ltopic = makeReadingName($1)."_";
$add = $autocreate eq "simple" ?
"{ json2nameValue(\$EVENT) }" :
"{ json2nameValue(\$EVENT, '$ltopic', \$JSONMAP) }";
} }
} }
if(!$add) { if(!$add) {
@ -206,7 +209,7 @@ MQTT2_DEVICE_Parse($$)
for my $ch (@{$cidArr}) { for my $ch (@{$cidArr}) {
my $nn = $ch->{NAME}; my $nn = $ch->{NAME};
next if(!AttrVal($nn, "autocreate", 1)); next if(!AttrVal($nn, "autocreate", 1)); # device autocreate
my $rl = AttrVal($nn, "readingList", ""); my $rl = AttrVal($nn, "readingList", "");
$rl .= "\n" if($rl); $rl .= "\n" if($rl);
my $regex = ($cid eq $newCid ? "$cid:" : "").$topic.":.*"; my $regex = ($cid eq $newCid ? "$cid:" : "").$topic.":.*";