98_cmdalias.pm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. ##############################################
  2. # $Id: 98_cmdalias.pm 12935 2017-01-02 19:51:46Z rudolfkoenig $
  3. # Avarage computing
  4. package main;
  5. use strict;
  6. use warnings;
  7. my %cmdalias;
  8. ##########################
  9. sub
  10. cmdalias_Initialize($)
  11. {
  12. my ($hash) = @_;
  13. $hash->{DefFn} = "cmdalias_Define";
  14. $hash->{UndefFn} = "cmdalias_Undefine";
  15. $hash->{AttrList} = "disable:0,1";
  16. }
  17. ##########################
  18. sub
  19. cmdalias_Define($$$)
  20. {
  21. my ($hash, $def) = @_;
  22. if($def !~ m/^([^ ]*) cmdalias ([^ ]*)(.*) AS (.*)$/s) {
  23. my $msg =
  24. "wrong syntax: define <name> cmdalias <cmd> [parameter] AS command...";
  25. return $msg;
  26. }
  27. my ($name, $alias, $param, $newcmd) = ($1, $2, $3, $4);
  28. $param =~ s/^ *//;
  29. # Checking for misleading regexps
  30. return "Bad regexp: starting with *" if($param =~ m/^\*/);
  31. eval { qr/^$param$/ };
  32. return "$name: Bad regexp in $param: $@" if($@);
  33. $alias = lc($alias);
  34. $hash->{ALIAS} = $alias;
  35. $hash->{PARAM} = $param;
  36. $hash->{NEWCMD} = $newcmd;
  37. $hash->{STATE} = "defined";
  38. $cmdalias{$alias}{Alias}{$name} = $hash;
  39. $cmdalias{$alias}{OrigFn} = $cmds{$alias}{Fn}
  40. if($cmds{$alias} &&
  41. $cmds{$alias}{Fn} &&
  42. $cmds{$alias}{Fn} ne "CommandCmdAlias");
  43. $cmds{$alias}{Fn} = "CommandCmdAlias";
  44. return undef;
  45. }
  46. sub
  47. cmdalias_Undefine($$)
  48. {
  49. my ($hash, $arg) = @_;
  50. my $alias = $hash->{ALIAS};
  51. delete $cmdalias{$alias}{Alias}{$hash->{NAME}};
  52. if(! keys %{$cmdalias{$alias}{Alias}}) {
  53. if($cmdalias{$alias}{OrigFn}) {
  54. $cmds{$alias}{Fn} = $cmdalias{$alias}{OrigFn};
  55. } else {
  56. delete($cmds{$alias});
  57. }
  58. delete($cmdalias{$alias});
  59. }
  60. return undef;
  61. }
  62. sub
  63. CommandCmdAlias($$$)
  64. {
  65. my ($cl, $param, $alias) = @_;
  66. my $a = $cmdalias{lc($alias)};
  67. return "Unknown command $a, internal error" if(!$a);
  68. foreach my $n (sort keys %{$a->{Alias}}) {
  69. my $h = $a->{Alias}{$n};
  70. if($h->{InExec} && $param =~ m/^$h->{PARAM}$/) {
  71. Log3 $n, 3, "cmdalias $n called recursively, skipping execution";
  72. next;
  73. }
  74. if($param =~ m/^$h->{PARAM}$/) {
  75. my %specials= ("%EVENT" => $param);
  76. my $exec = EvalSpecials($h->{NEWCMD}, %specials);
  77. $h->{InExec} = 1;
  78. my $r = AnalyzeCommandChain(undef, $exec);
  79. delete $h->{InExec};
  80. return $r;
  81. }
  82. }
  83. return undef if(!$a->{OrigFn});
  84. no strict "refs";
  85. return &{$a->{OrigFn} }($cl, $param, $alias);
  86. use strict "refs";
  87. }
  88. 1;
  89. =pod
  90. =item command
  91. =item summary create new commands or replace internal ones.
  92. =item summary_DE neue FHEM Befehle definieren oder existierende &auml;ndern
  93. =begin html
  94. <a name="cmdalias"></a>
  95. <h3>cmdalias</h3>
  96. <ul>
  97. create new commands or replace internal ones.
  98. <br>
  99. <a name="cmdaliasdefine"></a>
  100. <b>Define</b>
  101. <ul>
  102. <code>define &lt;name&gt; cmdalias &lt;cmd&gt; [parameter]
  103. AS newcommand..."</code><br>
  104. <br>
  105. <ul>
  106. parameter is optional and is a regexp which must match the command
  107. entered.
  108. If it matches, then the specified newcommand will be executed, which is
  109. a fhem command (see <a href="#command">Fhem command types</a> for
  110. details). Like in the <a href="#notify">notify</a> commands, $EVENT or
  111. $EVTPART may be used, in this case representing the command arguments as
  112. whole or the unique words entered.<br>
  113. Notes:<ul>
  114. <li>newcommand may contain cmd, but recursion is not allowed.</li>
  115. <li>if there are multiple definitions, they are checked/executed in
  116. alphabetically sorted name oder.</li>
  117. </ul>
  118. Examples:
  119. <ul><code>
  120. define s1 cmdalias shutdown update AS save;;shutdown<br>
  121. define s2 cmdalias set lamp .* AS { Log 1, "$EVENT";; fhem("set $EVENT") }
  122. </code></ul>
  123. </ul>
  124. </ul>
  125. </ul>
  126. =end html
  127. =cut