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::3M810;
29 my $rfid = RFID::Serial::3M810->new;
39 my $server = IO::Socket::INET->new(
41 LocalPort => $listen_port,
46 die "can't setup server: $!" unless $server;
48 print "Server $0 ready at $server_url\n";
50 while (my $client = $server->accept()) {
51 $client->autoflush(1);
52 my $request = <$client>;
54 warn "WEB << $request\n" if $debug;
57 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
60 if ( $method =~ s{\?(.+)}{} ) {
61 foreach my $p ( split(/[&;]/, $1) ) {
62 my ($n,$v) = split(/=/, $p, 2);
65 warn "WEB << param: ",dump( $param ) if $debug;
70 print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n$index_html";
71 } elsif ( $method =~ m{/scan} ) {
72 my $tags = $rfid->scan;
77 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
78 $param->{callback}, "(", to_json($json), ")\r\n";
79 } elsif ( $method =~ m{/program} ) {
81 my $status = 501; # Not implementd
83 foreach my $p ( keys %$param ) {
84 next unless $p =~ m/^(E[0-9A-F]{15})$/;
86 my $content = "\x04\x11\x00\x01" . $param->{$p};
87 $content = "\x00" if $param->{$p} eq 'blank';
90 warn "PROGRAM $tag $content\n";
91 write_tag( $tag, $content );
92 secure_tag_with( $tag, $param->{$p} =~ /^130/ ? 'DA' : 'D7' );
95 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
97 } elsif ( $method =~ m{/secure(.js)} ) {
101 my $status = 501; # Not implementd
103 foreach my $p ( keys %$param ) {
104 next unless $p =~ m/^(E[0-9A-F]{15})$/;
106 my $data = $param->{$p};
109 warn "SECURE $tag $data\n";
110 secure_tag_with( $tag, $data );
114 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
115 $param->{callback}, "({ ok: 1 })\r\n";
117 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
121 print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
124 print $client "HTTP/1.0 500 No method\r\n\r\n";
137 <title>RFID JSONP</title>
138 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
139 <style type="text/css">
152 label[for=pull-reader] {
160 <script type="text/javascript">
162 function got_visible_tags(data,textStatus) {
163 var html = 'No tags in range';
165 html = '<ul class="tags">';
166 $.each(data.tags, function(i,tag) {
167 console.debug( i, tag );
168 html += '<li><tt class=' + tag.security + '>' + tag.sid;
170 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>';
172 html += '<form method=get action=program style="display:inline">'
173 + '<input type=hidden name='+tag.sid+' value="blank">'
174 + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
179 html += ' <form method=get action=program style="display:inline">'
180 + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
181 + '<input type=text name='+tag.sid+' size=12>'
182 + '<input type=submit value="Program">'
190 var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
192 html = '<div class=status>'
194 + ' &#' + arrows[ data.time % arrows.length ] + ';'
198 $('#tags').html( html );
199 window.setTimeout(function(){
201 },200); // re-scan every 200ms
204 function scan_tags() {
205 console.info('scan_tags');
206 if ( $('input#pull-reader').attr('checked') )
207 $.getJSON("/scan?callback=?", got_visible_tags);
210 $(document).ready(function() {
211 $('input#pull-reader').click( function() {
214 $('input#pull-reader').attr('checked', true); // force check on load
216 $('div#tags').click( function() {
217 $('input#pull-reader').attr('checked', false);
226 <h1>RFID tags in range</h1>
228 <label for=pull-reader>
229 <input id=pull-reader type=checkbox checked=1>
234 RFID reader not found or driver program not started.