File Coverage

blib/lib/Device/RFXCOM/Decoder/OregonScale.pm
Criterion Covered Total %
statement 38 38 100.0
branch 8 8 100.0
condition 6 6 100.0
subroutine 8 8 100.0
pod 2 2 100.0
total 62 62 100.0


line stmt bran cond sub pod time code
1 3     3   3191 use strict;
  3         5  
  3         110  
2 3     3   14 use warnings;
  3         7  
  3         140  
3             package Device::RFXCOM::Decoder::OregonScale;
4             $Device::RFXCOM::Decoder::OregonScale::VERSION = '1.142010';
5             # ABSTRACT: Device::RFXCOM::Decoder::OregonScale decode Oregon Scale RF messages
6              
7              
8 3     3   62 use 5.006;
  3         11  
  3         135  
9 3     3   53 use constant DEBUG => $ENV{DEVICE_RFXCOM_DECODER_OREGON_SCALE_DEBUG};
  3         6  
  3         363  
10 3     3   16 use Carp qw/croak/;
  3         7  
  3         170  
11 3     3   18 use Device::RFXCOM::Decoder qw/hi_nibble lo_nibble/;
  3         14  
  3         1464  
12             our @ISA = qw(Device::RFXCOM::Decoder);
13              
14              
15             sub decode {
16 58     58 1 150 my ($self, $parent, $message, $bytes, $bits, $result) = @_;
17 58 100 100     354 if ($bits == 64 && lo_nibble($bytes->[0]) == 3) {
18 1         6 return parse_gr101($self, $parent, $message, $bytes, $bits, $result);
19             }
20 57 100       282 return unless (scalar @$bytes == 7);
21 4 100 100     31 return unless (($bytes->[0]&0xf0) == ($bytes->[5]&0xf0) &&
22             ($bytes->[1]&0xf) == ($bytes->[6]&0xf));
23 2         13 my $weight =
24             sprintf "%x%02x%x", $bytes->[5]&0x1, $bytes->[4], hi_nibble($bytes->[3]);
25 2 100       16 return unless ($weight =~ /^\d+$/);
26 1         4 $weight /= 10;
27 1         6 my $dev_str = sprintf 'bwr102.%02x', hi_nibble($bytes->[1]);
28 1         5 my $unknown = sprintf "%x%x", lo_nibble($bytes->[3]), hi_nibble($bytes->[2]);
29 1         3 push @{$result->{messages}},
  1         7  
30             Device::RFXCOM::Response::Sensor->new(device => $dev_str,
31             measurement => 'weight',
32             value => $weight,
33             unknown => $unknown,
34             );
35 1         6 return 1;
36             }
37              
38              
39             sub parse_gr101 {
40 1     1 1 4 my ($self, $parent, $message, $bytes, $bits, $result) = @_;
41              
42 1         5 my $weight =
43             (lo_nibble($bytes->[4])<<12) + ($bytes->[3]<<4) + ($bytes->[2]>>4);
44 1         15 $weight = sprintf "%.1f", $weight/400.8;
45 1         5 my $dev_str = sprintf 'gr101.%02x', $bytes->[1];
46 1         3 push @{$result->{messages}},
  1         9  
47             Device::RFXCOM::Response::Sensor->new(device => $dev_str,
48             measurement => 'weight',
49             value => $weight);
50 1         6 return 1;
51             }
52              
53             1;
54              
55             __END__