| 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 | |||
| 67 | }; | ||
| 68 | if ( $@ ) { | ||
| 69 | warn "ERROR reading $tag: $@\n"; | ||
| 70 | $self->_invalidate_tag( $tag ); | ||
| 71 | next; | ||
| 72 | } | ||
| 73 | |||
| 74 | $triggers->{enter}->( $tag ) if $triggers->{enter}; | ||
| 75 | } | ||
| 76 | |||
| 77 | $self->{_tags}->{$tag}->{time} = $t; | ||
| 78 | |||
| 79 | } | ||
| 80 | |||
| 81 | foreach my $tag ( grep { $self->{_tags}->{$_}->{time} == 0 } keys %{ $self->{_tags} } ) { | ||
| 82 | $triggers->{leave}->( $tag ) if $triggers->{leave}; | ||
| 83 | $self->_invalidate_tag( $tag ); | ||
| 84 | } | ||
| 85 | |||
| 86 | } | ||
| 87 | |||
| 88 | warn "## _tags ",dump( $self->{_tags} ); | ||
| 89 | |||
| 90 | 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 | sub blocks { $_[0]->{_tags}->{$_[1]}->{ 'blocks' } || confess "no blocks for $_[1]"; }; | ||
| 104 | sub afi { $_[0]->{_tags}->{$_[1]}->{ 'afi' } || confess "no afi for $_[1]"; }; | ||
| 105 | |||
| 106 | =head1 PRIVATE | ||
| 107 | |||
| 108 | =head2 _invalidate_tag | ||
| 109 | |||
| 110 | $rfid->_invalidate_tag( $tag ); | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | sub _invalidate_tag { | ||
| 115 | my ( $self, $tag ) = @_; | ||
| 116 | my @caller = caller(0); | ||
| 117 | warn "## _invalidate_tag caller $caller[0] $caller[1] +$caller[2]\n"; | ||
| 118 | my $old = delete $self->{_tags}->{$tag}; | ||
| 119 | 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 | my @readers = ( '3M810', 'CPRM02', 'librfid' ); | ||
| 131 | |||
| 132 | sub _available { | ||
| 133 | my ( $self, $filter ) = @_; | ||
| 134 | |||
| 135 | $filter = '' unless defined $filter; | ||
| 136 | |||
| 137 | warn "# filter: $filter"; | ||
| 138 | |||
| 139 | my @rfid; | ||
| 140 | |||
| 141 | foreach my $reader ( @readers ) { | ||
| 142 | next if $filter && $reader !~ /$filter/i; | ||
| 143 | my $module = "Biblio::RFID::Reader::$reader"; | ||
| 144 | eval "use $module"; | ||
| 145 | die $@ if $@; | ||
| 146 | if ( my $rfid = $module->new ) { | ||
| 147 | push @rfid, $rfid; | ||
| 148 | warn "# added $module\n"; | ||
| 149 | } else { | ||
| 150 | warn "# ignored $module\n"; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | die "no readers found" unless @rfid; | ||
| 155 | |||
| 156 | 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 {} | ||
| 167 | |||
| 168 | our $AUTOLOAD; | ||
| 169 | sub AUTOLOAD { | ||
| 170 | my $self = shift; | ||
| 171 | my $command = $AUTOLOAD; | ||
| 172 | $command =~ s/.*://; | ||
| 173 | |||
| 174 | my @out; | ||
| 175 | |||
| 176 | foreach my $r ( @{ $self->{_readers} } ) { | ||
| 177 | push @out, $r->$command(@_); | ||
| 178 | } | ||
| 179 | |||
| 180 | $self->_invalidate_tag( $_[0] ) if $command =~ m/write/; | ||
| 181 | |||
| 182 | return @out; | ||
| 183 | } | ||
| 184 | |||
| 185 | 1 | ||
| 186 | __END__ |