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 |
|
|
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 |
|
|
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 = "\x04\x11\x00\x01" . $param->{$p}; |
124 |
|
$content = "\x00" if $param->{$p} eq 'blank'; |
125 |
|
$status = 302; |
126 |
|
|
127 |
|
warn "PROGRAM $tag $content\n"; |
128 |
|
write_tag( $tag, $content ); |
129 |
|
} |
130 |
|
|
131 |
|
print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n"; |
132 |
|
|
133 |
} else { |
} else { |
134 |
print $client "HTTP/1.0 404 Unkown method\r\n"; |
print $client "HTTP/1.0 404 Unkown method\r\n"; |
135 |
} |
} |
320 |
if ( $http_server ) { |
if ( $http_server ) { |
321 |
http_server; |
http_server; |
322 |
} else { |
} else { |
323 |
scan_for_tags while 1; |
while (1) { |
324 |
|
scan_for_tags; |
325 |
|
sleep 1; |
326 |
|
} |
327 |
} |
} |
328 |
|
|
329 |
die "over and out"; |
die "over and out"; |
399 |
return $last_block + 1; |
return $last_block + 1; |
400 |
} |
} |
401 |
|
|
402 |
|
my $saved_in_log; |
403 |
|
|
404 |
sub decode_tag { |
sub decode_tag { |
405 |
my $tag = shift; |
my $tag = shift; |
406 |
|
|
422 |
custom => $custom, |
custom => $custom, |
423 |
}; |
}; |
424 |
|
|
425 |
|
if ( ! $saved_in_log->{$tag}++ ) { |
426 |
|
open(my $log, '>>', 'rfid-log.txt'); |
427 |
|
print $log strftime( "%Y-%m-%d %H:%M:%S", localtime ), ",$tag,$content\n"; |
428 |
|
close($log); |
429 |
|
} |
430 |
|
|
431 |
return $hash; |
return $hash; |
432 |
} |
} |
433 |
|
|
475 |
} |
} |
476 |
|
|
477 |
sub write_tag { |
sub write_tag { |
478 |
my ($tag) = @_; |
my ($tag,$data) = @_; |
479 |
|
|
480 |
my $path = "$program_path/$tag"; |
my $path = "$program_path/$tag"; |
481 |
|
$data = read_file( $path ) if -e $path; |
482 |
|
|
483 |
|
die "no data" unless $data; |
484 |
|
|
|
my $data = read_file( $path ); |
|
485 |
my $hex_data; |
my $hex_data; |
486 |
|
|
487 |
if ( $data =~ s{^hex\s+}{} ) { |
if ( $data =~ s{^hex\s+}{} ) { |
519 |
rename $path, $to; |
rename $path, $to; |
520 |
print ">> $to\n"; |
print ">> $to\n"; |
521 |
|
|
522 |
delete $tags_data->{$tag}; # force re-read of tag |
# force re-read of tag |
523 |
|
delete $tags_data->{$tag}; |
524 |
|
delete $visible_tags->{$tag}; |
525 |
} |
} |
526 |
|
|
527 |
sub secure_tag { |
sub secure_tag { |