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::Biblio::RFID501;
29 use RFID::Biblio::3M810;
30 my $rfid = RFID::Biblio::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::Biblio::RFID501->to_hash( $tags->{$tag} );
78 $hash->{security} = uc unpack 'H*', $rfid->read_afi( $tag );
79 push @{ $json->{tags} }, $hash;
81 warn "#### ", encode_json($json);
82 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
83 $param->{callback}, "(", encode_json($json), ")\r\n";
84 } elsif ( $method =~ m{/program} ) {
86 my $status = 501; # Not implementd
88 foreach my $p ( keys %$param ) {
89 next unless $p =~ m/^(E[0-9A-F]{15})$/;
91 my $content = "\x04\x11\x00\x01" . $param->{$p};
92 $content = "\x00" if $param->{$p} eq 'blank';
95 warn "PROGRAM $tag $content\n";
96 write_tag( $tag, $content );
97 secure_tag_with( $tag, $param->{$p} =~ /^130/ ? 'DA' : 'D7' );
100 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
102 } elsif ( $method =~ m{/secure(.js)} ) {
106 my $status = 501; # Not implementd
108 foreach my $p ( keys %$param ) {
109 next unless $p =~ m/^(E[0-9A-F]{15})$/;
111 my $data = $param->{$p};
114 warn "SECURE $tag $data\n";
115 secure_tag_with( $tag, $data );
119 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
120 $param->{callback}, "({ ok: 1 })\r\n";
122 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
126 print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
129 print $client "HTTP/1.0 500 No method\r\n\r\n";
142 <title>RFID JSONP</title>
143 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
144 <style type="text/css">
157 label[for=pull-reader] {
165 <script type="text/javascript">
168 if(!window.console) {
169 window.console = new function() {
170 this.info = function(str) {};
171 this.debug = function(str) {};
176 function got_visible_tags(data,textStatus) {
177 var html = 'No tags in range';
179 html = '<ul class="tags">';
180 $.each(data.tags, function(i,tag) {
181 console.debug( i, tag );
182 html += '<li><tt class=' + tag.security + '>' + tag.sid;
184 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>';
186 html += '<form method=get action=program style="display:inline">'
187 + '<input type=hidden name='+tag.sid+' value="blank">'
188 + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
193 html += ' <form method=get action=program style="display:inline">'
194 + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
195 + '<input type=text name='+tag.sid+' size=12>'
196 + '<input type=submit value="Program">'
204 var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
206 html = '<div class=status>'
208 + ' &#' + arrows[ data.time % arrows.length ] + ';'
212 $('#tags').html( html );
213 window.setTimeout(function(){
215 },200); // re-scan every 200ms
218 function scan_tags() {
219 console.info('scan_tags');
220 if ( $('input#pull-reader').attr('checked') )
221 $.getJSON("/scan?callback=?", got_visible_tags);
224 $(document).ready(function() {
225 $('input#pull-reader').click( function() {
228 $('input#pull-reader').attr('checked', true); // force check on load
230 $('div#tags').click( function() {
231 $('input#pull-reader').attr('checked', false);
240 <h1>RFID tags in range</h1>
242 <label for=pull-reader>
243 <input id=pull-reader type=checkbox checked=1>
248 RFID reader not found or driver program not started.