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 my $listen = '127.0.0.1:9000';
29 'listen=s', => \$listen,
30 'reader=s', => \$reader,
34 use Biblio::RFID::RFID501;
35 use Biblio::RFID::Reader;
36 my $rfid = Biblio::RFID::Reader->new( shift @ARGV );
48 my $server = IO::Socket::INET->new(
55 die "can't setup server: $!" unless $server;
57 $server_url = 'http://' . $listen;
58 print "Server $0 ready at $server_url\n";
60 while (my $client = $server->accept()) {
61 $client->autoflush(1);
62 my $request = <$client>;
64 warn "WEB << $request\n" if $debug;
67 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
70 if ( $method =~ s{\?(.+)}{} ) {
71 foreach my $p ( split(/[&;]/, $1) ) {
72 my ($n,$v) = split(/=/, $p, 2);
75 warn "WEB << param: ",dump( $param ) if $debug;
80 print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n$index_html";
81 } elsif ( $path =~ m{^/(examples/.+)} ) {
82 $path = $1; # FIXME prefix with dir for installation
84 warn "static $path $size bytes\n";
85 my $content_type = 'text/plain';
86 $content_type = 'application/javascript' if $path =~ /\.js/;
87 print $client "HTTP/1.0 200 OK\r\nContent-Type: $content_type\r\nContent-Length: $size\r\n\r\n";
90 open(my $fh, '<', $path) || die "can't open $path: $!";
96 } elsif ( $method =~ m{/scan} ) {
97 my @tags = $rfid->tags;
98 my $json = { time => time() };
99 foreach my $tag ( @tags ) {
100 my $hash = Biblio::RFID::RFID501->to_hash( $rfid->blocks( $tag ) );
102 $hash->{security} = uc unpack 'H*', $rfid->afi( $tag );
103 push @{ $json->{tags} }, $hash;
105 warn "#### ", encode_json($json);
106 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
107 $param->{callback}, "(", encode_json($json), ")\r\n";
108 } elsif ( $method =~ m{/program} ) {
110 my $status = 501; # Not implementd
112 foreach my $p ( keys %$param ) {
113 next unless $p =~ m/^(E[0-9A-F]{15})$/;
115 my $content = Biblio::RFID::RFID501->from_hash({ content => $param->{$p} });
116 $content = Biblio::RFID::RFID501->blank if $param->{$p} eq 'blank';
119 warn "PROGRAM $tag $content\n";
120 $rfid->write_blocks( $tag => $content );
121 $rfid->write_afi( $tag => chr( $param->{$p} =~ /^130/ ? 0xDA : 0xD7 ) );
124 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
126 } elsif ( $method =~ m{/secure(.js)} ) {
130 my $status = 501; # Not implementd
132 foreach my $p ( keys %$param ) {
133 next unless $p =~ m/^(E[0-9A-F]{15})$/;
135 my $data = $param->{$p};
138 warn "SECURE $tag $data\n";
139 $rfid->write_afi( $tag => hex($data) );
143 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
144 $param->{callback}, "({ ok: 1 })\r\n";
146 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
150 print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
153 print $client "HTTP/1.0 500 No method\r\n\r\n";
166 <title>RFID JSONP</title>
167 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
168 <style type="text/css">
181 label[for=pull-reader] {
189 <script type="text/javascript">
192 if(!window.console) {
193 window.console = new function() {
194 this.info = function(str) {};
195 this.debug = function(str) {};
200 function got_visible_tags(data,textStatus) {
201 var html = 'No tags in range';
203 html = '<ul class="tags">';
204 $.each(data.tags, function(i,tag) {
205 console.debug( i, tag );
206 html += '<li><tt class=' + tag.security + '>' + tag.sid;
208 html += ' <a href="https://koha-dev.rot13.org:8443/cgi-bin/koha/members/member.pl?member=' + tag.content + '" title="lookup in Koha" target="koha-lookup">' + tag.content + '</a>';
210 html += '<form method=get action=program style="display:inline">'
211 + '<input type=hidden name='+tag.sid+' value="blank">'
212 + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
217 html += ' <form method=get action=program style="display:inline">'
218 + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
219 + '<input type=text name='+tag.sid+' size=12>'
220 + '<input type=submit value="Program">'
228 var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
230 html = '<div class=status>'
232 + ' &#' + arrows[ data.time % arrows.length ] + ';'
236 $('#tags').html( html );
237 window.setTimeout(function(){
239 },200); // re-scan every 200ms
242 function scan_tags() {
243 console.info('scan_tags');
244 if ( $('input#pull-reader').attr('checked') )
245 $.getJSON("/scan?callback=?", got_visible_tags);
248 $(document).ready(function() {
249 $('input#pull-reader').click( function() {
252 $('input#pull-reader').attr('checked', true); // force check on load
254 $('div#tags').click( function() {
255 $('input#pull-reader').attr('checked', false);
264 <h1>RFID tags in range</h1>
266 <label for=pull-reader>
267 <input id=pull-reader type=checkbox checked=1>
272 RFID reader not found or driver program not started.