3 =head1 RFID-JSONP-server
5 This is simpliest possible JSONP server which provides local web interface to RFID readers
9 ./scripts/RFID-JSONP-server.pl
16 use Data::Dump qw/dump/;
22 my $listen = '127.0.0.1:9000';
29 'listen=s', => \$listen,
30 'reader=s', => \$reader,
34 use RFID::Biblio::RFID501;
35 use RFID::Biblio::Readers;
36 my $rfid = (RFID::Biblio::Readers->available( $reader ))[0]; # FIXME
37 warn "using $rfid reader\n";
49 my $server = IO::Socket::INET->new(
56 die "can't setup server: $!" unless $server;
58 $server_url = 'http://' . $listen;
59 print "Server $0 ready at $server_url\n";
61 while (my $client = $server->accept()) {
62 $client->autoflush(1);
63 my $request = <$client>;
65 warn "WEB << $request\n" if $debug;
68 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
71 if ( $method =~ s{\?(.+)}{} ) {
72 foreach my $p ( split(/[&;]/, $1) ) {
73 my ($n,$v) = split(/=/, $p, 2);
76 warn "WEB << param: ",dump( $param ) if $debug;
81 print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n$index_html";
82 } elsif ( $path =~ m{^/(examples/.+)} ) {
83 $path = $1; # FIXME prefix with dir for installation
85 warn "static $path $size bytes\n";
86 my $content_type = 'text/plain';
87 $content_type = 'application/javascript' if $path =~ /\.js/;
88 print $client "HTTP/1.0 200 OK\r\nContent-Type: $content_type\r\nContent-Length: $size\r\n\r\n";
91 open(my $fh, '<', $path) || die "can't open $path: $!";
97 } elsif ( $method =~ m{/scan} ) {
98 my $tags = $rfid->scan || {};
99 my $json = { time => time() };
100 foreach my $tag ( keys %$tags ) {
101 my $hash = RFID::Biblio::RFID501->to_hash( $tags->{$tag} );
103 $hash->{security} = uc unpack 'H*', $rfid->read_afi( $tag )
104 if $rfid->can('read_afi');
105 push @{ $json->{tags} }, $hash;
107 warn "#### ", encode_json($json);
108 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
109 $param->{callback}, "(", encode_json($json), ")\r\n";
110 } elsif ( $method =~ m{/program} ) {
112 my $status = 501; # Not implementd
114 foreach my $p ( keys %$param ) {
115 next unless $p =~ m/^(E[0-9A-F]{15})$/;
117 my $content = RFID::Biblio::RFID501->from_hash({ content => $param->{$p} });
118 $content = RFID::Biblio::RFID501->blank if $param->{$p} eq 'blank';
121 warn "PROGRAM $tag $content\n";
122 $rfid->write_blocks( $tag => $content );
123 $rfid->write_afi( $tag => chr( $param->{$p} =~ /^130/ ? 0xDA : 0xD7 ) );
126 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
128 } elsif ( $method =~ m{/secure(.js)} ) {
132 my $status = 501; # Not implementd
134 foreach my $p ( keys %$param ) {
135 next unless $p =~ m/^(E[0-9A-F]{15})$/;
137 my $data = $param->{$p};
140 warn "SECURE $tag $data\n";
141 $rfid->write_afi( $tag => hex($data) );
145 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
146 $param->{callback}, "({ ok: 1 })\r\n";
148 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
152 print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
155 print $client "HTTP/1.0 500 No method\r\n\r\n";
168 <title>RFID JSONP</title>
169 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
170 <style type="text/css">
183 label[for=pull-reader] {
191 <script type="text/javascript">
194 if(!window.console) {
195 window.console = new function() {
196 this.info = function(str) {};
197 this.debug = function(str) {};
202 function got_visible_tags(data,textStatus) {
203 var html = 'No tags in range';
205 html = '<ul class="tags">';
206 $.each(data.tags, function(i,tag) {
207 console.debug( i, tag );
208 html += '<li><tt class=' + tag.security + '>' + tag.sid;
210 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>';
212 html += '<form method=get action=program style="display:inline">'
213 + '<input type=hidden name='+tag.sid+' value="blank">'
214 + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
219 html += ' <form method=get action=program style="display:inline">'
220 + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
221 + '<input type=text name='+tag.sid+' size=12>'
222 + '<input type=submit value="Program">'
230 var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
232 html = '<div class=status>'
234 + ' &#' + arrows[ data.time % arrows.length ] + ';'
238 $('#tags').html( html );
239 window.setTimeout(function(){
241 },200); // re-scan every 200ms
244 function scan_tags() {
245 console.info('scan_tags');
246 if ( $('input#pull-reader').attr('checked') )
247 $.getJSON("/scan?callback=?", got_visible_tags);
250 $(document).ready(function() {
251 $('input#pull-reader').click( function() {
254 $('input#pull-reader').attr('checked', true); // force check on load
256 $('div#tags').click( function() {
257 $('input#pull-reader').attr('checked', false);
266 <h1>RFID tags in range</h1>
268 <label for=pull-reader>
269 <input id=pull-reader type=checkbox checked=1>
274 RFID reader not found or driver program not started.