Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SD_Protocols for received Somfy telegrams (remove leading '0') #1112

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions FHEM/lib/SD_Protocols.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@
#
# 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;
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);

Expand Down Expand Up @@ -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);

Expand Down
35 changes: 34 additions & 1 deletion t/SD_Protocols/02_mcBit2SomfyRTS.t
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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');
};