diff --git a/FHEM/lib/SD_Protocols.pm b/FHEM/lib/SD_Protocols.pm index 4fff937d2..548941ad2 100644 --- a/FHEM/lib/SD_Protocols.pm +++ b/FHEM/lib/SD_Protocols.pm @@ -6,8 +6,14 @@ # # 2016-2019 S.Butzek, Ralf9 # 2019-2021 S.Butzek, HomeAutoUser, elektron-bbs +# 2022 S.Butzek, HomeAutoUser, elektron-bbs, uwekaditz # ################################################################################ +# 2.07 2022-08-14 uwekaditz +# - CHG: mcBit2SomfyRTS() remove leading '0' in Somfy telegram that should start with '8' or 'F' if it is not expected +# 2.07 2022-08-07 uwekaditz +# - CHG: mcBit2SomfyRTS() remove leading '0' in Somfy telegram that should start with 'A' if it is not expected +################################################################################ package lib::SD_Protocols; use strict; @@ -15,8 +21,9 @@ use warnings; use Carp qw(croak carp); use constant HAS_DigestCRC => defined eval { require Digest::CRC; }; use constant HAS_JSON => defined eval { require JSON; }; +use List::Util qw/any/; -our $VERSION = '2.06'; +our $VERSION = '2.07'; use Storable qw(dclone); use Scalar::Util qw(blessed); @@ -1084,10 +1091,18 @@ sub mcBit2SomfyRTS { my $mcbitnum = shift // length $bitData; $self->_logging( qq[lib/mcBit2SomfyRTS, bitdata: $bitData ($mcbitnum)], 4 ); - - if ($mcbitnum == 57) { - $bitData = substr($bitData, 1, 56); + + #$self->_logging( qq[lib/mcBit2SomfyRTS, first bit: ]. substr($bitData,0,1), 4 ); + + # remove leading '0' in any Somfy telegram if it is not expected + if ($mcbitnum == 57 || ($mcbitnum == 81 && substr($bitData,0,1) eq '0')) { + # length not correct, byt leading '0' -> remove leading '0' + $bitData = substr($bitData, 1, $mcbitnum - 1); $self->_logging( qq[lib/mcBit2SomfyRTS, bitdata: $bitData, truncated to length: ]. length($bitData), 4 ); + } elsif ($mcbitnum == 80 && any { substr($bitData, 0, 5) eq $_ } qw(01010 01000 01111) ) { + # length correct but telegram does not start with character 'A', '8' or 'F' , remove leading '0' and add a '0' at the end, see https://forum.fhem.de/index.php/topic,72173.msg1075881.html#msg1075881 + $bitData = substr($bitData, 1, $mcbitnum - 1) . q[0]; + $self->_logging( qq[lib/mcBit2SomfyRTS, bitdata: $bitData, remove leading nibble, added last nibble - length: ]. length($bitData), 4 ); } my $encData = lib::SD_Protocols::binStr2hexStr($bitData); diff --git a/t/SD_Protocols/02_mcBit2SomfyRTS.t b/t/SD_Protocols/02_mcBit2SomfyRTS.t index 4a6c4b818..210734fd1 100644 --- a/t/SD_Protocols/02_mcBit2SomfyRTS.t +++ b/t/SD_Protocols/02_mcBit2SomfyRTS.t @@ -6,8 +6,9 @@ use Test2::V0; use lib::SD_Protocols; use Test2::Tools::Compare qw{is}; use Test2::Todo; +use Carp qw(carp); -plan(4); +plan(6); my $id=5043; my ($rcode,$hexresult); @@ -19,6 +20,20 @@ $Protocols->{_protocols}->{5043}{length_min} = 56; $Protocols->{_protocols}->{5043}{length_max} = 57; +sub createCallback { + + return sub { + my $message = shift // carp "message must be provided"; + my $level = shift // 0; + + print qq[ $level (/02_mcBit2SomfyRTS.t/ $message \n]; + }; +}; + + +my $myLocalLogCallback =createCallback(); +$Protocols->registerLogCallback($myLocalLogCallback); + my $target = undef; subtest 'mcbitnum != 57' => sub { @@ -62,3 +77,21 @@ subtest 'message to long' => sub { }; $todo->end; + +subtest 'message Button RC1 up truncate 81 to 80' => sub { + plan(2); + my $bitdata='010100000100000001000001100110100110010001100000101010101100010000000000000100010000'; + + ($rcode,$hexresult)=$Protocols->mcBit2SomfyRTS($target,$bitdata,$id,81); + is($rcode,1,"check returncode for SomfyRTS transmission"); + is($hexresult,'A0808334C8C155880022','check result SomfyRTS'); +}; + +subtest 'message Button RC2 up - length 80' => sub { + plan(2); + my $bitdata='01010101010001001100010110000000000100101001011111011101110001000000000000010001'; + + ($rcode,$hexresult)=$Protocols->mcBit2SomfyRTS($target,$bitdata,$id,80); + is($rcode,1,"check returncode for SomfyRTS transmission"); + is($hexresult,'AA898B00252FBB880022','check result SomfyRTS'); +};