store printed cards in /tmp/programmed.storable
[Biblio-RFID.git] / scripts / print.pl
index a09e0f7..9badbfe 100755 (executable)
@@ -8,6 +8,7 @@ use Getopt::Long;
 use lib 'lib';
 use RFID::Biblio::Reader;
 use RFID::Biblio::RFID501;
+use Storable;
 
 use lib '/home/dpavlin/klin/Printer-EVOLIS/lib';
 use Printer::EVOLIS::Parallel;
@@ -59,22 +60,36 @@ while ( $rfid->tags ) {
 
 print_card;
 
+my $persistant_path = '/tmp/programmed.storable';
+my $programmed;
+if ( -e $persistant_path ) {
+       $programmed = retrieve($persistant_path);
+       warn "# loaded ", scalar keys %$programmed, " programmed cards\n";
+}
+
 do {
        my @visible = $rfid->tags(
                enter => sub {
                        my $tag = shift;
-                       print localtime()." enter ", tag($tag);
+                       print localtime()." enter ", eval { tag($tag) };
+                       return if $@;
 
-                       my $card = shift @queue;
-                       $rfid->write_blocks( $tag => RFID::Biblio::RFID501->from_hash({ content => $card->[0] }) );
-                       $rfid->write_afi(    $tag => chr($afi) ) if $afi;
+                       if ( ! $programmed->{$tag} ) {
+                               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;
+
+                               $programmed->{$tag} = $number;
+                               store $programmed, $persistant_path;
+                       }
 
                },
                leave => sub {
                        my $tag = shift;
-                       print localtime()." leave ", tag($tag);
 
-                       print_card;
+                       print_card if $programmed->{$tag};
                },
        );
 
@@ -89,6 +104,7 @@ sub print_card {
 
        my $p = Printer::EVOLIS::Parallel->new( '/dev/usb/lp0' );
        print "insert card ", $p->command( 'Si' ),$/;
+       sleep 1;
        print "eject card ", $p->command( 'Ser' ),$/;
 }