88_Itach_Relay.pm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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: 88_Itach_Relay.pm 2078 2012-11-04 14:31:59Z rudolfkoenig $
  26. ##############################################
  27. package main;
  28. use strict;
  29. use warnings;
  30. use Data::Dumper;
  31. use Net::Telnet;
  32. sub
  33. ITACH_RELAY_Initialize($)
  34. {
  35. my ($hash) = @_;
  36. $hash->{SetFn} = "ITACH_RELAY_Set";
  37. $hash->{DefFn} = "ITACH_RELAY_Define";
  38. $hash->{AttrList} = "loglevel:0,1,2,3,4,5,6";
  39. }
  40. ###################################
  41. sub
  42. ITACH_RELAY_Set($@)
  43. {
  44. my ($hash, @a) = @_;
  45. return "no set value specified" if(int(@a) != 2);
  46. return "Unknown argument $a[1], choose one of on off toggle" if($a[1] eq "?");
  47. my $v = $a[1];
  48. if($v eq "toggle")
  49. {
  50. if(defined $hash->{READINGS}{state}{VAL})
  51. {
  52. if($hash->{READINGS}{state}{VAL} eq "off")
  53. {
  54. $v="on";
  55. }
  56. else
  57. {
  58. $v="off";
  59. }
  60. }
  61. else
  62. {
  63. $v="off";
  64. }
  65. }
  66. ITACH_RELAY_execute($hash->{DEF},$v);
  67. Log GetLogLevel($a[0],2), "ITACH_RELAY set @a";
  68. $hash->{CHANGED}[0] = $v;
  69. $hash->{STATE} = $v;
  70. $hash->{READINGS}{state}{TIME} = TimeNow();
  71. $hash->{READINGS}{state}{VAL} = $v;
  72. DoTrigger($hash->{NAME}, undef);
  73. return undef;
  74. }
  75. ###################################
  76. sub
  77. ITACH_RELAY_execute($@)
  78. {
  79. my ($target,$cmd) = @_;
  80. my $URL='';
  81. my $v='';
  82. my $err_log='';
  83. if($cmd eq "on") { $v=1; }
  84. else { $v=0; }
  85. my @a = split("[ \t][ \t]*", $target);
  86. my $tel=new Net::Telnet(Host => $a[0], Port => 4998,Timeout => 3, Binmode => 0, Telnetmode => 0, Errmode => "return");
  87. if(!defined($tel))
  88. {
  89. Log 4,"Error connecting to ".$a[0].":4998";
  90. }
  91. else
  92. {
  93. my $cmd="setstate,1:".$a[1].",".$v."\n";
  94. $tel->print($cmd);
  95. sleep(1);
  96. }
  97. return undef;
  98. }
  99. sub
  100. ITACH_RELAY_Define($$)
  101. {
  102. my ($hash, $def) = @_;
  103. my $name=$hash->{NAME};
  104. my @a = split("[ \t][ \t]*", $def);
  105. my $host = $a[2];
  106. my $host_port = $a[3];
  107. return "Wrong syntax: use define <name> ITACH_RELAY <ip-address> <port-nr>" if(int(@a) != 4);
  108. $hash->{Host} = $host;
  109. $hash->{Host_Port} = $host_port;
  110. return undef;
  111. }
  112. 1;
  113. =pod
  114. =begin html
  115. <a name="Itach_Relay"></a>
  116. <h3>ITACH_RELAY</h3>
  117. <ul>
  118. Note: this module needs the Net::Telnet module.
  119. <br><br>
  120. <a name="ITACH_RELAYdefine"></a>
  121. <b>Define</b>
  122. <ul>
  123. <code>define &lt;name&gt; ITACH_RELAY &lt;ip-address&gt; &lt;port&gt;</code>
  124. <br><br>
  125. Defines an Global Cache iTach Relay device (Box with 3 relays) via its ip address. <br><br>
  126. Examples:
  127. <ul>
  128. <code>define motor1 ITACH_RELAY 192.168.8.200 1</code><br>
  129. </ul>
  130. </ul>
  131. <br>
  132. <a name="ITACH_RELAYset"></a>
  133. <b>Set </b>
  134. <ul>
  135. <code>set &lt;name&gt; &lt;value&gt;</code>
  136. <br><br>
  137. where <code>value</code> is one of:<br>
  138. <pre>
  139. off
  140. on
  141. toggle
  142. </pre>
  143. Examples:
  144. <ul>
  145. <code>set motor1 on</code><br>
  146. </ul>
  147. <br>
  148. Notes:
  149. <ul>
  150. <li>Toggle is special implemented. List name returns "on" or "off" even after a toggle command</li>
  151. </ul>
  152. </ul>
  153. </ul>
  154. =end html
  155. =cut