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/;
24 my $listen = '127.0.0.1:9000';
27 my $koha_url = 'http://ffzg.koha-dev.rot13.org:8080';
28 # internal URL so we can find local address of machine and vmware NAT
29 my $rfid_url = 'http://rfid.koha-dev.vbz.ffzg.hr';
35 'listen=s', => \$listen,
36 'reader=s', => \$reader,
37 'koha=s', => \$koha_url,
44 if ( my $json = $rfid_sid_cache->{ $hash->{sid} } ) {
47 my $ua = LWP::UserAgent->new;
48 my $url = URI->new( $koha_url . '/cgi-bin/koha/ffzg/rfid/borrower.pl');
50 RFID_SID => $hash->{sid}
52 , JMBAG => $hash->{JMBAG}
54 warn "GET ",$url->as_string;
55 my $response = $ua->get($url);
56 if ( $response->is_success ) {
57 my $json = decode_json $response->decoded_content;
58 $rfid_sid_cache->{ $hash->{sid} } = $json;
61 warn "ERROR ", $response->status_line;
66 use Biblio::RFID::RFID501;
67 use Biblio::RFID::Reader;
68 my $rfid = Biblio::RFID::Reader->new( shift @ARGV );
69 $rfid->debug( $debug );
75 $index_html =~ s{http://koha.example.com:8080}{$koha_url}sg;
82 my $server = IO::Socket::INET->new(
89 die "can't setup server: $!" unless $server;
91 $server_url = 'http://' . $listen;
92 print "Server $0 ready at $server_url\n";
94 while (my $client = $server->accept()) {
95 $client->autoflush(1);
96 my $request = <$client>;
98 warn "WEB << $request\n" if $debug;
101 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
104 if ( $method =~ s{\?(.+)}{} ) {
105 foreach my $p ( split(/[&;]/, $1) ) {
106 my ($n,$v) = split(/=/, $p, 2);
109 warn "WEB << param: ",dump( $param ) if $debug;
113 if ( $path eq '/' ) {
114 print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n$index_html";
115 } elsif ( $path =~ m{^/(examples/.+)} ) {
116 $path = $1; # FIXME prefix with dir for installation
118 warn "static $path $size bytes\n";
119 my $content_type = 'text/plain';
120 $content_type = 'application/javascript' if $path =~ /\.js/;
121 print $client "HTTP/1.0 200 OK\r\nContent-Type: $content_type\r\nContent-Length: $size\r\n\r\n";
124 open(my $fh, '<', $path) || die "can't open $path: $!";
130 } elsif ( $method =~ m{/scan} ) {
131 my @tags = $rfid->tags;
132 my $json = { time => time() };
133 foreach my $tag ( @tags ) {
134 my $hash = $rfid->to_hash( $tag );
136 if ( $hash->{tag_type} eq 'SmartX' ) {
137 my $borrower = rfid_borrower $hash;
138 if ( exists $borrower->{error} ) {
139 warn "ERROR ", dump($borrower);
141 $hash->{borrower} = $borrower->{borrower};
144 $hash->{security} = uc unpack 'H*', $rfid->afi( $tag );
146 push @{ $json->{tags} }, $hash;
148 warn "#### ", encode_json($json);
149 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
150 $param->{callback}, "(", encode_json($json), ")\r\n";
151 } elsif ( $method =~ m{/program} ) {
153 my $status = 501; # Not implementd
155 foreach my $p ( keys %$param ) {
156 next unless $p =~ m/^(E[0-9A-F]{15})$/;
158 my $content = Biblio::RFID::RFID501->from_hash({ content => $param->{$p} });
159 $content = Biblio::RFID::RFID501->blank if $param->{$p} eq 'blank';
162 warn "PROGRAM $tag $content\n";
163 $rfid->write_blocks( $tag => $content );
164 $rfid->write_afi( $tag => chr( $param->{$p} =~ /^130/ ? 0xDA : 0xD7 ) );
167 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
169 } elsif ( $method =~ m{/secure(.js)} ) {
173 my $status = 501; # Not implementd
175 foreach my $p ( keys %$param ) {
176 next unless $p =~ m/^(E[0-9A-F]{15})$/;
178 my $data = $param->{$p};
181 warn "SECURE $tag $data\n";
182 $rfid->write_afi( $tag => hex($data) );
186 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
187 $param->{callback}, "({ ok: 1 })\r\n";
189 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
193 print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
196 print $client "HTTP/1.0 500 No method\r\n\r\n";
207 foreach ( split(/\n/, `ip addr` ) ) {
208 if ( /^\d:\s(\w+):\s/ ) {
210 } elsif ( /^\s+inet\s((\d+)\.(\d+)\.(\d+)\.(\d+))\/(\d+)/ ) {
211 $ip->{ $ip->{last} } = $1;
213 warn "# SKIP [$_]\n";
218 my $ua = LWP::UserAgent->new;
219 my $url = URI->new( $rfid_url . '/register.pl');
221 local_ip => $ip->{eth0},
223 warn "GET ",$url->as_string;
224 my $response = $ua->get($url);
225 if ( $response->is_success ) {
226 warn "# ", $response->decoded_content;
227 my $json = decode_json $response->decoded_content;
228 warn "REGISTER: ",dump($json);
231 warn "ERROR ", $response->status_line;
241 <title>RFID JSONP</title>
242 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
243 <style type="text/css">
256 label[for=pull-reader] {
264 <script type="text/javascript">
267 if(!window.console) {
268 window.console = new function() {
269 this.info = function(str) {};
270 this.debug = function(str) {};
275 function got_visible_tags(data,textStatus) {
276 var html = 'No tags in range';
278 html = '<ul class="tags">';
279 $.each(data.tags, function(i,tag) {
280 console.debug( i, tag );
281 html += '<li><tt class="' + tag.security + '">' + tag.sid;
282 var content = tag.content || tag.borrower.cardnumber;
285 html += ' <a href="http://koha.example.com:8080/cgi-bin/koha/';
286 if ( tag.type == 1 ) { // book
287 html += 'catalogue/search.pl?q=';
289 html += 'members/member.pl?member=';
291 html += content + '" title="lookup in Koha" target="koha-lookup">' + content + '</a>';
294 html += '<form method=get action=program style="display:inline">'
295 + '<input type=hidden name='+tag.sid+' value="blank">'
296 + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
302 html += ' <form method=get action=program style="display:inline">'
303 + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
304 + '<input type=text name='+tag.sid+' size=12>'
305 + '<input type=submit value="Program">'
313 var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
315 html = '<div class=status>'
317 + ' &#' + arrows[ data.time % arrows.length ] + ';'
321 $('#tags').html( html );
322 window.setTimeout(function(){
324 },200); // re-scan every 200ms
327 function scan_tags() {
328 console.info('scan_tags');
329 if ( $('input#pull-reader').attr('checked') )
330 $.getJSON("/scan?callback=?", got_visible_tags);
333 $(document).ready(function() {
334 $('input#pull-reader').click( function() {
337 $('input#pull-reader').attr('checked', true); // force check on load
339 $('div#tags').click( function() {
340 $('input#pull-reader').attr('checked', false);
349 <h1>RFID tags in range</h1>
351 <label for=pull-reader>
352 <input id=pull-reader type=checkbox checked=1>
357 RFID reader not found or driver program not started.