fix warning in SIP messages decode
[Biblio-RFID.git] / scripts / RFID-JSONP-server.pl
index 46abca9..f29ada0 100755 (executable)
@@ -24,8 +24,7 @@ use POSIX qw(strftime);
 use Encode;
 
 my $debug = 0;
-my $listen = '127.0.0.1:9000';
-$listen = ':9000';
+my $listen = $ENV{HTTP_LISTEN} || 'localhost:9000';
 my $reader;
 my $koha_url = $ENV{KOHA_URL};
 warn "$koha_url";
@@ -98,6 +97,8 @@ sub sip2_socket {
 sub sip2_message {
        my $send = shift;
 
+       my $retry = 0;
+
 send_again:
        my $sock = sip2_socket || die "no sip2 socket";
 
@@ -106,8 +107,6 @@ send_again:
        $send .= "\r" unless $send =~ m/\r$/;
        $send .= "\n" unless $send =~ m/\n$/;
 
-
-       my $retry = 0;
        warn "SIP2 >>>> ",dump($send), "\n";
        print $sock $send;
        $sock->flush;
@@ -134,12 +133,12 @@ send_again:
        die "expected $expect" unless substr($in,0,2) != $expect;
 
        my $hash;
-       if ( $in =~ s/^([0-9\s]+)// ) {
+       if ( $in =~ s/^([0-9\s\w]+)// ) {
                $hash->{fixed} = $1;
        }
        foreach ( split(/\|/, $in ) ) {
                my ( $f, $v ) = ( $1, $2 ) if m/([A-Z]{2})(.+)/;
-               $hash->{$f} = decode('utf-8',$v);
+               $hash->{$f} = decode('utf-8',$v) if $f;
        }
 
        warn "# sip2 hash response ",dump($hash);
@@ -217,6 +216,7 @@ sub http_server {
                                        }
                                        close($fh);
                                }
+                               $rfid_sid_cache = undef if $path eq 'examples/selfcheck.html'; # invalidate on reload
                        } elsif ( $method =~ m{/scan(/only/(.+))?} ) {
                                my $only = $2;
                                my @tags = $rfid->tags( reader => sub {
@@ -236,6 +236,7 @@ sub http_server {
                                                my $borrower = rfid_borrower $hash;
                                                if ( exists $borrower->{error} ) {
                                                        warn "ERROR ", dump($borrower);
+                                                       $hash->{error} = $borrower->{error};
                                                } else {
                                                        $hash->{borrower} = $borrower->{borrower};
                                                        $hash->{content}  = $borrower->{borrower}->{cardnumber}; # compatibile with 3M tags
@@ -369,8 +370,11 @@ sub rfid_register {
 
        my $ua = LWP::UserAgent->new;
        my $url = URI->new( $rfid_url . '/register.pl');
-       $url->query_form(
-               local_ip => $ip->{eth0} || $ip->{ (keys %$ip)[0] },
+       $url->query_form( %$ip,
+               HTTP_LISTEN => $listen,
+               RFID_LISTEN => $ENV{RFID_LISTEN},
+               KOHA_URL => $koha_url,
+               RFID_URL => $rfid_url,
        );
        warn "GET ",$url->as_string;
        my $response = $ua->get($url);