RFXCOM/Decoder/RFXSensor.pm

filter: Statementstmt 100% 83 / 83 Branchbran 100% 28 / 28 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 100% 133 / 133 SCAR 28.3
CC
17
Cov
100%
decode
15.0
decode_init
2.0
decode_sen
2.0
CRAP 17.0
« RFXCOM/TX.pm ↑ Summary RFXCOM/Base.pm »
1 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Device::RFXCOM::Decoder::RFXSensor;
4 4 $Device::RFXCOM::Decoder::RFXSensor::VERSION = '1.142010';
5 # ABSTRACT: Device::RFXCOM::Decoder::RFXSensor decode RFXSensor RF messages
6  
7  
8 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use constant DEBUG => $ENV{DEVICE_RFXCOM_DECODER_RFXSENSOR_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Device::RFXCOM::Decoder qw/nibble_sum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 our @ISA = qw(Device::RFXCOM::Decoder);
13  
14 4 my %info =
15 (
16 0x01 => "sensor addresses incremented",
17 0x02 => "battery low detected",
18 0x03 => "conversion not ready",
19 );
20  
21 4 my %error =
22 (
23 0x81 => "no 1-wire device connected",
24 0x82 => "1-wire ROM CRC error",
25 0x83 => "1-wire device connected is not a DS18B20 or DS2438",
26 0x84 => "no end of read signal received from 1-wire device",
27 0x85 => "1-wire scratchpad CRC error",
28 0x86 => "temperature conversion not ready in time",
29 0x87 => "A/D conversion not ready in time",
30 );
31  
32 4 my %types = (
33 'RFX' => { fun => \&decode_init, len => 32 },
34 'RF2' => { fun => \&decode_init, len => 32 },
35 'RF3' => { fun => \&decode_init, len => 32 },
36 'SEN' => { fun => \&decode_sen, len => 40 },
37 );
38  
39  
40 sub new {
41 8 ▶ my $pkg = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
42 8 $pkg->SUPER::new(rfxsensor_cache => {}, @_);
43 }
44  
45  
46 sub decode {
47 55 ▶ my ($self, $parent, $message, $bytes, $bits, $result) = @_;
Subroutine decode: called CC=15 SCAR=27.1
Pod documented
48 55 my $str = substr $message, 0, 3;
49 55 ▶ if (exists $types{$str} && $bits == $types{$str}->{len}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $types{$str} and $bits == $types{$str}{"len"}) 4 51 55
Condition100%3/3
exists $types{$str} and $bits == $types{$str}{"len"}
!ll&&!rl&&r
5014
MC/DC100%2/2
exists $types{$str} and $bits == $types{$str}{"len"}
exists $types{$str} $bits == $types{$str}{"len"}
Truth table100%3/3
A and B
A= exists $types{$str}B= $bits == $types{$str}{"len"}
ABresult
0X0
100
111
50 4 return $types{$str}->{fun}->($self, $parent,
51 $message, $bytes, $bits, $result, $str);
52 }
53 51 ▶ $bits == 32 or return;
Branch: 1 branch
BranchTrueFalseTotal
$bits == 32 or return 30 21 51
54 21 ▶ (($bytes->[0]^0xf0) == $bytes->[1]) or return;
Branch: 1 branch
BranchTrueFalseTotal
($bytes->[0] ^ 240) == $bytes->[1] or return 10 11 21
55 11 my @nib = map { hex $_ } split //, unpack "H*", $message;
56 11 ▶ ((nibble_sum(7, \@nib)&0xf)^0xf) == $nib[7] or return;
Branch: 1 branch
BranchTrueFalseTotal
(nibble_sum(7, \@nib) & 15 ^ 15) == $nib[7] or return 1 10 11
57 10 my $device = sprintf("rfxsensor%02x%02x", $bytes->[0], $bytes->[1]);
58 10 my $base = sprintf("%02x%02x", $bytes->[0]&0xfc, $bytes->[1]&0xfc);
59 10 my $cache = $self->{rfxsensor_cache};
60 10 my $supply_voltage = $cache->{$base}->{supply};
61 10 my $last_temp = $cache->{$base}->{temp};
62 10 my $flag = $bytes->[3]&0x10;
63 10 ▶ if ($flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag) 3 7 10
64 3 ▶ if (exists $info{$bytes->[2]}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $info{$bytes->[2]}) { } 1 2 3
elsif (exists $error{$bytes->[2]}) { } 1 1 2
65 1 warn "RFXSensor info $device: ".$info{$bytes->[2]}."\n";
66 } elsif (exists $error{$bytes->[2]}) {
67 1 warn "RFXSensor error $device: ".$error{$bytes->[2]}."\n";
68 } else {
69 1 warn sprintf "RFXSensor unknown status messages: %02x\n", $bytes->[2];
70 }
71 3 return;
72 }
73  
74 7 my $type = ($bytes->[0]&0x3);
75 7 ▶ if ($type == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type == 0) { } 2 5 7
elsif ($type == 1) { } 3 2 5
elsif ($type == 2) { } 1 1 2
76 # temp
77 2 my $temp = $bytes->[2] + (($bytes->[3]&0xe0)/0x100);
78 2 ▶ if ($temp > 150) {
Branch: 1 branch
BranchTrueFalseTotal
if ($temp > 150) 1 1 2
79 1 $temp = -1*(256-$temp);
80 }
81 2 $cache->{$base}->{temp} = $temp;
82 2 push @{$result->{messages}},
83 Device::RFXCOM::Response::Sensor->new(device => $device,
84 measurement => 'temp',
85 value => $temp,
86 base_device => $base);
87 2 return 1;
88 } elsif ($type == 1) {
89 3 my $v = ( ($bytes->[2]<<3) + ($bytes->[3]>>5) ) / 100;
90 3 push @{$result->{messages}},
91 Device::RFXCOM::Response::Sensor->new(device => $device,
92 measurement => 'voltage',
93 value => $v,
94 base_device => $base);
95 3 ▶ unless (defined $supply_voltage) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $supply_voltage) 1 2 3
96 1 warn "Don't have supply voltage for $device/$base yet\n";
97 1 $result->{dont_cache} = 1;
98 1 return 1;
99 }
100 # See http://archives.sensorsmag.com/articles/0800/62/main.shtml
101 2 my $hum = sprintf "%.2f", (($v/$supply_voltage) - 0.16)/0.0062;
102 #print STDERR "Sensor Hum: $hum\n";
103 2 ▶ if (defined $last_temp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last_temp) { } 1 1 2
104 #print STDERR "Last temp: $last_temp\n";
105 1 $hum = sprintf "%.2f", $hum / (1.0546 - 0.00216*$last_temp);
106 #print STDERR "True Hum: $hum\n";
107 } else {
108 1 $result->{dont_cache} = 1;
109 1 warn "Don't have temperature for $device/$base yet - assuming 25'C\n";
110 }
111 2 push @{$result->{messages}},
112 Device::RFXCOM::Response::Sensor->new(device => $device,
113 measurement => 'humidity',
114 value => $hum,
115 base_device => $base);
116 2 return 1;
117 } elsif ($type == 2) {
118 1 my $v = ( ($bytes->[2]<<3) + ($bytes->[3]>>5) ) / 100;
119 1 $cache->{$base}->{supply} = $v;
120 1 push @{$result->{messages}},
121 Device::RFXCOM::Response::Sensor->new(device => $device,
122 measurement => 'voltage',
123 value => $v,
124 base_device => $base);
125 1 return 1;
126 }
127  
128 1 warn "Unsupported RFXSensor: type=$type\n";
129 1 return;
130 }
131  
132  
133 sub decode_init {
134 2 ▶ my ($self, $parent, $message, $bytes, $bits, $result, $type) = @_;
Subroutine decode_init: called CC=2 SCAR=6.9
Pod documented
135  
136 warn sprintf "RFXSensor %s, version %02x, transmit mode %s, initialized\n",
137 2 ▶ { 0x58 => 'Type-1', 0x32 => 'Type-2', 0x33 => 'Type-3' }->{$bytes->[2]},
Branch: 1 branch
BranchTrueFalseTotal
$bytes->[3] & 128 ? : 1 1 2
138 $bytes->[3]&0x7f, $bytes->[3]&0x80 ? 'slow' : 'fast';
139 2 return 1;
140 }
141  
142  
143 sub decode_sen {
144 2 ▶ my ($self, $parent, $message, $bytes, $bits, $result, $str) = @_;
Subroutine decode_sen: called CC=2 SCAR=6.9
Pod documented
145  
146 warn sprintf "RFXSensor SEN%d, type %02x (%s)\n", $bytes->[3], $bytes->[4],
147 2 ▶ { 0x26 => 'DS2438', 0x28 => 'DS18B20' }->{$bytes->[4]} || 'unknown';
Condition100%2/2
{38, 'DS2438', 40, 'DS18B20'}->{$bytes->[4]} || 'unknown'
l!l
11
MC/DC100%1/1
{38, 'DS2438', 40, 'DS18B20'}->{$bytes->[4]} || 'unknown'
{38, 'DS2438', 40, 'DS18B20'}->{$bytes->[4]}
Truth table100%2/2
A
A= {38, 'DS2438', 40, 'DS18B20'}->{$bytes->[4]}
Aresult
11
00
148 2 return 1;
149 }
150  
151 4 1;
152  
153 __END__
« RFXCOM/TX.pm ↑ Summary RFXCOM/Base.pm »