rename module to Biblio::RFID in source
[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 Biblio::RFID::Reader;
10 use Biblio::RFID::RFID501;
11 use Storable;
12
13 my $evolis_dir = '/home/dpavlin/klin/Printer-EVOLIS'; # FIXME
14 use lib '/home/dpavlin/klin/Printer-EVOLIS/lib';
15 use Printer::EVOLIS::Parallel;
16
17 my $loop = 1;
18 my $reader = '3M';
19 my $debug = 0;
20 my $afi   = 0x00; # XXX
21 my $test  = 0;
22
23 my $log_print = 'log.print';
24 mkdir $log_print unless -d $log_print;
25
26 GetOptions(
27         'loop!'     => \$loop,
28         'reader=s', => \$reader,
29         'debug+'    => \$debug,
30         'test+'     => \$test,
31 ) || die $!;
32
33 die "Usage: $0 print.txt\n" unless @ARGV;
34
35 my $persistant_path = '/tmp/programmed.storable';
36 my $programmed;
37 my $numbers;
38 if ( -e $persistant_path ) {
39         $programmed = retrieve($persistant_path);
40         warn "# loaded ", scalar keys %$programmed, " programmed cards\n";
41         foreach my $tag ( keys %$programmed ) {
42                 $numbers->{ $programmed->{$tag} } = $tag;
43         }
44 }
45
46 my @queue;
47 my @done;
48 warn "# reading tab-delimited input: number login\@domain name surname\n";
49 while(<>) {
50         chomp;
51         my @a = split(/\t/,$_);
52         die "invalid: @a in line $_" if $a[0] !~ m/\d{12}/ && $a[1] !~ m/\@/;
53         push @queue, [ @a ] if ! $numbers->{ $a[0] };
54 }
55
56 print "# queue ", dump @queue;
57
58 my $rfid = Biblio::RFID::Reader->new( $reader );
59 $Biblio::RFID::debug = $debug;
60
61 sub tag {
62         my $tag = shift;
63         return $tag
64                 , " AFI: "
65                 , uc unpack('H2', $rfid->afi($tag))
66                 , " "
67                 , dump( Biblio::RFID::RFID501->to_hash( $rfid->blocks($tag) ) )
68                 , $/
69                 ;
70 }
71
72 sub iso_date {
73         my @t = localtime(time);
74         return sprintf "%04d-%02d-%02dT%02d:%02d:%02d", $t[5]+1900,$t[4]+1,$t[3],$t[2],$t[1],$t[0];
75 }
76
77 sub print_card;
78
79 my $log_path = "$log_print/" . iso_date . ".txt";
80 die "$log_path exists" if -e $log_path;
81 open(my $log, '>', $log_path) || die "$log_path: $!";
82
83 while ( $rfid->tags ) {
84         print "ERROR: remove all tags from output printer tray\n";
85         sleep 1;
86 }
87
88 print_card;
89
90 do {
91         my @visible = $rfid->tags(
92                 enter => sub {
93                         my $tag = shift;
94                         print localtime()." enter ", eval { tag($tag) };
95                         return if $@;
96
97                         if ( ! $programmed->{$tag} ) {
98                                 my $card = shift @queue;
99                                 my $number = $card->[0];
100                                 print "PROGRAM $tag $number\n";
101                                 $rfid->write_blocks( $tag => Biblio::RFID::RFID501->from_hash({ content => $number }) );
102                                 $rfid->write_afi( $tag => chr($afi) ) if $afi;
103
104                                 $programmed->{$tag} = $number;
105                                 store $programmed, $persistant_path;
106
107                                 print $log iso_date, ",$tag,$number\n";
108                         }
109
110                 },
111                 leave => sub {
112                         my $tag = shift;
113
114                         print_card if $programmed->{$tag};
115                 },
116         );
117
118         warn localtime()." visible: ",join(' ',@visible),"\n";
119
120         sleep 1;
121 } while $loop;
122
123 sub print_card {
124
125         if ( ! @queue ) {
126                 print "QUEUE EMPTY - printing finished\n";
127                 close($log);
128                 print "$log_path ", -s $log_path, " bytes created\n";
129                 exit;
130         }
131
132         my @data = @{$queue[0]};
133         print "XXX print_card @data\n";
134
135         if ( $test ) {
136
137                 my $p = Printer::EVOLIS::Parallel->new( '/dev/usb/lp0' );
138                 print "insert card ", $p->command( 'Si' ),$/;
139                 sleep 1;
140                 print "eject card ", $p->command( 'Ser' ),$/;
141
142         } else {
143
144                 system "$evolis_dir/scripts/inkscape-render.pl", "$evolis_dir/card/ffzg-2010.svg", @data;
145                 my $nr = $data[0];
146                 system "$evolis_dir/scripts/evolis-driver.pl out/$nr.front.pbm out/$nr.back.pbm > /dev/usb/lp0";
147
148         }
149
150 }
151