added com2tcp windows binary i386
[Biblio-RFID.git] / scripts / scan.pl
index 5d3fe3c..d2e18b9 100755 (executable)
@@ -6,21 +6,23 @@ use strict;
 use Data::Dump qw(dump);
 use Getopt::Long;
 use lib 'lib';
-use RFID::Biblio::Reader;
-use RFID::Biblio::RFID501;
+use Biblio::RFID::Reader;
+use Biblio::RFID::RFID501;
 
 my $loop = 0;
 my $reader;
 my $debug = 0;
+my $log;
 
 GetOptions(
        'loop!'     => \$loop,
        'reader=s', => \$reader,
        'debug+'    => \$debug,
+       'log=s'     => \$log,
 ) || die $!;
 
-my $rfid = RFID::Biblio::Reader->new( $reader );
-$RFID::Biblio::debug = $debug;
+my $rfid = Biblio::RFID::Reader->new( $reader );
+$Biblio::RFID::debug = $debug;
 
 sub tag {
        my $tag = shift;
@@ -28,24 +30,42 @@ sub tag {
                , " AFI: "
                , uc unpack('H2', $rfid->afi($tag))
                , " "
-               , dump( RFID::Biblio::RFID501->to_hash( $rfid->blocks($tag) ) )
+               , dump( $rfid->to_hash( $tag ) )
                , $/
                ;
 }
 
+my $saved;
+
+sub iso_date {
+       my @t = localtime(time);
+       return sprintf "%04d-%02d-%02dT%02d:%02d:%02d", $t[5]+1900,$t[4]+1,$t[3],$t[2],$t[1],$t[0];
+}
+
+sub log_tag {
+       my $tag = shift;
+       return if $saved->{tag} or ! $log;
+       my $hash = $rfid->to_hash( $tag );
+       open(my $fh, '>>', $log) || die "$log: $!";
+       print $fh iso_date,",$tag,", $hash->{content}, "\n";
+       close($fh);
+}
+
 do {
        my @visible = $rfid->tags(
                enter => sub {
                        my $tag = shift;
-                       print localtime." enter $tag ", tag($tag);
-
+                       print iso_date," reader ", $rfid->from_reader($tag), " enter ", tag($tag);
+                       log_tag $tag;
                },
                leave => sub {
                        my $tag = shift;
-                       print localtime." leave $tag ", tag($tag);
+                       print iso_date," leave ", tag($tag);
                },
        );
 
-       warn localtime." visible: ",join(' ',@visible),"\n";
+       warn iso_date," visible: ",join(' ',@visible),"\n";
+
+       sleep 1;
 
 } while $loop;