display time for all events
[Biblio-RFID.git] / scripts / scan.pl
index 572864e..5d3fe3c 100755 (executable)
@@ -5,40 +5,47 @@ use strict;
 
 use Data::Dump qw(dump);
 use Getopt::Long;
+use lib 'lib';
+use RFID::Biblio::Reader;
+use RFID::Biblio::RFID501;
 
 my $loop = 0;
-my $only;
+my $reader;
+my $debug = 0;
 
 GetOptions(
-       'loop!'   => \$loop,
-       'only=s', => \$only,
+       'loop!'     => \$loop,
+       'reader=s', => \$reader,
+       'debug+'    => \$debug,
 ) || die $!;
 
-my @readers = ( '3M810', 'CPRM02' );
-my @rfid;
-
-foreach my $reader ( @readers ) {
-       next if $only && $only ne $reader;
-       my $module = "RFID::Serial::$reader";
-       eval "use $module";
-       die $@ if $@;
-       if ( my $rfid = $module->new( device => '/dev/ttyUSB0' ) ) {
-               push @rfid, $rfid;
-               warn "# added $module\n";
-       } else {
-               warn "# ignored $module\n";
-       }
+my $rfid = RFID::Biblio::Reader->new( $reader );
+$RFID::Biblio::debug = $debug;
+
+sub tag {
+       my $tag = shift;
+       return $tag
+               , " AFI: "
+               , uc unpack('H2', $rfid->afi($tag))
+               , " "
+               , dump( RFID::Biblio::RFID501->to_hash( $rfid->blocks($tag) ) )
+               , $/
+               ;
 }
 
-use lib 'lib';
-
 do {
-       foreach my $rfid ( @rfid ) {
-               my $visible = $rfid->scan;
-               foreach my $tag ( keys %$visible ) {
-               warn "XXX $tag";
-                       print ref($rfid),"\t$tag\t", join('', @{ $visible->{$tag} }), $/;
-               }
-       }
+       my @visible = $rfid->tags(
+               enter => sub {
+                       my $tag = shift;
+                       print localtime." enter $tag ", tag($tag);
+
+               },
+               leave => sub {
+                       my $tag = shift;
+                       print localtime." leave $tag ", tag($tag);
+               },
+       );
+
+       warn localtime." visible: ",join(' ',@visible),"\n";
 
 } while $loop;