X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=scripts%2FRFID-JSONP-server.pl;h=215d6cafa94fe3630131469bf3b95b42772498b2;hb=201b8a9ac971a3039f101ed0bbb87a6ceec78f91;hp=298ff177aac5134c1ddcf7e7b1c0d21d7a99f541;hpb=0322ec7c429d2abc2020326e0ca09225f75be478;p=Biblio-RFID.git diff --git a/scripts/RFID-JSONP-server.pl b/scripts/RFID-JSONP-server.pl index 298ff17..215d6ca 100755 --- a/scripts/RFID-JSONP-server.pl +++ b/scripts/RFID-JSONP-server.pl @@ -19,23 +19,27 @@ use JSON::XS; use IO::Socket::INET; use LWP::UserAgent; use URI; +use URI::Escape; use POSIX qw(strftime); use Encode; -my $debug = 1; +my $debug = 0; my $listen = '127.0.0.1:9000'; $listen = ':9000'; my $reader; -my $koha_url = 'http://ffzg.koha-dev.rot13.org:8080'; +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 = 'http://rfid.koha-dev.vbz.ffzg.hr'; +my $rfid_url = $ENV{RFID_URL}; my $sip2 = { - server => '10.60.0.11:6002', # must be IP! -# user => 'sip2-user', -# password => 'sip2-passwd', - user => 'sip2user', - password => 'viva2koha', - loc => 'FFZG', + 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; @@ -44,9 +48,10 @@ GetOptions( 'debug!' => \$debug, 'listen=s', => \$listen, 'reader=s', => \$reader, - 'koha=s', => \$koha_url, ) || die $!; +die "need KOHA_URL, eg. http://ffzg.koha-dev.rot13.org:8080" unless $koha_url; + our $rfid_sid_cache; sub rfid_borrower { @@ -73,14 +78,36 @@ sub rfid_borrower { } +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 $sock = $sip2->{sock} || die "no sip2 socket"; + 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; @@ -88,13 +115,24 @@ sub sip2_message { my $expect = substr($send,0,2) | 0x01; my $in = <$sock>; - $in =~ s/^\n//; warn "SIP2 <<<< ",dump($in), "\n"; - die "expected $expect" unless substr($in,0,2) != $expect; - + $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; @@ -109,16 +147,6 @@ sub sip2_message { return $hash; } -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"; - } - -} use lib 'lib'; use Biblio::RFID::RFID501; @@ -150,6 +178,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>; @@ -186,18 +217,28 @@ sub http_server { } close($fh); } - } elsif ( $method =~ m{/scan} ) { - my @tags = $rfid->tags; + } 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 ); @@ -220,7 +261,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 +279,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,14 +304,21 @@ 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"; + print $client "HTTP/1.0 501 $method not implemented\r\n\r\n"; + warn "ERROR 501 $request\n"; } if ( $hash ) { @@ -278,13 +326,27 @@ sub http_server { 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"; @@ -295,19 +357,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); @@ -321,7 +384,7 @@ sub rfid_register { } } -rfid_register; +rfid_register if $rfid_url; http_server; __DATA__