support only afi change
[Biblio-RFID.git] / scripts / scan.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use Data::Dump qw(dump);
7 use Getopt::Long;
8 use lib 'lib';
9 use Biblio::RFID::Reader;
10
11 my $loop = 0;
12 my $reader;
13 my $debug = 0;
14 my $log;
15
16 GetOptions(
17         'loop!'     => \$loop,
18         'reader=s', => \$reader,
19         'debug+'    => \$debug,
20         'log=s'     => \$log,
21 ) || die $!;
22
23 my $rfid = Biblio::RFID::Reader->new( $reader );
24 $Biblio::RFID::debug = $debug;
25
26 sub tag {
27         my $tag = shift;
28         return $tag
29                 , " AFI: "
30                 , uc unpack('H2', $rfid->afi($tag))
31                 , " "
32                 , dump( $rfid->to_hash( $tag ) )
33                 , $/
34                 ;
35 }
36
37 my $saved;
38
39 sub iso_date {
40         my @t = localtime(time);
41         return sprintf "%04d-%02d-%02dT%02d:%02d:%02d", $t[5]+1900,$t[4]+1,$t[3],$t[2],$t[1],$t[0];
42 }
43
44 sub log_tag {
45         my $tag = shift;
46         return if $saved->{tag} or ! $log;
47         my $hash = $rfid->to_hash( $tag );
48         open(my $fh, '>>', $log) || die "$log: $!";
49         print $fh iso_date,",$tag,", $hash->{content}, "\n";
50         close($fh);
51 }
52
53 do {
54         my @visible = $rfid->tags(
55                 enter => sub {
56                         my $tag = shift;
57                         print iso_date," reader ", $rfid->from_reader($tag), " enter ", tag($tag);
58                         log_tag $tag;
59                 },
60                 leave => sub {
61                         my $tag = shift;
62                         print iso_date," leave ", tag($tag);
63                 },
64         );
65
66         warn iso_date," visible: ",join(' ',@visible),"\n";
67
68         sleep 1;
69
70 } while $loop;