2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-10 03:06:37 +00:00

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

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

View File

@ -36,7 +36,7 @@ MQTT2_CLIENT_Initialize($)
no warnings 'qw';
my @attrList = qw(
autocreate:1,0
autocreate:no,simple,complex
clientId
disable:1,0
disabledForIntervals
@ -341,10 +341,12 @@ MQTT2_CLIENT_Read($@)
if(!IsDisabled($name)) {
$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};
$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);
DoTrigger($name, "$tp:$val") if($re && $tp =~ m/$re/);
@ -516,12 +518,19 @@ MQTT2_CLIENT_getStr($$)
<ul>
<a name="autocreate"></a>
<li>autocreate<br>
if set, at least one MQTT2_DEVICE will be created, and its readingsList
will be expanded upon reception of published messages. Note: this is
slightly different from MQTT2_SERVER, where each connection has its own
clientId. This parameter is sadly not transferred via the MQTT protocol,
so the clientId of this MQTT2_CLIENT instance will be used.
<li>autocreate [no|simple|complex]<br>
if set to simple/complex, at least one MQTT2_DEVICE will be created, and
its readingsList will be expanded upon reception of published messages.
Note: this is slightly different from MQTT2_SERVER, where each connection
has its own clientId. This parameter is sadly not transferred via the
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>
<a name="clientId"></a>

View File

@ -40,7 +40,7 @@ MQTT2_SERVER_Initialize($)
no warnings 'qw';
my @attrList = qw(
SSL:0,1
autocreate:0,1
autocreate:no,simple,complex
disable:0,1
disabledForIntervals
keepaliveFactor
@ -427,8 +427,10 @@ MQTT2_SERVER_doPublish($$$$;$)
AttrVal($serverName, "rePublish", undef)) {
$cid = $src->{NAME} if(!defined($cid));
$cid =~ s,[^a-z0-9._],_,gi;
my $ac = AttrVal($serverName, "autocreate", 1) ? "autocreate\0":"";
Dispatch($server, "$ac$cid\0$tp\0$val", undef, !$ac);
my $ac = AttrVal($serverName, "autocreate", "simple");
$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);
DoTrigger($server->{NAME}, "$tp:$val") if($re && $tp =~ m/$re/);
}
@ -642,9 +644,16 @@ MQTT2_SERVER_ReadDebug($$)
</li><br>
<a name="autocreate"></a>
<li>autocreate<br>
<li>autocreate [no|simple|complex]<br>
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>
</ul>

View File

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