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 | |
---|
23 | use strict; |
---|
24 | use warnings; |
---|
25 | use Getopt::Std; |
---|
26 | use File::Basename; |
---|
27 | use POSIX qw(strftime); |
---|
28 | use POSIX qw(setsid); |
---|
29 | use LWP::UserAgent; |
---|
30 | |
---|
31 | my %config; |
---|
32 | my %options; |
---|
33 | my $ua; |
---|
34 | |
---|
35 | # default values |
---|
36 | my $cfile = "$ENV{'HOME'}/.flarmclient/client.config"; |
---|
37 | my $fifo = "$ENV{'HOME'}/.flarmclient/client.fifo"; |
---|
38 | |
---|
39 | # functions |
---|
40 | sub usage { |
---|
41 | print <<EOF; |
---|
42 | NAME |
---|
43 | $0 -- stream flarm data to server |
---|
44 | |
---|
45 | SYNOPSIS |
---|
46 | $0 [-c config_file] [-f data_file] [-h] |
---|
47 | |
---|
48 | DESCRIPTION |
---|
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 | |
---|
59 | EOF |
---|
60 | exit 0; |
---|
61 | } |
---|
62 | |
---|
63 | sub cleanup { |
---|
64 | if (-e "$fifo") { |
---|
65 | unlink($fifo) || die("unable to remove $fifo: $!"); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | sub 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. |
---|
84 | sub 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 |
---|
100 | getopts('c:f:h', \%options); |
---|
101 | |
---|
102 | # read config file |
---|
103 | if (defined($options{'c'})) { |
---|
104 | $cfile = $options{'c'}; |
---|
105 | } |
---|
106 | if (defined($options{'h'})) { |
---|
107 | usage(); |
---|
108 | } |
---|
109 | readconfig(); |
---|
110 | |
---|
111 | # validation: key must be present in config file |
---|
112 | die("no key found in config file (option: key)") unless defined($config{'key'}); |
---|
113 | |
---|
114 | # remove old leftovers |
---|
115 | cleanup(); |
---|
116 | |
---|
117 | # create pipe |
---|
118 | die("no fifo found in config file (option: fifo)") unless defined($fifo); |
---|
119 | if (! -d dirname($fifo)) { |
---|
120 | system("mkdir", "-p", dirname($fifo)) == 0 || die("failed to create fifo directory " . dirname($fifo) . ": $!") |
---|
121 | } |
---|
122 | system("mkfifo", $fifo) == 0 || die("failed to create fifo: $!"); |
---|
123 | |
---|
124 | # force a flush right away and after every write or print |
---|
125 | local $| = 1; |
---|
126 | |
---|
127 | # fork minicom and write to pipe |
---|
128 | defined( my $pid = fork() ) or die "can't fork: $!"; |
---|
129 | unless ($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; |
---|
155 | my $buf; |
---|
156 | # read data from pipe |
---|
157 | open(FIFO, "< $fifo") || die("failed to open fifo for reading: $!"); |
---|
158 | while(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 | } |
---|
169 | close(FIFO); |
---|
170 | |
---|
171 | cleanup(); |
---|
172 | exit 0; |
---|
173 | |
---|