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