5 RFID-JSONP-server - simpliest possible JSONP server which provides local web interface to RFID readers
9 ./scripts/RFID-JSONP-server.pl [--debug] [--listen=127.0.0.1:9000] [--reader=filter]
16 use Data::Dump qw/dump/;
24 my $listen = '127.0.0.1:9000';
32 'listen=s', => \$listen,
33 'reader=s', => \$reader,
40 if ( my $json = $rfid_sid_cache->{ $hash->{sid} } ) {
43 my $ua = LWP::UserAgent->new;
44 my $url = URI->new('http://ffzg.koha-dev.rot13.org:8080/cgi-bin/koha/ffzg/rfid-borrower.pl');
46 RFID_SID => $hash->{sid}
48 , JMBAG => $hash->{JMBAG}
50 warn "GET ",$url->as_string;
51 my $response = $ua->get($url);
52 if ( $response->is_success ) {
53 my $json = decode_json $response->decoded_content;
54 $rfid_sid_cache->{ $hash->{sid} } = $json;
57 warn "ERROR ", $response->status_line;
62 use Biblio::RFID::RFID501;
63 use Biblio::RFID::Reader;
64 my $rfid = Biblio::RFID::Reader->new( shift @ARGV );
76 my $server = IO::Socket::INET->new(
83 die "can't setup server: $!" unless $server;
85 $server_url = 'http://' . $listen;
86 print "Server $0 ready at $server_url\n";
88 while (my $client = $server->accept()) {
89 $client->autoflush(1);
90 my $request = <$client>;
92 warn "WEB << $request\n" if $debug;
95 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
98 if ( $method =~ s{\?(.+)}{} ) {
99 foreach my $p ( split(/[&;]/, $1) ) {
100 my ($n,$v) = split(/=/, $p, 2);
103 warn "WEB << param: ",dump( $param ) if $debug;
107 if ( $path eq '/' ) {
108 print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n$index_html";
109 } elsif ( $path =~ m{^/(examples/.+)} ) {
110 $path = $1; # FIXME prefix with dir for installation
112 warn "static $path $size bytes\n";
113 my $content_type = 'text/plain';
114 $content_type = 'application/javascript' if $path =~ /\.js/;
115 print $client "HTTP/1.0 200 OK\r\nContent-Type: $content_type\r\nContent-Length: $size\r\n\r\n";
118 open(my $fh, '<', $path) || die "can't open $path: $!";
124 } elsif ( $method =~ m{/scan} ) {
125 my @tags = $rfid->tags;
126 my $json = { time => time() };
127 foreach my $tag ( @tags ) {
128 my $hash = $rfid->to_hash( $tag );
130 if ( $hash->{tag_type} eq 'SmartX' ) {
131 my $borrower = rfid_borrower $hash;
132 if ( exists $borrower->{error} ) {
133 warn "ERROR ", dump($borrower);
135 $hash->{borrower} = $borrower->{borrower};
138 $hash->{security} = uc unpack 'H*', $rfid->afi( $tag );
140 push @{ $json->{tags} }, $hash;
142 warn "#### ", encode_json($json);
143 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
144 $param->{callback}, "(", encode_json($json), ")\r\n";
145 } elsif ( $method =~ m{/program} ) {
147 my $status = 501; # Not implementd
149 foreach my $p ( keys %$param ) {
150 next unless $p =~ m/^(E[0-9A-F]{15})$/;
152 my $content = Biblio::RFID::RFID501->from_hash({ content => $param->{$p} });
153 $content = Biblio::RFID::RFID501->blank if $param->{$p} eq 'blank';
156 warn "PROGRAM $tag $content\n";
157 $rfid->write_blocks( $tag => $content );
158 $rfid->write_afi( $tag => chr( $param->{$p} =~ /^130/ ? 0xDA : 0xD7 ) );
161 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
163 } elsif ( $method =~ m{/secure(.js)} ) {
167 my $status = 501; # Not implementd
169 foreach my $p ( keys %$param ) {
170 next unless $p =~ m/^(E[0-9A-F]{15})$/;
172 my $data = $param->{$p};
175 warn "SECURE $tag $data\n";
176 $rfid->write_afi( $tag => hex($data) );
180 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
181 $param->{callback}, "({ ok: 1 })\r\n";
183 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
187 print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
190 print $client "HTTP/1.0 500 No method\r\n\r\n";
203 <title>RFID JSONP</title>
204 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
205 <style type="text/css">
218 label[for=pull-reader] {
226 <script type="text/javascript">
229 if(!window.console) {
230 window.console = new function() {
231 this.info = function(str) {};
232 this.debug = function(str) {};
237 function got_visible_tags(data,textStatus) {
238 var html = 'No tags in range';
240 html = '<ul class="tags">';
241 $.each(data.tags, function(i,tag) {
242 console.debug( i, tag );
243 html += '<li><tt class="' + tag.security + '">' + tag.sid;
244 var borrowernumber = tag.content || tag.borrower.cardnumber;
246 if ( borrowernumber ) {
247 html += ' <a href="http://ffzg.koha-dev.rot13.org:8080/cgi-bin/koha/members/member.pl?member=' + borrowernumber + '" title="lookup in Koha" target="koha-lookup">' + borrowernumber + '</a>';
250 html += '<form method=get action=program style="display:inline">'
251 + '<input type=hidden name='+tag.sid+' value="blank">'
252 + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
258 html += ' <form method=get action=program style="display:inline">'
259 + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
260 + '<input type=text name='+tag.sid+' size=12>'
261 + '<input type=submit value="Program">'
269 var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
271 html = '<div class=status>'
273 + ' &#' + arrows[ data.time % arrows.length ] + ';'
277 $('#tags').html( html );
278 window.setTimeout(function(){
280 },200); // re-scan every 200ms
283 function scan_tags() {
284 console.info('scan_tags');
285 if ( $('input#pull-reader').attr('checked') )
286 $.getJSON("/scan?callback=?", got_visible_tags);
289 $(document).ready(function() {
290 $('input#pull-reader').click( function() {
293 $('input#pull-reader').attr('checked', true); // force check on load
295 $('div#tags').click( function() {
296 $('input#pull-reader').attr('checked', false);
305 <h1>RFID tags in range</h1>
307 <label for=pull-reader>
308 <input id=pull-reader type=checkbox checked=1>
313 RFID reader not found or driver program not started.