Merge remote-tracking branch 'mjesec/rfid-vm'
[Biblio-RFID.git] / scripts / RFID-JSONP-server.pl
index 298ff17..c5b07f7 100755 (executable)
@@ -37,6 +37,10 @@ my $sip2 = {
        password => 'viva2koha',
        loc      => 'FFZG',
 };
+my $afi = {
+       secure   => 0xDA,
+       unsecure => 0xD7,
+};
 
 use Getopt::Long;
 
@@ -150,6 +154,9 @@ sub http_server {
        print "Server $0 ready at $server_url\n";
 
        while (my $client = $server->accept()) {
+
+           eval { # don't die inside here!
+
                $client->autoflush(1);
                my $request = <$client>;
 
@@ -192,12 +199,14 @@ sub http_server {
                                foreach my $tag ( @tags ) {
                                        my $hash = $rfid->to_hash( $tag );
                                        $hash->{sid}  = $tag;
+                                       $hash->{reader} = $rfid->from_reader( $tag );
                                        if ( $hash->{tag_type} eq 'SmartX' ) {
                                                my $borrower = rfid_borrower $hash;
                                                if ( exists $borrower->{error} ) {
                                                        warn "ERROR ", dump($borrower);
                                                } else {
                                                        $hash->{borrower} = $borrower->{borrower};
+                                                       $hash->{content}  = $borrower->{borrower}->{cardnumber}; # compatibile with 3M tags
                                                }
                                        } else {
                                                $hash->{security} = uc unpack 'H*', $rfid->afi( $tag );
@@ -220,7 +229,7 @@ sub http_server {
 
                                        warn "PROGRAM $tag $content\n";
                                        $rfid->write_blocks( $tag => $content );
-                                       $rfid->write_afi(    $tag => chr( $param->{$p} =~ /^130/ ? 0xDA : 0xD7 ) );
+                                       $rfid->write_afi(    $tag => chr( $param->{$p} =~ /^130/ ? $afi->{secure} : $afi->{unsecure} ) );
                                }
 
                                print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
@@ -238,7 +247,7 @@ sub http_server {
                                        $status = 302;
 
                                        warn "SECURE $tag $data\n";
-                                       $rfid->write_afi( $tag => hex($data) );
+                                       $rfid->write_afi( $tag => chr(hex($data)) );
                                }
 
                                if ( $json ) {
@@ -263,12 +272,18 @@ sub http_server {
                                        $hash = sip2_message("63000${ts}          AO$loc|AA$patron|AC$password|");
 
                                } elsif ( $method eq 'checkout' ) {
-                                       my ($patron,$barcode) = split(/\//, $args, 2);
+                                       my ($patron,$barcode,$sid) = split(/\//, $args, 3);
                                        $hash = sip2_message("11YN${ts}                  AO$loc|AA$patron|AB$barcode|AC$password|BON|BIN|");
+                                       if ( substr( $hash->{fixed}, 2, 1 ) == 1 ) {
+                                               $rfid->write_afi( $sid => chr( $afi->{unsecure} ) );
+                                       }
 
                                } elsif ( $method eq 'checkin' ) {
-                                       my $barcode = $args;
+                                       my ($patron,$barcode,$sid) = split(/\//, $args, 3);
                                        $hash = sip2_message("09N${ts}${ts}AP|AO${loc}|AB$barcode|AC|BIN|");
+                                       if ( substr( $hash->{fixed}, 2, 1 ) == 1 ) {
+                                               $rfid->write_afi( $sid => chr( $afi->{secure} ) );
+                                       }
                                } else {
                                        print $client "HTTP/1.0 500 $method not implemented\r\n\r\n";
                                }
@@ -285,6 +300,13 @@ sub http_server {
                        print $client "HTTP/1.0 500 No method\r\n\r\n";
                }
                close $client;
+
+           }; # end of eval
+           if ( $@ ) {
+               print $client "HTTP/1.0 500 Error\r\n\r\nContent-Type: text/plain\r\n$@";
+               warn "ERROR: $@";
+           }
+
        }
 
        die "server died";
@@ -295,19 +317,20 @@ sub rfid_register {
 
        foreach ( split(/\n/, `ip addr` ) ) {
                if ( /^\d:\s(\w+):\s/ ) {
-                       $ip->{last} = $1;
+                       $ip->{_last} = $1;
                } elsif ( /^\s+inet\s((\d+)\.(\d+)\.(\d+)\.(\d+))\/(\d+)/ ) {
-                       $ip->{ $ip->{last} } = $1;
+                       $ip->{ $ip->{_last} } = $1;
                } else {
-                       warn "# SKIP [$_]\n";
+                       #warn "# SKIP [$_]\n";
                }
-
        }
 
+       warn dump($ip);
+
        my $ua = LWP::UserAgent->new;
        my $url = URI->new( $rfid_url . '/register.pl');
        $url->query_form(
-               local_ip => $ip->{eth0},
+               local_ip => $ip->{eth0} || $ip->{ (keys %$ip)[0] },
        );
        warn "GET ",$url->as_string;
        my $response = $ua->get($url);