33_readingsProxy.pm 11 KB

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