RFXCOM/Encoder/X10.pm

filter: Statementstmt 100% 55 / 55 Branchbran 100% 10 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 100% 75 / 75 SCAR 20.8
CC
8
Cov
100%
encode
6.0
_encode_x10
3.0
BEGIN
1.0
CRAP 8.0
« RFXCOM/Decoder/Electrisave.pm ↑ Summary RFXCOM/Decoder/CM119.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Device::RFXCOM::Encoder::X10;
4 1 $Device::RFXCOM::Encoder::X10::VERSION = '1.142010';
5 # ABSTRACT: Device::RFXCOM::Encoder::X10 encode X10 RF messages
6  
7  
8 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use constant DEBUG => $ENV{DEVICE_RFXCOM_ENCODER_X10_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp qw/croak carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use base 'Device::RFXCOM::Encoder';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Device::RFXCOM::Response::X10;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 my %command_to_byte =
15 (
16 'dim' => 0x98,
17 'bright' => 0x88,
18 'all_lights_on' => 0x90,
19 'all_lights_off' => 0x80,
20 'on' => 0x0,
21 'off' => 0x20,
22 );
23 1 my $i = 0;
24 my %house_to_byte =
25 1 map { $_ => $i++ } ('m', 'n', 'o', 'p', 'c', 'd', 'a', 'b',
26 'e', 'f', 'g', 'h', 'k', 'l', 'i', 'j');
27  
28 1 $i = 1;
29 my %bytes_to_unit =
30 1 map { $_ => $i++ } ( 0x00, 0x10, 0x08, 0x18, 0x40, 0x50, 0x48, 0x58 );
31 1 my %unit_to_bytes = reverse %bytes_to_unit;
32  
33  
34 sub encode {
35 4 ▶ my ($self, $parent, $p) = @_;
Subroutine encode: called CC=6 SCAR=17.9
Pod documented
36 4 my @res = ();
37 4 ▶ if ($p->{house}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p->{'house'}) { } 1 3 4
elsif ($p->{'device'}) { } 2 1 3
38 1 foreach (split //, $p->{house}) {
39 push @res, $self->_encode_x10({
40 command => $p->{command},
41 house => $p->{house},
42 1 });
43 }
44 } elsif ($p->{device}) {
45 2 foreach (split /,/, $p->{device}) {
46 4 ▶ my ($house, $unit) = /^([a-p])(\d+)$/i or next;
Branch: 1 branch
BranchTrueFalseTotal
() = /^([a-p])(\d+)$/i or next 1 3 4
47 push @res, $self->_encode_x10({
48 command => $p->{command},
49 3 house => $house,
50 unit => $unit,
51 });
52 }
53 } else {
54 1 carp $self.'->encode: Invalid x10 message';
55 }
56 4 return \@res;
57 }
58  
59 sub _encode_x10 {
60 4 ▶ my ($self, $p) = @_;
Subroutine _encode_x10: called CC=3 SCAR=11.0
61 4 my @bytes = ( 0, 0, 0, 0 );
62 4 $bytes[2] |= $command_to_byte{lc $p->{command}};
63 4 $bytes[0] |= ($house_to_byte{lc $p->{house}})<<4;
64 4 my $unit = $p->{unit};
65 4 ▶ unless ($bytes[2]&0x80) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bytes[2] & 128) 3 1 4
66 3 ▶ if ($unit > 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit > 8) 1 2 3
67 1 $unit -= 8;
68 1 $bytes[0] |= 0x4;
69 }
70 3 $bytes[2] |= $unit_to_bytes{$unit};
71 }
72 4 $bytes[1] = $bytes[0]^0xff;
73 4 $bytes[3] = $bytes[2]^0xff;
74 return { raw => (pack 'C5', 32, @bytes),
75 desc =>
76 'x10: '.(join '/',
77 12 grep { defined $_
78 4 } @{$p}{qw/command house unit/})
79 };
80 }
81  
82 1 1;
83  
84 __END__
« RFXCOM/Decoder/Electrisave.pm ↑ Summary RFXCOM/Decoder/CM119.pm »