2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-21 07:56:03 +00:00

00_MQTT: topic with space or colon

git-svn-id: https://svn.fhem.de/fhem/trunk@16252 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
eisler 2018-02-23 21:32:59 +00:00
parent 0c4a2b1e8a
commit 94e17f3ecc
3 changed files with 118 additions and 45 deletions

View File

@ -1,5 +1,6 @@
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Add changes at the top of the list. Keep it in ASCII, and 80-char wide.
# Do not insert empty lines here, update check depends on it. # Do not insert empty lines here, update check depends on it.
- feature: 00_MQTT: topic with space or colon
- bugfix: 98_GOOGLECAST: fix open connections and state value - bugfix: 98_GOOGLECAST: fix open connections and state value
- feature: 10_MQTT_DEVICE: add SetExtensions - feature: 10_MQTT_DEVICE: add SetExtensions
- bugfix: 00_MQTT: small bugfix - bugfix: 00_MQTT: small bugfix

View File

@ -66,7 +66,7 @@ package MQTT;
use Exporter ('import'); use Exporter ('import');
@EXPORT = (); @EXPORT = ();
@EXPORT_OK = qw(send_publish send_subscribe send_unsubscribe client_attr client_subscribe_topic client_unsubscribe_topic topic_to_regexp); @EXPORT_OK = qw(send_publish send_subscribe send_unsubscribe client_attr client_subscribe_topic client_unsubscribe_topic topic_to_regexp parseParams);
%EXPORT_TAGS = (all => [@EXPORT_OK]); %EXPORT_TAGS = (all => [@EXPORT_OK]);
use strict; use strict;
@ -242,6 +242,82 @@ sub Set($@) {
}; };
} }
sub parseParams($;$$) {
my ( $cmd, $separator, $joiner ) = @_;
$separator = ' ' if ( !$separator );
$joiner = $separator if ( !$joiner ); # needed if separator is a regexp
my ( @a, %h );
my @params;
if ( ref($cmd) eq 'ARRAY' ) {
@params = @{$cmd};
}
else {
@params = split( $separator, $cmd );
}
while (@params) {
my $param = shift(@params);
next if ( $param eq "" );
my ( $key, $value ) = split( ':', $param, 2 );
if ( !defined($value) ) {
$value = $key;
$key = undef;
# the key can not start with a { -> it must be a perl expression # vim:}
}
elsif ( $key =~ m/^\s*{/ ) { # for vim: }
$value = $param;
$key = undef;
}
#collect all parts until the closing ' or "
while ( $param && $value =~ m/^('|")/ && $value !~ m/$1$/ ) {
my $next = shift(@params);
last if ( !defined($next) );
$value .= $joiner . $next;
}
#remove matching ' or " from the start and end
if ( $value =~ m/^('|")/ && $value =~ m/$1$/ ) {
$value =~ s/^.(.*).$/$1/;
}
#collect all parts until opening { and closing } are matched
if ( $value =~ m/^\s*{/ ) { # } for match
my $count = 0;
for my $i ( 0 .. length($value) - 1 ) {
my $c = substr( $value, $i, 1 );
++$count if ( $c eq '{' );
--$count if ( $c eq '}' );
}
while ( $param && $count != 0 ) {
my $next = shift(@params);
last if ( !defined($next) );
$value .= $joiner . $next;
for my $i ( 0 .. length($next) - 1 ) {
my $c = substr( $next, $i, 1 );
++$count if ( $c eq '{' );
--$count if ( $c eq '}' );
}
}
}
if ( defined($key) ) {
$h{$key} = $value;
}
else {
push @a, $value;
}
}
return ( \@a, \%h );
}
sub parsePublishCmdStr($) { sub parsePublishCmdStr($) {
my ($str) = @_; my ($str) = @_;
@ -258,50 +334,45 @@ sub parsePublishCmdStr($) {
} }
sub parsePublishCmd(@) { sub parsePublishCmd(@) {
my @a = @_; my @a = @_;
# [qos:?] [retain:?] topic value my ( $aa, $bb ) = parseParams(\@a);
return undef if(!@a); my $qos = 0;
return undef if(scalar(@a)<1); my $retain = 0;
my $topic = undef;
my $value = "\0";
my $expression = undef;
my $qos = 0; if ( exists( $bb->{'qos'} ) ) {
my $retain = 0; $qos = $bb->{'qos'};
my $topic = undef;
my $value = "\0";
my $expression = undef;
while (scalar(@a)>0) {
my $av = shift(@a);
if($av =~ /\{.*\}/) {
$expression = $av;
next;
} }
my ($pn,$pv) = split(":",$av);
if(defined($pv)) { if ( exists $bb->{'retain'} ) {
if($pn eq "qos") { $retain = $bb->{'retain'};
if($pv >=0 && $pv <=2) {
$qos = $pv;
}
} elsif($pn eq "retain") {
if($pv >=0 && $pv <=1) {
$retain = $pv;
}
} else {
# ignore
next;
}
} else {
$topic = $av;
last;
} }
}
if(scalar(@a)>0) { my @aaa = ();
$value = join(" ", @a); my @xaa = @{$aa};
}
while ( scalar(@xaa) > 0 ) {
my $av = shift @xaa;
if ( $av =~ /\{.*\}/ ) {
$expression = $av;
next;
}
else {
push @aaa, $av;
}
}
$topic = shift(@aaa);
if ( scalar(@aaa) > 0 ) {
$value = join( " ", @aaa );
}
return undef unless $topic || $expression;
return ( $qos, $retain, $topic, $value, $expression );
return undef unless $topic || $expression;
return ($qos, $retain,$topic, $value, $expression);
} }
sub Notify($$) { sub Notify($$) {

View File

@ -188,7 +188,8 @@ sub Attr($$$$) {
}; };
$attribute =~ /^publishSet(_?)(.*)/ and do { $attribute =~ /^publishSet(_?)(.*)/ and do {
if ($command eq "set") { if ($command eq "set") {
my @values = split ("[ \t]+",$value); my ( $aa, $bb ) = parseParams($value);
my @values = @{$aa};
my $topic = pop @values; my $topic = pop @values;
$hash->{publishSets}->{$2} = { $hash->{publishSets}->{$2} = {
'values' => \@values, 'values' => \@values,