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

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

#117

  • 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";
[130]38my $cfile = "$ENV{'HOME'}/.flarmclient/client.config";
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");
78                print LOG localtime() . " $level $msg\n";
79                close(LOG);
80        }
81}
82
[130]83sub cleanup {
84        if (-e "$fifo") {
85                unlink($fifo) || die("unable to remove $fifo: $!");
86        }
87}
88
89sub readconfig {
90        open(CONF, "< $cfile") || die("failed to open config file for reading: $!");
91        while(my $line = <CONF>) {
92                chomp($line);
93                next if $line =~ /^\s*#/;
94                next if $line =~ /^\s*$/;
95                if ($line =~ /^\s*(\S*)\s*=\s*(\S*)\s*$/) {
96                        $config{$1} = $2;
97                }
98        }
99        close(CONF);
100}
101
102# send the records to the server. We don't make a request for each record for
103# performance reasons.
104sub flush {
105        my ($records, $url) = @_;
106        my $date = strftime "%Y/%m/%d", localtime;
107        my $resturl = $url . "/" . $date;
108        my $request = HTTP::Request->new('PUT');
109        $request->url($resturl);
110        $request->header('stationKey'=>$config{'key'});
111        $request->content($records);
112        # run the request
113        my $response = $ua->request($request);
114        # analyze the response
115        my $code = $response->code;
[204]116        $response->code == 200 || logit("ERROR", "error processing records (" . $response->code . ") records=[" . $records . "]");
[130]117}
118
119# parse options
120getopts('c:f:h', \%options);
121
122# read config file
123if (defined($options{'c'})) {
124        $cfile = $options{'c'};
125}
126if (defined($options{'h'})) {
127        usage();
128}
[204]129
130# read config file
[130]131readconfig();
132
133# validation: key must be present in config file
134die("no key found in config file (option: key)") unless defined($config{'key'});
135
136# remove old leftovers
137cleanup();
138
139# create pipe
140die("no fifo found in config file (option: fifo)") unless defined($fifo);
141if (! -d dirname($fifo)) {
142        system("mkdir", "-p", dirname($fifo)) == 0 || die("failed to create fifo directory " . dirname($fifo) . ": $!")
143}
144system("mkfifo", $fifo) == 0 || die("failed to create fifo: $!");
145
146# force a flush right away and after every write or print
147local $| = 1;
148
149# fork minicom and write to pipe
150defined( my $pid = fork() ) or die "can't fork: $!";
151unless ($pid) {
152        # we're the child
[133]153        # detach from session
154        setsid() or die "can't start a new session: $!";
155        close(STDIN);
156        close(STDOUT);
157        close(STDERR);
[130]158       
159        if (defined($options{'f'})) {
160                open(DATA, "< $options{'f'}") || die("failed to open data file $options{'f'}: $!");
161                open(FIFO, "> $fifo") || die("failed to open fifo for writing: $!");
162                while(my $line = <DATA>) {
163                        chomp($line);
164                        next if ($line =~ /^\s*$/);
165                        print FIFO $line, "\n" || die("failed to execute child command: $!");
166                }
167                close(DATA);
168                close(FIFO);
169        } else {
[133]170                exec("minicom", "-t", "xterm-color", "-C", $fifo) == 0 || die("failed to run minicom: $!");
[130]171        }
172        exit 0;
173}
174
175# create UserAgent object
176$ua = new LWP::UserAgent;
177my $buf;
178# read data from pipe
179open(FIFO, "< $fifo") || die("failed to open fifo for reading: $!");
180while(my $record = <FIFO>) {
181        chomp($record);
[204]182        logit("TRACE", $record);
[130]183        $buf = (defined($buf)) ? "$buf;$record" : $record;
184        if ($record =~ /^\$GPGGA,/) {
185                flush($buf, $config{'url'});
[133]186                if (defined($options{'f'})) {
187                        sleep(1);
188                }
[130]189                $buf = undef;
190        }
191}
192close(FIFO);
193
194cleanup();
195exit 0;
196
Note: See TracBrowser for help on using the repository browser.