lib/Biblio/RFID/Reader.pm

filter: Statementstmt 40.0% 38 / 95 Branchbran 12.5% 3 / 24 Conditioncond 14.2% 1 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 62.5% 10 / 16 Podpod 100% 4 / 4 totaltotal 37.3% 56 / 150 SCAR 51.1
CC
22
Cov
33%
tags
132.0
_available
18.6
AUTOLOAD
12.0
CRAP 165.4
« blib/lib/Biblio/RFID/Reader/CPRM02.pm ↑ Summary blib/lib/Biblio/RFID/Reader.pm »
1 package Biblio::RFID::Reader;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use lib 'lib';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Biblio::RFID;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Biblio::RFID::Reader - simple way to write RFID applications in perl
15  
16 =head1 DESCRIPTION
17  
18 This module will probe all available readers and use calls from
19 L<Biblio::RFID::Reader::API> to invoke correct reader.
20  
21 =head1 FUNCTIONS
22  
23 =head2 new
24  
25 my $rfid = Biblio::RFID::Reader->new( 'optional reader filter' );
26  
27 =cut
28  
29 sub new {
30 1 ▶ my ( $class, $filter ) = @_;
Subroutine new: called CC=1 SCAR=0.1
Pod documented
31 1 my $self = {};
32 1 bless $self, $class;
33 1 $self->{_readers} = [ $self->_available( $filter ) ];
34 0 return $self;
35 }
36  
37 =head2 tags
38  
39 my @visible = $rfid->tags(
40 enter => sub { my $tag = shift; },
41 leave => sub { my $tag = shift; },
42 );
43  
44 =cut
45  
46 sub tags {
47 0 ▶ my $self = shift;
Subroutine tags: not called CC=11 SCAR=48.8
Pod documented
48 0 my $triggers = {@_};
49  
50 0 ▶ $self->{_tags} ||= {};
Condition0%0/2
$self->{"_tags"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"_tags"} ||= {}
$self->{"_tags"}
Truth table0%0/2
A
A= $self->{"_tags"}
Aresult
11
00
51 0 $self->{_tags}->{$_}->{time} = 0 foreach keys %{$self->{_tags}};
52 0 my $t = time;
53  
54 0 foreach my $rfid ( @{ $self->{_readers} } ) {
55 0 warn "# inventory on $rfid";
56 0 my @tags = $rfid->inventory;
57  
58 0 foreach my $tag ( @tags ) {
59  
60 0 ▶ if ( ! exists $self->{_tags}->{$tag} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_tags"}{$tag}) 0 0 0
61 0 eval {
62 0 my $blocks = $rfid->read_blocks($tag);
63 0 ▶ $self->{_tags}->{$tag}->{blocks} = $blocks->{$tag} || die "no $tag in ",dump($blocks);
Condition0%0/2
$blocks->{$tag} || die("no $tag in ", Biblio::RFID::Reader::dump($blocks))
l!l
00
MC/DC0%0/1
$blocks->{$tag} || die("no $tag in ", Biblio::RFID::Reader::dump($blocks))
$blocks->{$tag}
Truth table0%0/2
A
A= $blocks->{$tag}
Aresult
11
00
64 0 my $afi = $rfid->read_afi($tag);
65 0 $self->{_tags}->{$tag}->{afi} = $afi;
66  
67 };
68 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
69 0 warn "ERROR reading $tag: $@\n";
70 0 $self->_invalidate_tag( $tag );
71 0 next;
72 }
73  
74 0 ▶ $triggers->{enter}->( $tag ) if $triggers->{enter};
Branch: 1 branch
BranchTrueFalseTotal
if $triggers->{"enter"} 0 0 0
75 }
76  
77 0 $self->{_tags}->{$tag}->{time} = $t;
78  
79 }
80
81 0 foreach my $tag ( grep { $self->{_tags}->{$_}->{time} == 0 } keys %{ $self->{_tags} } ) {
82 0 ▶ $triggers->{leave}->( $tag ) if $triggers->{leave};
Branch: 1 branch
BranchTrueFalseTotal
if $triggers->{"leave"} 0 0 0
83 0 $self->_invalidate_tag( $tag );
84 }
85  
86 }
87  
88 0 warn "## _tags ",dump( $self->{_tags} );
89  
90 0 return grep { $self->{_tags}->{$_}->{time} } keys %{ $self->{_tags} };
91 }
92  
93 =head2 blocks
94  
95 my $blocks_arrayref = $rfid->blocks( $tag );
96  
97 =head2 afi
98  
99 my $afi = $rfid->afi( $tag );
100  
101 =cut
102  
103 0 ▶sub blocks { $_[0]->{_tags}->{$_[1]}->{ 'blocks' } || confess "no blocks for $_[1]"; };
Subroutine blocks: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"_tags"}{$_[1]}{"blocks"} or confess("no blocks for $_[1]") 0 0 0
104 0 ▶sub afi { $_[0]->{_tags}->{$_[1]}->{ 'afi' } || confess "no afi for $_[1]"; };
Subroutine afi: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"_tags"}{$_[1]}{"afi"} or confess("no afi for $_[1]") 0 0 0
105  
106 =head1 PRIVATE
107  
108 =head2 _invalidate_tag
109  
110 $rfid->_invalidate_tag( $tag );
111  
112 =cut
113  
114 sub _invalidate_tag {
115 0 ▶ my ( $self, $tag ) = @_;
Subroutine _invalidate_tag: not called CC=1 SCAR=6.9
116 0 my @caller = caller(0);
117 0 warn "## _invalidate_tag caller $caller[0] $caller[1] +$caller[2]\n";
118 0 my $old = delete $self->{_tags}->{$tag};
119 0 warn "# _invalidate_tag $tag ", dump($old);
120 }
121  
122 =head2 _available
123  
124 Probe each RFID reader supported and returns succefull ones
125  
126 my $rfid_readers = Biblio::RFID::Reader->_available( $regex_filter );
127  
128 =cut
129  
130 3 my @readers = ( '3M810', 'CPRM02', 'librfid' );
131  
132 sub _available {
133 1 ▶ my ( $self, $filter ) = @_;
Subroutine _available: called CC=8 SCAR=29.2
134  
135 1 ▶ $filter = '' unless defined $filter;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filter 1 0 1
136  
137 1 warn "# filter: $filter";
138  
139 1 my @rfid;
140  
141 1 foreach my $reader ( @readers ) {
142 1 ▶ next if $filter && $reader !~ /$filter/i;
Branch: 1 branch
BranchTrueFalseTotal
if $filter and not $reader =~ /$filter/i 0 1 1
Condition33%1/3
$filter and not $reader =~ /$filter/i
!ll&&!rl&&r
100
MC/DC0%0/2
$filter and not $reader =~ /$filter/i
$filter not $reader =~ /$filter/i
Truth table33%1/3
A and not(B)
A= $filterB= not $reader =~ /$filter/i
ABresult
0X0
100
111
143 1 my $module = "Biblio::RFID::Reader::$reader";
144 1 ▶ eval "use $module";
Subroutine BEGIN: called CC=1 SCAR=1.2
145 1 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 0 1
146 0 ▶ if ( my $rfid = $module->new ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rfid = $module->new) { } 0 0 0
147 0 push @rfid, $rfid;
148 0 warn "# added $module\n";
149 } else {
150 0 warn "# ignored $module\n";
151 }
152 }
153  
154 0 ▶ die "no readers found" unless @rfid;
Branch: 1 branch
BranchTrueFalseTotal
unless @rfid 0 0 0
155  
156 0 return @rfid;
157 }
158  
159 =head1 AUTOLOAD
160  
161 On any other function calls, we just marshall to all readers
162  
163 =cut
164  
165 # we don't want DESTROY to fallback into AUTOLOAD
166 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
167  
168 3 our $AUTOLOAD;
169 sub AUTOLOAD {
170 0 ▶ my $self = shift;
Subroutine AUTOLOAD: not called CC=3 SCAR=24.8
171 0 my $command = $AUTOLOAD;
172 0 $command =~ s/.*://;
173  
174 0 my @out;
175  
176 0 foreach my $r ( @{ $self->{_readers} } ) {
177 0 push @out, $r->$command(@_);
178 }
179  
180 0 ▶ $self->_invalidate_tag( $_[0] ) if $command =~ m/write/;
Branch: 1 branch
BranchTrueFalseTotal
if $command =~ /write/ 0 0 0
181  
182 0 return @out;
183 }
184  
185 3 1
186 __END__
« blib/lib/Biblio/RFID/Reader/CPRM02.pm ↑ Summary blib/lib/Biblio/RFID/Reader.pm »