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";
28 use RFID::Serial::Decode::RFID501;
29 use RFID::Serial::3M810;
30 my $rfid = RFID::Serial::3M810->new;
40 my $server = IO::Socket::INET->new(
42 LocalPort => $listen_port,
47 die "can't setup server: $!" unless $server;
49 print "Server $0 ready at $server_url\n";
51 while (my $client = $server->accept()) {
52 $client->autoflush(1);
53 my $request = <$client>;
55 warn "WEB << $request\n" if $debug;
58 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
61 if ( $method =~ s{\?(.+)}{} ) {
62 foreach my $p ( split(/[&;]/, $1) ) {
63 my ($n,$v) = split(/=/, $p, 2);
66 warn "WEB << param: ",dump( $param ) if $debug;
71 print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n$index_html";
72 } elsif ( $method =~ m{/scan} ) {
73 my $tags = $rfid->scan;
74 my $json = { time => time() };
75 foreach my $tag ( keys %$tags ) {
76 my $hash = RFID::Serial::Decode::RFID501->to_hash( $tags->{$tag} );
78 push @{ $json->{tags} }, $hash;
80 warn "#### ", encode_json($json);
81 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
82 $param->{callback}, "(", encode_json($json), ")\r\n";
83 } elsif ( $method =~ m{/program} ) {
85 my $status = 501; # Not implementd
87 foreach my $p ( keys %$param ) {
88 next unless $p =~ m/^(E[0-9A-F]{15})$/;
90 my $content = "\x04\x11\x00\x01" . $param->{$p};
91 $content = "\x00" if $param->{$p} eq 'blank';
94 warn "PROGRAM $tag $content\n";
95 write_tag( $tag, $content );
96 secure_tag_with( $tag, $param->{$p} =~ /^130/ ? 'DA' : 'D7' );
99 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
101 } elsif ( $method =~ m{/secure(.js)} ) {
105 my $status = 501; # Not implementd
107 foreach my $p ( keys %$param ) {
108 next unless $p =~ m/^(E[0-9A-F]{15})$/;
110 my $data = $param->{$p};
113 warn "SECURE $tag $data\n";
114 secure_tag_with( $tag, $data );
118 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
119 $param->{callback}, "({ ok: 1 })\r\n";
121 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
125 print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
128 print $client "HTTP/1.0 500 No method\r\n\r\n";
141 <title>RFID JSONP</title>
142 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
143 <style type="text/css">
156 label[for=pull-reader] {
164 <script type="text/javascript">
166 function got_visible_tags(data,textStatus) {
167 var html = 'No tags in range';
169 html = '<ul class="tags">';
170 $.each(data.tags, function(i,tag) {
171 console.debug( i, tag );
172 html += '<li><tt class=' + tag.security + '>' + tag.sid;
174 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>';
176 html += '<form method=get action=program style="display:inline">'
177 + '<input type=hidden name='+tag.sid+' value="blank">'
178 + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
183 html += ' <form method=get action=program style="display:inline">'
184 + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
185 + '<input type=text name='+tag.sid+' size=12>'
186 + '<input type=submit value="Program">'
194 var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
196 html = '<div class=status>'
198 + ' &#' + arrows[ data.time % arrows.length ] + ';'
202 $('#tags').html( html );
203 window.setTimeout(function(){
205 },200); // re-scan every 200ms
208 function scan_tags() {
209 console.info('scan_tags');
210 if ( $('input#pull-reader').attr('checked') )
211 $.getJSON("/scan?callback=?", got_visible_tags);
214 $(document).ready(function() {
215 $('input#pull-reader').click( function() {
218 $('input#pull-reader').attr('checked', true); // force check on load
220 $('div#tags').click( function() {
221 $('input#pull-reader').attr('checked', false);
230 <h1>RFID tags in range</h1>
232 <label for=pull-reader>
233 <input id=pull-reader type=checkbox checked=1>
238 RFID reader not found or driver program not started.