4cac2b316cb2c3449ec303bff927613b49d68dc0
[Biblio-RFID.git] / lib / RFID / Biblio / Readers.pm
1 package RFID::Biblio::Readers;
2
3 use warnings;
4 use strict;
5
6 use lib 'lib';
7
8 =head1 NAME
9
10 RFID::Biblio::Readers - autodetect supported readers
11
12 =head1 FUNCTIONS
13
14 =head2 new
15
16   my $rfid = RFID::Biblio::Readers->new( 'optional reader filter' );
17
18 =cut
19
20 sub new {
21         my ( $class, $filter ) = @_;
22         my $self = {};
23         bless $self, $class;
24         $self->{_readers} = [ $self->_available( $filter ) ];
25         return $self;
26 }
27
28
29 =head1 PRIVATE
30
31 =head2 _available
32
33 Probe each RFID reader supported and returns succefull ones
34
35   my $rfid_readers = RFID::Biblio::Readers->_available( $regex_filter );
36
37 =cut
38
39 my @readers = ( '3M810', 'CPRM02', 'librfid' );
40
41 sub _available {
42         my ( $self, $filter ) = @_;
43
44         $filter = '' unless defined $filter;
45
46         warn "# filter: $filter";
47
48         my @rfid;
49
50         foreach my $reader ( @readers ) {
51                 next if $filter && $reader !~ /$filter/i;
52                 my $module = "RFID::Biblio::$reader";
53                 eval "use $module";
54                 die $@ if $@;
55                 if ( my $rfid = $module->new( device => '/dev/ttyUSB0' ) ) {
56                         push @rfid, $rfid;
57                         warn "# added $module\n";
58                 } else {
59                         warn "# ignored $module\n";
60                 }
61         }
62
63         die "no readers found" unless @rfid;
64
65         return @rfid;
66 }
67
68 =head1 AUTOLOAD
69
70 On any other function calls, we just marshall to all readers
71
72 =cut
73
74 # we don't want DESTROY to fallback into AUTOLOAD
75 sub DESTROY {}
76
77 our $AUTOLOAD;
78 sub AUTOLOAD {
79         my $self = shift;
80         my $command = $AUTOLOAD;
81         $command =~ s/.*://;
82
83         my @out;
84
85         foreach my $r ( @{ $self->{_readers} } ) {
86                 push @out, $r->$command(@_);
87         }
88
89         return @out;
90 }
91
92 1
93 __END__
94
95 =head1 SEE ALSO
96
97 =head2 RFID reader implementations
98
99 L<RFID::Biblio::3M810>
100
101 L<RFID::Biblio::CPRM02>
102
103 L<RFID::Biblio::librfid>
104