pass HEX AFI in security to json
[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} = uc unpack 'H*', $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 // mock console
168 if(!window.console) {
169         window.console = new function() {
170                 this.info = function(str) {};
171                 this.debug = function(str) {};
172         };
173 }
174
175
176 function got_visible_tags(data,textStatus) {
177         var html = 'No tags in range';
178         if ( data.tags ) {
179                 html = '<ul class="tags">';
180                 $.each(data.tags, function(i,tag) {
181                         console.debug( i, tag );
182                         html += '<li><tt class=' + tag.security + '>' + tag.sid;
183                         if ( tag.content ) {
184                                 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>';
185                                 html += '</tt>';
186                                 html += '<form method=get action=program style="display:inline">'
187                                         + '<input type=hidden name='+tag.sid+' value="blank">'
188                                         + '<input type=submit value="Blank" onclick="return confirm(\'Blank tag '+tag.sid+'\')">'
189                                         + '</form>'
190                                 ;
191                         } else {
192                                 html += '</tt>';
193                                 html += ' <form method=get action=program style="display:inline">'
194                                         + '<!-- <input type=checkbox name=secure value='+tag.sid+' title="secure tag"> -->'
195                                         + '<input type=text name='+tag.sid+' size=12>'
196                                         + '<input type=submit value="Program">'
197                                         + '</form>'
198                                 ;
199                         }
200                 });
201                 html += '</ul>';
202         }
203
204         var arrows = Array( 8592, 8598, 8593, 8599, 8594, 8600, 8595, 8601 );
205
206         html = '<div class=status>'
207                 + textStatus
208                 + ' &#' + arrows[ data.time % arrows.length ] + ';'
209                 + '</div>'
210                 + html
211                 ;
212         $('#tags').html( html );
213         window.setTimeout(function(){
214                 scan_tags();
215         },200); // re-scan every 200ms
216 };
217
218 function scan_tags() {
219         console.info('scan_tags');
220         if ( $('input#pull-reader').attr('checked') )
221                 $.getJSON("/scan?callback=?", got_visible_tags);
222 }
223
224 $(document).ready(function() {
225                 $('input#pull-reader').click( function() {
226                         scan_tags();
227                 });
228                 $('input#pull-reader').attr('checked', true); // force check on load
229
230                 $('div#tags').click( function() {
231                         $('input#pull-reader').attr('checked', false);
232                 } );
233
234                 scan_tags();
235 });
236 </script>
237 </head>
238 <body>
239
240 <h1>RFID tags in range</h1>
241
242 <label for=pull-reader>
243 <input id=pull-reader type=checkbox checked=1>
244 active
245 </label>
246
247 <div id="tags">
248 RFID reader not found or driver program not started.
249 </div>
250
251 </body>
252 </html>