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