push more variables to koha
[Biblio-RFID.git] / scripts / RFID-JSONP-server.pl
index 9f1e3d6..06576d2 100755 (executable)
@@ -1,12 +1,12 @@
 #!/usr/bin/perl
 
-=head1 RFID-JSONP-server
+=head1 NAME
 
-This is simpliest possible JSONP server which provides local web interface to RFID readers
+RFID-JSONP-server - simpliest possible JSONP server which provides local web interface to RFID readers
 
-Usage:
+=head1 USAGE
 
-  ./scripts/RFID-JSONP-server.pl
+  ./scripts/RFID-JSONP-server.pl [--debug] [--listen=127.0.0.1:9000] [--reader=filter]
 
 =cut
 
@@ -15,50 +15,176 @@ use warnings;
 
 use Data::Dump qw/dump/;
 
-use JSON;
+use JSON::XS;
 use IO::Socket::INET;
+use LWP::UserAgent;
+use URI;
+use URI::Escape;
+use POSIX qw(strftime);
+use Encode;
+
+my $debug = 0;
+my $listen = $ENV{HTTP_LISTEN} || 'localhost:9000';
+my $reader;
+my $koha_url = $ENV{KOHA_URL};
+warn "$koha_url";
+# internal URL so we can find local address of machine and vmware NAT
+my $rfid_url = $ENV{RFID_URL};
+my $sip2 = {
+       server   => $ENV{SIP2_SERVER}, # '10.60.0.11:6002' must be IP!
+       user     => $ENV{SIP2_USER},
+       password => $ENV{SIP2_PASSWORD},
+       loc      => $ENV{SIP2_LOC},
+};
+my $afi = {
+       secure   => 0xDA,
+       unsecure => 0xD7,
+};
+
+use Getopt::Long;
+
+GetOptions(
+       'debug!'    => \$debug,
+       'listen=s', => \$listen,
+       'reader=s', => \$reader,
+) || die $!;
+
+die "need KOHA_URL, eg. http://ffzg.koha-dev.rot13.org:8080" unless $koha_url;
+
+our $rfid_sid_cache;
+
+sub rfid_borrower {
+       my $hash = shift;
+       if ( my $json = $rfid_sid_cache->{ $hash->{sid} } ) {
+               return $json;
+       }
+       my $ua = LWP::UserAgent->new;
+       my $url = URI->new( $koha_url . '/cgi-bin/koha/ffzg/rfid/borrower.pl');
+       $url->query_form(
+                 RFID_SID => $hash->{sid}
+               , OIB => $hash->{OIB}
+               , JMBAG => $hash->{JMBAG}
+       );
+       warn "GET ",$url->as_string;
+       my $response = $ua->get($url);
+       if ( $response->is_success ) {
+               my $json = decode_json $response->decoded_content;
+               $rfid_sid_cache->{ $hash->{sid} } = $json;
+               return $json;
+       } else {
+               warn "ERROR ", $response->status_line;
+       }
+}
+
+
+sub sip2_socket {
+
+       return $sip2->{sock} if exists $sip2->{sock} && $sip2->{sock}->connected;
+
+       if ( my $server = $sip2->{server} ) {
+               my $sock = $sip2->{sock} = IO::Socket::INET->new( $server ) || die "can't connect to $server: $!";
+               warn "SIP2 server ", $sock->peerhost, ":", $sock->peerport, "\n";
+
+               # login
+               if ( sip2_message("9300CN$sip2->{user}|CO$sip2->{password}|")->{fixed} !~ m/^941/ ) {
+                       die "SIP2 login failed";
+               }
+
+       }
+       return $sip2->{sock};
+}
+
+sub sip2_message {
+       my $send = shift;
+
+       my $retry = 0;
+
+send_again:
+       my $sock = sip2_socket || die "no sip2 socket";
+
+       local $/ = "\r";
+
+       $send .= "\r" unless $send =~ m/\r$/;
+       $send .= "\n" unless $send =~ m/\n$/;
+
+       warn "SIP2 >>>> ",dump($send), "\n";
+       print $sock $send;
+       $sock->flush;
+       
+       my $expect = substr($send,0,2) | 0x01;
+
+       my $in = <$sock>;
+       warn "SIP2 <<<< ",dump($in), "\n";
+
+       $in =~ s/^\n//;
+       $in =~ s/\r$//;
+
+       if ( ! $in ) {
+               $retry++;
+               warn "empty read from SIP server, retry: $retry\n";
+               if ( $retry < 10 ) {
+                       close( $sip2->{sock} );
+                       goto send_again;
+               }
+               die "aborted";
+       }
+
+
+       die "expected $expect" unless substr($in,0,2) != $expect;
+
+       my $hash;
+       if ( $in =~ s/^([0-9\s]+)// ) {
+               $hash->{fixed} = $1;
+       }
+       foreach ( split(/\|/, $in ) ) {
+               my ( $f, $v ) = ( $1, $2 ) if m/([A-Z]{2})(.+)/;
+               $hash->{$f} = decode('utf-8',$v);
+       }
 
-my $debug = 1;
+       warn "# sip2 hash response ",dump($hash);
 
-my $listen_port = 9000;                  # pick something not in use
-my $server_url  = "http://localhost:$listen_port";
+       return $hash;
+}
 
 
 use lib 'lib';
-use RFID::Serial::3M810;
-my $rfid = RFID::Serial::3M810->new;
+use Biblio::RFID::RFID501;
+use Biblio::RFID::Reader;
+my $rfid = Biblio::RFID::Reader->new( shift @ARGV );
+$rfid->debug( $debug );
+
+my $index_html;
+{
+       local $/ = undef;
+       $index_html = <DATA>;
+       $index_html =~ s{http://koha.example.com:8080}{$koha_url}sg;
+}
+
+my $server_url;
 
 sub http_server {
 
        my $server = IO::Socket::INET->new(
                Proto     => 'tcp',
-               LocalPort => $listen_port,
+               LocalAddr => $listen,
                Listen    => SOMAXCONN,
                Reuse     => 1
        );
                                                                  
        die "can't setup server: $!" unless $server;
 
+       $server_url = 'http://' . $listen;
        print "Server $0 ready at $server_url\n";
 
-       sub static {
-               my ($client,$path) = @_;
-
-               return unless $path eq '/';
-
-               print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";
-               while(<DATA>) {
-                       print $client $_;
-               }
+       while (my $client = $server->accept()) {
 
-               return $path;
-       }
+           eval { # don't die inside here!
 
-       while (my $client = $server->accept()) {
                $client->autoflush(1);
                my $request = <$client>;
 
                warn "WEB << $request\n" if $debug;
+               my $path;
 
                if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
                        my $method = $1;
@@ -70,16 +196,57 @@ sub http_server {
                                }
                                warn "WEB << param: ",dump( $param ) if $debug;
                        }
-                       if ( my $path = static( $client,$1 ) ) {
-                               warn "WEB >> $path" if $debug;
-                       } elsif ( $method =~ m{/scan} ) {
-                               my $tags = $rfid->scan;
-                               my $json = {
-                                       time => time(),
-                                       tags => $tags,
+                       $path = $method;
+
+                       if ( $path eq '/' ) {
+                               print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n$index_html";
+                       } elsif ( $path =~ m{^/(examples/.+)} ) {
+                               $path = $1; # FIXME prefix with dir for installation
+                               my $size = -s $path;
+                               warn "static $path $size bytes\n";
+                               my $content_type = 'text/plain';
+                               $content_type = 'application/javascript' if $path =~ /\.js$/;
+                               $content_type = 'text/html' if $path =~ /\.html$/;
+                               print $client "HTTP/1.0 200 OK\r\nContent-Type: $content_type\r\nContent-Length: $size\r\n\r\n";
+                               {
+                                       local $/ = undef;
+                                       open(my $fh, '<', $path) || die "can't open $path: $!";
+                                       while(<$fh>) {
+                                               print $client $_;
+                                       }
+                                       close($fh);
+                               }
+                       } elsif ( $method =~ m{/scan(/only/(.+))?} ) {
+                               my $only = $2;
+                               my @tags = $rfid->tags( reader => sub {
+                                       my $reader = shift;
+                                       return 1 unless $only;
+                                       if ( ref($reader) =~ m/$only/i ) {
+                                               return 1;
+                                       }
+                                       return 0;
+                               });
+                               my $json = { time => time() };
+                               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 );
+                                       }
+                                       push @{ $json->{tags} }, $hash;
                                };
+                               warn "#### ", encode_json($json);
                                print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
-                                       $param->{callback}, "(", to_json($json), ")\r\n";
+                                       $param->{callback}, "(", encode_json($json), ")\r\n";
                        } elsif ( $method =~ m{/program} ) {
 
                                my $status = 501; # Not implementd
@@ -87,13 +254,13 @@ sub http_server {
                                foreach my $p ( keys %$param ) {
                                        next unless $p =~ m/^(E[0-9A-F]{15})$/;
                                        my $tag = $1;
-                                       my $content = "\x04\x11\x00\x01" . $param->{$p};
-                                       $content = "\x00" if $param->{$p} eq 'blank';
+                                       my $content = Biblio::RFID::RFID501->from_hash({ content => $param->{$p} });
+                                       $content    = Biblio::RFID::RFID501->blank if $param->{$p} eq 'blank';
                                        $status = 302;
 
                                        warn "PROGRAM $tag $content\n";
-                                       write_tag( $tag, $content );
-                                       secure_tag_with( $tag, $param->{$p} =~ /^130/ ? 'DA' : 'D7' );
+                                       $rfid->write_blocks( $tag => $content );
+                                       $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";
@@ -111,7 +278,7 @@ sub http_server {
                                        $status = 302;
 
                                        warn "SECURE $tag $data\n";
-                                       secure_tag_with( $tag, $data );
+                                       $rfid->write_afi( $tag => chr(hex($data)) );
                                }
 
                                if ( $json ) {
@@ -121,24 +288,111 @@ sub http_server {
                                        print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
                                }
 
+                       } elsif ( $method =~ m{/sip2/(\w+)/(.+)} ) {
+                               my ( $method, $args ) = ( $1, $2 );
+                               warn "SIP2: $method [$args]";
+
+                               my $ts = strftime('%Y%m%d    %H%M%S', localtime());
+                               my $loc      = $sip2->{loc} || die "missing sip->{loc}";
+                               my $password = $sip2->{password} || die "missing sip->{password}";
+
+                               my $hash;
+
+                               if ( $method eq 'patron_info' ) {
+                                       my $patron = $args;
+                                       $hash = sip2_message("63000${ts}          AO$loc|AA$patron|AC$password|");
+
+                               } elsif ( $method eq 'checkout' ) {
+                                       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 ($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 501 $method not implemented\r\n\r\n";
+                                       warn "ERROR 501 $request\n";
+                               }
+
+                               if ( $hash ) {
+                                       print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
+                                               encode_json( $hash );
+                               }
+
+                       } elsif ( $method =~ m{/beep/(.*)} ) {
+                               my $error = uri_unescape($1);
+                               system "beep -f 800 -r 2 -l 100";
+                               print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n{ beep: '$error' }\n";
+                               print "BEEP $error\n";
                        } else {
                                print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
+                               warn "ERROR 404 $request\n";
                        }
                } else {
                        print $client "HTTP/1.0 500 No method\r\n\r\n";
+                       warn "ERROR 500 $request\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";
 }
 
+sub rfid_register {
+       my $ip;
+
+       foreach ( split(/\n/, `ip addr` ) ) {
+               if ( /^\d:\s(\w+):\s/ ) {
+                       $ip->{_last} = $1;
+               } elsif ( /^\s+inet\s((\d+)\.(\d+)\.(\d+)\.(\d+))\/(\d+)/ ) {
+                       $ip->{ $ip->{_last} } = $1;
+               } else {
+                       #warn "# SKIP [$_]\n";
+               }
+       }
+
+       warn dump($ip);
+
+       my $ua = LWP::UserAgent->new;
+       my $url = URI->new( $rfid_url . '/register.pl');
+       $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);
+       if ( $response->is_success ) {
+               warn "# ", $response->decoded_content;
+               my $json = decode_json $response->decoded_content;
+               warn "REGISTER: ",dump($json);
+               return $json;
+       } else {
+               warn "ERROR ", $response->status_line;
+       }
+}
+
+rfid_register if $rfid_url;
 http_server;
 
 __DATA__
 <html>
 <head>
-<title>3M RFID</title>
+<title>RFID JSONP</title>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
 <style type="text/css">
 .status {
@@ -163,21 +417,40 @@ label[for=pull-reader] {
 </style>
 <script type="text/javascript">
 
+// mock console
+if(!window.console) {
+       window.console = new function() {
+               this.info = function(str) {};
+               this.debug = function(str) {};
+       };
+}
+
+
 function got_visible_tags(data,textStatus) {
        var html = 'No tags in range';
        if ( data.tags ) {
                html = '<ul class="tags">';
                $.each(data.tags, function(i,tag) {
                        console.debug( i, tag );
-                       html += '<li><tt class=' + tag.security + '>' + tag.sid;
-                       if ( tag.content ) {
-                               html += ' <a href="https://koha-dev.rot13.org:8443/cgi-bin/koha/members/member.pl?member=' + tag.content + '" title="lookup in Koha" target="koha-lookup">' + tag.content + '</a>';
+                       html += '<li><tt class="' + tag.security + '">' + tag.sid;
+                       var content = tag.content || tag.borrower.cardnumber;
+
+                       if ( content ) {
+                               html += ' <a href="http://koha.example.com:8080/cgi-bin/koha/';
+                               if ( tag.type == 1 ) { // book
+                                       html += 'catalogue/search.pl?q=';
+                               } else {
+                                       html += 'members/member.pl?member=';
+                               }
+                               html += content + '" title="lookup in Koha" target="koha-lookup">' + content + '</a>';
                                html += '</tt>';
+/*
                                html += '<form method=get action=program style="display:inline">'
                                        + '<input type=hidden name='+tag.sid+' value="blank">'
                                        + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
                                        + '</form>'
                                ;
+*/
                        } else {
                                html += '</tt>';
                                html += ' <form method=get action=program style="display:inline">'
@@ -208,7 +481,7 @@ function got_visible_tags(data,textStatus) {
 function scan_tags() {
        console.info('scan_tags');
        if ( $('input#pull-reader').attr('checked') )
-               $.getJSON("http://localhost:9000/scan?callback=?", got_visible_tags);
+               $.getJSON("/scan?callback=?", got_visible_tags);
 }
 
 $(document).ready(function() {