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