46_PW_Circle.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 17302 2018-09-09 07:43:23Z 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 eq "toggle") {
  115. $opt = ReadingsVal($name,"state","off") eq "off" ? "on" : "off";
  116. IOWrite($hash,$hash->{CODE},$opt);
  117. }elsif($opt =~ "(on|off)-for-timer")
  118. {
  119. if (@a == 1) {
  120. IOWrite($hash,$hash->{CODE},$1);
  121. RemoveInternalTimer("onofffortimer:".$name.":off");
  122. RemoveInternalTimer("onofffortimer:".$name.":on");
  123. InternalTimer(gettimeofday()+$value, "PW_Circle_OnOffTimer", 'onofffortimer:'.$name.':' . ($1 eq "on"?"off":"on"), 1);
  124. }
  125. } elsif ($opt eq "getLog") {
  126. IOWrite($hash,$hash->{CODE},$opt,$value) if (@a == 3);
  127. } elsif ($opt eq "syncTime") {
  128. IOWrite($hash,$hash->{CODE},$opt);
  129. } elsif ($opt eq "removeNode") {
  130. IOWrite($hash,$hash->{CODE},$opt);
  131. } elsif ($opt eq "ping") {
  132. IOWrite($hash,$hash->{CODE},$opt);
  133. } elsif ($opt eq "status") {
  134. IOWrite($hash,$hash->{CODE},$opt);
  135. }
  136. else
  137. {
  138. return "Unknown argument $opt, choose one of on-for-timer off-for-timer on off toggle getLog syncTime removeNode ping status";
  139. }
  140. }
  141. sub PW_Circle_OnOffTimer($) {
  142. my($in ) = shift;
  143. my(undef,$name,$pwr) = split(':',$in);
  144. return if (!$name || !defined $defs{$name});
  145. my $hash=$defs{$name};
  146. IOWrite($hash,$hash->{CODE},$pwr);
  147. }
  148. sub PW_Circle_Get($@)
  149. {
  150. # elsif ($opt eq "livepower") {
  151. # IOWrite($hash,$hash->{CODE},$opt);
  152. # }
  153. my ( $hash, @a ) = @_;
  154. my $n=1;
  155. return "\"get X\" needs at least one argument" if ( @a < 2 );
  156. my $name = shift @a;
  157. my $opt = shift @a;
  158. if(!$PW_gets{$opt}) {
  159. my @cList = keys %PW_gets;
  160. return "Unknown argument $opt, choose one of " . join(" ", @cList);
  161. }
  162. if($opt eq 'livepower') {
  163. IOWrite($hash,$hash->{CODE},$opt);
  164. }
  165. }
  166. sub PW_Circle_Parse($$)
  167. {
  168. my ($hash, $msg2) = @_;
  169. #Log 3,Dumper($msg2);
  170. my $msg=$hash->{RAWMSG};
  171. my $time = time();
  172. # RemoveInternalTimer($hash);
  173. # InternalTimer(gettimeofday()+3, "Circles_GetLog", $hash, 0);
  174. # Log 3,"SetTimer";
  175. #Log3 $hash,3,"Circles: Parse called ".$msg->{short};
  176. if ($msg->{type} eq "err") {return undef};
  177. # Log 3,Dumper($hash->{RAWMSG});
  178. $time_old = $time;
  179. Log3 $hash,5, Dumper($msg);
  180. my $device_name = "PW_Circle".$DOT.$msg->{short};
  181. Log3 $hash,5,"New Devicename: $device_name";
  182. my $def = $modules{PW_Circle}{defptr}{"$device_name"};
  183. if(!$def) {
  184. Log3 $hash, 3, "PW_Circle: Unknown device $device_name, please define it";
  185. return "UNDEFINED $device_name PW_Circle $msg->{'short'}";
  186. }
  187. # Use $def->{NAME}, because the device may be renamed:
  188. my $name = $def->{NAME};
  189. $hash->{helper}{circles}{$name}{lastContact}=time;
  190. $hash->{helper}{circles}{$name}{name}=$msg->{short};
  191. # Log 3,Dumper($hash->{helper});
  192. my $type = $msg->{type};
  193. Log3 $hash,5,"Circle: Type is '$type'";
  194. readingsBeginUpdate($def);
  195. Log3 $hash,5,Dumper($msg);
  196. if($type eq "output") {
  197. readingsBulkUpdate($def, "state", $msg->{text}) if (ReadingsVal($name,"state","off") ne $msg->{text});
  198. if ($msg->{text} eq "offline") {return $name}
  199. my $nr = $msg->{val1};
  200. $nr--;
  201. IOWrite($def,$def->{CODE},"getLog",$nr) if (ReadingsVal($name,"address","-") ne $nr);
  202. }
  203. if($type eq "ping") {
  204. readingsBulkUpdate($def, "ping", "$msg->{val1} - $msg->{val2} - $msg->{val3}");
  205. }
  206. if($type eq "power") {
  207. if ($msg->{val2} < 17000) {
  208. readingsBulkUpdate($def, "power", $msg->{val1});
  209. readingsBulkUpdate($def, "power8", $msg->{val2});
  210. } else
  211. {
  212. readingsBulkUpdate($def, "IgnoredSpikes",ReadingsVal($name,"IgnoredSpikes",0) + 1);
  213. }
  214. }
  215. if($type eq "energy") {
  216. readingsBulkUpdate($def, "energy", $msg->{val1});
  217. readingsBulkUpdate($def, "energy_ts", $msg->{val2});
  218. readingsBulkUpdate($def, "address", $msg->{val3})
  219. }
  220. #2015.09.11 05:56:25 3: $VAR1 = {
  221. # 'code' => '0013C40B000D6F0002907CC90000000200000376FFFF8C180007',
  222. # 'dest' => 'Circle',
  223. # 'device' => '000D6F0002907CC9',
  224. # 'schema' => 'plugwise.basic',
  225. # 'short' => '2907CC9',
  226. # 'text' => '',
  227. # 'type' => 'power',
  228. # 'unit1' => 'W',
  229. # 'unit2' => 'W',
  230. # 'val1' => '0',
  231. # 'val2' => '1'
  232. # };
  233. #2015.09.11 05:56:25 3: $VAR1 = {
  234. # 'code' => '0024C40C000D6F0002907CC90F09392A0006A05801856539070140264E0844C202',
  235. # 'dest' => 'Circle',
  236. # 'device' => '000D6F0002907CC9',
  237. # 'schema' => 'plugwise.basic',
  238. # 'short' => '2907CC9',
  239. # 'text' => 'on',
  240. # 'type' => 'output',
  241. # 'val1' => '19467',
  242. # 'val2' => '2015-09-11 03:54'
  243. # };
  244. readingsEndUpdate($def, 1);
  245. return $name;
  246. }
  247. "Cogito, ergo sum.";
  248. =pod
  249. =item device
  250. =item summary Submodule for 45_Plugwise
  251. =item summary_DE Untermodul zu 45_Plugwise
  252. =begin html
  253. <a name="PW_Circle"></a>
  254. <h3>PW_Circles</h3>
  255. <ul>
  256. The PW_Circles module is invoked by Plugwise. You need to define a Plugwise-Stick first.
  257. See <a href="#Plugwise">Plugwise</a>.
  258. <br>
  259. <a name="PW_Circle define"></a>
  260. <br>
  261. <b>Define</b>
  262. <ul>
  263. <code>define &lt;name&gt; PW_Circle &lt;ShortAddress&gt;</code> <br>
  264. <br>
  265. <code>&lt;ShortAddress&gt;</code>
  266. <ul>
  267. specifies the short (last 4 Bytes) of the Circle received by the Plugwise-Stick. <br>
  268. </ul>
  269. <br><br>
  270. </ul>
  271. <b>Set</b>
  272. <ul>
  273. <code>on / off</code> <br>
  274. <ul>
  275. Turns the circle on or off<br><br>
  276. </ul>
  277. <code>on-for-timer / off-for-timer sec</code> <br>
  278. <ul>
  279. Turns the circle on or off for a given interval<br><br>
  280. </ul>
  281. <code>syncTime</code> <br>
  282. <ul>
  283. Syncronises the internal clock of the Circle with your PC's clock<br><br>
  284. </ul>
  285. <code>removeNode</code> <br>
  286. <ul>
  287. Removes this device from your Plugwise-network<br><br>
  288. </ul>
  289. <code>ping</code> <br>
  290. <ul>
  291. Ping the circle and write the Ping-Runtime to reading "ping" in format "q_in - q_out - pingTime"<br><br>
  292. </ul>
  293. <code>status</code> <br>
  294. <ul>
  295. Gets the current state of this cirle.<br><br>
  296. </ul>
  297. </ul>
  298. <br><br>
  299. <b>Attributes</b>
  300. <ul>
  301. <code>interval</code> <br>
  302. <ul>
  303. specifies the polling time for this circle<br>
  304. </ul>
  305. </ul>
  306. <br><br>
  307. <b>Example</b> <br>
  308. <ul><code>define Circle_2907CC9 PW_Circle 2907CC9</code></ul>
  309. <br>
  310. <br>
  311. </ul>
  312. =end html
  313. =begin html_DE
  314. <a name="PW_Circle"></a>
  315. <h3>PW_Circles</h3>
  316. <ul>
  317. Das PW_Circles Modul setzt auf das Plugwise-System auf. Es muss zuerst ein Plugwise-Stick angelegt werden.
  318. Siehe <a href="#Plugwise">Plugwise</a>.
  319. <br>
  320. <a name="PW_Circle define"></a>
  321. <br>
  322. <b>Define</b>
  323. <ul>
  324. <code>define &lt;name&gt; PW_Circle &lt;ShortAddress&gt;</code> <br>
  325. <br>
  326. <code>&lt;ShortAddress&gt;</code>
  327. <ul>
  328. gibt die Kurzadresse (die letzten 4 Bytes) des Circles an. <br>
  329. </ul>
  330. <br><br>
  331. </ul>
  332. <b>Set</b>
  333. <ul>
  334. <code>on / off</code> <br>
  335. <ul>
  336. Schaltet den Circle ein oder aus<br><br>
  337. </ul>
  338. <code>on-for-timer / off-for-timer sec</code> <br>
  339. <ul>
  340. Schaltet den Circle für n Sekunden an oder aus<br><br>
  341. </ul>
  342. <code>syncTime</code> <br>
  343. <ul>
  344. Synchronisiert die interne Uhr des Circles mit der lokalen Systemzeit<br><br>
  345. </ul>
  346. <code>removeNode</code> <br>
  347. <ul>
  348. Entfernt den Circle aus dem Plugwise-Netzwerk<br><br>
  349. </ul>
  350. <code>ping</code> <br>
  351. <ul>
  352. Sendet ein Ping an den Circle und setzt das Reading "ping" im Format "q_in - q_out - pingZeit"<br><br>
  353. </ul>
  354. <code>status</code> <br>
  355. <ul>
  356. Liest den aktuellen Status des Circles aus<br><br>
  357. </ul>
  358. </ul>
  359. <br><br>
  360. <b>Attribute</b>
  361. <ul>
  362. <code>interval</code> <br>
  363. <ul>
  364. Setzt das Abruf-Intervall speziell für diesen einen Circle<br>
  365. </ul>
  366. </ul>
  367. <br><br>
  368. <b>Beispiel</b> <br>
  369. <ul><code>define Circle_2907CC9 PW_Circle 2907CC9</code></ul>
  370. <br>
  371. <br>
  372. </ul>
  373. =end html
  374. =cut