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