PERL을 사용하여 PHP 소켓 서버에서 스트림 데이터를 구문 분석하는 데 도움이 필요합니다.

StackOverflow https://stackoverflow.com/questions/857575

문제

소켓 서버 (PHP)를 사용하여 원격 장치에서 데이터를 수집하는 회사 프로젝트를 진행하고 있습니다. 이 Perl 프로그램이 서버가 먼저 서버가 TMP 파일에 쓰는 다음 해당 파일 에서이 스크립트를 실행 한 다음 데이터베이스에 삽입 할 CSV 파일을 작성하는 대신 스트림에서 직접 실행할 수있는 방법은 무엇입니까?

io :: 소켓 사용에 대해 생각했지만 어떻게 해야할지 잘 모르겠습니다.

또한이 코드를 정리하는 방법에 대한 팁/포인터가 있으면 감사하겠습니다. (나는 여전히 Perl N00B ;-))))))))

여기에 Perl Prog에 대해 지금까지 가지고있는 것은 다음과 같습니다.


#vim set sw=2 ts=2
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
&convert;

#open the source file, strip out any unneeded chars, reformat the data
sub convert{
    my $source = "$ARGV[0]";
    my $dest = "$ARGV[0]"."_tmp.txt";
    chomp $source;
    open SOURCE, '', $dest or die "Could not open '$dest' $!";

    # move the data from the source file into an array
    my %fields;
    my @field_names = qw/FIELD1 FIELD2 FIELD3 FIELD4 FIELD5 FIELD6/;
    my $pack_definition = 'a4 a2 a1 a4 a4 a8A*'; 
    while(){

        # strip out the packet header 
        s#T18##g;
        s#T00##g;
        s/^(FF14).*$//g;
        s/^(FF18).*$//g;
        s/(^|\n)[\n\s]*/$1/g;

        # arrange the data into the necessary order
        @fields{@field_names} = unpack($pack_definition, $_);
        s#(FF15)(.{2})(.{1})(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})#$1\t$2\t$3\t$5$4\t$7$6\t$11$10$9$8#g;
        s#(FF16)(.{2})(.{1})(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})#$1\t$2\t$3\t$4\t$5\t$6\t$7\t$8\t$9\t$10\t$11#g;
        s#(FF17)(.{2})(.{1})(.{2})(.{2})(.{2})(.{2})(.{4})(.{4})#$1\t$2\t$3\t$5$4\t$7$6\t$8\t$9#g;
        my @spds =  /(FF15)\t(.{2})\t(.{1})\t(.{4})\t(.{4})\t(.{8})/;

        # convert the data from hex to ascii
        foreach my $data (@spds){
            my $replacement = hex($data);  
            s#$data#$replacement#g;
        }
        my @psis = /(FF16)\t(.{2})\t(.{1})\t(.{2})\t(.{2})\t(.{2})\t(.{2})\t(.{2})\t(.{2})\t(.{2})\t(.{2})/;
        foreach my $data1(@psis){
            my $replacement1 = hex($data1);
            s#$data1#$replacement1#g;
        }
        my @rates=  /(FF17)\t(.{2})\t(.{1})\t(.{4})\t(.{1,4})\t(.{4})\t(.{4})/;
        foreach my $data2 (@rates){
            my $replacement2 = hex($data2);  
            s#$data2#$replacement2#g;
        }

        # print the converted data to the destination file
        print DEST;
    }
    # close the files
    close SOURCE;
    close DEST;
}
&create_vals;

# perform conversion from raw values to human readable output
sub create_vals{      
    my $source = "$ARGV[0]"."_tmp.txt";
    my $dest = "$ARGV[0]"."_converted.txt";
    chomp $source;
    open SOURCE, '', $dest or die "Cannot open '$dest' for writing $!";
    while(){
        s#(65301)\t(.{2})\t(8)\t(.{1,5})\t(.{1,5})\t(.{1,4})#"'".$1."','". $2."','". $3."','". ($4/8)."','". ($5/8)."','". ($6/20)."'"#eg;
        s#(65302)\t(.{2})\t(8)\t(.{1,3})\t(.{1,3})\t(.{1,3})\t(.{1,3})\t(.{1,3})\t(.{1,3})\t(.{1,3})\t(.{1,3})#"'".$1."','". $2."','". $3."','". $4."','". (($5*1.8)-40)."','". (($6*1.8)-40)."','". ($7*.58)."','".($8*.58)."','".($9*.29008)."','".(($10*1.8)-40)."','".$11."'"#eg;
        s#(65303)\t(.{2})\t(8)\t(.{1,5})\t(.{1,5})\t(.{1,5})\t(.{1,5})#"'".$1."','". $2."','". $3."','". ($4*0.014)."','". ($5*.05)."'"#eg;
        print DEST;
    }
}
    close SOURCE;
    close DEST;
__END__
도움이 되었습니까?

해결책

"스트림에서 직접 실행"이 "PHP 서버 대신 원격 장치에서 연결을 수락"하는 경우 io :: 소켓이 이동하는 방법입니다. Google이 있습니다 많은 예 그리고 당신은 또한 체크 아웃 할 수 있습니다 perlipc 선적 서류 비치.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top