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 | use Device::SerialPort; |
---|
32 | use constant { |
---|
33 | OPS => 0b001, |
---|
34 | DEBUG => 0b010, |
---|
35 | TRACE => 0b100 |
---|
36 | }; |
---|
37 | my %config; |
---|
38 | my %options; |
---|
39 | my $log; |
---|
40 | my $log_initialized = 0; |
---|
41 | |
---|
42 | # default values |
---|
43 | my $log_ops = 1; |
---|
44 | my $log_debug = 0; |
---|
45 | my $log_trace = 0; |
---|
46 | my $interval = 3; |
---|
47 | my $skip = 1; |
---|
48 | |
---|
49 | # signal handlers |
---|
50 | # toggle $log_trace with 'kill -USR1 <pid>' |
---|
51 | $SIG{USR1} = sub { |
---|
52 | if ($log_trace) { |
---|
53 | $log_trace = 0; |
---|
54 | } else { |
---|
55 | $log_trace = 1; |
---|
56 | } |
---|
57 | }; |
---|
58 | $SIG{INT} = sub { |
---|
59 | die("terminate program"); |
---|
60 | }; |
---|
61 | |
---|
62 | # display a help page |
---|
63 | sub usage { |
---|
64 | print <<EOF; |
---|
65 | NAME |
---|
66 | $0 -- stream flarm records to server |
---|
67 | |
---|
68 | SYNOPSIS |
---|
69 | $0 [-c config_file] [-d] [-f data_file] [-i n] [-j m] [-r] [-t] [-h] |
---|
70 | |
---|
71 | DESCRIPTION |
---|
72 | The following options are available: |
---|
73 | |
---|
74 | -c Use the specified configuration file. Use the default configuration |
---|
75 | file as a starting point for customization. |
---|
76 | |
---|
77 | -d Write out debug information |
---|
78 | WARNING: If your configuration is to write the out put to a file (see |
---|
79 | option 'log') then this might fill up the file system. |
---|
80 | |
---|
81 | -f Read the data from the specified data file instead of the serial |
---|
82 | device. This is mainly used for testing and development. |
---|
83 | |
---|
84 | -i Bundle records, send a request to the server every i-th GPGGA record. |
---|
85 | Used for bandwidth optimization. Defaults to 3. |
---|
86 | |
---|
87 | -j Send every j-th record to the server. Used for bandwidth |
---|
88 | optimization. Defaults to 1. |
---|
89 | |
---|
90 | -t Trace client operations, write out all data read from the flarm device |
---|
91 | WARNING: If your configuration is to write the out put to a file (see |
---|
92 | option 'log') then this might fill up the file system. |
---|
93 | |
---|
94 | -h Print this help. |
---|
95 | |
---|
96 | EOF |
---|
97 | exit 0; |
---|
98 | } |
---|
99 | |
---|
100 | # a very basic logger |
---|
101 | sub logit { |
---|
102 | my ($aspect, $msg) = @_; |
---|
103 | |
---|
104 | if (!$log_initialized) { |
---|
105 | if (exists($config{'log'})) { |
---|
106 | open($log, ">> $config{'log'}") || die("Failed to open log file $config{'log'}: $!"); |
---|
107 | } else { |
---|
108 | $log = *STDOUT; |
---|
109 | } |
---|
110 | $log_initialized = 1; |
---|
111 | } |
---|
112 | print $log "$msg\n" if (($aspect & OPS) && $log_ops); |
---|
113 | print $log "$msg\n" if (($aspect & TRACE) && $log_trace); |
---|
114 | print $log "$msg\n" if (($aspect & DEBUG) && $log_debug); |
---|
115 | } |
---|
116 | |
---|
117 | # read config file |
---|
118 | sub readconfig { |
---|
119 | my ($config_file) = @_; |
---|
120 | open(CONF, "< $config_file") || die("failed to open config file for reading: $!"); |
---|
121 | while(my $line = <CONF>) { |
---|
122 | chomp($line); |
---|
123 | next if $line =~ /^\s*#/; |
---|
124 | next if $line =~ /^\s*$/; |
---|
125 | if ($line =~ /^\s*(\S*)\s*=\s*(\S*)\s*$/) { |
---|
126 | $config{$1} = $2; |
---|
127 | } |
---|
128 | } |
---|
129 | close(CONF); |
---|
130 | } |
---|
131 | |
---|
132 | # as the name says |
---|
133 | sub exact_time { |
---|
134 | return strftime("%H:%M:%S", localtime()) . "." . (gettimeofday())[1]; |
---|
135 | } |
---|
136 | |
---|
137 | # send the records to the server. We don't make a request for each record for |
---|
138 | # performance reasons. |
---|
139 | sub flush { |
---|
140 | my ($records, $url, $ua) = @_; |
---|
141 | logit(DEBUG, exact_time() . " start flushing data to server"); |
---|
142 | |
---|
143 | my $date = `date -u +%Y/%m/%d`; |
---|
144 | chomp($date); |
---|
145 | my $resturl = $url . "/" . $date; |
---|
146 | logit(DEBUG, exact_time() . " request resource: " . $resturl); |
---|
147 | |
---|
148 | # compose the request |
---|
149 | my $request = HTTP::Request->new('PUT'); |
---|
150 | $request->url($resturl); |
---|
151 | $request->header('stationKey'=>$config{'key'}); |
---|
152 | my $content = compress($records); |
---|
153 | logit(DEBUG, exact_time() . " put on wire: " . $content); |
---|
154 | $request->content($content); |
---|
155 | |
---|
156 | # run the request |
---|
157 | logit(DEBUG, exact_time() . " start server push"); |
---|
158 | my $response = $ua->request($request); |
---|
159 | logit(DEBUG, exact_time() . " end server push"); |
---|
160 | |
---|
161 | # analyze the response |
---|
162 | my $code = $response->code; |
---|
163 | $response->code == 200 || logit(DEBUG, "error processing records (" . $response->code . ") records=[" . $records . "]"); |
---|
164 | logit(DEBUG, exact_time() . " end flushing data"); |
---|
165 | } |
---|
166 | |
---|
167 | # remove all unused records, debug information, etc. |
---|
168 | sub compress { |
---|
169 | my ($records) = @_; |
---|
170 | my $on_wire; |
---|
171 | foreach my $record (split(';', $records)) { |
---|
172 | if ($record =~ /^\$GPGGA,/ || $record =~ /^\$PFLAA,/) { |
---|
173 | $on_wire = (defined($on_wire)) ? $on_wire . ";" . $record : $record; |
---|
174 | } |
---|
175 | } |
---|
176 | return $on_wire; |
---|
177 | } |
---|
178 | |
---|
179 | # |
---|
180 | # here starts our main program |
---|
181 | # |
---|
182 | |
---|
183 | # parse options |
---|
184 | getopts('c:di:j:f:th', \%options); |
---|
185 | |
---|
186 | my $cfile; |
---|
187 | if (defined($options{'c'})) { |
---|
188 | $cfile = $options{'c'}; |
---|
189 | } elsif (-f "/etc/flarmclient.conf") { |
---|
190 | $cfile = "/etc/flarmclient.conf"; |
---|
191 | } else { |
---|
192 | # last resort, search for a config file in the local directory |
---|
193 | $cfile = "./flarmclient.conf"; |
---|
194 | } |
---|
195 | |
---|
196 | # read config file |
---|
197 | die ("no config file found") unless (-f "$cfile"); |
---|
198 | readconfig($cfile); |
---|
199 | |
---|
200 | if (defined($options{'d'})) { |
---|
201 | $log_debug = 1; |
---|
202 | } |
---|
203 | if (defined($options{'i'})) { |
---|
204 | $interval = $options{'i'}; |
---|
205 | } |
---|
206 | if (defined($options{'j'})) { |
---|
207 | $skip = $options{'j'} |
---|
208 | } |
---|
209 | if (defined($options{'h'})) { |
---|
210 | usage(); |
---|
211 | } |
---|
212 | if (defined($options{'t'})) { |
---|
213 | $log_trace = 1; |
---|
214 | } |
---|
215 | |
---|
216 | # validation: key must be present in config file |
---|
217 | die("no key found in config file " . $cfile . " (option: key)") unless defined($config{'key'}); |
---|
218 | |
---|
219 | # force a flush right away and after every write or print |
---|
220 | local $| = 1; |
---|
221 | |
---|
222 | # ok, everything is setup |
---|
223 | logit(OPS, "start client, connect to " . $config{'url'}); |
---|
224 | |
---|
225 | # create UserAgent object |
---|
226 | my $ua = new LWP::UserAgent; |
---|
227 | |
---|
228 | # create Flarm object |
---|
229 | my $flarm = new Device::SerialPort ($config{'serial_device'}); |
---|
230 | $flarm->baudrate($config{'baud'}); |
---|
231 | $flarm->parity("none"); |
---|
232 | $flarm->databits(8); |
---|
233 | $flarm->stopbits(1); |
---|
234 | $flarm->are_match("\r\n"); |
---|
235 | |
---|
236 | my $buf; |
---|
237 | my $i = 0; |
---|
238 | while(1) { |
---|
239 | my $record = $flarm->lookfor(); |
---|
240 | if ($record) { |
---|
241 | chomp($record); |
---|
242 | |
---|
243 | # send only n-th record to the server (option -s) |
---|
244 | if ($i % $skip == 0) { |
---|
245 | chomp($record); |
---|
246 | |
---|
247 | logit(TRACE, $record); |
---|
248 | $buf = (defined($buf)) ? "$buf;$record" : $record; |
---|
249 | } |
---|
250 | |
---|
251 | # a GPGGA record terminates the sequence |
---|
252 | if ($record =~ /^\$GPGGA,/) { |
---|
253 | if ($i % ($interval * $skip) == 0) { |
---|
254 | flush($buf, $config{'url'}, $ua) ; |
---|
255 | $buf = undef; |
---|
256 | sleep($interval * $skip) if (defined($options{'f'})); |
---|
257 | } |
---|
258 | |
---|
259 | $i++; |
---|
260 | } |
---|
261 | |
---|
262 | } else { |
---|
263 | sleep(1); |
---|
264 | } |
---|
265 | } |
---|
266 | exit 0; |
---|
267 | |
---|
268 | END { |
---|
269 | logit(OPS, "client is shutting down"); |
---|
270 | if (exists($config{"log"})) { |
---|
271 | close($log); |
---|
272 | } |
---|
273 | undef $ua; |
---|
274 | undef $flarm; |
---|
275 | } |
---|
276 | |
---|