22_ALL3076.pm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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: 22_ALL3076.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. ALL3076_Initialize($)
  35. {
  36. my ($hash) = @_;
  37. $hash->{SetFn} = "ALL3076_Set";
  38. $hash->{DefFn} = "ALL3076_Define";
  39. $hash->{AttrList} = "loglevel:0,1,2,3,4,5,6";
  40. }
  41. ###################################
  42. sub
  43. ALL3076_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 on off toggle dimdown dimup dim10% dim20% dim30% dim40% dim50% dim60% dim70% dim80% dim90% dim100%" if($a[1] eq "?");
  48. my $v = $a[1];
  49. my $v2 = "";
  50. my $err_log="";
  51. if(defined $a[2]) { $v2=$a[2]; }
  52. if($v eq "toggle")
  53. {
  54. if(defined $hash->{READINGS}{state}{VAL})
  55. {
  56. if($hash->{READINGS}{state}{VAL} eq "off")
  57. {
  58. $v="on";
  59. }
  60. else
  61. {
  62. $v="off";
  63. }
  64. }
  65. else
  66. {
  67. $v="off";
  68. }
  69. }
  70. Log GetLogLevel($a[0],2), "ALL3076 set @a";
  71. $err_log=ALL3076_execute($hash->{DEF},$v,$v2);
  72. if($err_log ne "")
  73. {
  74. Log GetLogLevel($a[0],2), "ALL3076 ".$err_log;
  75. }
  76. $hash->{CHANGED}[0] = $v.$v2;
  77. $hash->{STATE} = $v.$v2;
  78. $hash->{READINGS}{state}{TIME} = TimeNow();
  79. $hash->{READINGS}{state}{VAL} = $v.$v2;
  80. return undef;
  81. }
  82. ###################################
  83. sub
  84. ALL3076_execute($@)
  85. {
  86. my ($target,$cmd,$cmd2) = @_;
  87. my $URL='';
  88. my $log='';
  89. if($cmd eq "on")
  90. {
  91. $URL="http://".$target."/r?r=0&s=1";
  92. }
  93. elsif($cmd eq "off")
  94. {
  95. $URL="http://".$target."/r?r=0&s=0";
  96. }
  97. elsif($cmd eq "dimdown")
  98. {
  99. # We switch it on first
  100. $log.=ALL3076_execute($target,"on");
  101. $URL="http://".$target."/r?d=0";
  102. }
  103. elsif($cmd eq "dimup")
  104. {
  105. # We switch it on first
  106. $log.=ALL3076_execute($target,"on");
  107. $URL="http://".$target."/r?d=1";
  108. }
  109. elsif(substr($cmd,0,3) eq "dim")
  110. {
  111. # We switch it on first
  112. $log.=ALL3076_execute($target,"on");
  113. my $proz=substr($cmd,3,length($cmd)-4);
  114. my $proz_v=sprintf("%d",$proz*255/100);
  115. $URL="http://".$target."/r?d=".$proz_v;
  116. }
  117. elsif($cmd eq "on-old-for-timer")
  118. {
  119. sleep(1); # Todo
  120. }
  121. else
  122. {
  123. return($log);
  124. }
  125. # print "URL: $URL\n";
  126. my $agent = LWP::UserAgent->new(env_proxy => 1,keep_alive => 1, timeout => 30);
  127. my $header = HTTP::Request->new(GET => $URL);
  128. my $request = HTTP::Request->new('GET', $URL, $header);
  129. my $response = $agent->request($request);
  130. $log.= "Can't get $URL -- ".$response->status_line
  131. unless $response->is_success;
  132. return($log);
  133. }
  134. sub
  135. ALL3076_Define($$)
  136. {
  137. my ($hash, $def) = @_;
  138. my @a = split("[ \t][ \t]*", $def);
  139. return "Wrong syntax: use define <name> ALL3076 <ip-address>" if(int(@a) != 3);
  140. return undef;
  141. }
  142. 1;
  143. =pod
  144. =begin html
  145. <a name="ALL3076"></a>
  146. <h3>ALL3076</h3>
  147. <ul>
  148. Note: this module needs the HTTP::Request and LWP::UserAgent perl modules.
  149. <br><br>
  150. <a name="ALL3076define"></a>
  151. <b>Define</b>
  152. <ul>
  153. <code>define &lt;name&gt; ALL3076 &lt;ip-address&gt; </code>
  154. <br><br>
  155. Defines an Allnet 3076 device (Dimmable lightswitch) via its ip address or dns name<br><br>
  156. Examples:
  157. <ul>
  158. <code>define lamp1 ALL3076 192.168.1.200</code><br>
  159. </ul>
  160. </ul>
  161. <br>
  162. <a name="ALL3076set"></a>
  163. <b>Set </b>
  164. <ul>
  165. <code>set &lt;name&gt; &lt;value&gt;</code>
  166. <br><br>
  167. where <code>value</code> is one of:<br>
  168. <pre>
  169. dimdown
  170. dim10%
  171. dim20%
  172. dim30%
  173. dim40%
  174. dim50%
  175. dim60%
  176. dim70%
  177. dim80%
  178. dim90%
  179. dim100%
  180. dim[0-100]%
  181. dimup
  182. off
  183. on
  184. toggle
  185. </pre>
  186. Examples:
  187. <ul>
  188. <code>set lamp1 on</code><br>
  189. <code>set lamp1 dim11%</code><br>
  190. <code>set lamp2 toggle</code><br>
  191. </ul>
  192. <br>
  193. Notes:
  194. <ul>
  195. <li>Toggle is special implemented. List name returns "on" or "off" even after a toggle command</li>
  196. </ul>
  197. </ul>
  198. </ul>
  199. =end html
  200. =cut