X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=scripts%2FRFID-JSONP-server.pl;h=974a316911fe33f89002d566d59eabcc3ab35027;hb=60d7c29357435918e04d82436fd64c4212934ed1;hp=37dc0dc70f70aa44d499ef8997182e6c1a9d60b3;hpb=dd0ff7b7d37f0dcf436cd6921f8ffcaa16332a24;p=Biblio-RFID.git diff --git a/scripts/RFID-JSONP-server.pl b/scripts/RFID-JSONP-server.pl index 37dc0dc..974a316 100755 --- a/scripts/RFID-JSONP-server.pl +++ b/scripts/RFID-JSONP-server.pl @@ -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,38 +17,83 @@ use Data::Dump qw/dump/; use JSON::XS; use IO::Socket::INET; +use LWP::UserAgent; +use URI; my $debug = 1; - -my $listen_port = 9000; # pick something not in use -my $server_url = "http://localhost:$listen_port"; - +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; + +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::Decode::RFID501; -use RFID::Biblio::3M810; -my $rfid = RFID::Biblio::3M810->new; +use Biblio::RFID::RFID501; +use Biblio::RFID::Reader; +my $rfid = Biblio::RFID::Reader->new( shift @ARGV ); my $index_html; { local $/ = undef; $index_html = ; + $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"; while (my $client = $server->accept()) { + + eval { # don't die inside here! + $client->autoflush(1); my $request = <$client>; @@ -69,13 +114,38 @@ sub http_server { 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/; + 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} ) { - my $tags = $rfid->scan; + my @tags = $rfid->tags; my $json = { time => time() }; - foreach my $tag ( keys %$tags ) { - my $hash = RFID::Biblio::Decode::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}; + $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); @@ -88,13 +158,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/ ? 0xDA : 0xD7 ) ); } print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n"; @@ -112,7 +182,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 ) { @@ -129,11 +199,49 @@ sub http_server { print $client "HTTP/1.0 500 No method\r\n\r\n"; } close $client; + + }; # end of eval + if ( $@ ) { + 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"; + } + + } + + 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__ @@ -179,15 +287,25 @@ function got_visible_tags(data,textStatus) { html = '