2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-22 08:11:44 +00:00

fhem.pl: add ClientsKeepOrder feature (Forum #117737)

git-svn-id: https://svn.fhem.de/fhem/trunk@23609 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2021-01-24 18:51:58 +00:00
parent 0d3aee078b
commit 8e3a6b1b48
3 changed files with 37 additions and 19 deletions

View File

@ -20,7 +20,6 @@ MQTT2_CLIENT_Initialize($)
{ {
my ($hash) = @_; my ($hash) = @_;
MQTT2_CLIENT_resetClients($hash);
$hash->{ReadFn} = "MQTT2_CLIENT_Read"; $hash->{ReadFn} = "MQTT2_CLIENT_Read";
$hash->{DefFn} = "MQTT2_CLIENT_Define"; $hash->{DefFn} = "MQTT2_CLIENT_Define";
$hash->{AttrFn} = "MQTT2_CLIENT_Attr"; $hash->{AttrFn} = "MQTT2_CLIENT_Attr";
@ -63,6 +62,7 @@ MQTT2_CLIENT_resetClients($)
{ {
my ($hash) = @_; my ($hash) = @_;
$hash->{ClientsKeepOrder} = 1;
$hash->{Clients} = ":MQTT2_DEVICE:MQTT_GENERIC_BRIDGE:"; $hash->{Clients} = ":MQTT2_DEVICE:MQTT_GENERIC_BRIDGE:";
$hash->{MatchList}= { $hash->{MatchList}= {
"1:MQTT2_DEVICE" => "^.", "1:MQTT2_DEVICE" => "^.",
@ -80,6 +80,7 @@ MQTT2_CLIENT_Define($$)
return "Usage: define <name> MQTT2_CLIENT <hostname>:<tcp-portnr>" return "Usage: define <name> MQTT2_CLIENT <hostname>:<tcp-portnr>"
if(!$host); if(!$host);
MQTT2_CLIENT_resetClients($hash);
MQTT2_CLIENT_Undef($hash, undef) if($hash->{OLDDEF}); # modify MQTT2_CLIENT_Undef($hash, undef) if($hash->{OLDDEF}); # modify
$hash->{DeviceName} = $host; $hash->{DeviceName} = $host;
@ -332,13 +333,13 @@ MQTT2_CLIENT_Attr(@)
if($attrName eq "clientOrder") { if($attrName eq "clientOrder") {
if($type eq "set") { if($type eq "set") {
my @p = split(" ", $param[0]); my @p = split(" ", $param[0]);
$modules{MQTT2_CLIENT}{Clients} = ":".join(":",@p).":"; $hash->{Clients} = ":".join(":",@p).":";
my $cnt = 1; my $cnt = 1;
my %h = map { ($cnt++.":$_", "^.") } @p; my %h = map { ($cnt++.":$_", "^.") } @p;
$modules{MQTT2_CLIENT}{MatchList} = \%h; $hash->{MatchList} = \%h;
delete($modules{MQTT2_CLIENT}{".clientArray"}); # Force a recompute delete($hash->{".clientArray"}); # Force a recompute
} else { } else {
MQTT2_CLIENT_resetClients($modules{MQTT2_CLIENT}); MQTT2_CLIENT_resetClients($hash});
} }
} }

View File

@ -21,7 +21,6 @@ MQTT2_SERVER_Initialize($)
{ {
my ($hash) = @_; my ($hash) = @_;
MQTT2_SERVER_resetClients($hash);
$hash->{ReadFn} = "MQTT2_SERVER_Read"; $hash->{ReadFn} = "MQTT2_SERVER_Read";
$hash->{DefFn} = "MQTT2_SERVER_Define"; $hash->{DefFn} = "MQTT2_SERVER_Define";
$hash->{AttrFn} = "MQTT2_SERVER_Attr"; $hash->{AttrFn} = "MQTT2_SERVER_Attr";
@ -55,6 +54,7 @@ MQTT2_SERVER_resetClients($)
{ {
my ($hash) = @_; my ($hash) = @_;
$hash->{ClientsKeepOrder} = 1;
$hash->{Clients} = ":MQTT2_DEVICE:MQTT_GENERIC_BRIDGE:"; $hash->{Clients} = ":MQTT2_DEVICE:MQTT_GENERIC_BRIDGE:";
$hash->{MatchList}= { $hash->{MatchList}= {
"1:MQTT2_DEVICE" => "^.", "1:MQTT2_DEVICE" => "^.",
@ -72,6 +72,7 @@ MQTT2_SERVER_Define($$)
return "Usage: define <name> MQTT2_SERVER [IPV6:]<tcp-portnr> [global]" return "Usage: define <name> MQTT2_SERVER [IPV6:]<tcp-portnr> [global]"
if($port !~ m/^(IPV6:)?\d+$/); if($port !~ m/^(IPV6:)?\d+$/);
MQTT2_SERVER_resetClients($hash);
MQTT2_SERVER_Undef($hash, undef) if($hash->{OLDDEF}); # modify MQTT2_SERVER_Undef($hash, undef) if($hash->{OLDDEF}); # modify
my $ret = TcpServer_Open($hash, $port, $global); my $ret = TcpServer_Open($hash, $port, $global);
@ -161,13 +162,13 @@ MQTT2_SERVER_Attr(@)
if($attrName eq "clientOrder") { if($attrName eq "clientOrder") {
if($type eq "set") { if($type eq "set") {
my @p = split(" ", $param[0]); my @p = split(" ", $param[0]);
$modules{MQTT2_SERVER}{Clients} = ":".join(":",@p).":"; $hash->{Clients} = ":".join(":",@p).":";
my $cnt = 1; my $cnt = 1;
my %h = map { ($cnt++.":$_", "^.") } @p; my %h = map { ($cnt++.":$_", "^.") } @p;
$modules{MQTT2_SERVER}{MatchList} = \%h; $hash->{MatchList} = \%h;
delete($modules{MQTT2_SERVER}{".clientArray"}); # Force a recompute delete($hash->{".clientArray"}); # Force a recompute
} else { } else {
MQTT2_SERVER_resetClients($modules{MQTT2_SERVER}); MQTT2_SERVER_resetClients($hash);
} }
} }

View File

@ -4003,7 +4003,7 @@ Dispatch($$;$$)
foreach my $m (@{$clientArray}) { foreach my $m (@{$clientArray}) {
# Module is not loaded or the message is not for this module # Module is not loaded or the message is not for this module
next if(!$modules{$m} || $dmsg !~ m/$modules{$m}{Match}/s); next if(!$dmsg !~ m/$modules{$m}{Match}/s);
if( my $ffn = $modules{$m}{FingerprintFn} ) { if( my $ffn = $modules{$m}{FingerprintFn} ) {
($isdup, $idx) = CheckDuplicate($name, $dmsg, $ffn); ($isdup, $idx) = CheckDuplicate($name, $dmsg, $ffn);
@ -4038,10 +4038,20 @@ Dispatch($$;$$)
$mname = $newm if($newm ne "UNDEFINED"); $mname = $newm if($newm ne "UNDEFINED");
if($modules{$mname} && $modules{$mname}{ParseFn}) { if($modules{$mname} && $modules{$mname}{ParseFn}) {
no strict "refs"; $readingsUpdateDelayTrigger = 1; no strict "refs"; $readingsUpdateDelayTrigger = 1;
@found = &{$modules{$mname}{ParseFn}}($hash,$dmsg); my @tfound = &{$modules{$mname}{ParseFn}}($hash,$dmsg);
use strict "refs"; $readingsUpdateDelayTrigger = 0; use strict "refs"; $readingsUpdateDelayTrigger = 0;
$parserMod = $mname; $parserMod = $mname;
last if(defined($found[0]));
if(int(@tfound) && defined($tfound[0])) {
if($tfound[0] && $tfound[0] eq "[NEXT]") {
shift(@tfound);
push @found, @tfound;
} else {
push @found, @tfound;
last;
}
}
} else { } else {
Log 0, "ERROR: Cannot autoload $mname"; Log 0, "ERROR: Cannot autoload $mname";
} }
@ -5066,14 +5076,20 @@ computeClientArray($$)
{ {
my ($hash, $module) = @_; my ($hash, $module) = @_;
my @a = (); my @a = ();
my @mRe = split(":", $hash->{Clients} ? $hash->{Clients}:$module->{Clients}); my @mRe = split(":", $hash->{Clients} ? $hash->{Clients}:$module->{Clients});
foreach my $m (sort { $modules{$a}{ORDER}.$a cmp $modules{$b}{ORDER}.$b } if($hash->{ClientsKeepOrder}) {
grep { defined($modules{$_}{ORDER}) } keys %modules) { @a = grep { $modules{$_} && $modules{$_}{Match} } @mRe;
foreach my $re (@mRe) {
if($m =~ m/^$re$/) { } else {
push @a, $m if($modules{$m}{Match}); foreach my $m (sort { $modules{$a}{ORDER}.$a cmp $modules{$b}{ORDER}.$b }
last; grep { defined($modules{$_}{ORDER}) } keys %modules) {
foreach my $re (@mRe) {
if($m =~ m/^$re$/) {
push @a, $m if($modules{$m}{Match});
last;
}
} }
} }
} }