33_readingsProxy.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. # $Id: 33_readingsProxy.pm 16299 2018-03-01 08:06:55Z justme1968 $
  2. ##############################################################################
  3. #
  4. # This file is part of fhem.
  5. #
  6. # Fhem is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Fhem is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ##############################################################################
  20. package main;
  21. use strict;
  22. use warnings;
  23. use SetExtensions;
  24. use vars qw(%defs);
  25. use vars qw(%attr);
  26. use vars qw($readingFnAttributes);
  27. use vars qw($init_done);
  28. sub Log($$);
  29. sub Log3($$$);
  30. sub readingsProxy_Initialize($)
  31. {
  32. my ($hash) = @_;
  33. $hash->{DefFn} = "readingsProxy_Define";
  34. $hash->{NotifyFn} = "readingsProxy_Notify";
  35. $hash->{UndefFn} = "readingsProxy_Undefine";
  36. $hash->{SetFn} = "readingsProxy_Set";
  37. $hash->{GetFn} = "readingsProxy_Get";
  38. $hash->{AttrFn} = "readingsProxy_Attr";
  39. $hash->{AttrList} = "disable:1 "
  40. ."getList "
  41. ."setList "
  42. ."getFn:textField-long setFn:textField-long valueFn:textField-long "
  43. .$readingFnAttributes;
  44. }
  45. sub
  46. readingsProxy_setNotfiyDev($)
  47. {
  48. my ($hash) = @_;
  49. if( $hash->{DEVICE} ) {
  50. notifyRegexpChanged($hash,"(global|".$hash->{DEVICE}.")");
  51. } else {
  52. notifyRegexpChanged($hash,'');
  53. }
  54. }
  55. sub
  56. readingsProxy_updateDevices($)
  57. {
  58. my ($hash) = @_;
  59. my %list;
  60. delete $hash->{DEVICE};
  61. delete $hash->{READING};
  62. my @params = split(" ", $hash->{DEF});
  63. while (@params) {
  64. my $param = shift(@params);
  65. my @device = split(":", $param);
  66. if( defined($defs{$device[0]}) ) {
  67. $list{$device[0]} = 1;
  68. $hash->{DEVICE} = $device[0];
  69. $hash->{READING} = $device[1];
  70. $hash->{READING} = "state" if( !$hash->{READING} );
  71. }
  72. }
  73. InternalTimer(gettimeofday(), "readingsProxy_setNotfiyDev", $hash);
  74. $hash->{CONTENT} = \%list;
  75. readingsProxy_update($hash, undef);
  76. }
  77. sub readingsProxy_Define($$)
  78. {
  79. my ($hash, $def) = @_;
  80. my @args = split("[ \t]+", $def);
  81. return "Usage: define <name> readingsProxy <device>:<reading>" if(@args != 3);
  82. my $name = shift(@args);
  83. my $type = shift(@args);
  84. $hash->{STATE} = 'Initialized';
  85. if( $init_done ) {
  86. readingsProxy_updateDevices($hash);
  87. }
  88. return undef;
  89. }
  90. sub readingsProxy_Undefine($$)
  91. {
  92. my ($hash,$arg) = @_;
  93. return undef;
  94. }
  95. sub
  96. readingsProxy_update($$)
  97. {
  98. my ($hash,$value) = @_;
  99. my $name = $hash->{NAME};
  100. my $DEVICE = $hash->{DEVICE};
  101. return if( !$DEVICE );
  102. my $READING = $hash->{READING};
  103. $value = ReadingsVal($DEVICE,$READING,undef) if( $DEVICE && !defined($value) );
  104. #return if( !defined($value) );
  105. my $value_fn = AttrVal( $name, "valueFn", "" );
  106. if( $value_fn =~ m/^{.*}$/s ) {
  107. my $VALUE = $value;
  108. my $LASTCMD = ReadingsVal($name,"lastCmd",undef);
  109. my $value_fn = eval $value_fn;
  110. Log3 $name, 3, $name .": valueFn: ". $@ if($@);
  111. return undef if( !defined($value_fn) );
  112. $value = $value_fn if( $value_fn ne '' );
  113. }
  114. if( AttrVal($name, 'event-on-change-reading', undef ) || AttrVal($name, 'event-on-update-reading', undef ) ) {
  115. readingsSingleUpdate($hash, 'state', $value, 1)
  116. } else {
  117. readingsSingleUpdate($hash, 'state', $value, 0);
  118. DoTrigger( $name, $value );
  119. }
  120. }
  121. sub
  122. readingsProxy_Notify($$)
  123. {
  124. my ($hash,$dev) = @_;
  125. my $name = $hash->{NAME};
  126. my $events = deviceEvents($dev,1);
  127. return if( !$events );
  128. if( grep(m/^INITIALIZED$/, @{$events}) ) {
  129. readingsProxy_updateDevices($hash);
  130. return undef;
  131. } elsif( grep(m/^REREADCFG$/, @{$events}) ) {
  132. readingsProxy_updateDevices($hash);
  133. return undef;
  134. }
  135. return if( !$init_done );
  136. return if( AttrVal($name,"disable", 0) > 0 );
  137. return if($dev->{NAME} eq $name);
  138. my $max = int(@{$events});
  139. for (my $i = 0; $i < $max; $i++) {
  140. my $s = $events->[$i];
  141. $s = "" if(!defined($s));
  142. if( $dev->{NAME} eq "global" && $s =~ m/^RENAMED ([^ ]*) ([^ ]*)$/) {
  143. my ($old, $new) = ($1, $2);
  144. if( defined($hash->{CONTENT}{$old}) ) {
  145. $hash->{DEF} =~ s/(^|\s+)$old((:\S+)?\s*)/$1$new$2/g;
  146. readingsProxy_updateDevices($hash);
  147. }
  148. } elsif( $dev->{NAME} eq "global" && $s =~ m/^DELETED ([^ ]*)$/) {
  149. my ($name) = ($1);
  150. if( defined($hash->{CONTENT}{$name}) ) {
  151. #$hash->{DEF} =~ s/(^|\s+)$name((:\S+)?\s*)/ /g;
  152. #$hash->{DEF} =~ s/^ //;
  153. #$hash->{DEF} =~ s/ $//;
  154. readingsProxy_updateDevices($hash);
  155. }
  156. } elsif( $dev->{NAME} eq "global" && $s =~ m/^DEFINED ([^ ]*)$/) {
  157. my ($name) = ($1);
  158. readingsProxy_updateDevices($hash) if( !$hash->{DEVICE} );
  159. } else {
  160. next if( !$hash->{DEVICE} );
  161. next if( $dev->{NAME} ne $hash->{DEVICE} );
  162. my @parts = split(/: /,$s);
  163. my $reading = shift @parts;
  164. my $value = join(": ", @parts);
  165. $reading = "" if( !defined($reading) );
  166. $value = "" if( !defined($value) );
  167. if( $value eq "" ) {
  168. $reading = "state";
  169. $value = $s;
  170. }
  171. next if( $reading ne $hash->{READING} );
  172. readingsProxy_update($hash, $value);
  173. }
  174. }
  175. return undef;
  176. }
  177. sub
  178. readingsProxy_Set($@)
  179. {
  180. my ($hash, $name, @a) = @_;
  181. return "no set value specified" if(int(@a) < 1);
  182. my $setList = AttrVal($name, "setList", "");
  183. $setList = getAllSets($hash->{DEVICE}) if( $setList eq "%PARENT%" );
  184. return SetExtensions($hash,$setList,$name,@a) if(!$setList || $a[0] eq "?");
  185. my $found = 0;
  186. foreach my $set (split(" ", $setList)) {
  187. if( "$set " =~ m/^${a[0]}[ :]/ ) {
  188. $found = 1;
  189. last;
  190. } elsif( "$set " =~ m/^state[ :]/ ) {
  191. $found = 1;
  192. last;
  193. }
  194. }
  195. return SetExtensions($hash,$setList,$name,@a) if( !$found );
  196. SetExtensionsCancel($hash);
  197. my $v = join(" ", @a);
  198. my $set_fn = AttrVal( $hash->{NAME}, "setFn", "" );
  199. if( $set_fn =~ m/^{.*}$/s ) {
  200. my $CMD = $a[0];
  201. my $DEVICE = $hash->{DEVICE};
  202. my $READING = $hash->{READING};
  203. my $ARGS = join(" ", @a[1..$#a]);
  204. my $set_fn = eval $set_fn;
  205. Log3 $name, 3, $name .": setFn: ". $@ if($@);
  206. readingsSingleUpdate($hash, "lastCmd", $a[0], 0);
  207. return undef if( !defined($set_fn) );
  208. $v = $set_fn if( $set_fn ne '' );
  209. } else {
  210. readingsSingleUpdate($hash, "lastCmd", $a[0], 0);
  211. }
  212. if( $hash->{INSET} ) {
  213. Log3 $name, 2, "$name: ERROR: endless loop detected";
  214. return "ERROR: endless loop detected for $hash->{NAME}";
  215. }
  216. Log3 $name, 4, "$name: set hash->{DEVICE} $v";
  217. $hash->{INSET} = 1;
  218. my $ret = CommandSet(undef,"$hash->{DEVICE} ".$v);
  219. delete($hash->{INSET});
  220. return $ret;
  221. }
  222. sub
  223. readingsProxy_Get($@)
  224. {
  225. my ($hash, $name, @a) = @_;
  226. return "no get value specified" if(int(@a) < 1);
  227. my $getList = AttrVal($name, "getList", "");
  228. $getList = getAllGets($hash->{DEVICE}) if( $getList eq "%PARENT%" );
  229. return "Unknown argument ?, choose one of $getList" if(!$getList || $a[0] eq "?");
  230. my $found = 0;
  231. foreach my $get (split(" ", $getList)) {
  232. if( "$get " =~ m/^${a[0]}[ :]/ ) {
  233. $found = 1;
  234. last;
  235. }
  236. }
  237. return "Unknown argument $a[0], choose one of $getList" if(!$found);
  238. my $v = join(" ", @a);
  239. my $get_fn = AttrVal( $hash->{NAME}, "getFn", "" );
  240. if( $get_fn =~ m/^{.*}$/s ) {
  241. my $CMD = $a[0];
  242. my $DEVICE = $hash->{DEVICE};
  243. my $READING = $hash->{READING};
  244. my $ARGS = join(" ", @a[1..$#a]);
  245. my ($get_fn,$direct_return) = eval $get_fn;
  246. Log3 $name, 3, $name .": getFn: ". $@ if($@);
  247. return $get_fn if($direct_return);
  248. return undef if( !defined($get_fn) );
  249. $v = $get_fn if( $get_fn ne '' );
  250. }
  251. if( $hash->{INGET} ) {
  252. Log3 $name, 2, "$name: ERROR: endless loop detected";
  253. return "ERROR: endless loop detected for $hash->{NAME}";
  254. }
  255. Log3 $name, 4, "$name: get hash->{DEVICE} $v";
  256. $hash->{INSET} = 1;
  257. my$ret = CommandGet(undef,"$hash->{DEVICE} ".$v);
  258. delete($hash->{INSET});
  259. return $ret;
  260. }
  261. sub
  262. readingsProxy_Attr($$$;$)
  263. {
  264. my ($cmd, $name, $attrName, $attrVal) = @_;
  265. if( $cmd eq "set" ) {
  266. if( $attrName eq 'getFn' || $attrName eq 'setFn' || $attrName eq 'valueFn' ) {
  267. my %specials= (
  268. "%CMD" => $name,
  269. "%DEVICE" => $name,
  270. "%READING" => $name,
  271. "%ARGS" => $name,
  272. "%VALUE" => $name,
  273. "%LASTCMD" => $name,
  274. );
  275. my $err = perlSyntaxCheck($attrVal, %specials);
  276. return $err if($err);
  277. }
  278. }
  279. }
  280. 1;
  281. =pod
  282. =item helper
  283. =item summary make (a subset of) a reading from one device available as a new device
  284. =item summary_DE Reading eines Ger&auml;tes (oder einen Teil daraus) als eigenes Ger&auml;t
  285. =begin html
  286. <a name="readingsProxy"></a>
  287. <h3>readingsProxy</h3>
  288. <ul>
  289. Makes (a subset of) a reading from one device available as a new device.<br>
  290. This can be used to map channels from 1-Wire, EnOcean or SWAP devices to independend devices that
  291. can have state,icons and webCmd different from the parent device and can be used in a floorplan.
  292. <br><br>
  293. <a name="readingsProxy_Define"></a>
  294. <b>Define</b>
  295. <ul>
  296. <code>define &lt;name&gt; readingsProxy &lt;device&gt;:&lt;reading&gt;</code><br>
  297. <br>
  298. Examples:
  299. <ul>
  300. <code>define myProxy readingsProxy myDS2406:latch.A</code><br>
  301. </ul>
  302. </ul><br>
  303. <a name="readingsProxy_Set"></a>
  304. <b>Set</b>
  305. <ul>
  306. </ul><br>
  307. <a name="readingsProxy_Get"></a>
  308. <b>Get</b>
  309. <ul>
  310. </ul><br>
  311. <a name="readingsProxy_Attr"></a>
  312. <b>Attributes</b>
  313. <ul>
  314. <li>disable<br>
  315. 1 -> disable notify processing. Notice: this also disables rename and delete handling.</li>
  316. <li>getList<br>
  317. Space separated list of commands, which will be returned upon "get name ?",
  318. so the FHEMWEB frontend can construct a dropdown.
  319. %PARENT% will result in the complete list of commands from the parent device.
  320. get commands not in this list will be rejected.</li>
  321. <li>setList<br>
  322. Space separated list of commands, which will be returned upon "set name ?",
  323. so the FHEMWEB frontend can construct a dropdown and offer on/off switches.
  324. %PARENT% will result in the complete list of commands from the parent device.
  325. set commands not in this list will be rejected.
  326. Example: attr proxyName setList on off</li>
  327. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  328. <li>getFn<br>
  329. perl expresion that will return the get command forwarded to the parent device.
  330. has access to $DEVICE, $READING, $CMD and $ARGS.<br>
  331. undef -> do nothing<br>
  332. "" -> pass through<br>
  333. (&lt;value&gt;,1) -> directly return &lt;value&gt;, don't call parent getFn<br>
  334. everything else -> use this instead</li>
  335. <li>setFn<br>
  336. perl expresion that will return the set command forwarded to the parent device.
  337. has access to $CMD, $DEVICE, $READING and $ARGS.<br>
  338. undef -> do nothing<br>
  339. "" -> pass through<br>
  340. everything else -> use this instead<br>
  341. Examples:<br>
  342. <code>attr myProxy setFn {($CMD eq "on")?"off":"on"}</code>
  343. </li>
  344. <li>valueFn<br>
  345. perl expresion that will return the value that sould be used as state.
  346. has access to $LASTCMD, $DEVICE, $READING and $VALUE.<br>
  347. undef -> do nothing<br>
  348. "" -> pass through<br>
  349. everything else -> use this instead<br>
  350. Examples:<br>
  351. <code>attr myProxy valueFn {($VALUE == 0)?"off":"on"}</code>
  352. </li>
  353. <br><li><a href="#perlSyntaxCheck">perlSyntaxCheck</a></li>
  354. </ul><br>
  355. </ul>
  356. =end html
  357. =cut