2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 12:49:34 +00:00

NotifyOrderPrefix to simplify the average module

git-svn-id: https://svn.fhem.de/fhem/trunk@1168 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2012-01-05 13:09:07 +00:00
parent 852b157fc3
commit 44c08b6148
5 changed files with 46 additions and 61 deletions

View File

@ -1,3 +1,7 @@
- SVN
- feature: internal NotifyOrderPrefix: 98_average.pm is more straightforward
- 2011-12-31 (5.2)
- bugfix: applying smallscreen attributes to firefox/opera
- feature: CUL_TX added (thanks to Peterp)

View File

@ -325,6 +325,10 @@ CommandUsb($$)
my $msg;
my $dir = "/dev";
if($^O =~ m/Win/) {
return "This command is not yet supported on windows";
}
require "$attr{global}{modpath}/FHEM/DevIo.pm";
foreach my $dev (sort split("\n", `ls $dir`)) {

View File

@ -13,6 +13,7 @@ average_Initialize($)
my ($hash) = @_;
$hash->{DefFn} = "average_Define";
$hash->{NotifyFn} = "average_Notify";
$hash->{NotifyOrderPrefix} = "10-"; # Want to be called before the rest
$hash->{AttrList} = "disable:0,1";
}
@ -50,8 +51,8 @@ average_Notify($$)
my $re = $avg->{REGEXP};
my $max = int(@{$dev->{CHANGED}});
my $tn;
my $myIdx = $max;
my $trigger = "";
for (my $i = 0; $i < $max; $i++) {
my $s = $dev->{CHANGED}[$i];
@ -100,7 +101,7 @@ average_Notify($$)
$r->{$cumName}{VAL} = $cum;
$r->{$avgName}{VAL} = sprintf("%0.1f", $cum/$secNow);
} else {
$trigger .= " $avgName:".$r->{$avgName}{VAL};
$dev->{CHANGED}[$myIdx++] = "$avgName:".$r->{$avgName}{VAL};
$r->{$cumName}{VAL} = $secNow*$val;
$r->{$avgName}{VAL} = $val;
@ -108,24 +109,7 @@ average_Notify($$)
$r->{$cumName}{TIME} = $r->{$avgName}{TIME} = $tn;
}
}
$addNotifyCB{"avg:$avgName:$devName"} = "average_Callback $devName $trigger"
if($trigger);
return undef;
}
sub
average_Callback($)
{
my ($arg) = @_;
my ($dev, @list) = split(" ", $arg);
my $n = 0;
for(my $n = 0; $n < @list; $n++) {
my ($name, $value) = split(":", $list[$n]);
$defs{$dev}{CHANGED}[$n] = "$name: $value";
}
DoTrigger($dev, undef);
}
1;

View File

@ -1148,12 +1148,13 @@ CommandDefine($$)
{
my ($cl, $def) = @_;
my @a = split("[ \t][ \t]*", $def, 3);
my $name = $a[0];
return "Usage: define <name> <type> <type dependent arguments>"
if(int(@a) < 2);
return "$a[0] already defined, delete it first" if(defined($defs{$a[0]}));
return "Invalid characters in name (not A-Za-z0-9.:_): $a[0]"
if($a[0] !~ m/^[a-z0-9.:_]*$/i);
return "$name already defined, delete it first" if(defined($defs{$name}));
return "Invalid characters in name (not A-Za-z0-9.:_): $name"
if($name !~ m/^[a-z0-9.:_]*$/i);
my $m = $a[1];
if(!$modules{$m}) { # Perhaps just wrong case?
@ -1175,7 +1176,7 @@ CommandDefine($$)
my %hash;
$hash{NAME} = $a[0];
$hash{NAME} = $name;
$hash{TYPE} = $m;
$hash{STATE} = "???";
$hash{DEF} = $a[2] if(int(@a) > 2);
@ -1183,19 +1184,24 @@ CommandDefine($$)
# If the device wants to issue initialization gets/sets, then it needs to be
# in the global hash.
$defs{$a[0]} = \%hash;
$defs{$name} = \%hash;
my $ret = CallFn($a[0], "DefFn", \%hash, $def);
my $ret = CallFn($name, "DefFn", \%hash, $def);
if($ret) {
Log 1, "define: $ret";
delete $defs{$a[0]}; # Veto
delete $attr{$a[0]};
delete $defs{$name}; # Veto
delete $attr{$name};
} else {
foreach my $da (sort keys (%defaultattr)) { # Default attributes
CommandAttr($cl, "$a[0] $da $defaultattr{$da}");
CommandAttr($cl, "$name $da $defaultattr{$da}");
}
DoTrigger("global", "DEFINED $name");
if($modules{$m}{NotifyFn} && !$hash{NTFY_ORDER}) {
$hash{NTFY_ORDER} = ($modules{$m}{NotifyOrderPrefix} ?
$modules{$m}{NotifyOrderPrefix} : "50-") . $name;
}
DoTrigger("global", "DEFINED $a[0]");
}
return $ret;
@ -2099,8 +2105,26 @@ DoTrigger($$)
Log 5, "Triggering $dev ($max changes)";
return "" if(defined($attr{$dev}) && defined($attr{$dev}{do_not_notify}));
################
# Log/notify modules
# If modifying a device in its own trigger, do not call the triggers from
# the inner loop.
if(!defined($defs{$dev}{INTRIGGER})) {
$defs{$dev}{INTRIGGER}=1;
my @ntfyList = sort { $defs{$a}{NTFY_ORDER} cmp $defs{$b}{NTFY_ORDER} }
grep { $defs{$_}{NTFY_ORDER} } keys %defs;
foreach my $n (@ntfyList) {
next if(!defined($defs{$n})); # Was deleted in a previous notify
Log 5, "$dev trigger: Checking $n for notify";
my $r = CallFn($n, "NotifyFn", $defs{$n}, $defs{$dev});
$ret .= $r if($r);
}
delete($defs{$dev}{INTRIGGER});
}
################
# Inform
$max = int(@{$defs{$dev}{CHANGED}}); # can be enriched in the notifies
foreach my $c (keys %client) { # Do client loop first, is cheaper
next if(!$client{$c}{inform} || $client{$c}{inform} eq "raw");
my $tn = TimeNow();
@ -2118,24 +2142,6 @@ DoTrigger($$)
}
}
################
# Log/notify modules
# If modifying a device in its own trigger, do not call the triggers from
# the inner loop.
if(!defined($defs{$dev}{INTRIGGER})) {
$defs{$dev}{INTRIGGER}=1;
foreach my $n (sort keys %defs) {
next if(!defined($defs{$n})); # Was deleted in a previous notify
if(defined($modules{$defs{$n}{TYPE}})) {
if($modules{$defs{$n}{TYPE}}{NotifyFn}) {
Log 5, "$dev trigger: Checking $n for notify";
my $r = CallFn($n, "NotifyFn", $defs{$n}, $defs{$dev});
$ret .= $r if($r);
}
}
}
delete($defs{$dev}{INTRIGGER});
}
####################
# Used by triggered perl programs to check the old value
@ -2147,20 +2153,6 @@ DoTrigger($$)
Log 3, "NTFY return: $ret" if($ret);
# Enhancers like avarage need this
if(!defined($defs{$dev}{InNtfyCb}) && %addNotifyCB) {
$defs{$dev}{InNtfyCb}=1;
foreach my $cb (keys %addNotifyCB) {
my ($fn, $arg) = split(" ", $addNotifyCB{$cb}, 2);
delete $addNotifyCB{$cb};
no strict "refs";
&{$fn}($arg);
use strict "refs";
}
delete($defs{$dev}{CHANGED});
delete($defs{$dev}{InNtfyCb});
}
return $ret;
}

View File

@ -416,6 +416,7 @@ FW_AnswerCall($)
if($FW_inform) { # Longpoll header
$defs{$FW_cname}{inform} = $FW_room;
$defs{$FW_cname}{NTFY_ORDER} = $FW_cname; # else notifyfn won't be called
my $c = $defs{$FW_cname}{CD};
print $c "HTTP/1.1 200 OK\r\n",
"Content-Type: text/plain; charset=$FW_encoding\r\n\r\n";