46_PW_Scan.pm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #################################################################################
  2. # 46_PW_Scan.pm
  3. #
  4. # FHEM module Plugwise motion scanners
  5. #
  6. # Copyright (C) 2015 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_Scan.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. sub PW_Scan_Initialize($)
  33. {
  34. my ($hash) = @_;
  35. $hash->{Match} = "PW_Scan";
  36. $hash->{DefFn} = "PW_Scan_Define";
  37. $hash->{UndefFn} = "PW_Scan_Undef";
  38. $hash->{ParseFn} = "PW_Scan_Parse";
  39. $hash->{SetFn} = "PW_Scan_Set";
  40. $hash->{AttrList} = "IODev do_not_notify:1,0 ".
  41. $readingFnAttributes;
  42. Log3 $hash, 5, "PW_Scan_Initialize() Initialize";
  43. }
  44. #####################################
  45. sub PW_Scan_Define($$)
  46. {
  47. my ($hash, $def) = @_;
  48. my @a = split("[ \t][ \t]*", $def);
  49. my $a = int(@a);
  50. Log3 $hash,5,"PW_Scan: Define called --> $def";
  51. return "wrong syntax: define <name> PW_Scan address" if(int(@a) != 3);
  52. my $name = $a[0];
  53. my $code = $a[2];
  54. my $device_name = "PW_Scan".$DOT.$code;
  55. $hash->{CODE} = $code;
  56. $modules{PW_Scan}{defptr}{$device_name} = $hash;
  57. AssignIoPort($hash);
  58. if( $init_done ) {
  59. $attr{$name}{room}='Plugwise';
  60. }
  61. return undef;
  62. }
  63. #####################################
  64. sub PW_Scan_Undef($$)
  65. {
  66. my ($hash, $name) = @_;
  67. delete($modules{PW_Scan}{defptr}{$name});
  68. return undef;
  69. }
  70. sub PW_Scan_Set($@)
  71. {
  72. my ( $hash, @a ) = @_;
  73. return "\"set X\" needs at least an argument" if ( @a < 2 );
  74. my $name = shift @a;
  75. my $opt = shift @a;
  76. my $value = join("", @a);
  77. Log3 $hash,5,"$hash->{NAME} - PW_Scan-Set: N:$name O:$opt V:$value";
  78. if ($opt eq "removeNode") {
  79. IOWrite($hash,$hash->{CODE},$opt);
  80. } elsif ($opt eq "ping") {
  81. IOWrite($hash,$hash->{CODE},$opt);
  82. }
  83. else
  84. {
  85. return "Unknown argument $opt, choose one of removeNode ping";
  86. }
  87. }
  88. sub PW_Scan_Parse($$)
  89. {
  90. my ($hash, $msg2) = @_;
  91. my $msg=$hash->{RAWMSG};
  92. #Log 3,"SCAN: got a msg";
  93. my $time = time();
  94. if ($msg->{type} eq "err") {return undef};
  95. Log3 $hash,5,"PW_Scan: Parse called ".$msg->{short};
  96. $time_old = $time;
  97. Log3 $hash,5, Dumper($msg);
  98. my $device_name = "PW_Scan".$DOT.$msg->{short};
  99. Log3 $hash,5,"New Devicename: $device_name";
  100. my $def = $modules{PW_Scan}{defptr}{"$device_name"};
  101. if(!$def) {
  102. Log3 $hash, 3, "PW_Scan: Unknown device $device_name, please define it";
  103. return "UNDEFINED $device_name PW_Scan $msg->{short}";
  104. }
  105. # Use $def->{NAME}, because the device may be renamed:
  106. my $name = $def->{NAME};
  107. my $type = $msg->{type};
  108. Log3 $hash,5,"PW_Scan: Type is '$type'";
  109. Log3 $hash,5,Dumper($msg);
  110. readingsBeginUpdate($def);
  111. Log3 $hash,5,Dumper($msg);
  112. if($type eq "sense") {
  113. readingsBulkUpdate($def, "state", ($msg->{val1} eq 0 ? 'off' : 'on' ));
  114. }
  115. readingsEndUpdate($def, 1);
  116. return $name;
  117. }
  118. "Cogito, ergo sum.";
  119. =pod
  120. =item device
  121. =item summary Submodule for 45_Plugwise
  122. =item summary_DE Untermodul zu 45_Plugwise
  123. =begin html
  124. <a name="PW_Scan"></a>
  125. <h3>PW_Scan</h3>
  126. <ul>
  127. The PW_Scan module is invoked by Plugwise. You need to define a Plugwise-Stick first.
  128. See <a href="#PW_Scan">PW_Scan</a>.
  129. <br>
  130. <a name="PW_Scan define"></a>
  131. <br>
  132. <b>Define</b>
  133. <ul>
  134. <code>define &lt;name&gt; PW_Scan &lt;ShortAddress&gt;</code> <br>
  135. <br>
  136. <code>&lt;ShortAddress&gt;</code>
  137. <ul>
  138. specifies the short (last 4 Bytes) of the PW_Scan received by the Plugwise-Stick. <br>
  139. </ul>
  140. <br>
  141. Example: <br>
  142. <code>define PW_Scan_2907CC9 PW_Scan 2907CC9</code>
  143. <br>
  144. </ul>
  145. <br>
  146. </ul>
  147. =end html
  148. =begin html_DE
  149. <a name="PW_Scan"></a>
  150. <h3>PW_Scan</h3>
  151. <ul>
  152. Das PW_Scan Modul setzt auf das Plugwise-System auf. Es muss zuerst ein Plugwise-Stick angelegt werden.
  153. Siehe <a href="#Plugwise">Plugwise</a>.
  154. <br>
  155. <a name="PW_Scan define"></a>
  156. <br>
  157. <b>Define</b>
  158. <ul>
  159. <code>define &lt;name&gt; PW_Scan &lt;ShortAddress&gt;</code> <br>
  160. <br>
  161. <code>&lt;ShortAddress&gt;</code>
  162. <ul>
  163. gibt die Kurzadresse (die letzten 4 Bytes) des Gerätes an. <br>
  164. </ul>
  165. <br><br>
  166. </ul>
  167. </ul>
  168. =end html
  169. =cut