2009-05-13 16 views
1

私はリモートサーバからデータを収集するためにソケットサーバ(php)を使用する私の会社のプロジェクトに取り組んでいます。最初にサーバーがtmpファイルに書き込んだ後、そのファイルでこのスクリプトを実行してから、データベースに挿入するためのcsvファイルを書き込むのではなく、このperlプログラムを直接ストリーム上で実行できますか?PerlでPHPソケットサーバからストリームデータを解析する際に助けが必要

私はIO ::ソケットの使用について考えましたが、どうやってそれをどうやって行くのかは分かりません。

また、誰かがこのコードをクリーンアップする方法に関するヒント/ポインタを持っていれば、それは高く評価されます。 (私は;-)まだ非常にPerlのN00Bよ)

は、ここで私はPerlのプログレのために、これまで持っているものです(これが実行された後、私は2つの一時ファイルを羽目になる):。


#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__ 

答えて

1

「ストリーム上で直接実行する」とは、「PHPサーバではなくリモートデバイスからの接続を受け入れる」ことを意味し、IO :: Socketは方法です。 Googleにはplenty of examplesがあり、perlipcのドキュメントもご覧ください。

+0

私は、サーバーアプリケーションを別に保つことをお勧めします。私が考えていたのは、非常に多くの一時ファイルを書き込まなくても、データを実行する方法があれば(つまり、データがサーバに入る - >サーバがblah.txtにデータを書き込む - > perlプログラムがblah.txtを起動して変換するそのデータをblah_tmp.txtに書き込みます。 - >次に&create_valsを実行して、> blah_converted.txtにデータを書き込みます)。サーバーが一時ファイルに書き込みを行う前に、データをdbに転送することができます。 – bsisco

+0

サーバアプリケーションを変更できる場合は、http://devzone.zend.com/article/1712に記載されているように、PHP内からPerlスクリプトを呼び出すことができます。 パイプ、別のソケット、データベースなどを介してPerlスクリプトにデータを渡すこともできます。 BTWを保存する必要がない限り、おそらくblah_tmp.txtなしで行うことができます。 – Denys

+0

すごく面白かったです。私はチャンスを取るとすぐにそれらを試してみましょう... – bsisco

関連する問題