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 );
74 $index_html =~ s{http://koha.example.com:8080}{$koha_url}sg;
81 my $server = IO::Socket::INET->new(
88 die "can't setup server: $!" unless $server;
90 $server_url = 'http://' . $listen;
91 print "Server $0 ready at $server_url\n";
93 while (my $client = $server->accept()) {
95 eval { # don't die inside here!
97 $client->autoflush(1);
98 my $request = <$client>;
100 warn "WEB << $request\n" if $debug;
103 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
106 if ( $method =~ s{\?(.+)}{} ) {
107 foreach my $p ( split(/[&;]/, $1) ) {
108 my ($n,$v) = split(/=/, $p, 2);
111 warn "WEB << param: ",dump( $param ) if $debug;
115 if ( $path eq '/' ) {
116 print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n$index_html";
117 } elsif ( $path =~ m{^/(examples/.+)} ) {
118 $path = $1; # FIXME prefix with dir for installation
120 warn "static $path $size bytes\n";
121 my $content_type = 'text/plain';
122 $content_type = 'application/javascript' if $path =~ /\.js/;
123 print $client "HTTP/1.0 200 OK\r\nContent-Type: $content_type\r\nContent-Length: $size\r\n\r\n";
126 open(my $fh, '<', $path) || die "can't open $path: $!";
132 } elsif ( $method =~ m{/scan} ) {
133 my @tags = $rfid->tags;
134 my $json = { time => time() };
135 foreach my $tag ( @tags ) {
136 my $hash = $rfid->to_hash( $tag );
138 $hash->{reader} = $rfid->from_reader( $tag );
139 if ( $hash->{tag_type} eq 'SmartX' ) {
140 my $borrower = rfid_borrower $hash;
141 if ( exists $borrower->{error} ) {
142 warn "ERROR ", dump($borrower);
144 $hash->{borrower} = $borrower->{borrower};
145 $hash->{content} = $borrower->{borrower}->{cardnumber}; # compatibile with 3M tags
148 $hash->{security} = uc unpack 'H*', $rfid->afi( $tag );
150 push @{ $json->{tags} }, $hash;
152 warn "#### ", encode_json($json);
153 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
154 $param->{callback}, "(", encode_json($json), ")\r\n";
155 } elsif ( $method =~ m{/program} ) {
157 my $status = 501; # Not implementd
159 foreach my $p ( keys %$param ) {
160 next unless $p =~ m/^(E[0-9A-F]{15})$/;
162 my $content = Biblio::RFID::RFID501->from_hash({ content => $param->{$p} });
163 $content = Biblio::RFID::RFID501->blank if $param->{$p} eq 'blank';
166 warn "PROGRAM $tag $content\n";
167 $rfid->write_blocks( $tag => $content );
168 $rfid->write_afi( $tag => chr( $param->{$p} =~ /^130/ ? 0xDA : 0xD7 ) );
171 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
173 } elsif ( $method =~ m{/secure(.js)} ) {
177 my $status = 501; # Not implementd
179 foreach my $p ( keys %$param ) {
180 next unless $p =~ m/^(E[0-9A-F]{15})$/;
182 my $data = $param->{$p};
185 warn "SECURE $tag $data\n";
186 $rfid->write_afi( $tag => chr(hex($data)) );
190 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
191 $param->{callback}, "({ ok: 1 })\r\n";
193 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
197 print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
200 print $client "HTTP/1.0 500 No method\r\n\r\n";
206 print $client "HTTP/1.0 500 Error\r\n\r\nContent-Type: text/plain\r\n$@";
218 foreach ( split(/\n/, `ip addr` ) ) {
219 if ( /^\d:\s(\w+):\s/ ) {
221 } elsif ( /^\s+inet\s((\d+)\.(\d+)\.(\d+)\.(\d+))\/(\d+)/ ) {
222 $ip->{ $ip->{last} } = $1;
224 warn "# SKIP [$_]\n";
229 my $ua = LWP::UserAgent->new;
230 my $url = URI->new( $rfid_url . '/register.pl');
232 local_ip => $ip->{eth0},
234 warn "GET ",$url->as_string;
235 my $response = $ua->get($url);
236 if ( $response->is_success ) {
237 warn "# ", $response->decoded_content;
238 my $json = decode_json $response->decoded_content;
239 warn "REGISTER: ",dump($json);
242 warn "ERROR ", $response->status_line;
252 <title>RFID JSONP</title>
253 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
254 <style type="text/css">
267 label[for=pull-reader] {
275 <script type="text/javascript">
278 if(!window.console) {
279 window.console = new function() {
280 this.info = function(str) {};
281 this.debug = function(str) {};
286 function got_visible_tags(data,textStatus) {
287 var html = 'No tags in range';
289 html = '<ul class="tags">';
290 $.each(data.tags, function(i,tag) {
291 console.debug( i, tag );
292 html += '<li><tt class="' + tag.security + '">' + tag.sid;
293 var content = tag.content || tag.borrower.cardnumber;
296 html += ' <a href="http://koha.example.com:8080/cgi-bin/koha/';
297 if ( tag.type == 1 ) { // book
298 html += 'catalogue/search.pl?q=';
300 html += 'members/member.pl?member=';
302 html += content + '" title="lookup in Koha" target="koha-lookup">' + content + '</a>';
305 html += '<form method=get action=program style="display:inline">'
306 + '<input type=hidden name='+tag.sid+' value="blank">'
307 + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
313 html += ' <form method=get action=program style="display:inline">'
314 + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
315 + '<input type=text name='+tag.sid+' size=12>'
316 + '<input type=submit value="Program">'
324 var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
326 html = '<div class=status>'
328 + ' &#' + arrows[ data.time % arrows.length ] + ';'
332 $('#tags').html( html );
333 window.setTimeout(function(){
335 },200); // re-scan every 200ms
338 function scan_tags() {
339 console.info('scan_tags');
340 if ( $('input#pull-reader').attr('checked') )
341 $.getJSON("/scan?callback=?", got_visible_tags);
344 $(document).ready(function() {
345 $('input#pull-reader').click( function() {
348 $('input#pull-reader').attr('checked', true); // force check on load
350 $('div#tags').click( function() {
351 $('input#pull-reader').attr('checked', false);
360 <h1>RFID tags in range</h1>
362 <label for=pull-reader>
363 <input id=pull-reader type=checkbox checked=1>
368 RFID reader not found or driver program not started.