create print log for import back in Koha
[Biblio-RFID.git] / scripts / print.pl
index 3457531..04abc05 100755 (executable)
@@ -20,6 +20,9 @@ my $debug = 0;
 my $afi   = 0x42;
 my $test  = 0;
 
+my $log_print = 'log.print';
+mkdir $log_print unless -d $log_print;
+
 GetOptions(
        'loop!'     => \$loop,
        'reader=s', => \$reader,
@@ -29,13 +32,25 @@ 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;
+       }
+}
+
 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] };
 }
 
 print "# queue ", dump @queue;
@@ -54,8 +69,17 @@ sub 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;
@@ -63,13 +87,6 @@ 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 {
@@ -86,6 +103,8 @@ do {
 
                                $programmed->{$tag} = $number;
                                store $programmed, $persistant_path;
+
+                               print $log iso_date, ",$tag,$number\n";
                        }
 
                },
@@ -105,6 +124,8 @@ sub print_card {
 
        if ( ! @queue ) {
                print "QUEUE EMPTY - printing finished\n";
+               close($log);
+               print "$log_path ", -s $log_path, " bytes created\n";
                exit;
        }