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 Time::HiRes qw(gettimeofday); |
---|
28 | use POSIX qw(strftime); |
---|
29 | use POSIX qw(setsid); |
---|
30 | use LWP::UserAgent; |
---|
31 | |
---|
32 | my %config; |
---|
33 | my %options; |
---|
34 | my $ua; |
---|
35 | my $debug = 0; |
---|
36 | my $trace = 0; |
---|
37 | my $interval = 3; |
---|
38 | my $skip = 1; |
---|
39 | |
---|
40 | # default values |
---|
41 | my $cfile = "/etc/flarmclient.conf"; |
---|
42 | my $log = "$ENV{'HOME'}/flarmclient.trace"; |
---|
43 | my $fifo = "$ENV{'HOME'}/fifo"; |
---|
44 | |
---|
45 | # functions |
---|
46 | sub usage { |
---|
47 | print <<EOF; |
---|
48 | NAME |
---|
49 | $0 -- stream flarm data to server |
---|
50 | |
---|
51 | SYNOPSIS |
---|
52 | $0 [-c config_file] [-d] [-f data_file] [-i n] [-j m] [-t] [-h] |
---|
53 | |
---|
54 | DESCRIPTION |
---|
55 | The following options are available: |
---|
56 | |
---|
57 | -c Use the specified configuration file. Use the default configuration |
---|
58 | file as a starting point for customization. |
---|
59 | |
---|
60 | -d Write debug information. The debug information is written to STDOUT |
---|
61 | unless tracing (option -t) is turned on. With tracing switched on, |
---|
62 | the debug information is written to the trace file (see below). |
---|
63 | |
---|
64 | -f Read the data from the specified data file. This is mainly used for |
---|
65 | testing and development. |
---|
66 | |
---|
67 | -i Bundle records, send a request to the server every i-th GPGGA record. |
---|
68 | Used for bandwidth optimization. Defaults to 3. |
---|
69 | |
---|
70 | -j Send every j-th record to the server. Used for bandwidth |
---|
71 | optimization. Defaults to 1. |
---|
72 | |
---|
73 | -t Trace client operations. A trace file is created in |
---|
74 | \$HOME/flarmclient.trace |
---|
75 | WARNING: Do not use that in Production, the trace file is not truncated |
---|
76 | and might fill up the file system. |
---|
77 | |
---|
78 | -h Print this help. |
---|
79 | |
---|
80 | EOF |
---|
81 | exit 0; |
---|
82 | } |
---|
83 | |
---|
84 | # print statistic information to logfile |
---|
85 | $SIG{USR1} = sub { |
---|
86 | if ($trace) { |
---|
87 | $trace = 0; |
---|
88 | } else { |
---|
89 | $trace = 1; |
---|
90 | } |
---|
91 | }; |
---|
92 | |
---|
93 | sub openlog { |
---|
94 | if (tell(LOG) == -1) { |
---|
95 | open(LOG, ">> $log") || print "Failed to open trace file $log: $!"; |
---|
96 | } |
---|
97 | return 1; |
---|
98 | } |
---|
99 | |
---|
100 | sub logit { |
---|
101 | my ($level, $msg) = @_; |
---|
102 | if ($trace && $level eq "TRACE") { |
---|
103 | openlog() && print LOG "$msg\n"; |
---|
104 | } |
---|
105 | if ($debug && $level eq "DEBUG") { |
---|
106 | if ($trace) { |
---|
107 | openlog() && print LOG "$msg\n"; |
---|
108 | } else { |
---|
109 | print "$msg\n"; |
---|
110 | } |
---|
111 | |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | sub cleanup { |
---|
116 | if (-e "$fifo") { |
---|
117 | unlink($fifo) || die("unable to remove $fifo: $!"); |
---|
118 | } |
---|
119 | close(LOG); |
---|
120 | } |
---|
121 | |
---|
122 | sub readconfig { |
---|
123 | open(CONF, "< $cfile") || die("failed to open config file for reading: $!"); |
---|
124 | while(my $line = <CONF>) { |
---|
125 | chomp($line); |
---|
126 | next if $line =~ /^\s*#/; |
---|
127 | next if $line =~ /^\s*$/; |
---|
128 | if ($line =~ /^\s*(\S*)\s*=\s*(\S*)\s*$/) { |
---|
129 | $config{$1} = $2; |
---|
130 | } |
---|
131 | } |
---|
132 | close(CONF); |
---|
133 | } |
---|
134 | |
---|
135 | sub exact_time { |
---|
136 | return strftime("%H:%M:%S", localtime()) . "." . (gettimeofday())[1]; |
---|
137 | } |
---|
138 | |
---|
139 | # send the records to the server. We don't make a request for each record for |
---|
140 | # performance reasons. |
---|
141 | sub flush { |
---|
142 | my ($records, $url) = @_; |
---|
143 | logit("DEBUG", exact_time() . " Start flushing data to server"); |
---|
144 | |
---|
145 | my $date = `date -u +%Y/%m/%d`; |
---|
146 | chomp($date); |
---|
147 | my $resturl = $url . "/" . $date; |
---|
148 | logit("DEBUG", exact_time() . " Request resource: " . $resturl); |
---|
149 | |
---|
150 | # compose the request |
---|
151 | my $request = HTTP::Request->new('PUT'); |
---|
152 | $request->url($resturl); |
---|
153 | $request->header('stationKey'=>$config{'key'}); |
---|
154 | my $content = compress($records); |
---|
155 | logit("DEBUG", exact_time() . " Put on wire: " . $content); |
---|
156 | $request->content($content); |
---|
157 | |
---|
158 | # run the request |
---|
159 | logit("DEBUG", exact_time() . " Start server push"); |
---|
160 | my $response = $ua->request($request); |
---|
161 | logit("DEBUG", exact_time() . " End server push"); |
---|
162 | |
---|
163 | # analyze the response |
---|
164 | my $code = $response->code; |
---|
165 | $response->code == 200 || logit("DEBUG", "Error processing records (" . $response->code . ") records=[" . $records . "]"); |
---|
166 | logit("DEBUG", exact_time() . " End flushing data"); |
---|
167 | } |
---|
168 | |
---|
169 | # remove all unused records, debug information, etc. |
---|
170 | sub compress { |
---|
171 | my ($records) = @_; |
---|
172 | my $on_wire; |
---|
173 | foreach my $record (split(';', $records)) { |
---|
174 | if ($record =~ /^\$GPGGA,/ || $record =~ /^\$PFLAA,/) { |
---|
175 | $on_wire = (defined($on_wire)) ? $on_wire . ";" . $record : $record; |
---|
176 | } |
---|
177 | } |
---|
178 | return $on_wire; |
---|
179 | } |
---|
180 | |
---|
181 | # parse options |
---|
182 | getopts('c:di:j:f:th', \%options); |
---|
183 | |
---|
184 | # read config file |
---|
185 | if (defined($options{'c'})) { |
---|
186 | $cfile = $options{'c'}; |
---|
187 | } |
---|
188 | if (defined($options{'d'})) { |
---|
189 | $debug = 1; |
---|
190 | } |
---|
191 | if (defined($options{'i'})) { |
---|
192 | $interval = $options{'i'}; |
---|
193 | } |
---|
194 | if (defined($options{'j'})) { |
---|
195 | $skip = $options{'j'} |
---|
196 | } |
---|
197 | if (defined($options{'h'})) { |
---|
198 | usage(); |
---|
199 | } |
---|
200 | if (defined($options{'t'})) { |
---|
201 | $trace = 1; |
---|
202 | } |
---|
203 | |
---|
204 | # read config file |
---|
205 | readconfig(); |
---|
206 | |
---|
207 | # validation: key must be present in config file |
---|
208 | die("no key found in config file " . $cfile . " (option: key)") unless defined($config{'key'}); |
---|
209 | |
---|
210 | # remove old leftovers |
---|
211 | cleanup(); |
---|
212 | |
---|
213 | # create pipe |
---|
214 | die("no fifo found in config file (option: fifo)") unless defined($fifo); |
---|
215 | if (! -d dirname($fifo)) { |
---|
216 | system("mkdir", "-p", dirname($fifo)) == 0 || die("failed to create fifo directory " . dirname($fifo) . ": $!") |
---|
217 | } |
---|
218 | system("mkfifo", $fifo) == 0 || die("failed to create fifo: $!"); |
---|
219 | |
---|
220 | # force a flush right away and after every write or print |
---|
221 | local $| = 1; |
---|
222 | |
---|
223 | # fork minicom and write to pipe |
---|
224 | defined( my $pid = fork() ) or die "can't fork: $!"; |
---|
225 | unless ($pid) { |
---|
226 | # we're the child |
---|
227 | # detach from session |
---|
228 | setsid() or die "can't start a new session: $!"; |
---|
229 | close(STDIN); |
---|
230 | #close(STDOUT); |
---|
231 | #close(STDERR); |
---|
232 | |
---|
233 | if (defined($options{'f'})) { |
---|
234 | open(DATA, "< $options{'f'}") || die("failed to open data file $options{'f'}: $!"); |
---|
235 | open(FIFO, "> $fifo") || die("failed to open fifo for writing: $!"); |
---|
236 | while(my $line = <DATA>) { |
---|
237 | chomp($line); |
---|
238 | next if ($line =~ /^\s*$/); |
---|
239 | print FIFO $line, "\n" || die("failed to execute child command: $!"); |
---|
240 | } |
---|
241 | close(DATA); |
---|
242 | close(FIFO); |
---|
243 | } else { |
---|
244 | exec("exec minicom -t xterm-color -C $fifo> /dev/null 2>&1") == 0 || die("failed to run minicom: $!"); |
---|
245 | } |
---|
246 | exit 0; |
---|
247 | } |
---|
248 | |
---|
249 | # create UserAgent object |
---|
250 | $ua = new LWP::UserAgent; |
---|
251 | my $buf; |
---|
252 | # read data from pipe |
---|
253 | open(FIFO, "< $fifo") || die("failed to open fifo for reading: $!"); |
---|
254 | my $i = 0; |
---|
255 | while(my $record = <FIFO>) { |
---|
256 | # send only n-th record to the server (option -s) |
---|
257 | if ($i % $skip == 0) { |
---|
258 | chomp($record); |
---|
259 | logit("TRACE", $record); |
---|
260 | $buf = (defined($buf)) ? "$buf;$record" : $record; |
---|
261 | } |
---|
262 | |
---|
263 | # a GPGGA record terminates the sequence |
---|
264 | if ($record =~ /^\$GPGGA,/) { |
---|
265 | if ($i % ($interval * $skip) == 0) { |
---|
266 | flush($buf, $config{'url'}) ; |
---|
267 | $buf = undef; |
---|
268 | sleep($interval * $skip) if (defined($options{'f'})); |
---|
269 | } |
---|
270 | |
---|
271 | $i++; |
---|
272 | } |
---|
273 | } |
---|
274 | close(FIFO); |
---|
275 | |
---|
276 | cleanup(); |
---|
277 | exit 0; |
---|
278 | |
---|