23_WEBIO_12DIGITAL.pm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. ################################################################
  2. #
  3. # Copyright notice
  4. #
  5. # (c) 2011 Sacha Gloor (sacha@imp.ch)
  6. #
  7. # This script is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # The GNU General Public License can be found at
  13. # http://www.gnu.org/copyleft/gpl.html.
  14. # A copy is found in the textfile GPL.txt and important notices to the license
  15. # from the author is found in LICENSE.txt distributed with these scripts.
  16. #
  17. # This script is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU General Public License for more details.
  21. #
  22. # This copyright notice MUST APPEAR in all copies of the script!
  23. #
  24. ################################################################
  25. # $Id: 23_WEBIO_12DIGITAL.pm 2076 2012-11-04 13:49:43Z rudolfkoenig $
  26. ##############################################
  27. package main;
  28. use strict;
  29. use warnings;
  30. use Data::Dumper;
  31. use IO::Socket;
  32. #use LWP::UserAgent;
  33. #use HTTP::Request;
  34. sub
  35. WEBIO_12DIGITAL_Initialize($)
  36. {
  37. my ($hash) = @_;
  38. $hash->{SetFn} = "WEBIO_12DIGITAL_Set";
  39. $hash->{DefFn} = "WEBIO_12DIGITAL_Define";
  40. $hash->{AttrList} = "loglevel:0,1,2,3,4,5,6";
  41. }
  42. ###################################
  43. sub
  44. WEBIO_12DIGITAL_Set($@)
  45. {
  46. my ($hash, @a) = @_;
  47. return "no set value specified" if(int(@a) < 2);
  48. return "Unknown argument $a[1], choose one of on off on-for-timer" if($a[1] eq "?");
  49. my $v = $a[1];
  50. my $v2= "";
  51. if(defined($a[2])) { $v2=$a[2]; }
  52. RemoveInternalTimer("WEBIO_12DIGITAL_on_timeout");
  53. if($v eq "on-for-timer")
  54. {
  55. InternalTimer(gettimeofday()+$v2, "WEBIO_12DIGITAL_on_timeout",$hash, 0);
  56. # on-for-timer is now a on.
  57. $v="on";
  58. }
  59. WEBIO_12DIGITAL_execute($hash->{DEF},$v);
  60. Log GetLogLevel($a[0],2), "WEBIO_12DIGITAL set @a";
  61. $hash->{CHANGED}[0] = $v;
  62. $hash->{STATE} = $v;
  63. $hash->{READINGS}{state}{TIME} = TimeNow();
  64. $hash->{READINGS}{state}{VAL} = $v;
  65. DoTrigger($hash->{NAME}, undef);
  66. return undef;
  67. }
  68. sub
  69. WEBIO_12DIGITAL_on_timeout($)
  70. {
  71. my ($hash) = @_;
  72. my @a;
  73. $a[0]=$hash->{NAME};
  74. $a[1]="off";
  75. WEBIO_12DIGITAL_Set($hash,@a);
  76. return undef;
  77. }
  78. ###################################
  79. sub
  80. WEBIO_12DIGITAL_execute($@)
  81. {
  82. my ($target,$cmd) = @_;
  83. my $URL='';
  84. my $v='';
  85. my $err_log='';
  86. if($cmd eq "on") { $v="ON"; }
  87. else { $v="OFF"; }
  88. my @a = split("[ \t][ \t]*", $target);
  89. my $sock = new IO::Socket::INET (
  90. PeerAddr => $a[0],
  91. PeerPort => '80',
  92. Proto => 'tcp',
  93. Timeout => "3",
  94. );
  95. $err_log = "Could not create socket: $!\n" unless $sock;
  96. if($err_log ne "") { return undef; }
  97. print $sock "GET /outputaccess".$a[1]."?PW=&State=".$v."\n";
  98. close($sock);
  99. return undef;
  100. }
  101. sub
  102. WEBIO_12DIGITAL_Define($$)
  103. {
  104. my ($hash, $def) = @_;
  105. my $name=$hash->{NAME};
  106. my @a = split("[ \t][ \t]*", $def);
  107. my $host = $a[2];
  108. my $host_port = $a[3];
  109. my $delay=$a[4];
  110. $attr{$name}{delay}=$delay if $delay;
  111. return "Wrong syntax: use define <name> WEBIO_12DIGITAL <ip-address> <port-nr> <poll-delay>" if(int(@a) != 5);
  112. $hash->{Host} = $host;
  113. $hash->{Host_Port} = $host_port;
  114. InternalTimer(gettimeofday()+$delay, "WEBIO_12DIGITAL_GetStatus", $hash, 0);
  115. return undef;
  116. }
  117. #####################################
  118. sub
  119. WEBIO_12DIGITAL_GetStatus($)
  120. {
  121. my ($hash) = @_;
  122. my $err_log='';
  123. my $line;
  124. my $state;
  125. my $name = $hash->{NAME};
  126. my $host = $hash->{Host};
  127. my $delay=$attr{$name}{delay}||300;
  128. InternalTimer(gettimeofday()+$delay, "WEBIO_12DIGITAL_GetStatus", $hash, 0);
  129. if(!defined($hash->{Host_Port})) { return(""); }
  130. my $host_port = $hash->{Host_Port};
  131. my $sock = new IO::Socket::INET (
  132. PeerAddr => $host,
  133. PeerPort => '80',
  134. Proto => 'tcp',
  135. Timeout => "3",
  136. );
  137. $err_log = "Could not create socket: $!\n" unless $sock;
  138. if($err_log ne "")
  139. {
  140. Log GetLogLevel($name,2), "WEBIO_12DIGITAL ".$err_log;
  141. return("");
  142. }
  143. print $sock "GET /output".$host_port."?PW=&\n";
  144. $line = <$sock>;
  145. close($sock);
  146. if($line =~ /ON/) { $state="on"; } else { $state="off"; }
  147. if($hash->{STATE} ne $state)
  148. {
  149. Log 4, "WEBIO_12DIGITAL_GetStatus: $host_port ".$hash->{STATE}." -> ".$state;
  150. $hash->{STATE} = $state;
  151. $hash->{CHANGED}[0] = $state;
  152. $hash->{READINGS}{state}{TIME} = TimeNow();
  153. $hash->{READINGS}{state}{VAL} = $state;
  154. DoTrigger($name, undef) if($init_done);
  155. }
  156. }
  157. 1;
  158. =pod
  159. =begin html
  160. <a name="WEBIO_12DIGITAL"></a>
  161. <h3>WEBIO_12DIGITAL</h3>
  162. <ul>
  163. Note: this module needs the HTTP::Request and LWP::UserAgent perl modules.
  164. <br><br>
  165. <a name="WEBIO_12DIGITALdefine"></a>
  166. <b>Define</b>
  167. <ul>
  168. <code>define &lt;name&gt; WEBIO_12DIGITAL &lt;ip-address&gt; &lt;outputport&gt; &lt;delay&gt;</code>
  169. <br><br>
  170. Defines an Web-IO-Digital device (Box with up to 12 digital in/outputs, www.wut.de) via ip address. The status of the device is also pooled (delay interval).<br><br>
  171. Examples:
  172. <ul>
  173. <code>define motor1 WEBIO_12DIGITAL 192.168.8.200 1 60</code><br>
  174. </ul>
  175. </ul>
  176. <br>
  177. <a name="WEBIO_12DIGITALset"></a>
  178. <b>Set </b>
  179. <ul>
  180. <code>set &lt;name&gt; &lt;value&gt;</code>
  181. <br><br>
  182. where <code>value</code> is one of:<br>
  183. <pre>
  184. on off
  185. </pre>
  186. Examples:
  187. <ul>
  188. <code>set motor1 on</code><br>
  189. </ul>
  190. <br>
  191. </ul>
  192. </ul>
  193. =end html
  194. =cut