check if reader tcp connection is alive
[Biblio-RFID.git] / scripts / print.pl
index 04abc05..0c5d3f8 100755 (executable)
@@ -6,8 +6,8 @@ use strict;
 use Data::Dump qw(dump);
 use Getopt::Long;
 use lib 'lib';
-use RFID::Biblio::Reader;
-use RFID::Biblio::RFID501;
+use Biblio::RFID::Reader;
+use Biblio::RFID::RFID501;
 use Storable;
 
 my $evolis_dir = '/home/dpavlin/klin/Printer-EVOLIS'; # FIXME
@@ -17,7 +17,7 @@ use Printer::EVOLIS::Parallel;
 my $loop = 1;
 my $reader = '3M';
 my $debug = 0;
-my $afi   = 0x42;
+my $afi   = 0x00; # XXX
 my $test  = 0;
 
 my $log_print = 'log.print';
@@ -32,17 +32,21 @@ GetOptions(
 
 die "Usage: $0 print.txt\n" unless @ARGV;
 
-my $persistant_path = '/tmp/programmed.storable';
 my $programmed;
 my $numbers;
-if ( -e $persistant_path ) {
-       $programmed = retrieve($persistant_path);
-       warn "# loaded ", scalar keys %$programmed, " programmed cards\n";
-       foreach my $tag ( keys %$programmed ) {
-               $numbers->{ $programmed->{$tag} } = $tag;
+foreach my $log_path ( glob( "$log_print/*.txt" ) ) {
+       warn "# loading $log_path";
+       open( my $in, '<', $log_path ) || die $!;
+       while(<$in>) {
+               chomp;
+               my ( $date, $sid, $nr ) = split(/,/,$_,3);
+               $programmed->{ $sid } = $nr;
+               $numbers->{ $nr } = $sid;       
        }
 }
 
+warn "# ", scalar keys %$numbers, " programmed cards found\n";
+
 my @queue;
 my @done;
 warn "# reading tab-delimited input: number login\@domain name surname\n";
@@ -50,13 +54,16 @@ while(<>) {
        chomp;
        my @a = split(/\t/,$_);
        die "invalid: @a in line $_" if $a[0] !~ m/\d{12}/ && $a[1] !~ m/\@/;
-       push @queue, [ @a ] if ! $numbers->{ $a[0] };
+       push @queue, [ @a ] if ! $numbers->{ $a[0] } || $ENV{REPRINT};
 }
 
+# sort by card number
+@queue = sort { $b->[0] <=> $a->[0] } @queue;
+
 print "# queue ", dump @queue;
 
-my $rfid = RFID::Biblio::Reader->new( $reader );
-$RFID::Biblio::debug = $debug;
+my $rfid = Biblio::RFID::Reader->new( $reader );
+$Biblio::RFID::debug = $debug;
 
 sub tag {
        my $tag = shift;
@@ -64,7 +71,7 @@ sub tag {
                , " AFI: "
                , uc unpack('H2', $rfid->afi($tag))
                , " "
-               , dump( RFID::Biblio::RFID501->to_hash( $rfid->blocks($tag) ) )
+               , dump( $rfid->to_hash( $tag ) )
                , $/
                ;
 }
@@ -75,10 +82,19 @@ sub iso_date {
 }
 
 sub print_card;
+sub render_card;
 
 my $log_path = "$log_print/" . iso_date . ".txt";
 die "$log_path exists" if -e $log_path;
-open(my $log, '>', $log_path) || die "$log_path: $!";
+
+sub write_log {
+       my ( $tag, $number ) = @_;
+       open(my $log, '>>', $log_path) || die "$log_path: $!";
+       my $date = iso_date;
+       print $log "$date,$tag,$number\n";
+       close($log);
+       print "LOG $date $tag $number\n";
+}
 
 while ( $rfid->tags ) {
        print "ERROR: remove all tags from output printer tray\n";
@@ -98,13 +114,21 @@ do {
                                my $card = shift @queue;
                                my $number = $card->[0];
                                print "PROGRAM $tag $number\n";
-                               $rfid->write_blocks( $tag => RFID::Biblio::RFID501->from_hash({ content => $number }) );
-                               $rfid->write_afi( $tag => chr($afi) ) if $afi;
 
+                               while ( 1 ) {
+                                       eval {
+                                               $rfid->write_blocks( $tag => Biblio::RFID::RFID501->from_hash({ content => $number }) );
+                                               $rfid->write_afi( $tag => chr($afi) ) if $afi;
+                                       };
+                                       last unless $!;
+                                       warn "RETRY PROGRAM $tag $number\n";
+                                       sleep 1;
+                               }
+
+                               write_log $tag => $number;
                                $programmed->{$tag} = $number;
-                               store $programmed, $persistant_path;
 
-                               print $log iso_date, ",$tag,$number\n";
+                               render_card; # pre-render next one
                        }
 
                },
@@ -120,32 +144,67 @@ do {
        sleep 1;
 } while $loop;
 
+sub _counters {
+       my $p = shift;
+       my $counters;
+       $counters->{$_} = $p->command("Rco;$_") foreach ( qw/p c a m n l b e f i k s/ );
+       return $counters;
+}
+
+sub render_card {
+       return unless @queue;
+       my @data = @{$queue[0]};
+       my $nr = $data[0];
+
+       if ( $ENV{REPRINT} ) {
+               unlink $_ foreach glob("out/$nr.*");
+               warn "REPRINT: $nr";
+       }
+
+       if ( ! ( -e "out/$nr.front.pbm" && -e "out/$nr.back.pbm" ) ) {
+               print "RENDER @data\n";
+               system "$evolis_dir/scripts/inkscape-render.pl", "$evolis_dir/card/ffzg-2011.svg", @data;
+       }
+}
+
 sub print_card {
 
        if ( ! @queue ) {
                print "QUEUE EMPTY - printing finished\n";
-               close($log);
                print "$log_path ", -s $log_path, " bytes created\n";
                exit;
        }
 
        my @data = @{$queue[0]};
-       print "XXX print_card @data\n";
+       my $nr = $data[0];
+       print "PRINT @data\n";
+
+       my $p = Printer::EVOLIS::Parallel->new( '/dev/usb/lp0' );
+
+       my $before = _counters $p;
 
        if ( $test ) {
 
-               my $p = Printer::EVOLIS::Parallel->new( '/dev/usb/lp0' );
                print "insert card ", $p->command( 'Si' ),$/;
                sleep 1;
                print "eject card ", $p->command( 'Ser' ),$/;
 
        } else {
 
-               system "$evolis_dir/scripts/inkscape-render.pl", "$evolis_dir/card/ffzg-2010.svg", @data;
-               my $nr = $data[0];
+               render_card;
                system "$evolis_dir/scripts/evolis-driver.pl out/$nr.front.pbm out/$nr.back.pbm > /dev/usb/lp0";
 
        }
 
+       my $after = _counters $p;
+
+       if ( $before->{p} = $after->{p} - 2 ) {
+               print "OK printerd card $nr\n";
+       } else {
+               die "ERROR printing card $nr\n";
+       }
+
+       warn "# counters ", dump( $before, $after );
+
 }