00_HXB.pm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # $Id: 00_HXB.pm 16375 2018-03-10 15:40:02Z neubert $
  2. ################################################################
  3. #
  4. # Copyright notice
  5. #
  6. # (c) 2014 Copyright: Dr. Boris Neubert
  7. # e-mail: omega at online dot de
  8. #
  9. # This file is part of fhem.
  10. #
  11. # Fhem is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation, either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # Fhem is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License
  22. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  23. #
  24. ################################################################################
  25. package main;
  26. use strict;
  27. use warnings;
  28. use IO::Socket::Multicast6;
  29. #####################################
  30. sub
  31. HXB_Initialize($)
  32. {
  33. my ($hash) = @_;
  34. my %matchlist= (
  35. "1:HXBDevice" => "^HX0C.+",
  36. );
  37. # Provider
  38. #$hash->{WriteFn} = "HXB_Write";
  39. $hash->{ReadFn} = "HXB_Read";
  40. $hash->{Clients} = ":HXBDevice:";
  41. $hash->{MatchList} = \%matchlist;
  42. #$hash->{ReadyFn} = "HXB_Ready";
  43. # Consumer
  44. $hash->{DefFn} = "HXB_Define";
  45. $hash->{UndefFn} = "HXB_Undef";
  46. #$hash->{ReadyFn} = "HXB_Ready";
  47. #$hash->{GetFn} = "HXB_Get";
  48. #$hash->{SetFn} = "HXB_Set";
  49. #$hash->{AttrFn} = "HXB_Attr";
  50. #$hash->{AttrList}= "";
  51. }
  52. #####################################
  53. sub
  54. HXB_Define($$)
  55. {
  56. my ($hash, $def) = @_;
  57. my $name= $hash->{NAME};
  58. Log3 $hash, 3, "$name: Opening multicast socket...";
  59. my $socket = IO::Socket::Multicast6->new(
  60. Domain => AF_INET6,
  61. Proto => 'udp',
  62. LocalPort => '61616',
  63. );
  64. $socket->mcast_add('FF05::205');
  65. $hash->{TCPDev}= $socket;
  66. $hash->{FD} = $socket->fileno();
  67. delete($readyfnlist{"$name"});
  68. $selectlist{"$name"} = $hash;
  69. return undef;
  70. }
  71. #####################################
  72. sub
  73. HXB_Undef($$)
  74. {
  75. my ($hash, $arg) = @_;
  76. my $socket= $hash->{TCPDev};
  77. $socket->mcast_drop('FF05::205');
  78. $socket->close;
  79. return undef;
  80. }
  81. #####################################
  82. sub
  83. HXB_DoInit($)
  84. {
  85. my $hash = shift;
  86. $hash->{STATE} = "Initialized" if(!$hash->{STATE});
  87. return undef;
  88. }
  89. #####################################
  90. # called from the global loop, when the select for hash->{FD} reports data
  91. sub HXB_Read($)
  92. {
  93. my ($hash) = @_;
  94. my $name= $hash->{NAME};
  95. my $socket= $hash->{TCPDev};
  96. my $data;
  97. return unless $socket->recv($data, 128);
  98. Log3 $hash, 5, "$name: Received " . length($data) . " bytes.";
  99. Dispatch($hash, $data, undef); # dispatch result to HXBDevices
  100. }
  101. #############################
  102. 1;
  103. #############################
  104. =pod
  105. =item summary receive multicast messages from Hexabus devices
  106. =item summary_DE empfange Multicast-Nachrichten von Hexabus-Ger&auml;ten
  107. =begin html
  108. <a name="HXB"></a>
  109. <h3>HXB</h3>
  110. <ul>
  111. <br>
  112. <a name="HXB"></a>
  113. <b>Define</b>
  114. <ul>
  115. <code>define &lt;name&gt; HXB</code><br>
  116. <br>
  117. Defines a Hexabus. You need one Hexabus to receive multicast messages from <a href="#HXBDevice">Hexabus devices</a>.
  118. Have a look at the <a href="https://github.com/mysmartgrid/hexabus/wiki">Hexabus wiki</a> for more information on Hexabus.
  119. <br><br>
  120. You need the perl modules IO::Socket::Multicast6 and Digest::CRC. Under Debian and its derivatives they are installed with <code>apt-get install libio-socket-multicast6-perl libdigest-crc-perl</code>.
  121. </ul>
  122. </ul>
  123. =end html