2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-30 12:07:09 +00:00

fhem.pl: add featurelevel + set magic (Forum #38276)

git-svn-id: https://svn.fhem.de/fhem/trunk@8800 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2015-06-22 18:24:59 +00:00
parent 596c804001
commit 3350f1a9b9
4 changed files with 121 additions and 48 deletions

View File

@ -299,7 +299,8 @@ notify_fhemwebFn($$$$)
</ul></li> </ul></li>
<li>Note: the following is deprecated and will be removed in a future <li>Note: the following is deprecated and will be removed in a future
release. The described replacement is attempted if none of the above release. It is only active for featurelevel up to 5.6.
The described replacement is attempted if none of the above
variables ($NAME/$EVENT/etc) found in the command. variables ($NAME/$EVENT/etc) found in the command.
<ul> <ul>
<li>The character <code>%</code> will be replaced with the received <li>The character <code>%</code> will be replaced with the received
@ -308,17 +309,16 @@ notify_fhemwebFn($$$$)
the <code>%</code> into double quotes, else the shell may get a syntax the <code>%</code> into double quotes, else the shell may get a syntax
error.</li> error.</li>
<li>The character <code>@</code> will be replaced with the device <li>The character @ will be replaced with the device
name.</li> name.</li>
<li>To use % or @ in the text itself, use the double mode (%% or <li>To use % or @ in the text itself, use the double mode (%% or
@@).</li> @@).</li>
<li>Instead of <code>%</code> and <code>@</code>, the parameters <li>Instead of % and @, the parameters %EVENT (same as %), %NAME (same
<code>%EVENT</code> (same as <code>%</code>), <code>%NAME</code> (same as @) and %TYPE (contains the device type,
as <code>@</code>) and <code>%TYPE</code> (contains the device type, e.g. FHT) can be used. The space separated event "parts"
e.g. <code>FHT</code>) can be used. The space separated event "parts" are available as %EVTPART0, %EVTPART1, etc. A single %
are available as %EVTPART0, %EVTPART1, etc. A single <code>%</code>
looses its special meaning if any of these parameters appears in the looses its special meaning if any of these parameters appears in the
definition.</li> definition.</li>
</ul></li> </ul></li>
@ -481,32 +481,33 @@ notify_fhemwebFn($$$$)
Ger&auml;tes, z.B. <code>myFht</code></li> Ger&auml;tes, z.B. <code>myFht</code></li>
</ul></li> </ul></li>
<li>Achtung: Folgende Vorgehensweise ist abgek&uuml;ndigt und wird in <li>Achtung: Folgende Vorgehensweise ist abgek&uuml;ndigt, funktioniert
einem zuk&uuml;nftigen Release von FHEM nicht mehr unterst&uuml;tzt. bis featurelevel 5.6 und wird in einem zuk&uuml;nftigen Release von
Wenn keine der oben genannten Variablen ($NAME/$EVENT/usw.) in der FHEM nicht mehr unterst&uuml;tzt. Wenn keine der oben genannten
Anweisung gefunden wird, werden Platzhalter ersetzt. Variablen ($NAME/$EVENT/usw.) in der Anweisung gefunden wird, werden
Platzhalter ersetzt.
<ul> <ul>
<li>Das Zeichen <code>%</code> wird ersetzt mit dem empfangenen <li>Das Zeichen % wird ersetzt mit dem empfangenen
Ereignis (Event), z.B. mit <code>on</code> oder <code>off</code> oder Ereignis (Event), z.B. mit on oder off oder
<code>measured-temp: 21.7 (Celsius)</code>. <code>measured-temp: 21.7 (Celsius)</code>.
</li> </li>
<li>Das Zeichen <code>@</code> wird ersetzt durch den <li>Das Zeichen @ wird ersetzt durch den
Ger&auml;tenamen.</li> Ger&auml;tenamen.</li>
<li>Um % oder @ im Text selbst benutzen zu k&ouml;nnen, m&uuml;ssen <li>Um % oder @ im Text selbst benutzen zu k&ouml;nnen, m&uuml;ssen
sie verdoppelt werden (%% oder @@).</li> sie verdoppelt werden (%% oder @@).</li>
<li>Anstelle von <code>%</code> und <code>@</code>, k&ouml;nnen die <li>Anstelle von % und @, k&ouml;nnen die
Parameter <code>%EVENT</code> (funktionsgleich mit <code>%</code>), Parameter %EVENT (funktionsgleich mit %),
<code>%NAME</code> (funktionsgleich mit <code>@</code>) und %NAME (funktionsgleich mit @) und
<code>%TYPE</code> (enth&auml;lt den Typ des Ger&auml;tes, z.B. %TYPE (enth&auml;lt den Typ des Ger&auml;tes, z.B.
<code>FHT</code>) benutzt werden. Die von Leerzeichen unterbrochenen FHT) benutzt werden. Die von Leerzeichen unterbrochenen
Teile eines Ereignisses (Event) sind verf&uuml;gbar als %EVTPART0, Teile eines Ereignisses (Event) sind verf&uuml;gbar als %EVTPART0,
%EVTPART1, usw. Ein einzeln stehendes <code>%</code> verliert seine %EVTPART1, usw. Ein einzeln stehendes % verliert seine
%oben beschriebene Bedeutung, falls auch nur einer dieser Parameter oben beschriebene Bedeutung, falls auch nur einer dieser Parameter
%in der Definition auftaucht.</li> in der Definition auftaucht.</li>
</ul></li> </ul></li>

View File

@ -1092,8 +1092,16 @@ The following local attributes are used by a wider range of devices:
<br> <br>
Each device has different set parameters, see the corresponding device Each device has different set parameters, see the corresponding device
section for details.<br> section for details.<br>
<br> <br><br>
<br> From featurelevel 5.7 on the set command replaces:
<ul>
<li>[device:reading] with the corresponding reading of device, if both device
and reading exists.</li>
<li>{perlExpression} with the result of perlExpression, if the expression
does not generates an error.</li>
</ul>
<br><br>
<a name="setExtensions"></a> <a name="setExtensions"></a>
Some modules support a common list of <b>set extensions</b>, and point in Some modules support a common list of <b>set extensions</b>, and point in
their documentation to this section. If the module itself implements one of their documentation to this section. If the module itself implements one of
@ -1335,15 +1343,24 @@ The following local attributes are used by a wider range of devices:
</ul> </ul>
</li><br> </li><br>
<a name="featurelevel"></a>
<li>featurelevel<br>
Enable/disable old or new features, based on FHEM version.
E.g. the $value hash for notify is only set for featurelevel up to 5.6,
as it is deprecated, use the Value() function instead.
</li><br>
<a name="lastinclude"></a> <a name="lastinclude"></a>
<li>lastinclude<br> <li>lastinclude<br>
If this attribute is set, then the last command of the generated If this attribute is set, then the last command of the generated
configfile (see the <a href="#save">save</a> command) will be<br> configfile (see the <a href="#save">save</a> command) will be<br>
include &lt;lastinclude-value&gt;<br> include &lt;lastinclude-value&gt;<br>
This attribute is DEPRECATED, use <a href="#notify">notify</a>, with This attribute is DEPRECATED and valid for featurelevel up to 5.6, use
the INITIALIZED event to execute commands after initialization. <a href="#notify">notify</a>, with the INITIALIZED event to execute
commands after initialization.
</li><br> </li><br>
<a name="logfile"></a> <a name="logfile"></a>
<li>logfile<br> <li>logfile<br>
Specify the logfile to write. You can use "-" for Specify the logfile to write. You can use "-" for

View File

@ -1153,9 +1153,16 @@ Die folgenden lokalen Attribute werden von mehreren Ger&auml;ten verwendet:
Jedes Ger&auml;t hat verschiedene Parameter die mit &quot;set&quot; gesetzt Jedes Ger&auml;t hat verschiedene Parameter die mit &quot;set&quot; gesetzt
werden k&ouml;nnen. Lesen Sie bitte den entsprechenden Abschnitt f&uuml;r werden k&ouml;nnen. Lesen Sie bitte den entsprechenden Abschnitt f&uuml;r
das Ger&auml;t f&uuml;r Details durch. das Ger&auml;t f&uuml;r Details durch.
<br> <br><br>
Ab featurelevel 5.7 ersetzt das set Befehl
<ul>
<li>[device:reading] mit dem Wert des Readings f&uuml;r device, falls sowohl
device, als auch Reading existiert, und nicht leer ist.</li>
<li>{perlExpression} mit dem Ergebnis der perlExpression, falls der Ausdruck
keinen Fehler generiert.</li>
</ul>
<br><br>
<br>
<a name="setExtensions"></a> <a name="setExtensions"></a>
Manche Module unterst&uuml;tzen die sog. <b>set extensions</b>, und in der Manche Module unterst&uuml;tzen die sog. <b>set extensions</b>, und in der
entsprechenden Dokumentation ist ein Link auf diesem Text zu finden. Falls im entsprechenden Dokumentation ist ein Link auf diesem Text zu finden. Falls im
@ -1411,23 +1418,34 @@ Die folgenden lokalen Attribute werden von mehreren Ger&auml;ten verwendet:
<a name="holiday2we"></a> <a name="holiday2we"></a>
<li>holiday2we<br> <li>holiday2we<br>
Wenn dieses Attribut gesetzt wurde, dann wird die <a href="#perl">$we</a> Wenn dieses Attribut gesetzt wurde, dann wird die <a
Variable href="#perl">$we</a> Variable als &quot;true&quot; betrachtet, wenn der
als &quot;true&quot; betrachtet, wenn der Wert der <a href="#holiday">holiday</a> Wert der <a href="#holiday">holiday</a> Variable zu diesem Attribut
Variable zu diesem Attribut nicht &quot;none&quot; ist.<br> nicht &quot;none&quot; ist.<br>
Beispiel:<br> Beispiel:<br>
<ul> <ul>
attr global holiday2we hessen attr global holiday2we hessen
</ul> </ul>
</li><br> </li><br>
<a name="featurelevel"></a>
<li>featurelevel<br>
Aktiviere bzw. deaktiviere bestimmte alte oder neue Funktionen, basierend
auf die FHEM Version. Z.Bsp. das $value hash f&uuml;r notify wird nur bis featurelevel 5.6
bef&uuml;llt, da es unerw&uuml;nscht ist. Stattdessen sollte man die
Value() Funktion verwenden.
</li><br>
<a name="lastinclude"></a> <a name="lastinclude"></a>
<li>lastinclude<br> <li>lastinclude<br>
Wurde dieses Attribut gesetzt, dann wird der letzte Befehl der generierten Wurde dieses Attribut gesetzt, dann wird der letzte Befehl der
Konfigurationsdatei (siehe <a href="#save">save</a> Befehl) ber&uuml;cksichtigt &lt;lastinclude-value&gt;<br> generierten Konfigurationsdatei (siehe <a href="#save">save</a> Befehl)
Dieses Attribut ist DEPRECATED, bitte benutzen Sie <a href="#notify">notify</a>, ber&uuml;cksichtigt &lt;lastinclude-value&gt;<br>
mit dem&nbsp; INITIALIZED Ereignis um Befehle nach der Intialisierung Dieses Attribut ist DEPRECATED und nur bis featurelevel 5.6
auszuf&uuml;hren. unterst&uuml;tzt, bitte benutzen Sie <a href="#notify">notify</a>, mit
dem&nbsp; INITIALIZED Ereignis um Befehle nach der Intialisierung
auszuf&uuml;hren.
</li><br> </li><br>
<a name="logfile"></a> <a name="logfile"></a>

View File

@ -221,6 +221,7 @@ use vars qw(%value); # Current values, see commandref.html
use vars qw($lastDefChange); # number of last def/attr change use vars qw($lastDefChange); # number of last def/attr change
use vars qw(@structChangeHist); # Contains the last 10 structural changes use vars qw(@structChangeHist); # Contains the last 10 structural changes
use vars qw($cmdFromAnalyze); # used by the warnings-sub use vars qw($cmdFromAnalyze); # used by the warnings-sub
use vars qw($featurelevel);
my $AttrList = "verbose:0,1,2,3,4,5 room group comment alias ". my $AttrList = "verbose:0,1,2,3,4,5 room group comment alias ".
"eventMap userReadings"; "eventMap userReadings";
@ -243,6 +244,7 @@ my @cmdList; # Remaining commands in a chain. Used by sleep
$init_done = 0; $init_done = 0;
$lastDefChange = 0; $lastDefChange = 0;
$readytimeout = ($^O eq "MSWin32") ? 0.1 : 5.0; $readytimeout = ($^O eq "MSWin32") ? 0.1 : 5.0;
$featurelevel = 5.6;
$modules{Global}{ORDER} = -1; $modules{Global}{ORDER} = -1;
@ -261,6 +263,7 @@ my @globalAttrList = qw(
configfile configfile
dupTimeout dupTimeout
exclude_from_update exclude_from_update
featurelevel
holiday2we holiday2we
language:EN,DE language:EN,DE
lastinclude lastinclude
@ -561,6 +564,7 @@ if($motd eq "$sc_text\n\n") {
} }
my $osuser = "os $^O, user ".(getlogin || getpwuid($<) || "unknown"); my $osuser = "os $^O, user ".(getlogin || getpwuid($<) || "unknown");
Log 0, "Featurelevel: $featurelevel";
Log 0, "Server started with ".int(keys %defs). Log 0, "Server started with ".int(keys %defs).
" defined entities (version $attr{global}{version}, $osuser, pid $$)"; " defined entities (version $attr{global}{version}, $osuser, pid $$)";
@ -921,8 +925,10 @@ AnalyzePerlCommand($$)
# Make life easier for oneliners: # Make life easier for oneliners:
%value = (); %value = ();
foreach my $d (keys %defs) { if($featurelevel <= 5.6) {
$value{$d} = $defs{$d}{STATE} foreach my $d (keys %defs) {
$value{$d} = $defs{$d}{STATE}
}
} }
my ($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = localtime; my ($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = localtime;
my $hms = sprintf("%02d:%02d:%02d", $hour, $min, $sec); my $hms = sprintf("%02d:%02d:%02d", $hour, $min, $sec);
@ -1437,8 +1443,9 @@ CommandSave($$)
print $fh "attr $d $a $val\n"; print $fh "attr $d $a $val\n";
} }
} }
print SFH "include $attr{global}{lastinclude}\n" print SFH "include $attr{global}{lastinclude}\n"
if($attr{global}{lastinclude}); if($attr{global}{lastinclude} && $featurelevel <= 5.6);
foreach my $key (keys %fh) { foreach my $key (keys %fh) {
next if($fh{$key} eq "1"); ## R/O include files next if($fh{$key} eq "1"); ## R/O include files
@ -1477,6 +1484,24 @@ CommandShutdown($$)
exit(0); exit(0);
} }
#####################################
sub
ReplaceSetMagic(@) # Forum #38276
{
my $a = join(" ", @_);
$a =~ s/\[([a-z0-9._]+):([A-z0-9._]+)\]/{
my $x = ReadingsVal($1,$2,""); $x eq "" ? "$1:$2" : $x
}/egi;
$a =~ s/{([^}]+)}/{
my $x = eval $1; $@ ? $1 : $x
}/eg;
return split(" ", $a);
}
##################################### #####################################
sub sub
DoSet(@) DoSet(@)
@ -1493,6 +1518,8 @@ DoSet(@)
return CallFn($dev, "SetFn", $hash, @a) if($a[1] && $a[1] eq "?"); return CallFn($dev, "SetFn", $hash, @a) if($a[1] && $a[1] eq "?");
@a = ReplaceEventMap($dev, \@a, 0) if($attr{$dev}{eventMap}); @a = ReplaceEventMap($dev, \@a, 0) if($attr{$dev}{eventMap});
@a = ReplaceSetMagic(@a) if($featurelevel >= 5.7);
$hash->{".triggerUsed"} = 0; $hash->{".triggerUsed"} = 0;
my ($ret, $skipTrigger) = CallFn($dev, "SetFn", $hash, @a); my ($ret, $skipTrigger) = CallFn($dev, "SetFn", $hash, @a);
return $ret if($ret); return $ret if($ret);
@ -2269,6 +2296,11 @@ GlobalAttr($$$$)
} }
}
elsif($name eq "featurelevel") {
return "$val is not in the form N.N" if($val !~ m/^\d\.\d$/);
$featurelevel = $val;
} }
return undef; return undef;
@ -2822,7 +2854,7 @@ EvalSpecials($%)
return $exec; return $exec;
} }
$exec =~ s/%%/____/g; $exec =~ s/%%/____/g if($featurelevel < 5.7);
# perform macro substitution # perform macro substitution
@ -2830,14 +2862,19 @@ EvalSpecials($%)
foreach my $special (keys %specials) { foreach my $special (keys %specials) {
$extsyntax+= ($exec =~ s/$special/$specials{$special}/g); $extsyntax+= ($exec =~ s/$special/$specials{$special}/g);
} }
if(!$extsyntax) {
$exec =~ s/%/$specials{"%EVENT"}/g;
}
$exec =~ s/____/%/g;
$exec =~ s/@@/____/g; if($featurelevel < 5.7) {
$exec =~ s/@/$specials{"%NAME"}/g; if(!$extsyntax) {
$exec =~ s/____/@/g; $exec =~ s/%/$specials{"%EVENT"}/g;
}
$exec =~ s/____/%/g;
}
if($featurelevel < 5.7) {
$exec =~ s/@@/____/g;
$exec =~ s/@/$specials{"%NAME"}/g;
$exec =~ s/____/@/g;
}
return $exec; return $exec;
} }