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

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

#122

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