46_TRX_ELSE.pm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #################################################################################
  2. # 46_TRX_ELSE.pm
  3. #
  4. # FHEM module unkown RFXtrx433 messages
  5. #
  6. # Copyright (C) 2012-2016 Willi Herzig
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. #
  22. # The GNU General Public License may also be found at http://www.gnu.org/licenses/gpl-2.0.html .
  23. ###################################
  24. #
  25. # values for "set global verbose"
  26. # 4: log unknown protocols
  27. # 5: log decoding hexlines for debugging
  28. #
  29. # $Id: 46_TRX_ELSE.pm 11451 2016-05-15 19:04:06Z wherzig $
  30. package main;
  31. use strict;
  32. use warnings;
  33. my $time_old = 0;
  34. my $DOT = q{_};
  35. sub
  36. TRX_ELSE_Initialize($)
  37. {
  38. my ($hash) = @_;
  39. $hash->{Match} = "^..(0[0-9a-f]|1[a-f]|2[1-9a-f]|3[0-9a-f]|4[1-9a-d]|4f|53|59|5e|5f|[6-9a-f][0-9a-f]).*";
  40. $hash->{DefFn} = "TRX_ELSE_Define";
  41. $hash->{UndefFn} = "TRX_ELSE_Undef";
  42. $hash->{ParseFn} = "TRX_ELSE_Parse";
  43. $hash->{AttrList} = "IODev ignore:1,0 do_not_notify:1,0 ".
  44. $readingFnAttributes;
  45. Log3 $hash, 5, "TRX_ELSE_Initialize() Initialize";
  46. }
  47. #####################################
  48. sub
  49. TRX_ELSE_Define($$)
  50. {
  51. my ($hash, $def) = @_;
  52. my @a = split("[ \t][ \t]*", $def);
  53. my $a = int(@a);
  54. return "wrong syntax: define <name> TRX_ELSE code" if(int(@a) != 3);
  55. my $name = $a[0];
  56. my $code = $a[2];
  57. my $device_name = "TRX_UNKNOWN".$DOT.$code;
  58. $hash->{CODE} = $code;
  59. $modules{TRX_ELSE}{defptr}{$device_name} = $hash;
  60. AssignIoPort($hash);
  61. return undef;
  62. }
  63. #####################################
  64. sub
  65. TRX_ELSE_Undef($$)
  66. {
  67. my ($hash, $name) = @_;
  68. delete($modules{TRX_ELSE}{defptr}{$name});
  69. return undef;
  70. }
  71. sub
  72. TRX_ELSE_Parse($$)
  73. {
  74. my ($hash, $msg) = @_;
  75. my $time = time();
  76. if ($time_old ==0) {
  77. Log3 $hash, 5, "TRX_ELSE_Parse() decoding delay=0 hex=$msg";
  78. } else {
  79. my $time_diff = $time - $time_old ;
  80. Log3 $hash, 5, "TRX_ELSE_Parse() decoding delay=$time_diff hex=$msg";
  81. }
  82. $time_old = $time;
  83. # convert to binary
  84. my $bin_msg = pack('H*', $msg);
  85. Log3 $hash, 5, "TRX_ELSE_Parse() 2 hex=$msg";
  86. # convert string to array of bytes. Skip length byte
  87. my @rfxcom_data_array = ();
  88. foreach (split(//, substr($bin_msg,1))) {
  89. push (@rfxcom_data_array, ord($_) );
  90. }
  91. my $num_bytes = ord(substr($bin_msg,0,1));
  92. if ($num_bytes < 4) {
  93. return;
  94. }
  95. my $type = $rfxcom_data_array[0];
  96. Log3 $hash, 5, "TRX_ELSE_Parse() num_bytes=$num_bytes hex=$msg type=$type";
  97. my $res = "";
  98. if ($type == 0x02) {
  99. my $subtype = $rfxcom_data_array[1];
  100. my $msg = $rfxcom_data_array[3];
  101. if (($msg != 0x00) && ($msg != 0x01)) {
  102. Log3 $hash, 1, "TRX_ELSE_Parse() error transmit NACK=".sprintf("%02x",$msg);
  103. }
  104. return "";
  105. }
  106. my $type_hex = sprintf("%02x", $type);
  107. my $device_name = "TRX".$DOT."UNKNOWN".$DOT.$type_hex;
  108. my $def = $modules{TRX_ELSE}{defptr}{$device_name};
  109. if (!$def) {
  110. Log3 $hash, 3, "TRX_ELSE: Unknown device $device_name, please define it";
  111. return "UNDEFINED $device_name TRX_ELSE $type_hex";
  112. }
  113. my $name = $def->{NAME};
  114. return "" if(IsIgnored($name));
  115. readingsBeginUpdate($def);
  116. my $current = $msg;
  117. #my $sensor = "hexline";
  118. #readingsBulkUpdate($def, $sensor, $current);
  119. readingsBulkUpdate($def, "state", $current);
  120. readingsEndUpdate($def, 1);
  121. return $name;
  122. }
  123. 1;
  124. =pod
  125. =begin html
  126. <a name="TRX_ELSE"></a>
  127. <h3>TRX_ELSE</h3>
  128. <ul>
  129. The TRX_ELSE module is invoked by TRX if a code is received by RFXCOM RFXtrx433 RF receiver that is currently not handled by a TRX_-Module. You need to define an RFXtrx433 receiver first.
  130. See <a href="#TRX">TRX</a>.
  131. <br>
  132. <a name="TRX_SECURITYdefine"></a>
  133. <br>
  134. <b>Define</b>
  135. <ul>
  136. <code>define &lt;name&gt; TRX_ELSE &lt;hextype&gt;</code> <br>
  137. <br>
  138. <code>&lt;hextype&gt;</code>
  139. <ul>
  140. specifies the hexvalue (00 - ff) of the type received by the RFXtrx433 transceiver. <br>
  141. </ul>
  142. <br>
  143. Example: <br>
  144. <code>define TRX_UNKNOWN_9A TRX_ELSE 9A</code>
  145. <br>
  146. </ul>
  147. <br>
  148. </ul>
  149. =end html
  150. =cut