read programmed cards directly from log
[Biblio-RFID.git] / scripts / print.pl
index f067038..3bb7721 100755 (executable)
@@ -6,38 +6,64 @@ 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
 use lib '/home/dpavlin/klin/Printer-EVOLIS/lib';
 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';
+mkdir $log_print unless -d $log_print;
 
 GetOptions(
        'loop!'     => \$loop,
        'reader=s', => \$reader,
        'debug+'    => \$debug,
+       'test+'     => \$test,
 ) || die $!;
 
 die "Usage: $0 print.txt\n" unless @ARGV;
 
+my $programmed;
+my $numbers;
+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\n";
+warn "# reading tab-delimited input: number login\@domain name surname\n";
 while(<>) {
        chomp;
        my @a = split(/\t/,$_);
-       push @queue, [ @a ];
+       die "invalid: @a in line $_" if $a[0] !~ m/\d{12}/ && $a[1] !~ m/\@/;
+       push @queue, [ @a ] if ! $numbers->{ $a[0] };
 }
 
+# sort by card number
+@queue = sort { $a->[0] <=> $b->[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;
@@ -45,13 +71,22 @@ sub tag {
                , " AFI: "
                , uc unpack('H2', $rfid->afi($tag))
                , " "
-               , dump( RFID::Biblio::RFID501->to_hash( $rfid->blocks($tag) ) )
+               , dump( Biblio::RFID::RFID501->to_hash( $rfid->blocks($tag) ) )
                , $/
                ;
 }
 
+sub iso_date {
+       my @t = localtime(time);
+       return sprintf "%04d-%02d-%02dT%02d:%02d:%02d", $t[5]+1900,$t[4]+1,$t[3],$t[2],$t[1],$t[0];
+}
+
 sub print_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: $!";
+
 while ( $rfid->tags ) {
        print "ERROR: remove all tags from output printer tray\n";
        sleep 1;
@@ -59,8 +94,6 @@ while ( $rfid->tags ) {
 
 print_card;
 
-my $programmed;
-
 do {
        my @visible = $rfid->tags(
                enter => sub {
@@ -72,10 +105,12 @@ 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_blocks( $tag => Biblio::RFID::RFID501->from_hash({ content => $number }) );
                                $rfid->write_afi( $tag => chr($afi) ) if $afi;
 
                                $programmed->{$tag} = $number;
+
+                               print $log iso_date, ",$tag,$number\n";
                        }
 
                },
@@ -91,13 +126,52 @@ 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 print_card {
 
-       print "XXX print_card @{$queue[0]}\n";
+       if ( ! @queue ) {
+               print "QUEUE EMPTY - printing finished\n";
+               close($log);
+               print "$log_path ", -s $log_path, " bytes created\n";
+               exit;
+       }
+
+       my @data = @{$queue[0]};
+       my $nr = $data[0];
+       print "PRINT @data\n";
 
        my $p = Printer::EVOLIS::Parallel->new( '/dev/usb/lp0' );
-       print "insert card ", $p->command( 'Si' ),$/;
-       sleep 1;
-       print "eject card ", $p->command( 'Ser' ),$/;
+
+       my $before = _counters $p;
+
+       if ( $test ) {
+
+               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;
+               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 );
+
 }