source: core/branches/core_1.1/client/flarmclient.pl

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

#77

  • Property svn:mime-type set to text/plain
File size: 4.4 KB
Line 
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 POSIX qw(setsid);
29use LWP::UserAgent;
30
31my %config;
32my %options;
33my $ua;
34
35# default values
36my $cfile = "$ENV{'HOME'}/.flarmclient/client.config";
37my $fifo = "$ENV{'HOME'}/.flarmclient/client.fifo";
38
39# functions
40sub usage {
41  print <<EOF;
42NAME
43  $0 -- stream flarm data to server
44
45SYNOPSIS
46  $0 [-c config_file] [-f data_file] [-h]
47
48DESCRIPTION
49  The following options are available:
50 
51  -c    Use the specified configuration file. Use the default configuration
52                file as a starting point for customization.
53               
54  -f    Read the data from the specified data file. This is mainly used for
55                testing and development.
56               
57  -h    Print this help.
58
59EOF
60  exit 0;
61}
62
63sub cleanup {
64        if (-e "$fifo") {
65                unlink($fifo) || die("unable to remove $fifo: $!");
66        }
67}
68
69sub readconfig {
70        open(CONF, "< $cfile") || die("failed to open config file for reading: $!");
71        while(my $line = <CONF>) {
72                chomp($line);
73                next if $line =~ /^\s*#/;
74                next if $line =~ /^\s*$/;
75                if ($line =~ /^\s*(\S*)\s*=\s*(\S*)\s*$/) {
76                        $config{$1} = $2;
77                }
78        }
79        close(CONF);
80}
81
82# send the records to the server. We don't make a request for each record for
83# performance reasons.
84sub flush {
85        my ($records, $url) = @_;
86        my $date = strftime "%Y/%m/%d", localtime;
87        my $resturl = $url . "/" . $date;
88        my $request = HTTP::Request->new('PUT');
89        $request->url($resturl);
90        $request->header('stationKey'=>$config{'key'});
91        $request->content($records);
92        # run the request
93        my $response = $ua->request($request);
94        # analyze the response
95        my $code = $response->code;
96        $response->code == 200 || print("error processing records (code=" . $response->code . ") " . $records . "\n");
97}
98
99# parse options
100getopts('c:f:h', \%options);
101
102# read config file
103if (defined($options{'c'})) {
104        $cfile = $options{'c'};
105}
106if (defined($options{'h'})) {
107        usage();
108}
109readconfig();
110
111# validation: key must be present in config file
112die("no key found in config file (option: key)") unless defined($config{'key'});
113
114# remove old leftovers
115cleanup();
116
117# create pipe
118die("no fifo found in config file (option: fifo)") unless defined($fifo);
119if (! -d dirname($fifo)) {
120        system("mkdir", "-p", dirname($fifo)) == 0 || die("failed to create fifo directory " . dirname($fifo) . ": $!")
121}
122system("mkfifo", $fifo) == 0 || die("failed to create fifo: $!");
123
124# force a flush right away and after every write or print
125local $| = 1;
126
127# fork minicom and write to pipe
128defined( my $pid = fork() ) or die "can't fork: $!";
129unless ($pid) {
130        # we're the child
131        # detach from session
132        setsid() or die "can't start a new session: $!";
133        close(STDIN);
134        close(STDOUT);
135        close(STDERR);
136       
137        if (defined($options{'f'})) {
138                open(DATA, "< $options{'f'}") || die("failed to open data file $options{'f'}: $!");
139                open(FIFO, "> $fifo") || die("failed to open fifo for writing: $!");
140                while(my $line = <DATA>) {
141                        chomp($line);
142                        next if ($line =~ /^\s*$/);
143                        print FIFO $line, "\n" || die("failed to execute child command: $!");
144                }
145                close(DATA);
146                close(FIFO);
147        } else {
148                exec("minicom", "-t", "xterm-color", "-C", $fifo) == 0 || die("failed to run minicom: $!");
149        }
150        exit 0;
151}
152
153# create UserAgent object
154$ua = new LWP::UserAgent;
155my $buf;
156# read data from pipe
157open(FIFO, "< $fifo") || die("failed to open fifo for reading: $!");
158while(my $record = <FIFO>) {
159        chomp($record);
160        $buf = (defined($buf)) ? "$buf;$record" : $record;
161        if ($record =~ /^\$GPGGA,/) {
162                flush($buf, $config{'url'});
163                if (defined($options{'f'})) {
164                        sleep(1);
165                }
166                $buf = undef;
167        }
168}
169close(FIFO);
170
171cleanup();
172exit 0;
173
Note: See TracBrowser for help on using the repository browser.