File Coverage

blib/lib/AnyEvent/CurrentCost.pm
Criterion Covered Total %
statement 70 70 100.0
branch 9 12 75.0
condition 2 3 66.6
subroutine 20 20 100.0
pod 4 4 100.0
total 105 109 96.3


line stmt bran cond sub pod time code
1 3     3   2479 use strict;
  3         5  
  3         101  
2 3     3   15 use warnings;
  3         5  
  3         185  
3             package AnyEvent::CurrentCost;
4             {
5             $AnyEvent::CurrentCost::VERSION = '1.130190';
6             }
7              
8             # ABSTRACT: AnyEvent module for reading from Current Cost energy meters
9              
10              
11 3     3   14 use constant DEBUG => $ENV{ANYEVENT_CURRENT_COST_DEBUG};
  3         14  
  3         150  
12 3     3   17 use base qw/Device::CurrentCost/;
  3         4  
  3         2469  
13 3     3   239299 use AnyEvent;
  3         10254  
  3         104  
14 3     3   4441 use AnyEvent::Handle;
  3         81045  
  3         121  
15 3     3   3092 use AnyEvent::SerialPort;
  3         4508  
  3         87  
16 3     3   20 use Carp qw/croak carp/;
  3         6  
  3         147  
17 3     3   2413 use Sub::Name;
  3         2586  
  3         2407  
18              
19              
20             sub new {
21 3     3 1 22723 my ($pkg, %p) = @_;
22 3 100       213 croak $pkg.q{->new: 'callback' parameter is required} unless ($p{callback});
23 2         48 my $self = $pkg->SUPER::new(%p);
24 2         308 $self;
25             }
26              
27 1     1   73 sub DESTROY { shift->cleanup }
28              
29              
30             sub cleanup {
31 2     2 1 3 my $self = shift;
32 2         4 print STDERR "cleanup\n" if DEBUG;
33 2         5 delete $self->{handle};
34 2 50       10 close $self->filehandle if (defined $self->filehandle);
35             }
36              
37             sub _error {
38 1     1   3 my ($self, $fatal, $message) = @_;
39 1         5 $self->cleanup($message);
40 1 50       35 $self->{on_error}->($fatal, $message) if ($self->{on_error});
41             }
42              
43              
44             sub open {
45 2     2 1 83 my $self = shift;
46 2         29 my $fh = $self->filehandle;
47 2 50       71 my $handle =
48             $fh
49             ? AnyEvent::Handle->new(fh => $fh)
50             : AnyEvent::SerialPort->new(serial_port =>
51             [ $self->device,
52             [ baudrate => $self->baud ] ]);
53 2         302 print STDERR ref $self, "->open: created ", $handle, "\n" if DEBUG;
54 2         14 $self->{handle} = $handle;
55             $handle->on_error(subname 'on_error' => sub {
56 1     1   38 my ($handle, $fatal, $msg) = @_;
57 1         1 print STDERR $handle.": error $msg\n" if DEBUG;
58 1         7 $handle->destroy;
59 1         35 $self->_error($fatal, 'Error: '.$msg);
60 2         42 });
61             $handle->on_rtimeout(subname 'on_rtimeout' => sub {
62 1     1   501306 my $rbuf = \$handle->{rbuf};
63 1         327 carp $handle, ": Discarding '", $$rbuf, "'\n";
64 1         617 $$rbuf = '';
65 1         15 $handle->rtimeout(undef);
66 2         58 });
67             $handle->on_read(subname 'on_read_cb' => sub {
68 2     2   1577 my ($hdl) = @_;
69             $hdl->push_read(ref $self => $self,
70             subname 'push_read_cb' => sub {
71 3         14 $self->{callback}->(@_);
72 3         68 1;
73 2         56 });
74 2         46 });
75             }
76              
77             sub _time_now {
78 3     3   199 AnyEvent->now;
79             }
80              
81              
82             sub anyevent_read_type {
83 2     2 1 64 my ($handle, $cb, $self) = @_;
84             subname 'anyevent_read_type_reader' => sub {
85 4     4   999104 my $rbuf = \$handle->{rbuf};
86 4         8 while (1) { # read all message from the buffer
87 7         11 print STDERR "Before: ", (unpack 'H*', $$rbuf||''), "\n" if DEBUG;
88 7         60 my $res = $self->read_one($rbuf);
89 7 100       138 return unless ($res);
90 3         6 print STDERR "After: ", (unpack 'H*', $$rbuf), "\n" if DEBUG;
91 3 100 66     50 $handle->rtimeout($self->{discard_timeout}) if ($$rbuf && length $$rbuf);
92 3         71 $res = $cb->($res);
93             }
94             }
95 2         32 }
96              
97             1;
98              
99             __END__