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';
27 my $koha_url = 'http://ffzg.koha-dev.rot13.org:8080';
33 'listen=s', => \$listen,
34 'reader=s', => \$reader,
35 'koha=s', => \$koha_url,
42 if ( my $json = $rfid_sid_cache->{ $hash->{sid} } ) {
45 my $ua = LWP::UserAgent->new;
46 my $url = URI->new( $koha_url . '/cgi-bin/koha/ffzg/rfid-borrower.pl');
48 RFID_SID => $hash->{sid}
50 , JMBAG => $hash->{JMBAG}
52 warn "GET ",$url->as_string;
53 my $response = $ua->get($url);
54 if ( $response->is_success ) {
55 my $json = decode_json $response->decoded_content;
56 $rfid_sid_cache->{ $hash->{sid} } = $json;
59 warn "ERROR ", $response->status_line;
64 use Biblio::RFID::RFID501;
65 use Biblio::RFID::Reader;
66 my $rfid = Biblio::RFID::Reader->new( shift @ARGV );
72 $index_html =~ s{http://koha.example.com:8080}{$koha_url}sg;
79 my $server = IO::Socket::INET->new(
86 die "can't setup server: $!" unless $server;
88 $server_url = 'http://' . $listen;
89 print "Server $0 ready at $server_url\n";
91 while (my $client = $server->accept()) {
92 $client->autoflush(1);
93 my $request = <$client>;
95 warn "WEB << $request\n" if $debug;
98 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
101 if ( $method =~ s{\?(.+)}{} ) {
102 foreach my $p ( split(/[&;]/, $1) ) {
103 my ($n,$v) = split(/=/, $p, 2);
106 warn "WEB << param: ",dump( $param ) if $debug;
110 if ( $path eq '/' ) {
111 print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n$index_html";
112 } elsif ( $path =~ m{^/(examples/.+)} ) {
113 $path = $1; # FIXME prefix with dir for installation
115 warn "static $path $size bytes\n";
116 my $content_type = 'text/plain';
117 $content_type = 'application/javascript' if $path =~ /\.js/;
118 print $client "HTTP/1.0 200 OK\r\nContent-Type: $content_type\r\nContent-Length: $size\r\n\r\n";
121 open(my $fh, '<', $path) || die "can't open $path: $!";
127 } elsif ( $method =~ m{/scan} ) {
128 my @tags = $rfid->tags;
129 my $json = { time => time() };
130 foreach my $tag ( @tags ) {
131 my $hash = $rfid->to_hash( $tag );
133 if ( $hash->{tag_type} eq 'SmartX' ) {
134 my $borrower = rfid_borrower $hash;
135 if ( exists $borrower->{error} ) {
136 warn "ERROR ", dump($borrower);
138 $hash->{borrower} = $borrower->{borrower};
141 $hash->{security} = uc unpack 'H*', $rfid->afi( $tag );
143 push @{ $json->{tags} }, $hash;
145 warn "#### ", encode_json($json);
146 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
147 $param->{callback}, "(", encode_json($json), ")\r\n";
148 } elsif ( $method =~ m{/program} ) {
150 my $status = 501; # Not implementd
152 foreach my $p ( keys %$param ) {
153 next unless $p =~ m/^(E[0-9A-F]{15})$/;
155 my $content = Biblio::RFID::RFID501->from_hash({ content => $param->{$p} });
156 $content = Biblio::RFID::RFID501->blank if $param->{$p} eq 'blank';
159 warn "PROGRAM $tag $content\n";
160 $rfid->write_blocks( $tag => $content );
161 $rfid->write_afi( $tag => chr( $param->{$p} =~ /^130/ ? 0xDA : 0xD7 ) );
164 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
166 } elsif ( $method =~ m{/secure(.js)} ) {
170 my $status = 501; # Not implementd
172 foreach my $p ( keys %$param ) {
173 next unless $p =~ m/^(E[0-9A-F]{15})$/;
175 my $data = $param->{$p};
178 warn "SECURE $tag $data\n";
179 $rfid->write_afi( $tag => hex($data) );
183 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
184 $param->{callback}, "({ ok: 1 })\r\n";
186 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
190 print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
193 print $client "HTTP/1.0 500 No method\r\n\r\n";
206 <title>RFID JSONP</title>
207 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
208 <style type="text/css">
221 label[for=pull-reader] {
229 <script type="text/javascript">
232 if(!window.console) {
233 window.console = new function() {
234 this.info = function(str) {};
235 this.debug = function(str) {};
240 function got_visible_tags(data,textStatus) {
241 var html = 'No tags in range';
243 html = '<ul class="tags">';
244 $.each(data.tags, function(i,tag) {
245 console.debug( i, tag );
246 html += '<li><tt class="' + tag.security + '">' + tag.sid;
247 var borrowernumber = tag.content || tag.borrower.cardnumber;
249 if ( borrowernumber ) {
250 html += ' <a href="http://koha.example.com:8080/cgi-bin/koha/members/member.pl?member=' + borrowernumber + '" title="lookup in Koha" target="koha-lookup">' + borrowernumber + '</a>';
253 html += '<form method=get action=program style="display:inline">'
254 + '<input type=hidden name='+tag.sid+' value="blank">'
255 + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
261 html += ' <form method=get action=program style="display:inline">'
262 + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
263 + '<input type=text name='+tag.sid+' size=12>'
264 + '<input type=submit value="Program">'
272 var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
274 html = '<div class=status>'
276 + ' &#' + arrows[ data.time % arrows.length ] + ';'
280 $('#tags').html( html );
281 window.setTimeout(function(){
283 },200); // re-scan every 200ms
286 function scan_tags() {
287 console.info('scan_tags');
288 if ( $('input#pull-reader').attr('checked') )
289 $.getJSON("/scan?callback=?", got_visible_tags);
292 $(document).ready(function() {
293 $('input#pull-reader').click( function() {
296 $('input#pull-reader').attr('checked', true); // force check on load
298 $('div#tags').click( function() {
299 $('input#pull-reader').attr('checked', false);
308 <h1>RFID tags in range</h1>
310 <label for=pull-reader>
311 <input id=pull-reader type=checkbox checked=1>
316 RFID reader not found or driver program not started.