From 7065633b55a2934c68b0ea511a5dff2a44691d4c Mon Sep 17 00:00:00 2001 From: rudolfkoenig <> Date: Sun, 5 Feb 2017 13:23:25 +0000 Subject: [PATCH] fhem.pl: optimizing createNtfyHash (Forum #66402) git-svn-id: https://svn.fhem.de/fhem/trunk@13334 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/01_FHEMWEB.pm | 9 ++------- fhem/FHEM/98_telnet.pm | 10 ++-------- fhem/fhem.pl | 37 +++++++++++++++++++++---------------- 3 files changed, 25 insertions(+), 31 deletions(-) diff --git a/fhem/FHEM/01_FHEMWEB.pm b/fhem/FHEM/01_FHEMWEB.pm index 31d276b4e..0607443f4 100755 --- a/fhem/FHEM/01_FHEMWEB.pm +++ b/fhem/FHEM/01_FHEMWEB.pm @@ -129,7 +129,7 @@ FHEMWEB_Initialize($) $hash->{AttrFn} = "FW_Attr"; $hash->{DefFn} = "FW_Define"; $hash->{UndefFn} = "FW_Undef"; - $hash->{NotifyFn}= ($init_done ? "FW_Notify" : "FW_SecurityCheck"); + $hash->{NotifyFn}= "FW_Notify"; $hash->{AsyncOutputFn} = "FW_AsyncOutput"; $hash->{ActivateInformFn} = "FW_ActivateInform"; no warnings 'qw'; @@ -217,16 +217,13 @@ FHEMWEB_Initialize($) FW_readIcons($pe); } } - + InternalTimer(1, "FW_SecurityCheck", $hash); } ##################################### sub FW_SecurityCheck($$) { - my ($ntfy, $dev) = @_; - return if($dev->{NAME} ne "global" || - !grep(m/^INITIALIZED$/, @{$dev->{CHANGED}})); my $motd = AttrVal("global", "motd", ""); if($motd =~ "^SecurityCheck") { my @list1 = devspec2array("TYPE=FHEMWEB"); @@ -247,8 +244,6 @@ FW_SecurityCheck($$) if(@list3); $attr{global}{motd} = $motd; } - $modules{FHEMWEB}{NotifyFn}= "FW_Notify"; - return; } ##################################### diff --git a/fhem/FHEM/98_telnet.pm b/fhem/FHEM/98_telnet.pm index 76bf0fa3c..c4eb55ff7 100644 --- a/fhem/FHEM/98_telnet.pm +++ b/fhem/FHEM/98_telnet.pm @@ -20,7 +20,6 @@ telnet_Initialize($) $hash->{AsyncOutputFn} = "telnet_Output"; $hash->{UndefFn} = "telnet_Undef"; $hash->{AttrFn} = "telnet_Attr"; - $hash->{NotifyFn}= "telnet_SecurityCheck"; $hash->{AttrList} = "globalpassword password prompt allowedCommands ". "allowfrom SSL connectTimeout connectInterval ". "encoding:utf8,latin1 sslVersion"; @@ -34,6 +33,7 @@ telnet_Initialize($) $cmds{inform} = { Fn=>"CommandTelnetInform", ClientFilter => "telnet", Hlp=>"{on|off|log|raw|timer|status},echo all events to this client" }; + InternalTimer(1, "telnet_SecurityCheck", $hash); } sub @@ -58,11 +58,8 @@ CommandTelnetEncoding($$) ##################################### sub -telnet_SecurityCheck($$) +telnet_SecurityCheck() { - my ($ntfy, $dev) = @_; - return if($dev->{NAME} ne "global" || - !grep(m/^INITIALIZED$/, @{$dev->{CHANGED}})); my $motd = AttrVal("global", "motd", ""); if($motd =~ "^SecurityCheck") { my @list1 = devspec2array("TYPE=telnet"); @@ -85,9 +82,6 @@ telnet_SecurityCheck($$) if(@list3); $attr{global}{motd} = $motd; } - delete $modules{telnet}{NotifyFn}; - return; - return; } ########################## diff --git a/fhem/fhem.pl b/fhem/fhem.pl index 70bfe7dba..9e202345b 100755 --- a/fhem/fhem.pl +++ b/fhem/fhem.pl @@ -1864,8 +1864,8 @@ CommandModify($$) # Return a list of modules return "Define $a[0] first" if(!defined($defs{$a[0]})); - %ntfyHash = (); my $hash = $defs{$a[0]}; + %ntfyHash = () if($hash->{NTFY_ORDER}); $hash->{OLDDEF} = $hash->{DEF}; $hash->{DEF} = $a[1]; @@ -4499,25 +4499,30 @@ createNtfyHash() grep { $defs{$_}{NTFY_ORDER} && $defs{$_}{TYPE} && $modules{$defs{$_}{TYPE}}{NotifyFn} } keys %defs; + my %d2a_cache; + %ntfyHash = ("*" => []); foreach my $d (@ntfyList) { - if($defs{$d}{NOTIFYDEV}) { - foreach my $nd (devspec2array($defs{$d}{NOTIFYDEV})) { - $ntfyHash{$nd} = [] if($nd && !defined($ntfyHash{$nd})); - } - } - } - $ntfyHash{"*"} = []; - foreach my $d (@ntfyList) { - if($defs{$d}{NOTIFYDEV}) { - foreach my $nd (devspec2array($defs{$d}{NOTIFYDEV})) { - my $arr = $ntfyHash{$nd}; - push @{$arr}, $d if(!grep /^$d$/, @{$arr}); - } + my $ndl = $defs{$d}{NOTIFYDEV}; + next if(!$ndl); + my @ndlarr; + if($d2a_cache{$ndl}) { + @ndlarr = @{$d2a_cache{$ndl}}; } else { - foreach my $nd (keys %ntfyHash) { - push @{$ntfyHash{$nd}}, $d; + @ndlarr = devspec2array($ndl); + if(@ndlarr > 1) { + my %h = map { $_ => 1 } @ndlarr; + @ndlarr = keys %h; } + $d2a_cache{$ndl} = \@ndlarr; } + map { $ntfyHash{$_} = [] } @ndlarr; + } + + my @nhk = keys %ntfyHash; + foreach my $d (@ntfyList) { + my $ndl = $defs{$d}{NOTIFYDEV}; + my $arr = ($ndl ? $d2a_cache{$ndl} : \@nhk); + map { push @{$ntfyHash{$_}}, $d } @{$arr}; } }