use first rfid reader available
[Biblio-RFID.git] / scripts / RFID-JSONP-server.pl
1 #!/usr/bin/perl
2
3 =head1 RFID-JSONP-server
4
5 This is simpliest possible JSONP server which provides local web interface to RFID readers
6
7 Usage:
8
9   ./scripts/RFID-JSONP-server.pl
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
21 my $debug = 1;
22
23 my $listen_port = 9000;                  # pick something not in use
24 my $server_url  = "http://localhost:$listen_port";
25
26 my $reader = shift @ARGV;
27
28 use lib 'lib';
29 use RFID::Biblio::RFID501;
30 use RFID::Biblio::Readers;
31 my $rfid = (RFID::Biblio::Readers->available( $reader ))[0]; # FIXME
32
33 my $index_html;
34 {
35         local $/ = undef;
36         $index_html = <DATA>;
37 }
38
39 sub http_server {
40
41         my $server = IO::Socket::INET->new(
42                 Proto     => 'tcp',
43                 LocalPort => $listen_port,
44                 Listen    => SOMAXCONN,
45                 Reuse     => 1
46         );
47                                                                   
48         die "can't setup server: $!" unless $server;
49
50         print "Server $0 ready at $server_url\n";
51
52         while (my $client = $server->accept()) {
53                 $client->autoflush(1);
54                 my $request = <$client>;
55
56                 warn "WEB << $request\n" if $debug;
57                 my $path;
58
59                 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
60                         my $method = $1;
61                         my $param;
62                         if ( $method =~ s{\?(.+)}{} ) {
63                                 foreach my $p ( split(/[&;]/, $1) ) {
64                                         my ($n,$v) = split(/=/, $p, 2);
65                                         $param->{$n} = $v;
66                                 }
67                                 warn "WEB << param: ",dump( $param ) if $debug;
68                         }
69                         $path = $method;
70
71                         if ( $path eq '/' ) {
72                                 print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n$index_html";
73                         } elsif ( $method =~ m{/scan} ) {
74                                 my $tags = $rfid->scan;
75                                 my $json = { time => time() };
76                                 foreach my $tag ( keys %$tags ) {
77                                         my $hash = RFID::Biblio::RFID501->to_hash( $tags->{$tag} );
78                                         $hash->{sid}  = $tag;
79                                         $hash->{security} = uc unpack 'H*', $rfid->read_afi( $tag );
80                                         push @{ $json->{tags} }, $hash;
81                                 };
82                                 warn "#### ", encode_json($json);
83                                 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
84                                         $param->{callback}, "(", encode_json($json), ")\r\n";
85                         } elsif ( $method =~ m{/program} ) {
86
87                                 my $status = 501; # Not implementd
88
89                                 foreach my $p ( keys %$param ) {
90                                         next unless $p =~ m/^(E[0-9A-F]{15})$/;
91                                         my $tag = $1;
92                                         my $content = "\x04\x11\x00\x01" . $param->{$p};
93                                         $content = "\x00" if $param->{$p} eq 'blank';
94                                         $status = 302;
95
96                                         warn "PROGRAM $tag $content\n";
97                                         write_tag( $tag, $content );
98                                         secure_tag_with( $tag, $param->{$p} =~ /^130/ ? 'DA' : 'D7' );
99                                 }
100
101                                 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
102
103                         } elsif ( $method =~ m{/secure(.js)} ) {
104
105                                 my $json = $1;
106
107                                 my $status = 501; # Not implementd
108
109                                 foreach my $p ( keys %$param ) {
110                                         next unless $p =~ m/^(E[0-9A-F]{15})$/;
111                                         my $tag = $1;
112                                         my $data = $param->{$p};
113                                         $status = 302;
114
115                                         warn "SECURE $tag $data\n";
116                                         secure_tag_with( $tag, $data );
117                                 }
118
119                                 if ( $json ) {
120                                         print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
121                                                 $param->{callback}, "({ ok: 1 })\r\n";
122                                 } else {
123                                         print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
124                                 }
125
126                         } else {
127                                 print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
128                         }
129                 } else {
130                         print $client "HTTP/1.0 500 No method\r\n\r\n";
131                 }
132                 close $client;
133         }
134
135         die "server died";
136 }
137
138 http_server;
139
140 __DATA__
141 <html>
142 <head>
143 <title>RFID JSONP</title>
144 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
145 <style type="text/css">
146 .status {
147         background: #ff8;
148 }
149
150 .da {
151         background: #fcc;
152 }
153
154 .d7 {
155         background: #cfc;
156 }
157
158 label[for=pull-reader] {
159         position: absolute;
160         top: 1em;
161         right: 1em;
162         background: #eee;
163 }
164
165 </style>
166 <script type="text/javascript">
167
168 // mock console
169 if(!window.console) {
170         window.console = new function() {
171                 this.info = function(str) {};
172                 this.debug = function(str) {};
173         };
174 }
175
176
177 function got_visible_tags(data,textStatus) {
178         var html = 'No tags in range';
179         if ( data.tags ) {
180                 html = '<ul class="tags">';
181                 $.each(data.tags, function(i,tag) {
182                         console.debug( i, tag );
183                         html += '<li><tt class=' + tag.security + '>' + tag.sid;
184                         if ( tag.content ) {
185                                 html += ' <a href="https://koha-dev.rot13.org:8443/cgi-bin/koha/members/member.pl?member=' + tag.content + '" title="lookup in Koha" target="koha-lookup">' + tag.content + '</a>';
186                                 html += '</tt>';
187                                 html += '<form method=get action=program style="display:inline">'
188                                         + '<input type=hidden name='+tag.sid+' value="blank">'
189                                         + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
190                                         + '</form>'
191                                 ;
192                         } else {
193                                 html += '</tt>';
194                                 html += ' <form method=get action=program style="display:inline">'
195                                         + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
196                                         + '<input type=text name='+tag.sid+' size=12>'
197                                         + '<input type=submit value="Program">'
198                                         + '</form>'
199                                 ;
200                         }
201                 });
202                 html += '</ul>';
203         }
204
205         var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
206
207         html = '<div class=status>'
208                 + textStatus
209                 + ' &#' + arrows[ data.time % arrows.length ] + ';'
210                 + '</div>'
211                 + html
212                 ;
213         $('#tags').html( html );
214         window.setTimeout(function(){
215                 scan_tags();
216         },200); // re-scan every 200ms
217 };
218
219 function scan_tags() {
220         console.info('scan_tags');
221         if ( $('input#pull-reader').attr('checked') )
222                 $.getJSON("/scan?callback=?", got_visible_tags);
223 }
224
225 $(document).ready(function() {
226                 $('input#pull-reader').click( function() {
227                         scan_tags();
228                 });
229                 $('input#pull-reader').attr('checked', true); // force check on load
230
231                 $('div#tags').click( function() {
232                         $('input#pull-reader').attr('checked', false);
233                 } );
234
235                 scan_tags();
236 });
237 </script>
238 </head>
239 <body>
240
241 <h1>RFID tags in range</h1>
242
243 <label for=pull-reader>
244 <input id=pull-reader type=checkbox checked=1>
245 active
246 </label>
247
248 <div id="tags">
249 RFID reader not found or driver program not started.
250 </div>
251
252 </body>
253 </html>