added com2tcp windows binary i386
[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 $programmed;
36 my $numbers;
37 foreach my $log_path ( glob( "$log_print/*.txt" ) ) {
38         warn "# loading $log_path";
39         open( my $in, '<', $log_path ) || die $!;
40         while(<$in>) {
41                 chomp;
42                 my ( $date, $sid, $nr ) = split(/,/,$_,3);
43                 $programmed->{ $sid } = $nr;
44                 $numbers->{ $nr } = $sid;       
45         }
46 }
47
48 warn "# ", scalar keys %$numbers, " programmed cards found\n";
49
50 my @queue;
51 my @done;
52 warn "# reading tab-delimited input: number login\@domain name surname\n";
53 while(<>) {
54         chomp;
55         my @a = split(/\t/,$_);
56         die "invalid: @a in line $_" if $a[0] !~ m/\d{12}/ && $a[1] !~ m/\@/;
57         push @queue, [ @a ] if ! $numbers->{ $a[0] } || $ENV{REPRINT};
58 }
59
60 # sort by card number
61 @queue = sort { $b->[0] <=> $a->[0] } @queue;
62
63 print "# queue ", dump @queue;
64
65 my $rfid = Biblio::RFID::Reader->new( $reader );
66 $Biblio::RFID::debug = $debug;
67
68 sub tag {
69         my $tag = shift;
70         return $tag
71                 , " AFI: "
72                 , uc unpack('H2', $rfid->afi($tag))
73                 , " "
74                 , dump( $rfid->to_hash( $tag ) )
75                 , $/
76                 ;
77 }
78
79 sub iso_date {
80         my @t = localtime(time);
81         return sprintf "%04d-%02d-%02dT%02d:%02d:%02d", $t[5]+1900,$t[4]+1,$t[3],$t[2],$t[1],$t[0];
82 }
83
84 sub print_card;
85 sub render_card;
86
87 my $log_path = "$log_print/" . iso_date . ".txt";
88 die "$log_path exists" if -e $log_path;
89
90 sub write_log {
91         my ( $tag, $number ) = @_;
92         open(my $log, '>>', $log_path) || die "$log_path: $!";
93         my $date = iso_date;
94         print $log "$date,$tag,$number\n";
95         close($log);
96         print "LOG $date $tag $number\n";
97 }
98
99 while ( $rfid->tags ) {
100         print "ERROR: remove all tags from output printer tray\n";
101         sleep 1;
102 }
103
104 print_card;
105
106 do {
107         my @visible = $rfid->tags(
108                 enter => sub {
109                         my $tag = shift;
110                         print localtime()." enter ", eval { tag($tag) };
111                         return if $@;
112
113                         if ( ! $programmed->{$tag} ) {
114                                 my $card = shift @queue;
115                                 my $number = $card->[0];
116                                 print "PROGRAM $tag $number\n";
117
118                                 while ( 1 ) {
119                                         eval {
120                                                 $rfid->write_blocks( $tag => Biblio::RFID::RFID501->from_hash({ content => $number }) );
121                                                 $rfid->write_afi( $tag => chr($afi) ) if $afi;
122                                         };
123                                         last unless $!;
124                                         warn "RETRY PROGRAM $tag $number\n";
125                                         sleep 1;
126                                 }
127
128                                 write_log $tag => $number;
129                                 $programmed->{$tag} = $number;
130
131                                 render_card; # pre-render next one
132                         }
133
134                 },
135                 leave => sub {
136                         my $tag = shift;
137
138                         print_card if $programmed->{$tag};
139                 },
140         );
141
142         warn localtime()." visible: ",join(' ',@visible),"\n";
143
144         sleep 1;
145 } while $loop;
146
147 sub _counters {
148         my $p = shift;
149         my $counters;
150         $counters->{$_} = $p->command("Rco;$_") foreach ( qw/p c a m n l b e f i k s/ );
151         return $counters;
152 }
153
154 sub render_card {
155         return unless @queue;
156         my @data = @{$queue[0]};
157         my $nr = $data[0];
158
159         if ( $ENV{REPRINT} ) {
160                 unlink $_ foreach glob("out/$nr.*");
161                 warn "REPRINT: $nr";
162         }
163
164         if ( ! ( -e "out/$nr.front.pbm" && -e "out/$nr.back.pbm" ) ) {
165                 print "RENDER @data\n";
166                 system "$evolis_dir/scripts/inkscape-render.pl", "$evolis_dir/card/ffzg-2011.svg", @data;
167         }
168 }
169
170 sub print_card {
171
172         if ( ! @queue ) {
173                 print "QUEUE EMPTY - printing finished\n";
174                 print "$log_path ", -s $log_path, " bytes created\n";
175                 exit;
176         }
177
178         my @data = @{$queue[0]};
179         my $nr = $data[0];
180         print "PRINT @data\n";
181
182         my $p = Printer::EVOLIS::Parallel->new( '/dev/usb/lp0' );
183
184         my $before = _counters $p;
185
186         if ( $test ) {
187
188                 print "insert card ", $p->command( 'Si' ),$/;
189                 sleep 1;
190                 print "eject card ", $p->command( 'Ser' ),$/;
191
192         } else {
193
194                 render_card;
195                 system "$evolis_dir/scripts/evolis-driver.pl out/$nr.front.pbm out/$nr.back.pbm > /dev/usb/lp0";
196
197         }
198
199         my $after = _counters $p;
200
201         if ( $before->{p} = $after->{p} - 2 ) {
202                 print "OK printerd card $nr\n";
203         } else {
204                 die "ERROR printing card $nr\n";
205         }
206
207         warn "# counters ", dump( $before, $after );
208
209 }
210