10_Itach_IR.pm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. ################################################################################
  2. # $Id: 10_Itach_IR.pm 10723 2016-02-04 18:13:54Z ulimaass $
  3. # 10_Itach_IR
  4. #
  5. ################################################################################
  6. #
  7. # Copyright notice
  8. #
  9. # (c) 2014 Copyright: Ulrich Maass
  10. #
  11. # This file is part of fhem.
  12. #
  13. # Fhem is free software: you can redistribute it and/or modify
  14. # it under the terms of the GNU General Public License as published by
  15. # the Free Software Foundation, either version 2 of the License, or
  16. # (at your option) any later version.
  17. #
  18. # Fhem is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. # GNU General Public License for more details.
  22. #
  23. # You should have received a copy of the GNU General Public License
  24. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  25. #
  26. # Disclaimer: The Author takes no responsibility whatsoever
  27. # for damages potentially done by this program.
  28. #
  29. ################################################################################
  30. #
  31. # This module serves as communication layer for 88_Itach_IRDevice
  32. #
  33. ################################################################################
  34. package main;
  35. use strict;
  36. use warnings;
  37. use IO::Socket::INET;
  38. #########################
  39. # Forward declaration
  40. sub IIR_Define();
  41. sub IIR_Send($$);
  42. sub IIR_Write($@);
  43. #####################################
  44. # Initialize module
  45. sub
  46. Itach_IR_Initialize($)
  47. {
  48. my ($hash) = @_;
  49. # provider
  50. # $hash->{ReadFn} = "IIR_Write";
  51. $hash->{WriteFn} = "IIR_Write";
  52. $hash->{Clients} = ":Itach_IRDevice:";
  53. #consumer
  54. $hash->{DefFn} = "IIR_Define";
  55. $hash->{AttrList} = "verbose:0,1,2,3,4,5,6 timeout";
  56. }
  57. #####################################
  58. # Initialize every new instance
  59. sub
  60. IIR_Define()
  61. {
  62. my ($hash, $def) = @_;
  63. my @args = split("[ \t]+", $def);
  64. return "Usage: define <name> Itach_IR <host>" if($#args != 2);
  65. my ($name, $type, $host) = @args;
  66. $hash->{STATE} = "Initialized";
  67. $hash->{IPADR} = $host if ($host); #format-check required
  68. my $cmdret= CommandAttr(undef,"$name room ItachIR") if (!AttrVal($name,'room',undef));
  69. return undef;
  70. }
  71. #####################################
  72. # Execute IOWrite-calls from clients
  73. sub IIR_Write($@) {
  74. my ($hash,$name,$cmd,$IRcode)= @_;
  75. Log3 $name, 5, "IIR_Write called with $name,$cmd";
  76. my $newstate = $name.':'.$cmd;
  77. readingsSingleUpdate($hash,"lastcommand",$newstate,0);
  78. my $ret=IIR_Send($hash,$IRcode);
  79. return undef;
  80. }
  81. #####################################
  82. # Send IR-code
  83. sub
  84. IIR_Send($$){
  85. my ($hash,$IR)=@_;
  86. if (!$IR) {
  87. Log3 $hash->{NAME}, 2, "Called without IR-code to send.";
  88. return;
  89. }
  90. my $socket = new IO::Socket::INET (
  91. PeerHost => $hash->{IPADR},
  92. PeerPort => '4998',
  93. Proto => 'tcp',
  94. Timeout => AttrVal($hash->{NAME},'timeout','2.0'),
  95. );
  96. # send Itach command
  97. if ($socket) {
  98. my @codes = split(';',$IR);
  99. foreach my $IRcode (@codes) {
  100. my $data = $IRcode."\r\n";
  101. $socket->send($data);
  102. select(undef, undef, undef, 0.3); #pause 0.3 seconds
  103. Log3 $hash->{NAME}, 5, 'Sent to '.$hash->{IPADR}.' : '.$IRcode;
  104. }
  105. $socket->close();
  106. readingsSingleUpdate($hash,"state",'Initialized',0) if ($hash->{STATE} ne 'Initialized');
  107. } else {
  108. Log3 $hash->{NAME}, 1, 'Could not open socket with '.$hash->{IPADR}.' : '.$@;
  109. readingsSingleUpdate($hash,"state",$@,0);
  110. }
  111. return undef;
  112. }
  113. 1;
  114. =pod
  115. =begin html
  116. <a name="Itach_IR"></a>
  117. <h3>Itach_IR</h3>
  118. <ul>
  119. Defines a device representing a physical Itach IR. Serves as communication layer for <a href="#Itach_IRDevice">Itach_IRDevice</a>.<br>
  120. For more information, check the <a href="http://www.fhemwiki.de/wiki/ITach">Wiki page</a>.<br>
  121. <a name="Itach_IRdefine"></a><br>
  122. <b>Define</b>
  123. <ul>
  124. <code>define &lt;name&gt; Itach_IR &lt;IP-address&gt;</code><br>
  125. Example:<br>
  126. <code>define Itach Itach_IR 192.168.1.2</code>
  127. </ul>
  128. <a name="Itach_IRset"></a><br>
  129. <b>Set</b><br><ul>N/A</ul><br>
  130. <a name="Itach_IRDeviceget"></a><br>
  131. <b>Get</b><br><ul>N/A</ul><br>
  132. <a name="Itach_IRDeviceattr"></a><br>
  133. <b>Attributes</b>
  134. <ul>
  135. <li><a href="#verbose">verbose</a></li>
  136. <li>timeout<br>
  137. Can be used to change the timeout-value for tcp-communication. Default is 2.0.</li>
  138. </ul>
  139. </ul>
  140. =end html
  141. =cut