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()) {
94 $client->autoflush(1);
95 my $request = <$client>;
97 warn "WEB << $request\n" if $debug;
100 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
103 if ( $method =~ s{\?(.+)}{} ) {
104 foreach my $p ( split(/[&;]/, $1) ) {
105 my ($n,$v) = split(/=/, $p, 2);
108 warn "WEB << param: ",dump( $param ) if $debug;
112 if ( $path eq '/' ) {
113 print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n$index_html";
114 } elsif ( $path =~ m{^/(examples/.+)} ) {
115 $path = $1; # FIXME prefix with dir for installation
117 warn "static $path $size bytes\n";
118 my $content_type = 'text/plain';
119 $content_type = 'application/javascript' if $path =~ /\.js/;
120 print $client "HTTP/1.0 200 OK\r\nContent-Type: $content_type\r\nContent-Length: $size\r\n\r\n";
123 open(my $fh, '<', $path) || die "can't open $path: $!";
129 } elsif ( $method =~ m{/scan} ) {
130 my @tags = $rfid->tags;
131 my $json = { time => time() };
132 foreach my $tag ( @tags ) {
133 my $hash = $rfid->to_hash( $tag );
135 if ( $hash->{tag_type} eq 'SmartX' ) {
136 my $borrower = rfid_borrower $hash;
137 if ( exists $borrower->{error} ) {
138 warn "ERROR ", dump($borrower);
140 $hash->{borrower} = $borrower->{borrower};
143 $hash->{security} = uc unpack 'H*', $rfid->afi( $tag );
145 push @{ $json->{tags} }, $hash;
147 warn "#### ", encode_json($json);
148 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
149 $param->{callback}, "(", encode_json($json), ")\r\n";
150 } elsif ( $method =~ m{/program} ) {
152 my $status = 501; # Not implementd
154 foreach my $p ( keys %$param ) {
155 next unless $p =~ m/^(E[0-9A-F]{15})$/;
157 my $content = Biblio::RFID::RFID501->from_hash({ content => $param->{$p} });
158 $content = Biblio::RFID::RFID501->blank if $param->{$p} eq 'blank';
161 warn "PROGRAM $tag $content\n";
162 $rfid->write_blocks( $tag => $content );
163 $rfid->write_afi( $tag => chr( $param->{$p} =~ /^130/ ? 0xDA : 0xD7 ) );
166 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
168 } elsif ( $method =~ m{/secure(.js)} ) {
172 my $status = 501; # Not implementd
174 foreach my $p ( keys %$param ) {
175 next unless $p =~ m/^(E[0-9A-F]{15})$/;
177 my $data = $param->{$p};
180 warn "SECURE $tag $data\n";
181 $rfid->write_afi( $tag => hex($data) );
185 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
186 $param->{callback}, "({ ok: 1 })\r\n";
188 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
192 print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
195 print $client "HTTP/1.0 500 No method\r\n\r\n";
206 foreach ( split(/\n/, `ip addr` ) ) {
207 if ( /^\d:\s(\w+):\s/ ) {
209 } elsif ( /^\s+inet\s((\d+)\.(\d+)\.(\d+)\.(\d+))\/(\d+)/ ) {
210 $ip->{ $ip->{last} } = $1;
212 warn "# SKIP [$_]\n";
217 my $ua = LWP::UserAgent->new;
218 my $url = URI->new( $rfid_url . '/register.pl');
220 local_ip => $ip->{eth0},
222 warn "GET ",$url->as_string;
223 my $response = $ua->get($url);
224 if ( $response->is_success ) {
225 warn "# ", $response->decoded_content;
226 my $json = decode_json $response->decoded_content;
227 warn "REGISTER: ",dump($json);
230 warn "ERROR ", $response->status_line;
240 <title>RFID JSONP</title>
241 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
242 <style type="text/css">
255 label[for=pull-reader] {
263 <script type="text/javascript">
266 if(!window.console) {
267 window.console = new function() {
268 this.info = function(str) {};
269 this.debug = function(str) {};
274 function got_visible_tags(data,textStatus) {
275 var html = 'No tags in range';
277 html = '<ul class="tags">';
278 $.each(data.tags, function(i,tag) {
279 console.debug( i, tag );
280 html += '<li><tt class="' + tag.security + '">' + tag.sid;
281 var content = tag.content || tag.borrower.cardnumber;
284 html += ' <a href="http://koha.example.com:8080/cgi-bin/koha/';
285 if ( tag.type == 1 ) { // book
286 html += 'catalogue/search.pl?q=';
288 html += 'members/member.pl?member=';
290 html += content + '" title="lookup in Koha" target="koha-lookup">' + content + '</a>';
293 html += '<form method=get action=program style="display:inline">'
294 + '<input type=hidden name='+tag.sid+' value="blank">'
295 + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
301 html += ' <form method=get action=program style="display:inline">'
302 + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
303 + '<input type=text name='+tag.sid+' size=12>'
304 + '<input type=submit value="Program">'
312 var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
314 html = '<div class=status>'
316 + ' &#' + arrows[ data.time % arrows.length ] + ';'
320 $('#tags').html( html );
321 window.setTimeout(function(){
323 },200); // re-scan every 200ms
326 function scan_tags() {
327 console.info('scan_tags');
328 if ( $('input#pull-reader').attr('checked') )
329 $.getJSON("/scan?callback=?", got_visible_tags);
332 $(document).ready(function() {
333 $('input#pull-reader').click( function() {
336 $('input#pull-reader').attr('checked', true); // force check on load
338 $('div#tags').click( function() {
339 $('input#pull-reader').attr('checked', false);
348 <h1>RFID tags in range</h1>
350 <label for=pull-reader>
351 <input id=pull-reader type=checkbox checked=1>
356 RFID reader not found or driver program not started.