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 ( $path =~ m{^/(examples/.+)} ) {
74 $path = $1; # FIXME prefix with dir for installation
76 warn "static $path $size bytes\n";
77 my $content_type = 'text/plain';
78 $content_type = 'application/javascript' if $path =~ /\.js/;
79 print $client "HTTP/1.0 200 OK\r\nContent-Type: $content_type\r\nContent-Length: $size\r\n\r\n";
82 open(my $fh, '<', $path) || die "can't open $path: $!";
88 } elsif ( $method =~ m{/scan} ) {
89 my $tags = $rfid->scan;
90 my $json = { time => time() };
91 foreach my $tag ( keys %$tags ) {
92 my $hash = RFID::Biblio::RFID501->to_hash( $tags->{$tag} );
94 $hash->{security} = uc unpack 'H*', $rfid->read_afi( $tag );
95 push @{ $json->{tags} }, $hash;
97 warn "#### ", encode_json($json);
98 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
99 $param->{callback}, "(", encode_json($json), ")\r\n";
100 } elsif ( $method =~ m{/program} ) {
102 my $status = 501; # Not implementd
104 foreach my $p ( keys %$param ) {
105 next unless $p =~ m/^(E[0-9A-F]{15})$/;
107 my $content = "\x04\x11\x00\x01" . $param->{$p};
108 $content = "\x00" if $param->{$p} eq 'blank';
111 warn "PROGRAM $tag $content\n";
112 write_tag( $tag, $content );
113 secure_tag_with( $tag, $param->{$p} =~ /^130/ ? 'DA' : 'D7' );
116 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
118 } elsif ( $method =~ m{/secure(.js)} ) {
122 my $status = 501; # Not implementd
124 foreach my $p ( keys %$param ) {
125 next unless $p =~ m/^(E[0-9A-F]{15})$/;
127 my $data = $param->{$p};
130 warn "SECURE $tag $data\n";
131 secure_tag_with( $tag, $data );
135 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
136 $param->{callback}, "({ ok: 1 })\r\n";
138 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
142 print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
145 print $client "HTTP/1.0 500 No method\r\n\r\n";
158 <title>RFID JSONP</title>
159 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
160 <style type="text/css">
173 label[for=pull-reader] {
181 <script type="text/javascript">
184 if(!window.console) {
185 window.console = new function() {
186 this.info = function(str) {};
187 this.debug = function(str) {};
192 function got_visible_tags(data,textStatus) {
193 var html = 'No tags in range';
195 html = '<ul class="tags">';
196 $.each(data.tags, function(i,tag) {
197 console.debug( i, tag );
198 html += '<li><tt class=' + tag.security + '>' + tag.sid;
200 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>';
202 html += '<form method=get action=program style="display:inline">'
203 + '<input type=hidden name='+tag.sid+' value="blank">'
204 + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
209 html += ' <form method=get action=program style="display:inline">'
210 + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
211 + '<input type=text name='+tag.sid+' size=12>'
212 + '<input type=submit value="Program">'
220 var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
222 html = '<div class=status>'
224 + ' &#' + arrows[ data.time % arrows.length ] + ';'
228 $('#tags').html( html );
229 window.setTimeout(function(){
231 },200); // re-scan every 200ms
234 function scan_tags() {
235 console.info('scan_tags');
236 if ( $('input#pull-reader').attr('checked') )
237 $.getJSON("/scan?callback=?", got_visible_tags);
240 $(document).ready(function() {
241 $('input#pull-reader').click( function() {
244 $('input#pull-reader').attr('checked', true); // force check on load
246 $('div#tags').click( function() {
247 $('input#pull-reader').attr('checked', false);
256 <h1>RFID tags in range</h1>
258 <label for=pull-reader>
259 <input id=pull-reader type=checkbox checked=1>
264 RFID reader not found or driver program not started.