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/;
23 my $listen = '127.0.0.1:9000';
25 my $reader = shift @ARGV;
28 use RFID::Biblio::RFID501;
29 use RFID::Biblio::Readers;
30 my $rfid = (RFID::Biblio::Readers->available( $reader ))[0]; # FIXME
31 warn "using $rfid reader\n";
43 my $server = IO::Socket::INET->new(
50 die "can't setup server: $!" unless $server;
52 $server_url = 'http://' . $listen;
53 print "Server $0 ready at $server_url\n";
55 while (my $client = $server->accept()) {
56 $client->autoflush(1);
57 my $request = <$client>;
59 warn "WEB << $request\n" if $debug;
62 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
65 if ( $method =~ s{\?(.+)}{} ) {
66 foreach my $p ( split(/[&;]/, $1) ) {
67 my ($n,$v) = split(/=/, $p, 2);
70 warn "WEB << param: ",dump( $param ) if $debug;
75 print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n$index_html";
76 } elsif ( $path =~ m{^/(examples/.+)} ) {
77 $path = $1; # FIXME prefix with dir for installation
79 warn "static $path $size bytes\n";
80 my $content_type = 'text/plain';
81 $content_type = 'application/javascript' if $path =~ /\.js/;
82 print $client "HTTP/1.0 200 OK\r\nContent-Type: $content_type\r\nContent-Length: $size\r\n\r\n";
85 open(my $fh, '<', $path) || die "can't open $path: $!";
91 } elsif ( $method =~ m{/scan} ) {
92 my $tags = $rfid->scan;
93 my $json = { time => time() };
94 foreach my $tag ( keys %$tags ) {
95 my $hash = RFID::Biblio::RFID501->to_hash( $tags->{$tag} );
97 $hash->{security} = uc unpack 'H*', $rfid->read_afi( $tag );
98 push @{ $json->{tags} }, $hash;
100 warn "#### ", encode_json($json);
101 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
102 $param->{callback}, "(", encode_json($json), ")\r\n";
103 } elsif ( $method =~ m{/program} ) {
105 my $status = 501; # Not implementd
107 foreach my $p ( keys %$param ) {
108 next unless $p =~ m/^(E[0-9A-F]{15})$/;
110 my $content = RFID::Biblio::RFID501->from_hash({ content => $param->{$p} });
111 $content = RFID::Biblio::RFID501->blank if $param->{$p} eq 'blank';
114 warn "PROGRAM $tag $content\n";
115 $rfid->write_blocks( $tag => $content );
116 $rfid->write_afi( $tag => chr( $param->{$p} =~ /^130/ ? 0xDA : 0xD7 ) );
119 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
121 } elsif ( $method =~ m{/secure(.js)} ) {
125 my $status = 501; # Not implementd
127 foreach my $p ( keys %$param ) {
128 next unless $p =~ m/^(E[0-9A-F]{15})$/;
130 my $data = $param->{$p};
133 warn "SECURE $tag $data\n";
134 secure_tag_with( $tag, $data );
138 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
139 $param->{callback}, "({ ok: 1 })\r\n";
141 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
145 print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
148 print $client "HTTP/1.0 500 No method\r\n\r\n";
161 <title>RFID JSONP</title>
162 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
163 <style type="text/css">
176 label[for=pull-reader] {
184 <script type="text/javascript">
187 if(!window.console) {
188 window.console = new function() {
189 this.info = function(str) {};
190 this.debug = function(str) {};
195 function got_visible_tags(data,textStatus) {
196 var html = 'No tags in range';
198 html = '<ul class="tags">';
199 $.each(data.tags, function(i,tag) {
200 console.debug( i, tag );
201 html += '<li><tt class=' + tag.security + '>' + tag.sid;
203 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>';
205 html += '<form method=get action=program style="display:inline">'
206 + '<input type=hidden name='+tag.sid+' value="blank">'
207 + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
212 html += ' <form method=get action=program style="display:inline">'
213 + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
214 + '<input type=text name='+tag.sid+' size=12>'
215 + '<input type=submit value="Program">'
223 var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
225 html = '<div class=status>'
227 + ' &#' + arrows[ data.time % arrows.length ] + ';'
231 $('#tags').html( html );
232 window.setTimeout(function(){
234 },200); // re-scan every 200ms
237 function scan_tags() {
238 console.info('scan_tags');
239 if ( $('input#pull-reader').attr('checked') )
240 $.getJSON("/scan?callback=?", got_visible_tags);
243 $(document).ready(function() {
244 $('input#pull-reader').click( function() {
247 $('input#pull-reader').attr('checked', true); // force check on load
249 $('div#tags').click( function() {
250 $('input#pull-reader').attr('checked', false);
259 <h1>RFID tags in range</h1>
261 <label for=pull-reader>
262 <input id=pull-reader type=checkbox checked=1>
267 RFID reader not found or driver program not started.