register RFID reader using intranet address
[Biblio-RFID.git] / scripts / RFID-JSONP-server.pl
index 43a5920..8d44048 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
 
@@ -17,10 +17,16 @@ use Data::Dump qw/dump/;
 
 use JSON::XS;
 use IO::Socket::INET;
+use LWP::UserAgent;
+use URI;
 
 my $debug = 1;
 my $listen = '127.0.0.1:9000';
+$listen = ':9000';
 my $reader;
+my $koha_url = 'http://ffzg.koha-dev.rot13.org:8080';
+# internal URL so we can find local address of machine and vmware NAT
+my $rfid_url = 'http://rfid.koha-dev.vbz.ffzg.hr';
 
 use Getopt::Long;
 
@@ -28,18 +34,44 @@ GetOptions(
        'debug!'    => \$debug,
        'listen=s', => \$listen,
        'reader=s', => \$reader,
+       'koha=s',       => \$koha_url,
 ) || die $!;
 
+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;
+       }
+}
+
 use lib 'lib';
-use RFID::Biblio::RFID501;
-use RFID::Biblio::Readers;
-my $rfid = (RFID::Biblio::Readers->available( $reader ))[0]; # FIXME
-warn "using $rfid reader\n";
+use Biblio::RFID::RFID501;
+use Biblio::RFID::Reader;
+my $rfid = Biblio::RFID::Reader->new( shift @ARGV );
 
 my $index_html;
 {
        local $/ = undef;
        $index_html = <DATA>;
+       $index_html =~ s{http://koha.example.com:8080}{$koha_url}sg;
 }
 
 my $server_url;
@@ -95,12 +127,21 @@ sub http_server {
                                        close($fh);
                                }
                        } elsif ( $method =~ m{/scan} ) {
-                               my $tags = $rfid->scan;
+                               my @tags = $rfid->tags;
                                my $json = { time => time() };
-                               foreach my $tag ( keys %$tags ) {
-                                       my $hash = RFID::Biblio::RFID501->to_hash( $tags->{$tag} );
+                               foreach my $tag ( @tags ) {
+                                       my $hash = $rfid->to_hash( $tag );
                                        $hash->{sid}  = $tag;
-                                       $hash->{security} = uc unpack 'H*', $rfid->read_afi( $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};
+                                               }
+                                       } else {
+                                               $hash->{security} = uc unpack 'H*', $rfid->afi( $tag );
+                                       }
                                        push @{ $json->{tags} }, $hash;
                                };
                                warn "#### ", encode_json($json);
@@ -113,8 +154,8 @@ sub http_server {
                                foreach my $p ( keys %$param ) {
                                        next unless $p =~ m/^(E[0-9A-F]{15})$/;
                                        my $tag = $1;
-                                       my $content = RFID::Biblio::RFID501->from_hash({ content => $param->{$p} });
-                                       $content    = RFID::Biblio::RFID501->blank 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";
@@ -137,7 +178,7 @@ sub http_server {
                                        $status = 302;
 
                                        warn "SECURE $tag $data\n";
-                                       secure_tag_with( $tag, $data );
+                                       $rfid->write_afi( $tag => hex($data) );
                                }
 
                                if ( $json ) {
@@ -159,6 +200,38 @@ sub http_server {
        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";
+               }
+
+       }
+
+       my $ua = LWP::UserAgent->new;
+       my $url = URI->new( $rfid_url . '/register.pl');
+       $url->query_form(
+               local_ip => $ip->{eth0},
+       );
+       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;
 http_server;
 
 __DATA__
@@ -204,15 +277,25 @@ function got_visible_tags(data,textStatus) {
                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">'