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