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/;
22 use POSIX qw(strftime);
26 my $listen = '127.0.0.1:9000';
29 my $koha_url = 'http://ffzg.koha-dev.rot13.org:8080';
30 # internal URL so we can find local address of machine and vmware NAT
31 my $rfid_url = 'http://rfid.koha-dev.vbz.ffzg.hr';
33 server => '10.60.0.11:6002', # must be IP!
34 # user => 'sip2-user',
35 # password => 'sip2-passwd',
37 password => 'viva2koha',
49 'listen=s', => \$listen,
50 'reader=s', => \$reader,
51 'koha=s', => \$koha_url,
58 if ( my $json = $rfid_sid_cache->{ $hash->{sid} } ) {
61 my $ua = LWP::UserAgent->new;
62 my $url = URI->new( $koha_url . '/cgi-bin/koha/ffzg/rfid/borrower.pl');
64 RFID_SID => $hash->{sid}
66 , JMBAG => $hash->{JMBAG}
68 warn "GET ",$url->as_string;
69 my $response = $ua->get($url);
70 if ( $response->is_success ) {
71 my $json = decode_json $response->decoded_content;
72 $rfid_sid_cache->{ $hash->{sid} } = $json;
75 warn "ERROR ", $response->status_line;
83 my $sock = $sip2->{sock} || die "no sip2 socket";
87 $send .= "\r" unless $send =~ m/\r$/;
88 warn "SIP2 >>>> ",dump($send), "\n";
92 my $expect = substr($send,0,2) | 0x01;
96 warn "SIP2 <<<< ",dump($in), "\n";
98 die "expected $expect" unless substr($in,0,2) != $expect;
103 if ( $in =~ s/^([0-9\s]+)// ) {
106 foreach ( split(/\|/, $in ) ) {
107 my ( $f, $v ) = ( $1, $2 ) if m/([A-Z]{2})(.+)/;
108 $hash->{$f} = decode('utf-8',$v);
111 warn "# sip2 hash response ",dump($hash);
116 if ( my $server = $sip2->{server} ) {
117 my $sock = $sip2->{sock} = IO::Socket::INET->new( $server ) || die "can't connect to $server: $!";
118 warn "SIP2 server ", $sock->peerhost, ":", $sock->peerport, "\n";
121 if ( sip2_message("9300CN$sip2->{user}|CO$sip2->{password}|")->{fixed} !~ m/^941/ ) {
122 die "SIP2 login failed";
128 use Biblio::RFID::RFID501;
129 use Biblio::RFID::Reader;
130 my $rfid = Biblio::RFID::Reader->new( shift @ARGV );
131 $rfid->debug( $debug );
136 $index_html = <DATA>;
137 $index_html =~ s{http://koha.example.com:8080}{$koha_url}sg;
144 my $server = IO::Socket::INET->new(
146 LocalAddr => $listen,
151 die "can't setup server: $!" unless $server;
153 $server_url = 'http://' . $listen;
154 print "Server $0 ready at $server_url\n";
156 while (my $client = $server->accept()) {
157 $client->autoflush(1);
158 my $request = <$client>;
160 warn "WEB << $request\n" if $debug;
163 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
166 if ( $method =~ s{\?(.+)}{} ) {
167 foreach my $p ( split(/[&;]/, $1) ) {
168 my ($n,$v) = split(/=/, $p, 2);
171 warn "WEB << param: ",dump( $param ) if $debug;
175 if ( $path eq '/' ) {
176 print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n$index_html";
177 } elsif ( $path =~ m{^/(examples/.+)} ) {
178 $path = $1; # FIXME prefix with dir for installation
180 warn "static $path $size bytes\n";
181 my $content_type = 'text/plain';
182 $content_type = 'application/javascript' if $path =~ /\.js$/;
183 $content_type = 'text/html' if $path =~ /\.html$/;
184 print $client "HTTP/1.0 200 OK\r\nContent-Type: $content_type\r\nContent-Length: $size\r\n\r\n";
187 open(my $fh, '<', $path) || die "can't open $path: $!";
193 } elsif ( $method =~ m{/scan} ) {
194 my @tags = $rfid->tags;
195 my $json = { time => time() };
196 foreach my $tag ( @tags ) {
197 my $hash = $rfid->to_hash( $tag );
199 if ( $hash->{tag_type} eq 'SmartX' ) {
200 my $borrower = rfid_borrower $hash;
201 if ( exists $borrower->{error} ) {
202 warn "ERROR ", dump($borrower);
204 $hash->{borrower} = $borrower->{borrower};
207 $hash->{security} = uc unpack 'H*', $rfid->afi( $tag );
209 push @{ $json->{tags} }, $hash;
211 warn "#### ", encode_json($json);
212 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
213 $param->{callback}, "(", encode_json($json), ")\r\n";
214 } elsif ( $method =~ m{/program} ) {
216 my $status = 501; # Not implementd
218 foreach my $p ( keys %$param ) {
219 next unless $p =~ m/^(E[0-9A-F]{15})$/;
221 my $content = Biblio::RFID::RFID501->from_hash({ content => $param->{$p} });
222 $content = Biblio::RFID::RFID501->blank if $param->{$p} eq 'blank';
225 warn "PROGRAM $tag $content\n";
226 $rfid->write_blocks( $tag => $content );
227 $rfid->write_afi( $tag => chr( $param->{$p} =~ /^130/ ? $afi->{secure} : $afi->{unsecure} ) );
230 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
232 } elsif ( $method =~ m{/secure(.js)} ) {
236 my $status = 501; # Not implementd
238 foreach my $p ( keys %$param ) {
239 next unless $p =~ m/^(E[0-9A-F]{15})$/;
241 my $data = $param->{$p};
244 warn "SECURE $tag $data\n";
245 $rfid->write_afi( $tag => hex($data) );
249 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
250 $param->{callback}, "({ ok: 1 })\r\n";
252 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
255 } elsif ( $method =~ m{/sip2/(\w+)/(.+)} ) {
256 my ( $method, $args ) = ( $1, $2 );
257 warn "SIP2: $method [$args]";
259 my $ts = strftime('%Y%m%d %H%M%S', localtime());
260 my $loc = $sip2->{loc} || die "missing sip->{loc}";
261 my $password = $sip2->{password} || die "missing sip->{password}";
265 if ( $method eq 'patron_info' ) {
267 $hash = sip2_message("63000${ts} AO$loc|AA$patron|AC$password|");
269 } elsif ( $method eq 'checkout' ) {
270 my ($patron,$barcode,$sid) = split(/\//, $args, 3);
271 $hash = sip2_message("11YN${ts} AO$loc|AA$patron|AB$barcode|AC$password|BON|BIN|");
272 if ( substr( $hash->{fixed}, 2, 1 ) == 1 ) {
273 $rfid->write_afi( $sid => chr( $afi->{unsecure} ) );
276 } elsif ( $method eq 'checkin' ) {
277 my ($patron,$barcode,$sid) = split(/\//, $args, 3);
278 $hash = sip2_message("09N${ts}${ts}AP|AO${loc}|AB$barcode|AC|BIN|");
279 if ( substr( $hash->{fixed}, 2, 1 ) == 1 ) {
280 $rfid->write_afi( $sid => chr( $afi->{secure} ) );
283 print $client "HTTP/1.0 500 $method not implemented\r\n\r\n";
287 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
288 encode_json( $hash );
292 print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
295 print $client "HTTP/1.0 500 No method\r\n\r\n";
306 foreach ( split(/\n/, `ip addr` ) ) {
307 if ( /^\d:\s(\w+):\s/ ) {
309 } elsif ( /^\s+inet\s((\d+)\.(\d+)\.(\d+)\.(\d+))\/(\d+)/ ) {
310 $ip->{ $ip->{last} } = $1;
312 warn "# SKIP [$_]\n";
317 my $ua = LWP::UserAgent->new;
318 my $url = URI->new( $rfid_url . '/register.pl');
320 local_ip => $ip->{eth0},
322 warn "GET ",$url->as_string;
323 my $response = $ua->get($url);
324 if ( $response->is_success ) {
325 warn "# ", $response->decoded_content;
326 my $json = decode_json $response->decoded_content;
327 warn "REGISTER: ",dump($json);
330 warn "ERROR ", $response->status_line;
340 <title>RFID JSONP</title>
341 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
342 <style type="text/css">
355 label[for=pull-reader] {
363 <script type="text/javascript">
366 if(!window.console) {
367 window.console = new function() {
368 this.info = function(str) {};
369 this.debug = function(str) {};
374 function got_visible_tags(data,textStatus) {
375 var html = 'No tags in range';
377 html = '<ul class="tags">';
378 $.each(data.tags, function(i,tag) {
379 console.debug( i, tag );
380 html += '<li><tt class="' + tag.security + '">' + tag.sid;
381 var content = tag.content || tag.borrower.cardnumber;
384 html += ' <a href="http://koha.example.com:8080/cgi-bin/koha/';
385 if ( tag.type == 1 ) { // book
386 html += 'catalogue/search.pl?q=';
388 html += 'members/member.pl?member=';
390 html += content + '" title="lookup in Koha" target="koha-lookup">' + content + '</a>';
393 html += '<form method=get action=program style="display:inline">'
394 + '<input type=hidden name='+tag.sid+' value="blank">'
395 + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
401 html += ' <form method=get action=program style="display:inline">'
402 + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
403 + '<input type=text name='+tag.sid+' size=12>'
404 + '<input type=submit value="Program">'
412 var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
414 html = '<div class=status>'
416 + ' &#' + arrows[ data.time % arrows.length ] + ';'
420 $('#tags').html( html );
421 window.setTimeout(function(){
423 },200); // re-scan every 200ms
426 function scan_tags() {
427 console.info('scan_tags');
428 if ( $('input#pull-reader').attr('checked') )
429 $.getJSON("/scan?callback=?", got_visible_tags);
432 $(document).ready(function() {
433 $('input#pull-reader').click( function() {
436 $('input#pull-reader').attr('checked', true); // force check on load
438 $('div#tags').click( function() {
439 $('input#pull-reader').attr('checked', false);
448 <h1>RFID tags in range</h1>
450 <label for=pull-reader>
451 <input id=pull-reader type=checkbox checked=1>
456 RFID reader not found or driver program not started.