c5b07f7f61e529711aca1b3d4ab0238fa725e599
[Biblio-RFID.git] / scripts / RFID-JSONP-server.pl
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 RFID-JSONP-server - simpliest possible JSONP server which provides local web interface to RFID readers
6
7 =head1 USAGE
8
9   ./scripts/RFID-JSONP-server.pl [--debug] [--listen=127.0.0.1:9000] [--reader=filter]
10
11 =cut
12
13 use strict;
14 use warnings;
15
16 use Data::Dump qw/dump/;
17
18 use JSON::XS;
19 use IO::Socket::INET;
20 use LWP::UserAgent;
21 use URI;
22 use POSIX qw(strftime);
23 use Encode;
24
25 my $debug = 1;
26 my $listen = '127.0.0.1:9000';
27 $listen = ':9000';
28 my $reader;
29 my $koha_url = 'http://ffzg.koha-dev.rot13.org:8080';
30 # internal URL so we can find local address of machine and vmware NAT
31 my $rfid_url = 'http://rfid.koha-dev.vbz.ffzg.hr';
32 my $sip2 = {
33         server   => '10.60.0.11:6002', # must be IP!
34 #       user     => 'sip2-user',
35 #       password => 'sip2-passwd',
36         user     => 'sip2user',
37         password => 'viva2koha',
38         loc      => 'FFZG',
39 };
40 my $afi = {
41         secure   => 0xDA,
42         unsecure => 0xD7,
43 };
44
45 use Getopt::Long;
46
47 GetOptions(
48         'debug!'    => \$debug,
49         'listen=s', => \$listen,
50         'reader=s', => \$reader,
51         'koha=s',       => \$koha_url,
52 ) || die $!;
53
54 our $rfid_sid_cache;
55
56 sub rfid_borrower {
57         my $hash = shift;
58         if ( my $json = $rfid_sid_cache->{ $hash->{sid} } ) {
59                 return $json;
60         }
61         my $ua = LWP::UserAgent->new;
62         my $url = URI->new( $koha_url . '/cgi-bin/koha/ffzg/rfid/borrower.pl');
63         $url->query_form(
64                   RFID_SID => $hash->{sid}
65                 , OIB => $hash->{OIB}
66                 , JMBAG => $hash->{JMBAG}
67         );
68         warn "GET ",$url->as_string;
69         my $response = $ua->get($url);
70         if ( $response->is_success ) {
71                 my $json = decode_json $response->decoded_content;
72                 $rfid_sid_cache->{ $hash->{sid} } = $json;
73                 return $json;
74         } else {
75                 warn "ERROR ", $response->status_line;
76         }
77 }
78
79
80 sub sip2_message {
81         my $send = shift;
82
83         my $sock = $sip2->{sock} || die "no sip2 socket";
84
85         local $/ = "\r";
86
87         $send .= "\r" unless $send =~ m/\r$/;
88         warn "SIP2 >>>> ",dump($send), "\n";
89         print $sock $send;
90         $sock->flush;
91         
92         my $expect = substr($send,0,2) | 0x01;
93
94         my $in = <$sock>;
95         $in =~ s/^\n//;
96         warn "SIP2 <<<< ",dump($in), "\n";
97
98         die "expected $expect" unless substr($in,0,2) != $expect;
99
100         $in =~ s/\r$//;
101
102         my $hash;
103         if ( $in =~ s/^([0-9\s]+)// ) {
104                 $hash->{fixed} = $1;
105         }
106         foreach ( split(/\|/, $in ) ) {
107                 my ( $f, $v ) = ( $1, $2 ) if m/([A-Z]{2})(.+)/;
108                 $hash->{$f} = decode('utf-8',$v);
109         }
110
111         warn "# sip2 hash response ",dump($hash);
112
113         return $hash;
114 }
115
116 if ( my $server = $sip2->{server} ) {
117         my $sock = $sip2->{sock} = IO::Socket::INET->new( $server ) || die "can't connect to $server: $!";
118         warn "SIP2 server ", $sock->peerhost, ":", $sock->peerport, "\n";
119
120         # login
121         if ( sip2_message("9300CN$sip2->{user}|CO$sip2->{password}|")->{fixed} !~ m/^941/ ) {
122                 die "SIP2 login failed";
123         }
124
125 }
126
127 use lib 'lib';
128 use Biblio::RFID::RFID501;
129 use Biblio::RFID::Reader;
130 my $rfid = Biblio::RFID::Reader->new( shift @ARGV );
131 $rfid->debug( $debug );
132
133 my $index_html;
134 {
135         local $/ = undef;
136         $index_html = <DATA>;
137         $index_html =~ s{http://koha.example.com:8080}{$koha_url}sg;
138 }
139
140 my $server_url;
141
142 sub http_server {
143
144         my $server = IO::Socket::INET->new(
145                 Proto     => 'tcp',
146                 LocalAddr => $listen,
147                 Listen    => SOMAXCONN,
148                 Reuse     => 1
149         );
150                                                                   
151         die "can't setup server: $!" unless $server;
152
153         $server_url = 'http://' . $listen;
154         print "Server $0 ready at $server_url\n";
155
156         while (my $client = $server->accept()) {
157
158             eval { # don't die inside here!
159
160                 $client->autoflush(1);
161                 my $request = <$client>;
162
163                 warn "WEB << $request\n" if $debug;
164                 my $path;
165
166                 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
167                         my $method = $1;
168                         my $param;
169                         if ( $method =~ s{\?(.+)}{} ) {
170                                 foreach my $p ( split(/[&;]/, $1) ) {
171                                         my ($n,$v) = split(/=/, $p, 2);
172                                         $param->{$n} = $v;
173                                 }
174                                 warn "WEB << param: ",dump( $param ) if $debug;
175                         }
176                         $path = $method;
177
178                         if ( $path eq '/' ) {
179                                 print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n$index_html";
180                         } elsif ( $path =~ m{^/(examples/.+)} ) {
181                                 $path = $1; # FIXME prefix with dir for installation
182                                 my $size = -s $path;
183                                 warn "static $path $size bytes\n";
184                                 my $content_type = 'text/plain';
185                                 $content_type = 'application/javascript' if $path =~ /\.js$/;
186                                 $content_type = 'text/html' if $path =~ /\.html$/;
187                                 print $client "HTTP/1.0 200 OK\r\nContent-Type: $content_type\r\nContent-Length: $size\r\n\r\n";
188                                 {
189                                         local $/ = undef;
190                                         open(my $fh, '<', $path) || die "can't open $path: $!";
191                                         while(<$fh>) {
192                                                 print $client $_;
193                                         }
194                                         close($fh);
195                                 }
196                         } elsif ( $method =~ m{/scan} ) {
197                                 my @tags = $rfid->tags;
198                                 my $json = { time => time() };
199                                 foreach my $tag ( @tags ) {
200                                         my $hash = $rfid->to_hash( $tag );
201                                         $hash->{sid}  = $tag;
202                                         $hash->{reader} = $rfid->from_reader( $tag );
203                                         if ( $hash->{tag_type} eq 'SmartX' ) {
204                                                 my $borrower = rfid_borrower $hash;
205                                                 if ( exists $borrower->{error} ) {
206                                                         warn "ERROR ", dump($borrower);
207                                                 } else {
208                                                         $hash->{borrower} = $borrower->{borrower};
209                                                         $hash->{content}  = $borrower->{borrower}->{cardnumber}; # compatibile with 3M tags
210                                                 }
211                                         } else {
212                                                 $hash->{security} = uc unpack 'H*', $rfid->afi( $tag );
213                                         }
214                                         push @{ $json->{tags} }, $hash;
215                                 };
216                                 warn "#### ", encode_json($json);
217                                 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
218                                         $param->{callback}, "(", encode_json($json), ")\r\n";
219                         } elsif ( $method =~ m{/program} ) {
220
221                                 my $status = 501; # Not implementd
222
223                                 foreach my $p ( keys %$param ) {
224                                         next unless $p =~ m/^(E[0-9A-F]{15})$/;
225                                         my $tag = $1;
226                                         my $content = Biblio::RFID::RFID501->from_hash({ content => $param->{$p} });
227                                         $content    = Biblio::RFID::RFID501->blank if $param->{$p} eq 'blank';
228                                         $status = 302;
229
230                                         warn "PROGRAM $tag $content\n";
231                                         $rfid->write_blocks( $tag => $content );
232                                         $rfid->write_afi(    $tag => chr( $param->{$p} =~ /^130/ ? $afi->{secure} : $afi->{unsecure} ) );
233                                 }
234
235                                 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
236
237                         } elsif ( $method =~ m{/secure(.js)} ) {
238
239                                 my $json = $1;
240
241                                 my $status = 501; # Not implementd
242
243                                 foreach my $p ( keys %$param ) {
244                                         next unless $p =~ m/^(E[0-9A-F]{15})$/;
245                                         my $tag = $1;
246                                         my $data = $param->{$p};
247                                         $status = 302;
248
249                                         warn "SECURE $tag $data\n";
250                                         $rfid->write_afi( $tag => chr(hex($data)) );
251                                 }
252
253                                 if ( $json ) {
254                                         print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
255                                                 $param->{callback}, "({ ok: 1 })\r\n";
256                                 } else {
257                                         print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
258                                 }
259
260                         } elsif ( $method =~ m{/sip2/(\w+)/(.+)} ) {
261                                 my ( $method, $args ) = ( $1, $2 );
262                                 warn "SIP2: $method [$args]";
263
264                                 my $ts = strftime('%Y%m%d    %H%M%S', localtime());
265                                 my $loc      = $sip2->{loc} || die "missing sip->{loc}";
266                                 my $password = $sip2->{password} || die "missing sip->{password}";
267
268                                 my $hash;
269
270                                 if ( $method eq 'patron_info' ) {
271                                         my $patron = $args;
272                                         $hash = sip2_message("63000${ts}          AO$loc|AA$patron|AC$password|");
273
274                                 } elsif ( $method eq 'checkout' ) {
275                                         my ($patron,$barcode,$sid) = split(/\//, $args, 3);
276                                         $hash = sip2_message("11YN${ts}                  AO$loc|AA$patron|AB$barcode|AC$password|BON|BIN|");
277                                         if ( substr( $hash->{fixed}, 2, 1 ) == 1 ) {
278                                                 $rfid->write_afi( $sid => chr( $afi->{unsecure} ) );
279                                         }
280
281                                 } elsif ( $method eq 'checkin' ) {
282                                         my ($patron,$barcode,$sid) = split(/\//, $args, 3);
283                                         $hash = sip2_message("09N${ts}${ts}AP|AO${loc}|AB$barcode|AC|BIN|");
284                                         if ( substr( $hash->{fixed}, 2, 1 ) == 1 ) {
285                                                 $rfid->write_afi( $sid => chr( $afi->{secure} ) );
286                                         }
287                                 } else {
288                                         print $client "HTTP/1.0 500 $method not implemented\r\n\r\n";
289                                 }
290
291                                 if ( $hash ) {
292                                         print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
293                                                 encode_json( $hash );
294                                 }
295
296                         } else {
297                                 print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
298                         }
299                 } else {
300                         print $client "HTTP/1.0 500 No method\r\n\r\n";
301                 }
302                 close $client;
303
304             }; # end of eval
305             if ( $@ ) {
306                 print $client "HTTP/1.0 500 Error\r\n\r\nContent-Type: text/plain\r\n$@";
307                 warn "ERROR: $@";
308             }
309
310         }
311
312         die "server died";
313 }
314
315 sub rfid_register {
316         my $ip;
317
318         foreach ( split(/\n/, `ip addr` ) ) {
319                 if ( /^\d:\s(\w+):\s/ ) {
320                         $ip->{_last} = $1;
321                 } elsif ( /^\s+inet\s((\d+)\.(\d+)\.(\d+)\.(\d+))\/(\d+)/ ) {
322                         $ip->{ $ip->{_last} } = $1;
323                 } else {
324                         #warn "# SKIP [$_]\n";
325                 }
326         }
327
328         warn dump($ip);
329
330         my $ua = LWP::UserAgent->new;
331         my $url = URI->new( $rfid_url . '/register.pl');
332         $url->query_form(
333                 local_ip => $ip->{eth0} || $ip->{ (keys %$ip)[0] },
334         );
335         warn "GET ",$url->as_string;
336         my $response = $ua->get($url);
337         if ( $response->is_success ) {
338                 warn "# ", $response->decoded_content;
339                 my $json = decode_json $response->decoded_content;
340                 warn "REGISTER: ",dump($json);
341                 return $json;
342         } else {
343                 warn "ERROR ", $response->status_line;
344         }
345 }
346
347 rfid_register;
348 http_server;
349
350 __DATA__
351 <html>
352 <head>
353 <title>RFID JSONP</title>
354 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
355 <style type="text/css">
356 .status {
357         background: #ff8;
358 }
359
360 .da {
361         background: #fcc;
362 }
363
364 .d7 {
365         background: #cfc;
366 }
367
368 label[for=pull-reader] {
369         position: absolute;
370         top: 1em;
371         right: 1em;
372         background: #eee;
373 }
374
375 </style>
376 <script type="text/javascript">
377
378 // mock console
379 if(!window.console) {
380         window.console = new function() {
381                 this.info = function(str) {};
382                 this.debug = function(str) {};
383         };
384 }
385
386
387 function got_visible_tags(data,textStatus) {
388         var html = 'No tags in range';
389         if ( data.tags ) {
390                 html = '<ul class="tags">';
391                 $.each(data.tags, function(i,tag) {
392                         console.debug( i, tag );
393                         html += '<li><tt class="' + tag.security + '">' + tag.sid;
394                         var content = tag.content || tag.borrower.cardnumber;
395
396                         if ( content ) {
397                                 html += ' <a href="http://koha.example.com:8080/cgi-bin/koha/';
398                                 if ( tag.type == 1 ) { // book
399                                         html += 'catalogue/search.pl?q=';
400                                 } else {
401                                         html += 'members/member.pl?member=';
402                                 }
403                                 html += content + '" title="lookup in Koha" target="koha-lookup">' + content + '</a>';
404                                 html += '</tt>';
405 /*
406                                 html += '<form method=get action=program style="display:inline">'
407                                         + '<input type=hidden name='+tag.sid+' value="blank">'
408                                         + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
409                                         + '</form>'
410                                 ;
411 */
412                         } else {
413                                 html += '</tt>';
414                                 html += ' <form method=get action=program style="display:inline">'
415                                         + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
416                                         + '<input type=text name='+tag.sid+' size=12>'
417                                         + '<input type=submit value="Program">'
418                                         + '</form>'
419                                 ;
420                         }
421                 });
422                 html += '</ul>';
423         }
424
425         var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
426
427         html = '<div class=status>'
428                 + textStatus
429                 + ' &#' + arrows[ data.time % arrows.length ] + ';'
430                 + '</div>'
431                 + html
432                 ;
433         $('#tags').html( html );
434         window.setTimeout(function(){
435                 scan_tags();
436         },200); // re-scan every 200ms
437 };
438
439 function scan_tags() {
440         console.info('scan_tags');
441         if ( $('input#pull-reader').attr('checked') )
442                 $.getJSON("/scan?callback=?", got_visible_tags);
443 }
444
445 $(document).ready(function() {
446                 $('input#pull-reader').click( function() {
447                         scan_tags();
448                 });
449                 $('input#pull-reader').attr('checked', true); // force check on load
450
451                 $('div#tags').click( function() {
452                         $('input#pull-reader').attr('checked', false);
453                 } );
454
455                 scan_tags();
456 });
457 </script>
458 </head>
459 <body>
460
461 <h1>RFID tags in range</h1>
462
463 <label for=pull-reader>
464 <input id=pull-reader type=checkbox checked=1>
465 active
466 </label>
467
468 <div id="tags">
469 RFID reader not found or driver program not started.
470 </div>
471
472 </body>
473 </html>