source: core/trunk/client/flarmclient.pl @ 130

Last change on this file since 130 was 130, checked in by smoser, 12 years ago

#76

  • Property svn:mime-type set to text/plain
File size: 4.3 KB
RevLine 
[130]1#!/usr/bin/perl
2#-------------------------------------------------------------------------------
3# This file is part of the FLARM¨-Radar Project.
4#   
5#   Copyright 2013 Netzschmiede GmbH (http://www.netzschmiede.ch)
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11#   http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19#   Project Website: www.flarmradar.ch
20#   Email: info@flarmradar.ch
21#-------------------------------------------------------------------------------
22
23use strict;
24use warnings;
25use Getopt::Std;
26use File::Basename;
27use POSIX qw/strftime/;
28use LWP::UserAgent;
29
30my %config;
31my %options;
32my $ua;
33
34# default values
35my $cfile = "$ENV{'HOME'}/.flarmclient/client.config";
36my $fifo = "$ENV{'HOME'}/.flarmclient/client.fifo";
37
38# functions
39sub usage {
40  print <<EOF;
41NAME
42  $0 -- stream flarm data to server
43
44SYNOPSIS
45  $0 [-c config_file] [-f data_file] [-h]
46
47DESCRIPTION
48  The following options are available:
49 
50  -c    Use the specified configuration file. Use the default configuration
51                file as a starting point for customization.
52               
53  -f    Read the data from the specified data file. This is mainly used for
54                testing and development.
55               
56  -h    Print this help.
57
58EOF
59  exit 0;
60}
61
62sub cleanup {
63        if (-e "$fifo") {
64                unlink($fifo) || die("unable to remove $fifo: $!");
65        }
66}
67
68sub readconfig {
69        open(CONF, "< $cfile") || die("failed to open config file for reading: $!");
70        while(my $line = <CONF>) {
71                chomp($line);
72                next if $line =~ /^\s*#/;
73                next if $line =~ /^\s*$/;
74                if ($line =~ /^\s*(\S*)\s*=\s*(\S*)\s*$/) {
75                        $config{$1} = $2;
76                }
77        }
78        close(CONF);
79}
80
81# send the records to the server. We don't make a request for each record for
82# performance reasons.
83sub flush {
84        my ($records, $url) = @_;
85        my $date = strftime "%Y/%m/%d", localtime;
86        my $resturl = $url . "/" . $date;
87        my $request = HTTP::Request->new('PUT');
88        $request->url($resturl);
89        $request->header('stationKey'=>$config{'key'});
90        $request->content($records);
91        # run the request
92        my $response = $ua->request($request);
93        # analyze the response
94        my $code = $response->code;
95        $response->code == 200 || print("error processing records (code=" . $response->code . ") " . $records . "\n");
96}
97
98# parse options
99getopts('c:f:h', \%options);
100
101# read config file
102if (defined($options{'c'})) {
103        $cfile = $options{'c'};
104}
105if (defined($options{'h'})) {
106        usage();
107}
108readconfig();
109
110# validation: key must be present in config file
111die("no key found in config file (option: key)") unless defined($config{'key'});
112
113# remove old leftovers
114cleanup();
115
116# create pipe
117die("no fifo found in config file (option: fifo)") unless defined($fifo);
118if (! -d dirname($fifo)) {
119        system("mkdir", "-p", dirname($fifo)) == 0 || die("failed to create fifo directory " . dirname($fifo) . ": $!")
120}
121system("mkfifo", $fifo) == 0 || die("failed to create fifo: $!");
122
123# force a flush right away and after every write or print
124local $| = 1;
125
126# fork minicom and write to pipe
127defined( my $pid = fork() ) or die "can't fork: $!";
128unless ($pid) {
129        # we're the child
130        open( STDIN, '/dev/null' ) or die "can't read /dev/null: $!";
131       
132        if (defined($options{'f'})) {
133                open(DATA, "< $options{'f'}") || die("failed to open data file $options{'f'}: $!");
134                open(FIFO, "> $fifo") || die("failed to open fifo for writing: $!");
135                while(my $line = <DATA>) {
136                        chomp($line);
137                        next if ($line =~ /^\s*$/);
138                        print FIFO $line, "\n" || die("failed to execute child command: $!");
139                }
140                close(DATA);
141                close(FIFO);
142        } else {
143                system("minicom", "-C", $fifo) == 0 || die("failed to start minicom: $!");
144        }
145        exit 0;
146}
147
148# create UserAgent object
149$ua = new LWP::UserAgent;
150my $buf;
151# read data from pipe
152open(FIFO, "< $fifo") || die("failed to open fifo for reading: $!");
153while(my $record = <FIFO>) {
154        chomp($record);
155        $buf = (defined($buf)) ? "$buf;$record" : $record;
156        if ($record =~ /^\$GPGGA,/) {
157                flush($buf, $config{'url'});
158                sleep(1);
159                $buf = undef;
160        }
161}
162close(FIFO);
163
164cleanup();
165exit 0;
166
Note: See TracBrowser for help on using the repository browser.