register RFID reader using intranet address
[Biblio-RFID.git] / scripts / RFID-JSONP-server.pl
index 08258d1..8d44048 100755 (executable)
@@ -24,6 +24,9 @@ my $debug = 1;
 my $listen = '127.0.0.1:9000';
 $listen = ':9000';
 my $reader;
+my $koha_url = 'http://ffzg.koha-dev.rot13.org:8080';
+# internal URL so we can find local address of machine and vmware NAT
+my $rfid_url = 'http://rfid.koha-dev.vbz.ffzg.hr';
 
 use Getopt::Long;
 
@@ -31,6 +34,7 @@ GetOptions(
        'debug!'    => \$debug,
        'listen=s', => \$listen,
        'reader=s', => \$reader,
+       'koha=s',       => \$koha_url,
 ) || die $!;
 
 our $rfid_sid_cache;
@@ -41,7 +45,7 @@ sub rfid_borrower {
                return $json;
        }
        my $ua = LWP::UserAgent->new;
-       my $url = URI->new('http://ffzg.koha-dev.rot13.org:8080/cgi-bin/koha/ffzg/rfid-borrower.pl');
+       my $url = URI->new( $koha_url . '/cgi-bin/koha/ffzg/rfid/borrower.pl');
        $url->query_form(
                  RFID_SID => $hash->{sid}
                , OIB => $hash->{OIB}
@@ -67,6 +71,7 @@ my $index_html;
 {
        local $/ = undef;
        $index_html = <DATA>;
+       $index_html =~ s{http://koha.example.com:8080}{$koha_url}sg;
 }
 
 my $server_url;
@@ -195,6 +200,38 @@ sub http_server {
        die "server died";
 }
 
+sub rfid_register {
+       my $ip;
+
+       foreach ( split(/\n/, `ip addr` ) ) {
+               if ( /^\d:\s(\w+):\s/ ) {
+                       $ip->{last} = $1;
+               } elsif ( /^\s+inet\s((\d+)\.(\d+)\.(\d+)\.(\d+))\/(\d+)/ ) {
+                       $ip->{ $ip->{last} } = $1;
+               } else {
+                       warn "# SKIP [$_]\n";
+               }
+
+       }
+
+       my $ua = LWP::UserAgent->new;
+       my $url = URI->new( $rfid_url . '/register.pl');
+       $url->query_form(
+               local_ip => $ip->{eth0},
+       );
+       warn "GET ",$url->as_string;
+       my $response = $ua->get($url);
+       if ( $response->is_success ) {
+               warn "# ", $response->decoded_content;
+               my $json = decode_json $response->decoded_content;
+               warn "REGISTER: ",dump($json);
+               return $json;
+       } else {
+               warn "ERROR ", $response->status_line;
+       }
+}
+
+rfid_register;
 http_server;
 
 __DATA__
@@ -241,10 +278,16 @@ function got_visible_tags(data,textStatus) {
                $.each(data.tags, function(i,tag) {
                        console.debug( i, tag );
                        html += '<li><tt class="' + tag.security + '">' + tag.sid;
-                       var borrowernumber = tag.content || tag.borrower.cardnumber;
+                       var content = tag.content || tag.borrower.cardnumber;
 
-                       if ( borrowernumber ) {
-                               html += ' <a href="http://ffzg.koha-dev.rot13.org:8080/cgi-bin/koha/members/member.pl?member=' + borrowernumber + '" title="lookup in Koha" target="koha-lookup">' + borrowernumber + '</a>';
+                       if ( content ) {
+                               html += ' <a href="http://koha.example.com:8080/cgi-bin/koha/';
+                               if ( tag.type == 1 ) { // book
+                                       html += 'catalogue/search.pl?q=';
+                               } else {
+                                       html += 'members/member.pl?member=';
+                               }
+                               html += content + '" title="lookup in Koha" target="koha-lookup">' + content + '</a>';
                                html += '</tt>';
 /*
                                html += '<form method=get action=program style="display:inline">'