File Coverage

blib/lib/Device/RFXCOM/Decoder/CM119.pm
Criterion Covered Total %
statement 49 49 100.0
branch 8 8 100.0
condition 3 3 100.0
subroutine 10 10 100.0
pod 1 1 100.0
total 71 71 100.0


line stmt bran cond sub pod time code
1 3     3   4058 use strict;
  3         8  
  3         154  
2 3     3   19 use warnings;
  3         6  
  3         167  
3             package Device::RFXCOM::Decoder::CM119;
4             $Device::RFXCOM::Decoder::CM119::VERSION = '1.142010';
5             # ABSTRACT: Device::RFXCOM::Decoder::CM119 decode OWL CM119 RF messages
6              
7              
8 3     3   83 use 5.006;
  3         11  
  3         125  
9 3     3   16 use constant DEBUG => $ENV{DEVICE_RFXCOM_DECODER_CM119_DEBUG};
  3         7  
  3         281  
10 3     3   18 use Carp qw/croak/;
  3         17  
  3         194  
11 3     3   18 use Device::RFXCOM::Decoder qw/hi_nibble lo_nibble nibble_sum/;
  3         4  
  3         140  
12 3     3   15 use base 'Device::RFXCOM::Decoder';
  3         6  
  3         286  
13 3     3   17 use Device::RFXCOM::Response::Sensor;
  3         4  
  3         1489  
14              
15              
16             sub decode {
17 89     89 1 217 my ($self, $parent, $message, $bytes, $bits, $result) = @_;
18 89 100       808 $bits == 108 or return;
19 5 100       19 ($bytes->[0]&0xf)==0xa or return;
20              
21 4         15 my $s = _ns(1, 10, $bytes);
22 4         12 $s += lo_nibble($bytes->[11]);
23 4         12 $s -= (lo_nibble($bytes->[12])<<4) + hi_nibble($bytes->[11]);
24 4 100       17 $s == 0 or return;
25              
26 3         8 my $ch = $bytes->[0]>>4;
27 3 100 100     18 if ($ch < 1 || $ch > 3) {
28 2         18 warn "CM119 channel not 1 - 3?\n";
29             }
30 3         20 my $device = sprintf "%02x", $bytes->[2];
31 3         11 my $counter = lo_nibble($bytes->[1]);
32 3         12 my $now = (lo_nibble($bytes->[5])<<16) + ($bytes->[4]<<8) + $bytes->[3];
33 3         16 my $total =
34             ($bytes->[10] << 36) + ($bytes->[9] << 28) + ($bytes->[8] << 20) +
35             ($bytes->[7] << 12) + ($bytes->[6] << 4) + hi_nibble($bytes->[5]);
36 3         7 $total /= 223000; # kWh
37 3         8 my $dev = $device.'.'.$ch;
38 3         6 printf "cm119 d=%s power=%dW\n", $dev, $now if DEBUG;
39 3         4 push @{$result->{messages}},
  3         24  
40             Device::RFXCOM::Response::Sensor->new(device => 'cm119.'.$dev,
41             measurement => 'power',
42             value => $now);
43 3         14 return 1;
44             }
45              
46             sub _ns {
47 4     4   7 my ($s, $e, $b) = @_;
48 4         7 my $sum = 0;
49 4         12 foreach ($s .. $e) {
50 40         103 $sum += lo_nibble($b->[$_]) + hi_nibble($b->[$_]);
51             }
52 4         11 $sum;
53             }
54              
55             1;
56              
57             __END__