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',
45 'listen=s', => \$listen,
46 'reader=s', => \$reader,
47 'koha=s', => \$koha_url,
54 if ( my $json = $rfid_sid_cache->{ $hash->{sid} } ) {
57 my $ua = LWP::UserAgent->new;
58 my $url = URI->new( $koha_url . '/cgi-bin/koha/ffzg/rfid/borrower.pl');
60 RFID_SID => $hash->{sid}
62 , JMBAG => $hash->{JMBAG}
64 warn "GET ",$url->as_string;
65 my $response = $ua->get($url);
66 if ( $response->is_success ) {
67 my $json = decode_json $response->decoded_content;
68 $rfid_sid_cache->{ $hash->{sid} } = $json;
71 warn "ERROR ", $response->status_line;
79 my $sock = $sip2->{sock} || die "no sip2 socket";
83 $send .= "\r" unless $send =~ m/\r$/;
84 warn "SIP2 >>>> ",dump($send), "\n";
88 my $expect = substr($send,0,2) | 0x01;
92 warn "SIP2 <<<< ",dump($in), "\n";
94 die "expected $expect" unless substr($in,0,2) != $expect;
99 if ( $in =~ s/^([0-9\s]+)// ) {
102 foreach ( split(/\|/, $in ) ) {
103 my ( $f, $v ) = ( $1, $2 ) if m/([A-Z]{2})(.+)/;
104 $hash->{$f} = decode('utf-8',$v);
107 warn "# sip2 hash response ",dump($hash);
112 if ( my $server = $sip2->{server} ) {
113 my $sock = $sip2->{sock} = IO::Socket::INET->new( $server ) || die "can't connect to $server: $!";
114 warn "SIP2 server ", $sock->peerhost, ":", $sock->peerport, "\n";
117 if ( sip2_message("9300CN$sip2->{user}|CO$sip2->{password}|")->{fixed} !~ m/^941/ ) {
118 die "SIP2 login failed";
124 use Biblio::RFID::RFID501;
125 use Biblio::RFID::Reader;
126 my $rfid = Biblio::RFID::Reader->new( shift @ARGV );
127 $rfid->debug( $debug );
132 $index_html = <DATA>;
133 $index_html =~ s{http://koha.example.com:8080}{$koha_url}sg;
140 my $server = IO::Socket::INET->new(
142 LocalAddr => $listen,
147 die "can't setup server: $!" unless $server;
149 $server_url = 'http://' . $listen;
150 print "Server $0 ready at $server_url\n";
152 while (my $client = $server->accept()) {
153 $client->autoflush(1);
154 my $request = <$client>;
156 warn "WEB << $request\n" if $debug;
159 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
162 if ( $method =~ s{\?(.+)}{} ) {
163 foreach my $p ( split(/[&;]/, $1) ) {
164 my ($n,$v) = split(/=/, $p, 2);
167 warn "WEB << param: ",dump( $param ) if $debug;
171 if ( $path eq '/' ) {
172 print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n$index_html";
173 } elsif ( $path =~ m{^/(examples/.+)} ) {
174 $path = $1; # FIXME prefix with dir for installation
176 warn "static $path $size bytes\n";
177 my $content_type = 'text/plain';
178 $content_type = 'application/javascript' if $path =~ /\.js$/;
179 $content_type = 'text/html' if $path =~ /\.html$/;
180 print $client "HTTP/1.0 200 OK\r\nContent-Type: $content_type\r\nContent-Length: $size\r\n\r\n";
183 open(my $fh, '<', $path) || die "can't open $path: $!";
189 } elsif ( $method =~ m{/scan} ) {
190 my @tags = $rfid->tags;
191 my $json = { time => time() };
192 foreach my $tag ( @tags ) {
193 my $hash = $rfid->to_hash( $tag );
195 if ( $hash->{tag_type} eq 'SmartX' ) {
196 my $borrower = rfid_borrower $hash;
197 if ( exists $borrower->{error} ) {
198 warn "ERROR ", dump($borrower);
200 $hash->{borrower} = $borrower->{borrower};
203 $hash->{security} = uc unpack 'H*', $rfid->afi( $tag );
205 push @{ $json->{tags} }, $hash;
207 warn "#### ", encode_json($json);
208 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
209 $param->{callback}, "(", encode_json($json), ")\r\n";
210 } elsif ( $method =~ m{/program} ) {
212 my $status = 501; # Not implementd
214 foreach my $p ( keys %$param ) {
215 next unless $p =~ m/^(E[0-9A-F]{15})$/;
217 my $content = Biblio::RFID::RFID501->from_hash({ content => $param->{$p} });
218 $content = Biblio::RFID::RFID501->blank if $param->{$p} eq 'blank';
221 warn "PROGRAM $tag $content\n";
222 $rfid->write_blocks( $tag => $content );
223 $rfid->write_afi( $tag => chr( $param->{$p} =~ /^130/ ? 0xDA : 0xD7 ) );
226 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
228 } elsif ( $method =~ m{/secure(.js)} ) {
232 my $status = 501; # Not implementd
234 foreach my $p ( keys %$param ) {
235 next unless $p =~ m/^(E[0-9A-F]{15})$/;
237 my $data = $param->{$p};
240 warn "SECURE $tag $data\n";
241 $rfid->write_afi( $tag => hex($data) );
245 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
246 $param->{callback}, "({ ok: 1 })\r\n";
248 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
251 } elsif ( $method =~ m{/sip2/(\w+)/(.+)} ) {
252 my ( $method, $args ) = ( $1, $2 );
253 warn "SIP2: $method [$args]";
255 my $ts = strftime('%Y%m%d %H%M%S', localtime());
256 my $loc = $sip2->{loc} || die "missing sip->{loc}";
257 my $password = $sip2->{password} || die "missing sip->{password}";
261 if ( $method eq 'patron_info' ) {
263 $hash = sip2_message("63000${ts} AO$loc|AA$patron|AC$password|");
265 } elsif ( $method eq 'checkout' ) {
266 my ($patron,$barcode) = split(/\//, $args, 2);
267 $hash = sip2_message("11YN${ts} AO$loc|AA$patron|AB$barcode|AC$password|BON|BIN|");
269 } elsif ( $method eq 'checkin' ) {
271 $hash = sip2_message("09N${ts}${ts}AP|AO${loc}|AB$barcode|AC|BIN|");
273 print $client "HTTP/1.0 500 $method not implemented\r\n\r\n";
277 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
278 encode_json( $hash );
282 print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
285 print $client "HTTP/1.0 500 No method\r\n\r\n";
296 foreach ( split(/\n/, `ip addr` ) ) {
297 if ( /^\d:\s(\w+):\s/ ) {
299 } elsif ( /^\s+inet\s((\d+)\.(\d+)\.(\d+)\.(\d+))\/(\d+)/ ) {
300 $ip->{ $ip->{last} } = $1;
302 warn "# SKIP [$_]\n";
307 my $ua = LWP::UserAgent->new;
308 my $url = URI->new( $rfid_url . '/register.pl');
310 local_ip => $ip->{eth0},
312 warn "GET ",$url->as_string;
313 my $response = $ua->get($url);
314 if ( $response->is_success ) {
315 warn "# ", $response->decoded_content;
316 my $json = decode_json $response->decoded_content;
317 warn "REGISTER: ",dump($json);
320 warn "ERROR ", $response->status_line;
330 <title>RFID JSONP</title>
331 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
332 <style type="text/css">
345 label[for=pull-reader] {
353 <script type="text/javascript">
356 if(!window.console) {
357 window.console = new function() {
358 this.info = function(str) {};
359 this.debug = function(str) {};
364 function got_visible_tags(data,textStatus) {
365 var html = 'No tags in range';
367 html = '<ul class="tags">';
368 $.each(data.tags, function(i,tag) {
369 console.debug( i, tag );
370 html += '<li><tt class="' + tag.security + '">' + tag.sid;
371 var content = tag.content || tag.borrower.cardnumber;
374 html += ' <a href="http://koha.example.com:8080/cgi-bin/koha/';
375 if ( tag.type == 1 ) { // book
376 html += 'catalogue/search.pl?q=';
378 html += 'members/member.pl?member=';
380 html += content + '" title="lookup in Koha" target="koha-lookup">' + content + '</a>';
383 html += '<form method=get action=program style="display:inline">'
384 + '<input type=hidden name='+tag.sid+' value="blank">'
385 + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
391 html += ' <form method=get action=program style="display:inline">'
392 + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
393 + '<input type=text name='+tag.sid+' size=12>'
394 + '<input type=submit value="Program">'
402 var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
404 html = '<div class=status>'
406 + ' &#' + arrows[ data.time % arrows.length ] + ';'
410 $('#tags').html( html );
411 window.setTimeout(function(){
413 },200); // re-scan every 200ms
416 function scan_tags() {
417 console.info('scan_tags');
418 if ( $('input#pull-reader').attr('checked') )
419 $.getJSON("/scan?callback=?", got_visible_tags);
422 $(document).ready(function() {
423 $('input#pull-reader').click( function() {
426 $('input#pull-reader').attr('checked', true); // force check on load
428 $('div#tags').click( function() {
429 $('input#pull-reader').attr('checked', false);
438 <h1>RFID tags in range</h1>
440 <label for=pull-reader>
441 <input id=pull-reader type=checkbox checked=1>
446 RFID reader not found or driver program not started.