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