decode RFID 501
[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                                         push @{ $json->{tags} }, $hash;
79                                 };
80                                 warn "#### ", encode_json($json);
81                                 print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
82                                         $param->{callback}, "(", encode_json($json), ")\r\n";
83                         } elsif ( $method =~ m{/program} ) {
84
85                                 my $status = 501; # Not implementd
86
87                                 foreach my $p ( keys %$param ) {
88                                         next unless $p =~ m/^(E[0-9A-F]{15})$/;
89                                         my $tag = $1;
90                                         my $content = "\x04\x11\x00\x01" . $param->{$p};
91                                         $content = "\x00" if $param->{$p} eq 'blank';
92                                         $status = 302;
93
94                                         warn "PROGRAM $tag $content\n";
95                                         write_tag( $tag, $content );
96                                         secure_tag_with( $tag, $param->{$p} =~ /^130/ ? 'DA' : 'D7' );
97                                 }
98
99                                 print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
100
101                         } elsif ( $method =~ m{/secure(.js)} ) {
102
103                                 my $json = $1;
104
105                                 my $status = 501; # Not implementd
106
107                                 foreach my $p ( keys %$param ) {
108                                         next unless $p =~ m/^(E[0-9A-F]{15})$/;
109                                         my $tag = $1;
110                                         my $data = $param->{$p};
111                                         $status = 302;
112
113                                         warn "SECURE $tag $data\n";
114                                         secure_tag_with( $tag, $data );
115                                 }
116
117                                 if ( $json ) {
118                                         print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
119                                                 $param->{callback}, "({ ok: 1 })\r\n";
120                                 } else {
121                                         print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
122                                 }
123
124                         } else {
125                                 print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
126                         }
127                 } else {
128                         print $client "HTTP/1.0 500 No method\r\n\r\n";
129                 }
130                 close $client;
131         }
132
133         die "server died";
134 }
135
136 http_server;
137
138 __DATA__
139 <html>
140 <head>
141 <title>RFID JSONP</title>
142 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
143 <style type="text/css">
144 .status {
145         background: #ff8;
146 }
147
148 .da {
149         background: #fcc;
150 }
151
152 .d7 {
153         background: #cfc;
154 }
155
156 label[for=pull-reader] {
157         position: absolute;
158         top: 1em;
159         right: 1em;
160         background: #eee;
161 }
162
163 </style>
164 <script type="text/javascript">
165
166 function got_visible_tags(data,textStatus) {
167         var html = 'No tags in range';
168         if ( data.tags ) {
169                 html = '<ul class="tags">';
170                 $.each(data.tags, function(i,tag) {
171                         console.debug( i, tag );
172                         html += '<li><tt class=' + tag.security + '>' + tag.sid;
173                         if ( tag.content ) {
174                                 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>';
175                                 html += '</tt>';
176                                 html += '<form method=get action=program style="display:inline">'
177                                         + '<input type=hidden name='+tag.sid+' value="blank">'
178                                         + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
179                                         + '</form>'
180                                 ;
181                         } else {
182                                 html += '</tt>';
183                                 html += ' <form method=get action=program style="display:inline">'
184                                         + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
185                                         + '<input type=text name='+tag.sid+' size=12>'
186                                         + '<input type=submit value="Program">'
187                                         + '</form>'
188                                 ;
189                         }
190                 });
191                 html += '</ul>';
192         }
193
194         var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
195
196         html = '<div class=status>'
197                 + textStatus
198                 + ' &#' + arrows[ data.time % arrows.length ] + ';'
199                 + '</div>'
200                 + html
201                 ;
202         $('#tags').html( html );
203         window.setTimeout(function(){
204                 scan_tags();
205         },200); // re-scan every 200ms
206 };
207
208 function scan_tags() {
209         console.info('scan_tags');
210         if ( $('input#pull-reader').attr('checked') )
211                 $.getJSON("/scan?callback=?", got_visible_tags);
212 }
213
214 $(document).ready(function() {
215                 $('input#pull-reader').click( function() {
216                         scan_tags();
217                 });
218                 $('input#pull-reader').attr('checked', true); // force check on load
219
220                 $('div#tags').click( function() {
221                         $('input#pull-reader').attr('checked', false);
222                 } );
223
224                 scan_tags();
225 });
226 </script>
227 </head>
228 <body>
229
230 <h1>RFID tags in range</h1>
231
232 <label for=pull-reader>
233 <input id=pull-reader type=checkbox checked=1>
234 active
235 </label>
236
237 <div id="tags">
238 RFID reader not found or driver program not started.
239 </div>
240
241 </body>
242 </html>