pass debug option down since we controll warn
[Biblio-RFID.git] / lib / Biblio / RFID / Reader.pm
1 package Biblio::RFID::Reader;
2
3 use warnings;
4 use strict;
5
6 use Data::Dump qw(dump);
7 use Time::HiRes;
8 use lib 'lib';
9 use Biblio::RFID;
10 use Carp qw(confess);
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         my ( $class, $filter ) = @_;
31         my $self = {};
32         bless $self, $class;
33         $self->{_readers} = [ $self->_available( $filter ) ];
34         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         my $self = shift;
48         my $triggers = {@_};
49
50         $self->{_tags} ||= {};
51         $self->{_tags}->{$_}->{time} = 0 foreach keys %{$self->{_tags}};
52         my $t = time;
53
54         foreach my $rfid ( @{ $self->{_readers} } ) {
55                 warn "# inventory on $rfid";
56                 my @tags = $rfid->inventory;
57
58                 foreach my $tag ( @tags ) {
59
60                         if ( ! exists $self->{_tags}->{$tag} ) {
61                                 eval {
62                                         my $blocks = $rfid->read_blocks($tag);
63                                         $self->{_tags}->{$tag}->{blocks} = $blocks->{$tag} || die "no $tag in ",dump($blocks);
64                                         my $afi = $rfid->read_afi($tag);
65                                         $self->{_tags}->{$tag}->{afi} = $afi;
66                                         $self->{_tags}->{$tag}->{type} = $rfid->tag_type( $tag );
67
68                                 };
69                                 if ( $@ ) {
70                                         warn "ERROR reading $tag: $@\n";
71                                         $self->_invalidate_tag( $tag );
72                                         next;
73                                 }
74
75                                 $triggers->{enter}->( $tag ) if $triggers->{enter};
76                         }
77
78                         $self->{_tags}->{$tag}->{time} = $t;
79
80                 }
81         
82         }
83
84         foreach my $tag ( grep { $self->{_tags}->{$_}->{time} == 0 } keys %{ $self->{_tags} } ) {
85                 $triggers->{leave}->( $tag ) if $triggers->{leave};
86                 $self->_invalidate_tag( $tag );
87         }
88
89         warn "## _tags ",dump( $self->{_tags} );
90
91         return grep { $self->{_tags}->{$_}->{time} } keys %{ $self->{_tags} };
92 }
93
94 =head2 blocks
95
96   my $blocks_arrayref = $rfid->blocks( $tag );
97
98 =head2 afi
99
100   my $afi = $rfid->afi( $tag );
101
102 =cut
103
104 sub blocks { $_[0]->{_tags}->{$_[1]}->{ 'blocks' } || confess "no blocks for $_[1]"; };
105 sub afi    { $_[0]->{_tags}->{$_[1]}->{ 'afi'    } || confess "no afi for $_[1]"; };
106
107 =head2 to_hash
108
109   $self->to_hash( $tag );
110
111 =cut
112
113 sub to_hash {
114         my ( $self, $tag ) = @_;
115         return unless exists $self->{_tags}->{$tag};
116         my $type = $self->{_tags}->{$tag}->{type} || confess "can't find type for tag $tag ",dump( $self->{_tags} );
117         my $decode = 'Biblio::RFID::' . $type;
118         my $hash = $decode->to_hash( $self->blocks( $tag ) );
119         $hash->{tag_type} = $type;
120         return $hash;
121 }
122
123 =head2 debug
124
125   $self->debug(1); # or more
126
127 =cut
128
129 sub debug {
130         my ( $self, $level ) = @_;
131         $debug = $level;
132         warn "debug level $level\n" if $level;
133 }
134
135 =head1 PRIVATE
136
137 =head2 _invalidate_tag
138
139   $rfid->_invalidate_tag( $tag );
140
141 =cut
142
143 sub _invalidate_tag {
144         my ( $self, $tag ) = @_;
145         my @caller = caller(0);
146         warn "## _invalidate_tag caller $caller[0] $caller[1] +$caller[2]\n";
147         my $old = delete $self->{_tags}->{$tag};
148         warn "# _invalidate_tag $tag ", dump($old);
149 }
150
151 =head2 _available
152
153 Probe each RFID reader supported and returns succefull ones
154
155   my $rfid_readers = Biblio::RFID::Reader->_available( $regex_filter );
156
157 =cut
158
159 my @readers = ( '3M810', 'CPRM02', 'librfid' );
160
161 sub _available {
162         my ( $self, $filter ) = @_;
163
164         $filter = '' unless defined $filter;
165
166         warn "# filter: $filter";
167
168         my @rfid;
169
170         foreach my $reader ( @readers ) {
171                 next if $filter && $reader !~ /$filter/i;
172                 my $module = "Biblio::RFID::Reader::$reader";
173                 eval "use $module";
174                 die $@ if $@;
175                 if ( my $rfid = $module->new ) {
176                         push @rfid, $rfid;
177                         warn "# added $module\n";
178                 } else {
179                         warn "# ignored $module\n";
180                 }
181         }
182
183         die "no readers found" unless @rfid;
184
185         return @rfid;
186 }
187
188 =head1 AUTOLOAD
189
190 On any other function calls, we just marshall to all readers
191
192 =cut
193
194 # we don't want DESTROY to fallback into AUTOLOAD
195 sub DESTROY {}
196
197 our $AUTOLOAD;
198 sub AUTOLOAD {
199         my $self = shift;
200         my $command = $AUTOLOAD;
201         $command =~ s/.*://;
202
203         my @out;
204
205         foreach my $r ( @{ $self->{_readers} } ) {
206                 push @out, $r->$command(@_);
207         }
208
209         $self->_invalidate_tag( $_[0] ) if $command =~ m/write/;
210
211         return @out;
212 }
213
214 1
215 __END__
216
217 =head1 SEE ALSO
218
219 =head2 RFID reader implementations
220
221 L<Biblio::RFID::Reader::3M810>
222
223 L<Biblio::RFID::Reader::CPRM02>
224
225 L<Biblio::RFID::Reader::librfid>
226