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_port = 9000; # pick something not in use
24 my $server_url = "http://localhost:$listen_port";
26 my $reader = shift @ARGV;
29 use RFID::Biblio::RFID501;
30 use RFID::Biblio::Readers;
31 my $rfid = (RFID::Biblio::Readers->available( $reader ))[0]; # FIXME
41 my $server = IO::Socket::INET->new(
43 LocalPort => $listen_port,
48 die "can't setup server: $!" unless $server;
50 print "Server $0 ready at $server_url\n";
52 while (my $client = $server->accept()) {
53 $client->autoflush(1);
54 my $request = <$client>;
56 warn "WEB << $request\n" if $debug;
59 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
62 if ( $method =~ s{\?(.+)}{} ) {
63 foreach my $p ( split(/[&;]/, $1) ) {
64 my ($n,$v) = split(/=/, $p, 2);
67 warn "WEB << param: ",dump( $param ) if $debug;
72 print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n$index_html";
73 } elsif ( $method =~ m{/scan} ) {
74 my $tags = $rfid->scan;
75 my $json = { time => time() };
76 foreach my $tag ( keys %$tags ) {
77 my $hash = RFID::Biblio::RFID501->to_hash( $tags->{$tag} );
79 $hash->{security} = uc unpack 'H*', $rfid->read_afi( $tag );
80 push @{ $json->{tags} }, $hash;
82 warn "#### ", encode_json($json);
83 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
84 $param->{callback}, "(", encode_json($json), ")\r\n";
85 } elsif ( $method =~ m{/program} ) {
87 my $status = 501; # Not implementd
89 foreach my $p ( keys %$param ) {
90 next unless $p =~ m/^(E[0-9A-F]{15})$/;
92 my $content = "\x04\x11\x00\x01" . $param->{$p};
93 $content = "\x00" if $param->{$p} eq 'blank';
96 warn "PROGRAM $tag $content\n";
97 write_tag( $tag, $content );
98 secure_tag_with( $tag, $param->{$p} =~ /^130/ ? 'DA' : 'D7' );
101 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
103 } elsif ( $method =~ m{/secure(.js)} ) {
107 my $status = 501; # Not implementd
109 foreach my $p ( keys %$param ) {
110 next unless $p =~ m/^(E[0-9A-F]{15})$/;
112 my $data = $param->{$p};
115 warn "SECURE $tag $data\n";
116 secure_tag_with( $tag, $data );
120 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
121 $param->{callback}, "({ ok: 1 })\r\n";
123 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
127 print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
130 print $client "HTTP/1.0 500 No method\r\n\r\n";
143 <title>RFID JSONP</title>
144 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
145 <style type="text/css">
158 label[for=pull-reader] {
166 <script type="text/javascript">
169 if(!window.console) {
170 window.console = new function() {
171 this.info = function(str) {};
172 this.debug = function(str) {};
177 function got_visible_tags(data,textStatus) {
178 var html = 'No tags in range';
180 html = '<ul class="tags">';
181 $.each(data.tags, function(i,tag) {
182 console.debug( i, tag );
183 html += '<li><tt class=' + tag.security + '>' + tag.sid;
185 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>';
187 html += '<form method=get action=program style="display:inline">'
188 + '<input type=hidden name='+tag.sid+' value="blank">'
189 + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
194 html += ' <form method=get action=program style="display:inline">'
195 + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
196 + '<input type=text name='+tag.sid+' size=12>'
197 + '<input type=submit value="Program">'
205 var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
207 html = '<div class=status>'
209 + ' &#' + arrows[ data.time % arrows.length ] + ';'
213 $('#tags').html( html );
214 window.setTimeout(function(){
216 },200); // re-scan every 200ms
219 function scan_tags() {
220 console.info('scan_tags');
221 if ( $('input#pull-reader').attr('checked') )
222 $.getJSON("/scan?callback=?", got_visible_tags);
225 $(document).ready(function() {
226 $('input#pull-reader').click( function() {
229 $('input#pull-reader').attr('checked', true); // force check on load
231 $('div#tags').click( function() {
232 $('input#pull-reader').attr('checked', false);
241 <h1>RFID tags in range</h1>
243 <label for=pull-reader>
244 <input id=pull-reader type=checkbox checked=1>
249 RFID reader not found or driver program not started.