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

Graceful load JSON and Digest:CRC Modules #1066

Merged
merged 26 commits into from
Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
84307a2
00_SIGNALduino.pm
sidey79 Jan 18, 2022
55c6b6c
lib/SD_Protocols.pm
sidey79 Jan 18, 2022
8f30747
cpamnfile
sidey79 Jan 18, 2022
4feed62
Update Versiondate
actions-user Jan 18, 2022
f917e22
00_SIGNALduino.pm
sidey79 Jan 18, 2022
57f1e5d
SD_Protocols:
sidey79 Jan 18, 2022
b6bfc18
00_SIGNALduino.pm
sidey79 Jan 18, 2022
151a6e2
00_SIGNALduino_initialite.t
sidey79 Jan 18, 2022
ed4d5f3
Merge branch 'missing-modules' of https://github.com/RFD-FHEM/RFFHEM …
sidey79 Jan 18, 2022
da8bcb7
Update Versiondate
actions-user Jan 18, 2022
75f2dc3
completion load Digest::CRC
elektron-bbs Jan 19, 2022
ffc4b1e
Update Versiondate
actions-user Jan 19, 2022
6aabcae
Merge branch 'master' into missing-modules
sidey79 Jan 21, 2022
30163a2
Update Versiondate
actions-user Jan 21, 2022
114f93b
SD_Protocos.pm
sidey79 Jan 21, 2022
20df03e
Merge branch 'missing-modules' of https://github.com/RFD-FHEM/RFFHEM …
sidey79 Jan 21, 2022
2a36efc
Update Versiondate
actions-user Jan 21, 2022
8c0f679
00_SIGNALduino.pm
sidey79 Jan 21, 2022
21afe3f
Merge branch 'missing-modules' of https://github.com/RFD-FHEM/RFFHEM …
sidey79 Jan 21, 2022
4947f08
Update Versiondate
actions-user Jan 21, 2022
2c041a9
00_SIGNALduino.pm
sidey79 Jan 22, 2022
8a068c5
Merge branch 'missing-modules' of https://github.com/RFD-FHEM/RFFHEM …
sidey79 Jan 22, 2022
2a1b791
Update Versiondate
actions-user Jan 22, 2022
31885cc
01_SIGNALduino_Parse_MN.t
sidey79 Jan 22, 2022
2b03550
Merge branch 'missing-modules' of https://github.com/RFD-FHEM/RFFHEM …
sidey79 Jan 22, 2022
c9fa62f
Update Versiondate
actions-user Jan 22, 2022
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
22 changes: 13 additions & 9 deletions FHEM/00_SIGNALduino.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# $Id: 00_SIGNALduino.pm v3.5.3 2022-01-17 20:29:25Z sidey79 $
# $Id: 00_SIGNALduino.pm v3.5.3 2022-01-22 16:38:42Z sidey79 $
# v3.5.3 - https://github.com/RFD-FHEM/RFFHEM/tree/master
# The module is inspired by the FHEMduino project and modified in serval ways for processing the incoming messages
# see http://www.fhemwiki.de/wiki/SIGNALDuino
Expand All @@ -16,16 +16,16 @@ use strict;
use warnings;
#use version 0.77; our $VERSION = version->declare('v3.5.3+20220116');

my $missingModulSIGNALduino = '';
my $missingModulSIGNALduino = ' ';

use DevIo;
require "99_Utils.pm" if (!defined $modules{"Utils"} || !exists $modules{"Utils"}{"LOADED"} ); ## no critic
use Carp;
no warnings 'portable';

eval {use Data::Dumper qw(Dumper);1};
eval {use Digest::CRC;1 or $missingModulSIGNALduino .= 'Digest::CRC '};
eval {use JSON;1 or $missingModulSIGNALduino .= 'JSON '};

use constant HAS_JSON => defined eval { require JSON; JSON->import; };

eval {use Scalar::Util qw(looks_like_number);1};
eval {use Time::HiRes qw(gettimeofday);1} ;
Expand All @@ -38,7 +38,7 @@ use List::Util qw(first);


use constant {
SDUINO_VERSION => '3.5.2+20220117', # Datum wird automatisch bei jedem pull request aktualisiert
SDUINO_VERSION => '3.5.2+20220122', # Datum wird automatisch bei jedem pull request aktualisiert
SDUINO_INIT_WAIT_XQ => 1.5, # wait disable device
SDUINO_INIT_WAIT => 2,
SDUINO_INIT_MAXRETRY => 3,
Expand Down Expand Up @@ -295,15 +295,17 @@ sub SIGNALduino_Initialize {
Log3 'SIGNALduino', 4, qq[SIGNALduino_Initialize: rfmode list: @rfmode];
}

$hash->{DefFn} = \&SIGNALduino_Define;
$hash->{UndefFn} = \&SIGNALduino_Undef;


# Provider
$hash->{ReadFn} = \&SIGNALduino_Read;
$hash->{WriteFn} = \&SIGNALduino_Write;
$hash->{ReadyFn} = \&SIGNALduino_Ready;

# Normal devices
$hash->{DefFn} = \&SIGNALduino_Define;
$hash->{FingerprintFn} = \&SIGNALduino_FingerprintFn;
$hash->{UndefFn} = \&SIGNALduino_Undef;
$hash->{GetFn} = \&SIGNALduino_Get;
$hash->{SetFn} = \&SIGNALduino_Set;
$hash->{AttrFn} = \&SIGNALduino_Attr;
Expand Down Expand Up @@ -1065,7 +1067,7 @@ sub SIGNALduino_Get_FhemWebList {
sub SIGNALduino_Get_availableFirmware {
my ($hash, @a) = @_;

if ($missingModulSIGNALduino =~ m/JSON/ )
if ( !HAS_JSON )
{
$hash->{logMethod}->($hash->{NAME}, 1, "$hash->{NAME}: get $a[0] failed. Please install Perl module JSON. Example: sudo apt-get install libjson-perl");
return "$a[0]: \n\nFetching from github is not possible. Please install JSON. Example:<br><code>sudo apt-get install libjson-perl</code>";
Expand Down Expand Up @@ -2940,7 +2942,8 @@ sub SIGNALduino_Parse_MN {
my $method = $hash->{protocolObject}->getProperty($id,'method',undef);
my @methodReturn = defined $method ? $method->($hash->{protocolObject},$rawData) : ($rawData);
if ($#methodReturn != 0) {
$hash->{logMethod}->($name, 4, qq{$name: Parse_MN, Error! method $methodReturn[1]});
my $vl = $methodReturn[1] =~ /missing\smodule/xms ? 1 : 4;
$hash->{logMethod}->($name, $vl, qq{$name: Parse_MN, Error! method $methodReturn[1]});
next mnIDLoop;
}
$dmsg = sprintf('%s%s',$hash->{protocolObject}->checkProperty($id,'preamble',''),$methodReturn[0]);
Expand Down Expand Up @@ -3958,6 +3961,7 @@ sub SIGNALduino_githubParseHttpResponse {
}
elsif($data ne '' && defined($hardware)) # wenn die Abfrage erfolgreich war ($data enthaelt die Ergebnisdaten des HTTP Aufrufes)
{

my $json_array = decode_json($data);
#print Dumper($json_array);
if ($param->{command} eq 'queryReleases') {
Expand Down
26 changes: 22 additions & 4 deletions FHEM/lib/SD_Protocols.pm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
################################################################################
# $Id: SD_ProtocolData.pm 3.5.x 2022-01-16 12:25:06Z elektron-bbs $
# $Id: SD_ProtocolData.pm 3.5.x 2022-01-22 16:38:42Z sidey79 $
#
# The file is part of the SIGNALduino project
# v3.5.x - https://github.com/RFD-FHEM/RFFHEM
Expand All @@ -13,7 +13,9 @@ package lib::SD_Protocols;
use strict;
use warnings;
use Carp qw(croak carp);
use Digest::CRC;
use constant HAS_DigestCRC => defined eval { require Digest::CRC; };
use constant HAS_JSON => defined eval { require JSON; };

our $VERSION = '2.05';
use Storable qw(dclone);
use Scalar::Util qw(blessed);
Expand Down Expand Up @@ -142,7 +144,11 @@ sub LoadHashFromJson {
my $json_text = do { local $/ = undef; <$json_fh> };
close $json_fh or croak "Can't close '$filename' after reading";

use JSON;
if (!HAS_JSON)
{
croak("Perl Module JSON not availble. Needs to be installed.");
}

my $json = JSON->new;
$json = $json->relaxed(1);
my $ver = $json->incr_parse($json_text);
Expand Down Expand Up @@ -1819,7 +1825,13 @@ sub ConvBresser_6in1 {
my $hexLength = length ($hexData);

return ( 1, 'ConvBresser_6in1, hexData is to short' ) if ( $hexLength < 36 ); # check double, in def length_min set



return ( 1,'ConvBresser_6in1, missing module , please install modul Digest::CRC' )
if (!HAS_DigestCRC);



my $crc = substr( $hexData, 0, 4 );
my $ctx = Digest::CRC->new(width => 16, poly => 0x1021);
my $calcCrc = sprintf( "%04X", $ctx->add( pack 'H*', substr( $hexData, 4, 30 ) )->digest );
Expand Down Expand Up @@ -1858,6 +1870,9 @@ sub ConvPCA301 {
'ConvPCA301, Usage: Input #1, $hexData needs to be at least 24 chars long'
) if ( length($hexData) < 24 ); # check double, in def length_min set

return ( 1,'ConvPCA301, missing module , please install modul Digest::CRC' )
if (!HAS_DigestCRC);

my $checksum = substr( $hexData, 20, 4 );
my $ctx = Digest::CRC->new(
width => 16,
Expand Down Expand Up @@ -1980,6 +1995,9 @@ sub ConvLaCrosse {
return ( 1,'ConvLaCrosse, Usage: Input #1, $hexData needs to be at least 8 chars long' )
if ( length($hexData) < 8 ) ; # check number of length for this sub to not throw an error

return ( 1,'ConvLaCrosse, missing module , please install modul Digest::CRC' )
if (!HAS_DigestCRC);

my $ctx = Digest::CRC->new( width => 8, poly => 0x31 );
my $calcCrc = $ctx->add( pack 'H*', substr( $hexData, 0, 8 ) )->digest;
my $checksum = sprintf( "%d", hex( substr( $hexData, 8, 2 ) ) );
Expand Down
4 changes: 2 additions & 2 deletions controls_signalduino.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
UPD 2022-01-17_21:39:32 234219 FHEM/00_SIGNALduino.pm
UPD 2022-01-22_00:28:34 234213 FHEM/00_SIGNALduino.pm
UPD 2021-11-21_21:18:05 17872 FHEM/10_FS10.pm
UPD 2020-05-26_11:51:12 20465 FHEM/10_SD_GT.pm
UPD 2021-11-28_11:34:02 23375 FHEM/10_SD_Rojaflex.pm
Expand All @@ -15,4 +15,4 @@ UPD 2020-04-13_23:15:56 14325 FHEM/14_SD_WS_Maverick.pm
UPD 2021-08-11_21:38:30 37904 FHEM/41_OREGON.pm
UPD 2020-12-17_23:16:30 15582 FHEM/90_SIGNALduino_un.pm
UPD 2022-01-16_13:13:44 225365 FHEM/lib/SD_ProtocolData.pm
UPD 2022-01-17_21:27:24 73246 FHEM/lib/SD_Protocols.pm
UPD 2022-01-22_00:28:34 73768 FHEM/lib/SD_Protocols.pm
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ requires 'Net::SSLeay';
requires 'Digest::CRC';
requires 'Math::Trig';
requires 'Storable';
requires 'Test::Without::Module';
23 changes: 20 additions & 3 deletions t/FHEM/00_SIGNALduino/00_SIGNALduino_initialize.t
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env perl
use strict;
use warnings;

use Test2::V0;
use Test2::Tools::Compare qw{ is };

Expand All @@ -10,7 +9,6 @@ our %defs;
InternalTimer(time(), sub {
my $target = shift;
my $targetHash = $defs{$target};
plan(3);
my %validationHash;
SIGNALduino_Initialize(\%validationHash);

Expand All @@ -33,9 +31,28 @@ InternalTimer(time(), sub {
item 'PCA301';
item 'Rojaflex';
item 'SlowRF';
etc();
}
,q[Test rfmodes and order]);


FhemTestUtils_resetLogs();
},'dummyDuino');


InternalTimer(time()+0.11, sub {
my $target = shift;
my $targetHash = $defs{$target};
my %validationHash;
no warnings qw(redefine);
CommandReload(undef,"00_SIGNALduino.pm");
use warnings qw(redefine);
SIGNALduino_Initialize(\%validationHash);

is(FhemTestUtils_gotLog("Error"), 0, q[No JSON errors in logfile]);
done_testing();

eval q[ no Test::Without::Module qw( JSON ) ];

exit(0);
},'dummyDuino');

Expand Down
20 changes: 15 additions & 5 deletions t/FHEM/00_SIGNALduino/01_SIGNALduino_Parse_MN.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use Test2::Tools::Compare qw{is item U D match hash array bag};
use Test2::Todo;



our %defs;
our %modules;

my @mockData = (
{
Expand Down Expand Up @@ -114,14 +116,20 @@ my @mockData = (
rValue => 0,
rfmode => 'Lacrosse_mode2'
},

{
deviceName => q[dummyDuino],
plan => 2,
testname => q[message ok],
input => q[MN;D=3BF120B00C1618FF77FF0458152293FFF06B0000;R=242;],
rValue => 1,
rfmode => 'Bresser_6in1'
},
);
plan (scalar @mockData );

BEGIN {
};

InternalTimer(time()+1, sub() {
plan (scalar @mockData);

InternalTimer(time()+0.9, sub() {
while (@mockData)
{
my $element = pop(@mockData);
Expand Down Expand Up @@ -158,6 +166,8 @@ InternalTimer(time()+1, sub() {
done_testing();
exit(0);


}, 0);


1;