9 |
use Getopt::Long; |
use Getopt::Long; |
10 |
use File::Slurp; |
use File::Slurp; |
11 |
use JSON; |
use JSON; |
12 |
|
use POSIX qw(strftime); |
13 |
|
|
14 |
use IO::Socket::INET; |
use IO::Socket::INET; |
15 |
|
|
16 |
my $debug = 0; |
my $debug = 0; |
17 |
|
|
18 |
my $meteor_server = '192.168.1.13:4671'; |
my $tags_data; |
19 |
|
my $tags_security; |
20 |
|
my $visible_tags; |
21 |
|
|
22 |
|
my $meteor_server; # = '192.168.1.13:4671'; |
23 |
my $meteor_fh; |
my $meteor_fh; |
24 |
|
|
25 |
sub meteor { |
sub meteor { |
47 |
} |
} |
48 |
|
|
49 |
my $listen_port = 9000; # pick something not in use |
my $listen_port = 9000; # pick something not in use |
50 |
|
my $server_url = "http://localhost:$listen_port"; |
51 |
|
|
52 |
sub http_server { |
sub http_server { |
53 |
|
|
54 |
my $server = IO::Socket::INET->new( |
my $server = IO::Socket::INET->new( |
60 |
|
|
61 |
die "can't setup server" unless $server; |
die "can't setup server" unless $server; |
62 |
|
|
63 |
print "Server $0 accepting clients at http://localhost:$listen_port/\n"; |
print "Server $0 ready at $server_url\n"; |
64 |
|
|
65 |
sub static { |
sub static { |
66 |
my ($client,$path) = @_; |
my ($client,$path) = @_; |
67 |
|
|
68 |
$path = "www/$path"; |
$path = "www/$path"; |
69 |
|
$path .= 'rfid.html' if $path =~ m{/$}; |
70 |
|
|
71 |
return unless -e $path; |
return unless -e $path; |
72 |
|
|
104 |
warn "WEB >> $path" if $debug; |
warn "WEB >> $path" if $debug; |
105 |
} elsif ( $method =~ m{/scan} ) { |
} elsif ( $method =~ m{/scan} ) { |
106 |
my $tags = scan_for_tags(); |
my $tags = scan_for_tags(); |
107 |
my $json = {}; |
my $json = { time => time() }; |
108 |
map { |
map { |
109 |
my $d = decode_tag($_); |
my $d = decode_tag($_); |
110 |
$d->{sid} = $_; |
$d->{sid} = $_; |
111 |
|
$d->{security} = $tags_security->{$_}; |
112 |
push @{ $json->{tags} }, $d; |
push @{ $json->{tags} }, $d; |
113 |
} keys %$tags; |
} keys %$tags; |
114 |
print $client "HTTP/1.0 200 OK\r\nContent-Type: application/x-javascript\r\n\r\n", |
print $client "HTTP/1.0 200 OK\r\nContent-Type: application/x-javascript\r\n\r\n", |
115 |
$param->{callback}, "(", to_json($json), ")\r\n"; |
$param->{callback}, "(", to_json($json), ")\r\n"; |
116 |
|
} elsif ( $method =~ m{/program} ) { |
117 |
|
|
118 |
|
my $status = 501; # Not implementd |
119 |
|
|
120 |
|
foreach my $p ( keys %$param ) { |
121 |
|
next unless $p =~ m/^tag_(\S+)/; |
122 |
|
my $tag = $1; |
123 |
|
my $content = $param->{$p}; |
124 |
|
$status = 302; |
125 |
|
|
126 |
|
warn "PROGRAM $tag $content\n"; |
127 |
|
write_tag( $tag, $content ); |
128 |
|
} |
129 |
|
|
130 |
|
print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n"; |
131 |
|
|
132 |
} else { |
} else { |
133 |
print $client "HTTP/1.0 404 Unkown method\r\n"; |
print $client "HTTP/1.0 404 Unkown method\r\n"; |
134 |
} |
} |
230 |
|
|
231 |
=cut |
=cut |
232 |
|
|
|
my $tags_data; |
|
|
my $visible_tags; |
|
|
|
|
233 |
my $item_type = { |
my $item_type = { |
234 |
1 => 'Book', |
1 => 'Book', |
235 |
6 => 'CD/CD ROM', |
6 => 'CD/CD ROM', |
319 |
if ( $http_server ) { |
if ( $http_server ) { |
320 |
http_server; |
http_server; |
321 |
} else { |
} else { |
322 |
scan_for_tags while 1; |
while (1) { |
323 |
|
scan_for_tags; |
324 |
|
sleep 1; |
325 |
|
} |
326 |
} |
} |
327 |
|
|
328 |
die "over and out"; |
die "over and out"; |
398 |
return $last_block + 1; |
return $last_block + 1; |
399 |
} |
} |
400 |
|
|
401 |
|
my $saved_in_log; |
402 |
|
|
403 |
sub decode_tag { |
sub decode_tag { |
404 |
my $tag = shift; |
my $tag = shift; |
405 |
|
|
421 |
custom => $custom, |
custom => $custom, |
422 |
}; |
}; |
423 |
|
|
424 |
|
if ( ! $saved_in_log->{$tag}++ ) { |
425 |
|
open(my $log, '>>', 'rfid-log.txt'); |
426 |
|
print $log strftime( "%Y-%m-%d %H:%M:%S", localtime ), ",$tag,$content\n"; |
427 |
|
close($log); |
428 |
|
} |
429 |
|
|
430 |
return $hash; |
return $hash; |
431 |
} |
} |
432 |
|
|
465 |
( $from_tag, $security ) = ( substr($rest,0,8), substr($rest,8,1) ); |
( $from_tag, $security ) = ( substr($rest,0,8), substr($rest,8,1) ); |
466 |
die "security from other tag: ",as_hex( $from_tag ) if $from_tag ne str2bytes( $tag ); |
die "security from other tag: ",as_hex( $from_tag ) if $from_tag ne str2bytes( $tag ); |
467 |
$security = as_hex( $security ); |
$security = as_hex( $security ); |
468 |
|
$tags_security->{$tag} = $security; |
469 |
warn "# SECURITY $tag = $security\n"; |
warn "# SECURITY $tag = $security\n"; |
470 |
} |
} |
471 |
); |
); |
474 |
} |
} |
475 |
|
|
476 |
sub write_tag { |
sub write_tag { |
477 |
my ($tag) = @_; |
my ($tag,$data) = @_; |
478 |
|
|
479 |
my $path = "$program_path/$tag"; |
my $path = "$program_path/$tag"; |
480 |
|
$data = read_file( $path ) if -e $path; |
481 |
|
|
482 |
|
die "no data" unless $data; |
483 |
|
|
|
my $data = read_file( $path ); |
|
484 |
my $hex_data; |
my $hex_data; |
485 |
|
|
486 |
if ( $data =~ s{^hex\s+}{} ) { |
if ( $data =~ s{^hex\s+}{} ) { |
693 |
warn "## DISPATCH payload to with rest", dump( $payload, $to, $rest ) if $debug; |
warn "## DISPATCH payload to with rest", dump( $payload, $to, $rest ) if $debug; |
694 |
$dispatch->{ $to }->( $rest ); |
$dispatch->{ $to }->( $rest ); |
695 |
} else { |
} else { |
696 |
print "NO DISPATCH for ",dump( $full ),"\n"; |
print "NO DISPATCH for ",as_hex( $full ),"\n"; |
697 |
} |
} |
698 |
|
|
699 |
return $data; |
return $data; |