23_WEBIO.pm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. ################################################################
  2. #
  3. # Copyright notice
  4. #
  5. # (c) 2010 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.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 LWP::UserAgent;
  32. use HTTP::Request;
  33. sub
  34. WEBIO_Initialize($)
  35. {
  36. my ($hash) = @_;
  37. $hash->{SetFn} = "WEBIO_Set";
  38. $hash->{DefFn} = "WEBIO_Define";
  39. $hash->{AttrList} = "loglevel:0,1,2,3,4,5,6";
  40. }
  41. ###################################
  42. sub
  43. WEBIO_Set($@)
  44. {
  45. my ($hash, @a) = @_;
  46. return "no set value specified" if(int(@a) != 2);
  47. return "Unknown argument $a[1], choose one of 0 1 2 3 4 5 6 7 8 9 10" if($a[1] eq "?");
  48. my $v = $a[1];
  49. my $sensor="volt";
  50. WEBIO_execute($hash->{DEF},$v);
  51. Log GetLogLevel($a[0],2), "WEBIO set @a";
  52. $hash->{CHANGED}[0] = "Volt:";
  53. $hash->{STATE} = "V: ".$v;
  54. $hash->{READINGS}{$sensor}{TIME} = TimeNow();
  55. $hash->{READINGS}{$sensor}{VAL} = $v." (Volt)";
  56. return undef;
  57. }
  58. ###################################
  59. sub
  60. WEBIO_execute($@)
  61. {
  62. my ($target,$cmd) = @_;
  63. my $URL='';
  64. my @a = split("[ \t][ \t]*", $target);
  65. $URL="http://".$a[0]."/outputaccess".$a[1]."?PW=&State=".$cmd."&";
  66. my $agent = LWP::UserAgent->new(env_proxy => 1,keep_alive => 1, timeout => 1);
  67. my $header = HTTP::Request->new(GET => $URL);
  68. my $request = HTTP::Request->new('GET', $URL, $header);
  69. my $response = $agent->request($request);
  70. return undef;
  71. }
  72. sub
  73. WEBIO_Define($$)
  74. {
  75. my ($hash, $def) = @_;
  76. my $name=$hash->{NAME};
  77. my @a = split("[ \t][ \t]*", $def);
  78. my $host = $a[2];
  79. my $host_port = $a[3];
  80. my $delay=$a[4];
  81. $attr{$name}{delay}=$delay if $delay;
  82. return "Wrong syntax: use define <name> WEBIO <ip-address> <port-nr> <poll-delay>" if(int(@a) != 5);
  83. $hash->{Host} = $host;
  84. $hash->{Host_Port} = $host_port;
  85. InternalTimer(gettimeofday()+$delay, "WEBIO_GetStatus", $hash, 0);
  86. return undef;
  87. }
  88. #####################################
  89. sub
  90. WEBIO_GetStatus($)
  91. {
  92. my ($hash) = @_;
  93. my $err_log='';
  94. my $line;
  95. my $name = $hash->{NAME};
  96. my $host = $hash->{Host};
  97. my $delay=$attr{$name}{delay}||300;
  98. InternalTimer(gettimeofday()+$delay, "WEBIO_GetStatus", $hash, 0);
  99. if(!defined($hash->{Host_Port})) { return(""); }
  100. my $host_port = $hash->{Host_Port};
  101. my $URL="http://".$host."/Single".$host_port;
  102. my $agent = LWP::UserAgent->new(env_proxy => 1,keep_alive => 1, timeout => 3);
  103. my $header = HTTP::Request->new(GET => $URL);
  104. my $request = HTTP::Request->new('GET', $URL, $header);
  105. my $response = $agent->request($request);
  106. $err_log.= "Can't get $URL -- ".$response->status_line
  107. unless $response->is_success;
  108. if($err_log ne "")
  109. {
  110. Log GetLogLevel($name,2), "WEBIO ".$err_log;
  111. return("");
  112. }
  113. my $body = $response->content;
  114. # print $body."\n";
  115. my @values=split(/;/,$body);
  116. my $last=$values[$#values];
  117. my @v=split(/ /,$last);
  118. my $state=$v[0];
  119. $state=~s/,/./g;
  120. my $sensor="volt";
  121. Log 4, "WEBIO_GetStatus: $host_port ".$hash->{STATE}." -> ".$state;
  122. $hash->{STATE} = "V: ".$state;
  123. $hash->{CHANGED}[0] = $state;
  124. $hash->{READINGS}{$sensor}{TIME} = TimeNow();
  125. $hash->{READINGS}{$sensor}{VAL} = $state." (Volt)";
  126. DoTrigger($name, undef) if($init_done);
  127. }
  128. 1;
  129. =pod
  130. =begin html
  131. <a name="WEBIO"></a>
  132. <h3>WEBIO</h3>
  133. <ul>
  134. Note: this module needs the HTTP::Request and LWP::UserAgent perl modules.
  135. <br><br>
  136. <a name="WEBIOdefine"></a>
  137. <b>Define</b>
  138. <ul>
  139. <code>define &lt;name&gt; WEBIO &lt;ip-address&gt; &lt;port&gt; &lt;delay&gt;</code>
  140. <br><br>
  141. Defines an Web-IO device (Box with 2 Analog-In/Out 0..10V, www.wut.de) via ip address. The status of the device is also pooled (delay interval).<br><br>
  142. Examples:
  143. <ul>
  144. <code>define pumpspeed WEBIO 192.168.8.200 1 60</code><br>
  145. </ul>
  146. </ul>
  147. <br>
  148. <a name="WEBIOset"></a>
  149. <b>Set </b>
  150. <ul>
  151. <code>set &lt;name&gt; &lt;value&gt;</code>
  152. <br><br>
  153. where <code>value</code> is one of:<br>
  154. <pre>
  155. 0.00 - 10.00
  156. </pre>
  157. Examples:
  158. <ul>
  159. <code>set pumpspeed 6.75</code><br>
  160. </ul>
  161. <br>
  162. </ul>
  163. </ul>
  164. =end html
  165. =cut