5 RFID-JSONP-server - simpliest possible JSONP server which provides local web interface to RFID readers
9 ./scripts/RFID-JSONP-server.pl [--debug] [--listen=127.0.0.1:9000] [--reader=filter]
16 use Data::Dump qw/dump/;
23 use POSIX qw(strftime);
27 my $listen = '127.0.0.1:9000';
30 my $koha_url = $ENV{KOHA_URL};
32 # internal URL so we can find local address of machine and vmware NAT
33 my $rfid_url = $ENV{RFID_URL};
35 server => $ENV{SIP2_SERVER}, # '10.60.0.11:6002' must be IP!
36 user => $ENV{SIP2_USER},
37 password => $ENV{SIP2_PASSWORD},
38 loc => $ENV{SIP2_LOC},
49 'listen=s', => \$listen,
50 'reader=s', => \$reader,
53 die "need KOHA_URL, eg. http://ffzg.koha-dev.rot13.org:8080" unless $koha_url;
59 if ( my $json = $rfid_sid_cache->{ $hash->{sid} } ) {
62 my $ua = LWP::UserAgent->new;
63 my $url = URI->new( $koha_url . '/cgi-bin/koha/ffzg/rfid/borrower.pl');
65 RFID_SID => $hash->{sid}
67 , JMBAG => $hash->{JMBAG}
69 warn "GET ",$url->as_string;
70 my $response = $ua->get($url);
71 if ( $response->is_success ) {
72 my $json = decode_json $response->decoded_content;
73 $rfid_sid_cache->{ $hash->{sid} } = $json;
76 warn "ERROR ", $response->status_line;
84 my $sock = $sip2->{sock} || die "no sip2 socket";
88 $send .= "\r" unless $send =~ m/\r$/;
89 warn "SIP2 >>>> ",dump($send), "\n";
93 my $expect = substr($send,0,2) | 0x01;
97 while ( $in eq '' && $repeat < 10 ) {
101 warn "SIP2 <<<< ",dump($in), " repeat: $repeat\n";
105 die "expected $expect" unless substr($in,0,2) != $expect;
108 if ( $in =~ s/^([0-9\s]+)// ) {
111 foreach ( split(/\|/, $in ) ) {
112 my ( $f, $v ) = ( $1, $2 ) if m/([A-Z]{2})(.+)/;
113 $hash->{$f} = decode('utf-8',$v);
116 warn "# sip2 hash response ",dump($hash);
121 if ( my $server = $sip2->{server} ) {
122 my $sock = $sip2->{sock} = IO::Socket::INET->new( $server ) || die "can't connect to $server: $!";
123 warn "SIP2 server ", $sock->peerhost, ":", $sock->peerport, "\n";
126 if ( sip2_message("9300CN$sip2->{user}|CO$sip2->{password}|")->{fixed} !~ m/^941/ ) {
127 die "SIP2 login failed";
133 use Biblio::RFID::RFID501;
134 use Biblio::RFID::Reader;
135 my $rfid = Biblio::RFID::Reader->new( shift @ARGV );
136 $rfid->debug( $debug );
141 $index_html = <DATA>;
142 $index_html =~ s{http://koha.example.com:8080}{$koha_url}sg;
149 my $server = IO::Socket::INET->new(
151 LocalAddr => $listen,
156 die "can't setup server: $!" unless $server;
158 $server_url = 'http://' . $listen;
159 print "Server $0 ready at $server_url\n";
161 while (my $client = $server->accept()) {
163 eval { # don't die inside here!
165 $client->autoflush(1);
166 my $request = <$client>;
168 warn "WEB << $request\n" if $debug;
171 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
174 if ( $method =~ s{\?(.+)}{} ) {
175 foreach my $p ( split(/[&;]/, $1) ) {
176 my ($n,$v) = split(/=/, $p, 2);
179 warn "WEB << param: ",dump( $param ) if $debug;
183 if ( $path eq '/' ) {
184 print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n$index_html";
185 } elsif ( $path =~ m{^/(examples/.+)} ) {
186 $path = $1; # FIXME prefix with dir for installation
188 warn "static $path $size bytes\n";
189 my $content_type = 'text/plain';
190 $content_type = 'application/javascript' if $path =~ /\.js$/;
191 $content_type = 'text/html' if $path =~ /\.html$/;
192 print $client "HTTP/1.0 200 OK\r\nContent-Type: $content_type\r\nContent-Length: $size\r\n\r\n";
195 open(my $fh, '<', $path) || die "can't open $path: $!";
201 } elsif ( $method =~ m{/scan(/only/(.+))?} ) {
203 my @tags = $rfid->tags( reader => sub {
205 return 1 unless $only;
206 if ( ref($reader) =~ m/$only/i ) {
211 my $json = { time => time() };
212 foreach my $tag ( @tags ) {
213 my $hash = $rfid->to_hash( $tag );
215 $hash->{reader} = $rfid->from_reader( $tag );
216 if ( $hash->{tag_type} eq 'SmartX' ) {
217 my $borrower = rfid_borrower $hash;
218 if ( exists $borrower->{error} ) {
219 warn "ERROR ", dump($borrower);
221 $hash->{borrower} = $borrower->{borrower};
222 $hash->{content} = $borrower->{borrower}->{cardnumber}; # compatibile with 3M tags
225 $hash->{security} = uc unpack 'H*', $rfid->afi( $tag );
227 push @{ $json->{tags} }, $hash;
229 warn "#### ", encode_json($json);
230 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
231 $param->{callback}, "(", encode_json($json), ")\r\n";
232 } elsif ( $method =~ m{/program} ) {
234 my $status = 501; # Not implementd
236 foreach my $p ( keys %$param ) {
237 next unless $p =~ m/^(E[0-9A-F]{15})$/;
239 my $content = Biblio::RFID::RFID501->from_hash({ content => $param->{$p} });
240 $content = Biblio::RFID::RFID501->blank if $param->{$p} eq 'blank';
243 warn "PROGRAM $tag $content\n";
244 $rfid->write_blocks( $tag => $content );
245 $rfid->write_afi( $tag => chr( $param->{$p} =~ /^130/ ? $afi->{secure} : $afi->{unsecure} ) );
248 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
250 } elsif ( $method =~ m{/secure(.js)} ) {
254 my $status = 501; # Not implementd
256 foreach my $p ( keys %$param ) {
257 next unless $p =~ m/^(E[0-9A-F]{15})$/;
259 my $data = $param->{$p};
262 warn "SECURE $tag $data\n";
263 $rfid->write_afi( $tag => chr(hex($data)) );
267 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
268 $param->{callback}, "({ ok: 1 })\r\n";
270 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
273 } elsif ( $method =~ m{/sip2/(\w+)/(.+)} ) {
274 my ( $method, $args ) = ( $1, $2 );
275 warn "SIP2: $method [$args]";
277 my $ts = strftime('%Y%m%d %H%M%S', localtime());
278 my $loc = $sip2->{loc} || die "missing sip->{loc}";
279 my $password = $sip2->{password} || die "missing sip->{password}";
283 if ( $method eq 'patron_info' ) {
285 $hash = sip2_message("63000${ts} AO$loc|AA$patron|AC$password|");
287 } elsif ( $method eq 'checkout' ) {
288 my ($patron,$barcode,$sid) = split(/\//, $args, 3);
289 $hash = sip2_message("11YN${ts} AO$loc|AA$patron|AB$barcode|AC$password|BON|BIN|");
290 if ( substr( $hash->{fixed}, 2, 1 ) == 1 ) {
291 $rfid->write_afi( $sid => chr( $afi->{unsecure} ) );
294 } elsif ( $method eq 'checkin' ) {
295 my ($patron,$barcode,$sid) = split(/\//, $args, 3);
296 $hash = sip2_message("09N${ts}${ts}AP|AO${loc}|AB$barcode|AC|BIN|");
297 if ( substr( $hash->{fixed}, 2, 1 ) == 1 ) {
298 $rfid->write_afi( $sid => chr( $afi->{secure} ) );
301 print $client "HTTP/1.0 501 $method not implemented\r\n\r\n";
302 warn "ERROR 501 $request\n";
306 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
307 encode_json( $hash );
310 } elsif ( $method =~ m{/beep/(.*)} ) {
311 my $error = uri_unescape($1);
312 system "beep -f 800 -r 2 -l 100";
313 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n{ beep: '$error' }\n";
314 print "BEEP $error\n";
316 print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
317 warn "ERROR 404 $request\n";
320 print $client "HTTP/1.0 500 No method\r\n\r\n";
321 warn "ERROR 500 $request\n";
327 print $client "HTTP/1.0 500 Error\r\n\r\nContent-Type: text/plain\r\n$@";
339 foreach ( split(/\n/, `ip addr` ) ) {
340 if ( /^\d:\s(\w+):\s/ ) {
342 } elsif ( /^\s+inet\s((\d+)\.(\d+)\.(\d+)\.(\d+))\/(\d+)/ ) {
343 $ip->{ $ip->{_last} } = $1;
345 #warn "# SKIP [$_]\n";
351 my $ua = LWP::UserAgent->new;
352 my $url = URI->new( $rfid_url . '/register.pl');
354 local_ip => $ip->{eth0} || $ip->{ (keys %$ip)[0] },
356 warn "GET ",$url->as_string;
357 my $response = $ua->get($url);
358 if ( $response->is_success ) {
359 warn "# ", $response->decoded_content;
360 my $json = decode_json $response->decoded_content;
361 warn "REGISTER: ",dump($json);
364 warn "ERROR ", $response->status_line;
368 rfid_register if $rfid_url;
374 <title>RFID JSONP</title>
375 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
376 <style type="text/css">
389 label[for=pull-reader] {
397 <script type="text/javascript">
400 if(!window.console) {
401 window.console = new function() {
402 this.info = function(str) {};
403 this.debug = function(str) {};
408 function got_visible_tags(data,textStatus) {
409 var html = 'No tags in range';
411 html = '<ul class="tags">';
412 $.each(data.tags, function(i,tag) {
413 console.debug( i, tag );
414 html += '<li><tt class="' + tag.security + '">' + tag.sid;
415 var content = tag.content || tag.borrower.cardnumber;
418 html += ' <a href="http://koha.example.com:8080/cgi-bin/koha/';
419 if ( tag.type == 1 ) { // book
420 html += 'catalogue/search.pl?q=';
422 html += 'members/member.pl?member=';
424 html += content + '" title="lookup in Koha" target="koha-lookup">' + content + '</a>';
427 html += '<form method=get action=program style="display:inline">'
428 + '<input type=hidden name='+tag.sid+' value="blank">'
429 + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
435 html += ' <form method=get action=program style="display:inline">'
436 + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
437 + '<input type=text name='+tag.sid+' size=12>'
438 + '<input type=submit value="Program">'
446 var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
448 html = '<div class=status>'
450 + ' &#' + arrows[ data.time % arrows.length ] + ';'
454 $('#tags').html( html );
455 window.setTimeout(function(){
457 },200); // re-scan every 200ms
460 function scan_tags() {
461 console.info('scan_tags');
462 if ( $('input#pull-reader').attr('checked') )
463 $.getJSON("/scan?callback=?", got_visible_tags);
466 $(document).ready(function() {
467 $('input#pull-reader').click( function() {
470 $('input#pull-reader').attr('checked', true); // force check on load
472 $('div#tags').click( function() {
473 $('input#pull-reader').attr('checked', false);
482 <h1>RFID tags in range</h1>
484 <label for=pull-reader>
485 <input id=pull-reader type=checkbox checked=1>
490 RFID reader not found or driver program not started.