46_PW_Circle.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. #################################################################################
  2. # 46_PW_Circle.pm
  3. #
  4. # FHEM module Plugwise PW_Circle
  5. #
  6. # Copyright (C) 2014 Stefan Guttmann
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. #
  22. # The GNU General Public License may also be found at http://www.gnu.org/licenses/gpl-2.0.html .
  23. ###################################
  24. #
  25. # # $Id: 46_PW_Circle.pm 11989 2016-08-19 17:50:52Z icinger $
  26. package main;
  27. use strict;
  28. use warnings;
  29. use Data::Dumper;
  30. my $time_old = 0;
  31. my $DOT = q{_};
  32. my %Subtype = (
  33. "Stealth" => 02,
  34. "Circle" => 01,
  35. "Unknown" => 00
  36. );
  37. my %PW_gets = (
  38. "livepower" => 'Z'
  39. );
  40. sub PW_Circle_Initialize($)
  41. {
  42. my ($hash) = @_;
  43. $hash->{Match} = "PW_Circle";
  44. $hash->{DefFn} = "PW_Circle_Define";
  45. $hash->{UndefFn} = "PW_Circle_Undef";
  46. $hash->{ParseFn} = "PW_Circle_Parse";
  47. $hash->{SetFn} = "PW_Circle_Set";
  48. $hash->{GetFn} = "PW_Circle_Get";
  49. $hash->{AttrList} = "IODev interval do_not_notify:1,0 ".
  50. $readingFnAttributes;
  51. $hash->{AutoCreate} =
  52. { "PW_Circle.*" => { ATTR => "room:Plugwise interval:10"} };
  53. # Log3 $hash, 3, "PW_Circle_Initialize() Initialize";
  54. }
  55. #####################################
  56. sub PW_Circle_Define($$)
  57. {
  58. my ($hash, $def) = @_;
  59. my @a = split("[ \t][ \t]*", $def);
  60. my $a = int(@a);
  61. #Log 3,"Circle define $a[0]";
  62. return "wrong syntax: define <name> PW_Circle address" if(int(@a) != 3);
  63. my $name = $a[0];
  64. my $code = $a[2];
  65. my $device_name = "PW_Circle".$DOT.$code;
  66. #Log 3,Dumper($hash);
  67. $hash->{CODE} = $code;
  68. $modules{PW_Circle}{defptr}{$device_name} = $hash;
  69. AssignIoPort($hash);
  70. # $attr{$name}{interval}=$hash->{NAME}{interval};
  71. if( $init_done ) {
  72. # $attr{$name}{room}='Plugwise';
  73. # $attr{$name}{interval}=$hash->{NAME}{interval};
  74. }
  75. # PW_InternalTimer($code,gettimeofday()+2, "Circles_GetLog", $hash, 0);
  76. InternalTimer(gettimeofday()+2, "PW_Circle_GetLog", 'getLog:'.$name, 1);
  77. return undef;
  78. }
  79. sub PW_Circle_GetLog($){
  80. my($in ) = shift;
  81. my(undef,$name) = split(':',$in);
  82. return if (!$name || !defined $defs{$name});
  83. my $hash=$defs{$name};
  84. my $IOName = $hash->{IODev}->{NAME};
  85. # my $name = $hash->{NAME};
  86. my $int = AttrVal($name,"interval",undef);
  87. if (!defined($int)) {$int = AttrVal($IOName,"interval",undef);}
  88. # Log 3,"Get Info from Circle $name on interval $int";
  89. IOWrite($hash,$hash->{CODE},"status");
  90. IOWrite($hash,$hash->{CODE},"livepower");
  91. InternalTimer(gettimeofday()+$int, "PW_Circle_GetLog", 'getLog:'.$name, 0) if defined($int);
  92. }
  93. #####################################
  94. sub PW_Circle_Undef($$)
  95. {
  96. my ($hash, $name) = @_;
  97. RemoveInternalTimer("onofffortimer:".$name.":off");
  98. RemoveInternalTimer("onofffortimer:".$name.":on");
  99. RemoveInternalTimer("getLog:".$name);
  100. delete($modules{PW_Circle}{defptr}{$name});
  101. return undef;
  102. }
  103. sub PW_Circle_Set($@)
  104. {
  105. my ( $hash, @a ) = @_;
  106. return "\"set X\" needs at least an argument" if ( @a < 2 );
  107. my $name = shift @a;
  108. my $opt = shift @a;
  109. my $value = join("", @a);
  110. #Log3 $hash,3,"$hash->{NAME} - Circle-Set: N:$name O:$opt V:$value";
  111. if($opt eq "on"||$opt eq "off")
  112. {
  113. IOWrite($hash,$hash->{CODE},$opt);
  114. } elsif($opt =~ "(on|off)-for-timer")
  115. {
  116. if (@a == 1) {
  117. IOWrite($hash,$hash->{CODE},$1);
  118. RemoveInternalTimer("onofffortimer:".$name.":off");
  119. RemoveInternalTimer("onofffortimer:".$name.":on");
  120. InternalTimer(gettimeofday()+$value, "PW_Circle_OnOffTimer", 'onofffortimer:'.$name.':' . ($1 eq "on"?"off":"on"), 1);
  121. }
  122. } elsif ($opt eq "getLog") {
  123. IOWrite($hash,$hash->{CODE},$opt,$value) if (@a == 3);
  124. } elsif ($opt eq "syncTime") {
  125. IOWrite($hash,$hash->{CODE},$opt);
  126. } elsif ($opt eq "removeNode") {
  127. IOWrite($hash,$hash->{CODE},$opt);
  128. } elsif ($opt eq "ping") {
  129. IOWrite($hash,$hash->{CODE},$opt);
  130. } elsif ($opt eq "status") {
  131. IOWrite($hash,$hash->{CODE},$opt);
  132. }
  133. else
  134. {
  135. return "Unknown argument $opt, choose one of on-for-timer off-for-timer on off getLog syncTime removeNode ping status";
  136. }
  137. }
  138. sub PW_Circle_OnOffTimer($) {
  139. my($in ) = shift;
  140. my(undef,$name,$pwr) = split(':',$in);
  141. return if (!$name || !defined $defs{$name});
  142. my $hash=$defs{$name};
  143. IOWrite($hash,$hash->{CODE},$pwr);
  144. }
  145. sub PW_Circle_Get($@)
  146. {
  147. # elsif ($opt eq "livepower") {
  148. # IOWrite($hash,$hash->{CODE},$opt);
  149. # }
  150. my ( $hash, @a ) = @_;
  151. my $n=1;
  152. return "\"get X\" needs at least one argument" if ( @a < 2 );
  153. my $name = shift @a;
  154. my $opt = shift @a;
  155. if(!$PW_gets{$opt}) {
  156. my @cList = keys %PW_gets;
  157. return "Unknown argument $opt, choose one of " . join(" ", @cList);
  158. }
  159. if($opt eq 'livepower') {
  160. IOWrite($hash,$hash->{CODE},$opt);
  161. }
  162. }
  163. sub PW_Circle_Parse($$)
  164. {
  165. my ($hash, $msg2) = @_;
  166. #Log 3,Dumper($msg2);
  167. my $msg=$hash->{RAWMSG};
  168. my $time = time();
  169. # RemoveInternalTimer($hash);
  170. # InternalTimer(gettimeofday()+3, "Circles_GetLog", $hash, 0);
  171. # Log 3,"SetTimer";
  172. #Log3 $hash,3,"Circles: Parse called ".$msg->{short};
  173. if ($msg->{type} eq "err") {return undef};
  174. # Log 3,Dumper($hash->{RAWMSG});
  175. $time_old = $time;
  176. Log3 $hash,5, Dumper($msg);
  177. my $device_name = "PW_Circle".$DOT.$msg->{short};
  178. Log3 $hash,5,"New Devicename: $device_name";
  179. my $def = $modules{PW_Circle}{defptr}{"$device_name"};
  180. if(!$def) {
  181. Log3 $hash, 3, "PW_Circle: Unknown device $device_name, please define it";
  182. return "UNDEFINED $device_name PW_Circle $msg->{'short'}";
  183. }
  184. # Use $def->{NAME}, because the device may be renamed:
  185. my $name = $def->{NAME};
  186. $hash->{helper}{circles}{$name}{lastContact}=time;
  187. $hash->{helper}{circles}{$name}{name}=$msg->{short};
  188. # Log 3,Dumper($hash->{helper});
  189. my $type = $msg->{type};
  190. Log3 $hash,5,"Circle: Type is '$type'";
  191. readingsBeginUpdate($def);
  192. Log3 $hash,5,Dumper($msg);
  193. if($type eq "output") {
  194. readingsBulkUpdate($def, "state", $msg->{text}) if (ReadingsVal($name,"state","off") ne $msg->{text});
  195. if ($msg->{text} eq "offline") {return $name}
  196. my $nr = $msg->{val1};
  197. $nr--;
  198. IOWrite($def,$def->{CODE},"getLog",$nr) if (ReadingsVal($name,"address","-") ne $nr);
  199. }
  200. if($type eq "ping") {
  201. readingsBulkUpdate($def, "ping", "$msg->{val1} - $msg->{val2} - $msg->{val3}");
  202. }
  203. if($type eq "power") {
  204. if ($msg->{val2} < 17000) {
  205. readingsBulkUpdate($def, "power", $msg->{val1});
  206. readingsBulkUpdate($def, "power8", $msg->{val2});
  207. } else
  208. {
  209. readingsBulkUpdate($def, "IgnoredSpikes",ReadingsVal($name,"IgnoredSpikes",0) + 1);
  210. }
  211. }
  212. if($type eq "energy") {
  213. readingsBulkUpdate($def, "energy", $msg->{val1});
  214. readingsBulkUpdate($def, "energy_ts", $msg->{val2});
  215. readingsBulkUpdate($def, "address", $msg->{val3})
  216. }
  217. #2015.09.11 05:56:25 3: $VAR1 = {
  218. # 'code' => '0013C40B000D6F0002907CC90000000200000376FFFF8C180007',
  219. # 'dest' => 'Circle',
  220. # 'device' => '000D6F0002907CC9',
  221. # 'schema' => 'plugwise.basic',
  222. # 'short' => '2907CC9',
  223. # 'text' => '',
  224. # 'type' => 'power',
  225. # 'unit1' => 'W',
  226. # 'unit2' => 'W',
  227. # 'val1' => '0',
  228. # 'val2' => '1'
  229. # };
  230. #2015.09.11 05:56:25 3: $VAR1 = {
  231. # 'code' => '0024C40C000D6F0002907CC90F09392A0006A05801856539070140264E0844C202',
  232. # 'dest' => 'Circle',
  233. # 'device' => '000D6F0002907CC9',
  234. # 'schema' => 'plugwise.basic',
  235. # 'short' => '2907CC9',
  236. # 'text' => 'on',
  237. # 'type' => 'output',
  238. # 'val1' => '19467',
  239. # 'val2' => '2015-09-11 03:54'
  240. # };
  241. readingsEndUpdate($def, 1);
  242. return $name;
  243. }
  244. "Cogito, ergo sum.";
  245. =pod
  246. =item device
  247. =item summary Submodule for 45_Plugwise
  248. =item summary_DE Untermodul zu 45_Plugwise
  249. =begin html
  250. <a name="PW_Circle"></a>
  251. <h3>PW_Circles</h3>
  252. <ul>
  253. The PW_Circles module is invoked by Plugwise. You need to define a Plugwise-Stick first.
  254. See <a href="#Plugwise">Plugwise</a>.
  255. <br>
  256. <a name="PW_Circle define"></a>
  257. <br>
  258. <b>Define</b>
  259. <ul>
  260. <code>define &lt;name&gt; PW_Circle &lt;ShortAddress&gt;</code> <br>
  261. <br>
  262. <code>&lt;ShortAddress&gt;</code>
  263. <ul>
  264. specifies the short (last 4 Bytes) of the Circle received by the Plugwise-Stick. <br>
  265. </ul>
  266. <br><br>
  267. </ul>
  268. <b>Set</b>
  269. <ul>
  270. <code>on / off</code> <br>
  271. <ul>
  272. Turns the circle on or off<br><br>
  273. </ul>
  274. <code>on-for-timer / off-for-timer sec</code> <br>
  275. <ul>
  276. Turns the circle on or off for a given interval<br><br>
  277. </ul>
  278. <code>syncTime</code> <br>
  279. <ul>
  280. Syncronises the internal clock of the Circle with your PC's clock<br><br>
  281. </ul>
  282. <code>removeNode</code> <br>
  283. <ul>
  284. Removes this device from your Plugwise-network<br><br>
  285. </ul>
  286. <code>ping</code> <br>
  287. <ul>
  288. Ping the circle and write the Ping-Runtime to reading "ping" in format "q_in - q_out - pingTime"<br><br>
  289. </ul>
  290. <code>status</code> <br>
  291. <ul>
  292. Gets the current state of this cirle.<br><br>
  293. </ul>
  294. </ul>
  295. <br><br>
  296. <b>Attributes</b>
  297. <ul>
  298. <code>interval</code> <br>
  299. <ul>
  300. specifies the polling time for this circle<br>
  301. </ul>
  302. </ul>
  303. <br><br>
  304. <b>Example</b> <br>
  305. <ul><code>define Circle_2907CC9 PW_Circle 2907CC9</code></ul>
  306. <br>
  307. <br>
  308. </ul>
  309. =end html
  310. =begin html_DE
  311. <a name="PW_Circle"></a>
  312. <h3>PW_Circles</h3>
  313. <ul>
  314. Das PW_Circles Modul setzt auf das Plugwise-System auf. Es muss zuerst ein Plugwise-Stick angelegt werden.
  315. Siehe <a href="#Plugwise">Plugwise</a>.
  316. <br>
  317. <a name="PW_Circle define"></a>
  318. <br>
  319. <b>Define</b>
  320. <ul>
  321. <code>define &lt;name&gt; PW_Circle &lt;ShortAddress&gt;</code> <br>
  322. <br>
  323. <code>&lt;ShortAddress&gt;</code>
  324. <ul>
  325. gibt die Kurzadresse (die letzten 4 Bytes) des Circles an. <br>
  326. </ul>
  327. <br><br>
  328. </ul>
  329. <b>Set</b>
  330. <ul>
  331. <code>on / off</code> <br>
  332. <ul>
  333. Schaltet den Circle ein oder aus<br><br>
  334. </ul>
  335. <code>on-for-timer / off-for-timer sec</code> <br>
  336. <ul>
  337. Schaltet den Circle für n Sekunden an oder aus<br><br>
  338. </ul>
  339. <code>syncTime</code> <br>
  340. <ul>
  341. Synchronisiert die interne Uhr des Circles mit der lokalen Systemzeit<br><br>
  342. </ul>
  343. <code>removeNode</code> <br>
  344. <ul>
  345. Entfernt den Circle aus dem Plugwise-Netzwerk<br><br>
  346. </ul>
  347. <code>ping</code> <br>
  348. <ul>
  349. Sendet ein Ping an den Circle und setzt das Reading "ping" im Format "q_in - q_out - pingZeit"<br><br>
  350. </ul>
  351. <code>status</code> <br>
  352. <ul>
  353. Liest den aktuellen Status des Circles aus<br><br>
  354. </ul>
  355. </ul>
  356. <br><br>
  357. <b>Attribute</b>
  358. <ul>
  359. <code>interval</code> <br>
  360. <ul>
  361. Setzt das Abruf-Intervall speziell für diesen einen Circle<br>
  362. </ul>
  363. </ul>
  364. <br><br>
  365. <b>Beispiel</b> <br>
  366. <ul><code>define Circle_2907CC9 PW_Circle 2907CC9</code></ul>
  367. <br>
  368. <br>
  369. </ul>
  370. =end html
  371. =cut