From 68b1a74a349f14c22e4a4eeedd70e01145979c4d Mon Sep 17 00:00:00 2001
From: rudolfkoenig <>
Date: Tue, 5 Mar 2019 10:56:08 +0000
Subject: [PATCH] MQTT2*: change autocreate to simple/complex (Forum #98130)
git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@18794 2b470e98-0d58-463d-a4d8-8e2adae1ed80
---
FHEM/00_MQTT2_CLIENT.pm | 27 ++++++++++++++++++---------
FHEM/00_MQTT2_SERVER.pm | 19 ++++++++++++++-----
FHEM/10_MQTT2_DEVICE.pm | 19 +++++++++++--------
3 files changed, 43 insertions(+), 22 deletions(-)
diff --git a/FHEM/00_MQTT2_CLIENT.pm b/FHEM/00_MQTT2_CLIENT.pm
index debb44ce4..e17cf28cf 100644
--- a/FHEM/00_MQTT2_CLIENT.pm
+++ b/FHEM/00_MQTT2_CLIENT.pm
@@ -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($$)
- - autocreate
- 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.
+ - autocreate [no|simple|complex]
+ 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.
+ 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.
diff --git a/FHEM/00_MQTT2_SERVER.pm b/FHEM/00_MQTT2_SERVER.pm
index 8fcb913bb..a88bb92fb 100644
--- a/FHEM/00_MQTT2_SERVER.pm
+++ b/FHEM/00_MQTT2_SERVER.pm
@@ -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($$)
- - autocreate
+ - autocreate [no|simple|complex]
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.
+ 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.
diff --git a/FHEM/10_MQTT2_DEVICE.pm b/FHEM/10_MQTT2_DEVICE.pm
index a0cc91ad2..7e0cd61d7 100644
--- a/FHEM/10_MQTT2_DEVICE.pm
+++ b/FHEM/10_MQTT2_DEVICE.pm
@@ -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.":.*";