Merge branch 'master' of github.com:dpavlin/RFID-Biblio
[Biblio-RFID.git] / scripts / RFID-JSONP-server.pl
index 9f1e3d6..7877fca 100755 (executable)
@@ -15,7 +15,7 @@ use warnings;
 
 use Data::Dump qw/dump/;
 
-use JSON;
+use JSON::XS;
 use IO::Socket::INET;
 
 my $debug = 1;
@@ -23,10 +23,18 @@ my $debug = 1;
 my $listen_port = 9000;                  # pick something not in use
 my $server_url  = "http://localhost:$listen_port";
 
+my $reader = shift @ARGV;
 
 use lib 'lib';
-use RFID::Serial::3M810;
-my $rfid = RFID::Serial::3M810->new;
+use RFID::Biblio::RFID501;
+use RFID::Biblio::Readers;
+my $rfid = (RFID::Biblio::Readers->available( $reader ))[0]; # FIXME
+
+my $index_html;
+{
+       local $/ = undef;
+       $index_html = <DATA>;
+}
 
 sub http_server {
 
@@ -41,24 +49,12 @@ sub http_server {
 
        print "Server $0 ready at $server_url\n";
 
-       sub static {
-               my ($client,$path) = @_;
-
-               return unless $path eq '/';
-
-               print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n";
-               while(<DATA>) {
-                       print $client $_;
-               }
-
-               return $path;
-       }
-
        while (my $client = $server->accept()) {
                $client->autoflush(1);
                my $request = <$client>;
 
                warn "WEB << $request\n" if $debug;
+               my $path;
 
                if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
                        my $method = $1;
@@ -70,16 +66,22 @@ sub http_server {
                                }
                                warn "WEB << param: ",dump( $param ) if $debug;
                        }
-                       if ( my $path = static( $client,$1 ) ) {
-                               warn "WEB >> $path" if $debug;
+                       $path = $method;
+
+                       if ( $path eq '/' ) {
+                               print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n$index_html";
                        } elsif ( $method =~ m{/scan} ) {
                                my $tags = $rfid->scan;
-                               my $json = {
-                                       time => time(),
-                                       tags => $tags,
+                               my $json = { time => time() };
+                               foreach my $tag ( keys %$tags ) {
+                                       my $hash = RFID::Biblio::RFID501->to_hash( $tags->{$tag} );
+                                       $hash->{sid}  = $tag;
+                                       $hash->{security} = uc unpack 'H*', $rfid->read_afi( $tag );
+                                       push @{ $json->{tags} }, $hash;
                                };
+                               warn "#### ", encode_json($json);
                                print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
-                                       $param->{callback}, "(", to_json($json), ")\r\n";
+                                       $param->{callback}, "(", encode_json($json), ")\r\n";
                        } elsif ( $method =~ m{/program} ) {
 
                                my $status = 501; # Not implementd
@@ -138,7 +140,7 @@ http_server;
 __DATA__
 <html>
 <head>
-<title>3M RFID</title>
+<title>RFID JSONP</title>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
 <style type="text/css">
 .status {
@@ -163,6 +165,15 @@ label[for=pull-reader] {
 </style>
 <script type="text/javascript">
 
+// mock console
+if(!window.console) {
+       window.console = new function() {
+               this.info = function(str) {};
+               this.debug = function(str) {};
+       };
+}
+
+
 function got_visible_tags(data,textStatus) {
        var html = 'No tags in range';
        if ( data.tags ) {
@@ -208,7 +219,7 @@ function got_visible_tags(data,textStatus) {
 function scan_tags() {
        console.info('scan_tags');
        if ( $('input#pull-reader').attr('checked') )
-               $.getJSON("http://localhost:9000/scan?callback=?", got_visible_tags);
+               $.getJSON("/scan?callback=?", got_visible_tags);
 }
 
 $(document).ready(function() {