a09e0f7b1e95fb930fab1782f4d7dcfb77164802
[Biblio-RFID.git] / scripts / print.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 RFID::Biblio::Reader;
10 use RFID::Biblio::RFID501;
11
12 use lib '/home/dpavlin/klin/Printer-EVOLIS/lib';
13 use Printer::EVOLIS::Parallel;
14
15 my $loop = 1;
16 my $reader = '3M';
17 my $debug = 0;
18 my $afi   = 0x42;
19
20 GetOptions(
21         'loop!'     => \$loop,
22         'reader=s', => \$reader,
23         'debug+'    => \$debug,
24 ) || die $!;
25
26 die "Usage: $0 print.txt\n" unless @ARGV;
27
28 my @queue;
29 my @done;
30 warn "# reading tab-delimited input\n";
31 while(<>) {
32         chomp;
33         my @a = split(/\t/,$_);
34         push @queue, [ @a ];
35 }
36
37 print "# queue ", dump @queue;
38
39 my $rfid = RFID::Biblio::Reader->new( $reader );
40 $RFID::Biblio::debug = $debug;
41
42 sub tag {
43         my $tag = shift;
44         return $tag
45                 , " AFI: "
46                 , uc unpack('H2', $rfid->afi($tag))
47                 , " "
48                 , dump( RFID::Biblio::RFID501->to_hash( $rfid->blocks($tag) ) )
49                 , $/
50                 ;
51 }
52
53 sub print_card;
54
55 while ( $rfid->tags ) {
56         print "ERROR: remove all tags from output printer tray\n";
57         sleep 1;
58 }
59
60 print_card;
61
62 do {
63         my @visible = $rfid->tags(
64                 enter => sub {
65                         my $tag = shift;
66                         print localtime()." enter ", tag($tag);
67
68                         my $card = shift @queue;
69                         $rfid->write_blocks( $tag => RFID::Biblio::RFID501->from_hash({ content => $card->[0] }) );
70                         $rfid->write_afi(    $tag => chr($afi) ) if $afi;
71
72                 },
73                 leave => sub {
74                         my $tag = shift;
75                         print localtime()." leave ", tag($tag);
76
77                         print_card;
78                 },
79         );
80
81         warn localtime()." visible: ",join(' ',@visible),"\n";
82
83         sleep 1;
84 } while $loop;
85
86 sub print_card {
87
88         print "XXX print_card @{$queue[0]}\n";
89
90         my $p = Printer::EVOLIS::Parallel->new( '/dev/usb/lp0' );
91         print "insert card ", $p->command( 'Si' ),$/;
92         print "eject card ", $p->command( 'Ser' ),$/;
93 }
94