--- 3m-810.pl 2009/07/04 08:33:56 58 +++ 3m-810.pl 2009/11/16 16:23:05 59 @@ -9,6 +9,7 @@ use Getopt::Long; use File::Slurp; use JSON; +use POSIX qw(strftime); use IO::Socket::INET; @@ -46,6 +47,8 @@ } my $listen_port = 9000; # pick something not in use +my $server_url = "http://localhost:$listen_port"; + sub http_server { my $server = IO::Socket::INET->new( @@ -57,7 +60,7 @@ die "can't setup server" unless $server; - print "Server $0 accepting clients at http://localhost:$listen_port/\n"; + print "Server $0 ready at $server_url\n"; sub static { my ($client,$path) = @_; @@ -110,6 +113,22 @@ } keys %$tags; print $client "HTTP/1.0 200 OK\r\nContent-Type: application/x-javascript\r\n\r\n", $param->{callback}, "(", to_json($json), ")\r\n"; + } elsif ( $method =~ m{/program} ) { + + my $status = 501; # Not implementd + + foreach my $p ( keys %$param ) { + next unless $p =~ m/^tag_(\S+)/; + my $tag = $1; + my $content = $param->{$p}; + $status = 302; + + warn "PROGRAM $tag $content\n"; + write_tag( $tag, $content ); + } + + print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n"; + } else { print $client "HTTP/1.0 404 Unkown method\r\n"; } @@ -379,6 +398,8 @@ return $last_block + 1; } +my $saved_in_log; + sub decode_tag { my $tag = shift; @@ -400,6 +421,12 @@ custom => $custom, }; + if ( ! $saved_in_log->{$tag}++ ) { + open(my $log, '>>', 'rfid-log.txt'); + print $log strftime( "%Y-%m-%d %H:%M:%S", localtime ), ",$tag,$content\n"; + close($log); + } + return $hash; } @@ -447,11 +474,13 @@ } sub write_tag { - my ($tag) = @_; + my ($tag,$data) = @_; my $path = "$program_path/$tag"; + $data = read_file( $path ) if -e $path; + + die "no data" unless $data; - my $data = read_file( $path ); my $hex_data; if ( $data =~ s{^hex\s+}{} ) {