[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 | |
---|
| 23 | use strict; |
---|
| 24 | use warnings; |
---|
| 25 | use Getopt::Std; |
---|
| 26 | use File::Basename; |
---|
[224] | 27 | use Time::HiRes qw(gettimeofday); |
---|
[133] | 28 | use POSIX qw(strftime); |
---|
| 29 | use POSIX qw(setsid); |
---|
[130] | 30 | use LWP::UserAgent; |
---|
[278] | 31 | use Device::SerialPort; |
---|
| 32 | use constant { |
---|
| 33 | OPS => 0b001, |
---|
| 34 | DEBUG => 0b010, |
---|
| 35 | TRACE => 0b100 |
---|
| 36 | }; |
---|
[130] | 37 | my %config; |
---|
| 38 | my %options; |
---|
[278] | 39 | my $log; |
---|
| 40 | my $log_initialized = 0; |
---|
[288] | 41 | my $flarm; |
---|
[278] | 42 | |
---|
| 43 | # default values |
---|
| 44 | my $log_ops = 1; |
---|
[282] | 45 | my $log_debug = 0; |
---|
[278] | 46 | my $log_trace = 0; |
---|
[251] | 47 | my $interval = 3; |
---|
| 48 | my $skip = 1; |
---|
[130] | 49 | |
---|
[278] | 50 | # signal handlers |
---|
| 51 | # toggle $log_trace with 'kill -USR1 <pid>' |
---|
| 52 | $SIG{USR1} = sub { |
---|
| 53 | if ($log_trace) { |
---|
| 54 | $log_trace = 0; |
---|
| 55 | } else { |
---|
| 56 | $log_trace = 1; |
---|
| 57 | } |
---|
| 58 | }; |
---|
| 59 | $SIG{INT} = sub { |
---|
| 60 | die("terminate program"); |
---|
| 61 | }; |
---|
[130] | 62 | |
---|
[278] | 63 | # display a help page |
---|
[130] | 64 | sub usage { |
---|
| 65 | print <<EOF; |
---|
| 66 | NAME |
---|
[278] | 67 | $0 -- stream flarm records to server |
---|
[130] | 68 | |
---|
| 69 | SYNOPSIS |
---|
[278] | 70 | $0 [-c config_file] [-d] [-f data_file] [-i n] [-j m] [-r] [-t] [-h] |
---|
[130] | 71 | |
---|
| 72 | DESCRIPTION |
---|
| 73 | The following options are available: |
---|
| 74 | |
---|
| 75 | -c Use the specified configuration file. Use the default configuration |
---|
| 76 | file as a starting point for customization. |
---|
| 77 | |
---|
[284] | 78 | -d Write out debug information |
---|
| 79 | WARNING: If your configuration is to write the out put to a file (see |
---|
| 80 | option 'log') then this might fill up the file system. |
---|
[224] | 81 | |
---|
[284] | 82 | -f Read the data from the specified data file instead of the serial |
---|
| 83 | device. This is mainly used for testing and development. |
---|
[253] | 84 | |
---|
[251] | 85 | -i Bundle records, send a request to the server every i-th GPGGA record. |
---|
| 86 | Used for bandwidth optimization. Defaults to 3. |
---|
[212] | 87 | |
---|
[251] | 88 | -j Send every j-th record to the server. Used for bandwidth |
---|
| 89 | optimization. Defaults to 1. |
---|
| 90 | |
---|
[284] | 91 | -t Trace client operations, write out all data read from the flarm device |
---|
| 92 | WARNING: If your configuration is to write the out put to a file (see |
---|
| 93 | option 'log') then this might fill up the file system. |
---|
[224] | 94 | |
---|
[130] | 95 | -h Print this help. |
---|
| 96 | |
---|
| 97 | EOF |
---|
| 98 | exit 0; |
---|
| 99 | } |
---|
| 100 | |
---|
[278] | 101 | # a very basic logger |
---|
[204] | 102 | sub logit { |
---|
[278] | 103 | my ($aspect, $msg) = @_; |
---|
| 104 | |
---|
| 105 | if (!$log_initialized) { |
---|
[279] | 106 | if (exists($config{'log'})) { |
---|
[284] | 107 | open($log, ">> $config{'log'}") || die("Failed to open log file $config{'log'}: $!"); |
---|
[278] | 108 | } else { |
---|
| 109 | $log = *STDOUT; |
---|
| 110 | } |
---|
| 111 | $log_initialized = 1; |
---|
[204] | 112 | } |
---|
[278] | 113 | print $log "$msg\n" if (($aspect & OPS) && $log_ops); |
---|
| 114 | print $log "$msg\n" if (($aspect & TRACE) && $log_trace); |
---|
| 115 | print $log "$msg\n" if (($aspect & DEBUG) && $log_debug); |
---|
[204] | 116 | } |
---|
| 117 | |
---|
[278] | 118 | # read config file |
---|
[130] | 119 | sub readconfig { |
---|
[278] | 120 | my ($config_file) = @_; |
---|
| 121 | open(CONF, "< $config_file") || die("failed to open config file for reading: $!"); |
---|
[130] | 122 | while(my $line = <CONF>) { |
---|
| 123 | chomp($line); |
---|
| 124 | next if $line =~ /^\s*#/; |
---|
| 125 | next if $line =~ /^\s*$/; |
---|
| 126 | if ($line =~ /^\s*(\S*)\s*=\s*(\S*)\s*$/) { |
---|
| 127 | $config{$1} = $2; |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | close(CONF); |
---|
| 131 | } |
---|
| 132 | |
---|
[288] | 133 | # abstraction for reading data (from flarm device or from file) |
---|
| 134 | sub readdata { |
---|
| 135 | unless (defined($flarm)) { |
---|
| 136 | # initialize our flarm object |
---|
| 137 | if (defined($options{'f'})) { |
---|
| 138 | logit(OPS, "read flarm data from file " . $options{'f'}); |
---|
| 139 | open($flarm, "< $options{'f'}") || die("failed to open flarm data file for reading: $!"); |
---|
| 140 | } else { |
---|
| 141 | # check that we have the required configuration options |
---|
| 142 | die("missing option 'serial_device' in configuration file") unless (defined($config{'serial_device'})); |
---|
| 143 | die("device does not exist: " . $config{'serial_device'}) unless (-c $config{'serial_device'}); |
---|
| 144 | die("missing option 'baud' in configuration file") unless (defined($config{'baud'})); |
---|
| 145 | |
---|
| 146 | # create serial device object |
---|
| 147 | logit(OPS, "read flarm data from " . $config{'serial_device'} . " with baud=" . $config{'baud'}); |
---|
[289] | 148 | $flarm = new Device::SerialPort ($config{'serial_device'}) || die "Can't open $config{'serial_device'}!\n"; |
---|
[288] | 149 | $flarm->baudrate($config{'baud'}); |
---|
| 150 | $flarm->parity("none"); |
---|
| 151 | $flarm->databits(8); |
---|
| 152 | $flarm->stopbits(1); |
---|
| 153 | $flarm->are_match("\r\n"); |
---|
[289] | 154 | |
---|
| 155 | # === BEGIN Flarm Config === |
---|
| 156 | my $cmd = undef; |
---|
| 157 | # ACFT = Type of aircraft: unknown |
---|
| 158 | # NMEAOUT = Send all data incl. Flarm propriatery data: 1 |
---|
| 159 | # UI = Disable buzzer, enable led warnings: 3 |
---|
| 160 | # PRIV = Disable stealth mode: 0 |
---|
| 161 | # THRE = Threshold for aircraft on ground is 2 m/s: 2 |
---|
| 162 | # Range = Set detection range of aircraft to maximum: 25500 |
---|
| 163 | my @item = ("\$PFLAC,S,ACFT,0\r\n","\$PFLAC,S,NMEAOUT,1\r\n","\$PFLAC,S,UI,3\r\n", |
---|
| 164 | "\$PFLAC,S,PRIV,0\r\n","\$PFLAC,S,THRE,2\r\n","\$PFLAC,S,RANGE,25500\r\n"); |
---|
| 165 | |
---|
| 166 | foreach $cmd (@item) { |
---|
| 167 | local $/ = "\r\n"; |
---|
| 168 | # Read data back from Flarm |
---|
| 169 | my $continue = 1; |
---|
| 170 | my $record = undef; |
---|
| 171 | |
---|
| 172 | while ($continue) { |
---|
| 173 | $record = $flarm->lookfor(); |
---|
| 174 | if (length($record)) { |
---|
| 175 | my ($fanswer,$checksum) = split('\*',$record); |
---|
| 176 | undef $checksum; |
---|
| 177 | my $cmd2 = $cmd; |
---|
| 178 | $cmd2 =~ s/,S,/,A,/g; |
---|
| 179 | chomp($cmd2); |
---|
| 180 | # print $cmd2 . "\n"; |
---|
| 181 | if ($fanswer ~~ $cmd2) { |
---|
| 182 | # print "Setting accepted by Flarm: " . $cmd2 . "\n"; |
---|
| 183 | logit(OPS, "Setting accepted by Flarm: " . $cmd2); |
---|
| 184 | $continue = 0; |
---|
| 185 | } else { |
---|
| 186 | my $cmd3 = $cmd; |
---|
| 187 | chomp($cmd3); |
---|
| 188 | logit(OPS, "Send Flarm config command " . $cmd3); |
---|
| 189 | my $count_out = $flarm->write($cmd); |
---|
| 190 | warn "write failed\n" unless ($count_out); |
---|
| 191 | warn "write incomplete\n" if ( $count_out != length($cmd) ); |
---|
| 192 | } #else |
---|
| 193 | } #endif |
---|
| 194 | } #while |
---|
| 195 | } #foreach |
---|
| 196 | undef $cmd; |
---|
| 197 | undef @item; |
---|
| 198 | # === END Flarm Config === |
---|
[288] | 199 | } |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | my $record = undef; |
---|
| 203 | if (defined($options{'f'})) { |
---|
| 204 | $record = <$flarm>; |
---|
| 205 | } else { |
---|
| 206 | until($record = $flarm->lookfor()) { |
---|
| 207 | sleep(1); |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | chomp($record); |
---|
| 211 | return $record; |
---|
| 212 | } |
---|
| 213 | |
---|
[278] | 214 | # as the name says |
---|
[224] | 215 | sub exact_time { |
---|
| 216 | return strftime("%H:%M:%S", localtime()) . "." . (gettimeofday())[1]; |
---|
| 217 | } |
---|
| 218 | |
---|
[130] | 219 | # send the records to the server. We don't make a request for each record for |
---|
| 220 | # performance reasons. |
---|
| 221 | sub flush { |
---|
[278] | 222 | my ($records, $url, $ua) = @_; |
---|
| 223 | logit(DEBUG, exact_time() . " start flushing data to server"); |
---|
[224] | 224 | |
---|
[217] | 225 | my $date = `date -u +%Y/%m/%d`; |
---|
[224] | 226 | chomp($date); |
---|
[130] | 227 | my $resturl = $url . "/" . $date; |
---|
[278] | 228 | logit(DEBUG, exact_time() . " request resource: " . $resturl); |
---|
[224] | 229 | |
---|
| 230 | # compose the request |
---|
[130] | 231 | my $request = HTTP::Request->new('PUT'); |
---|
| 232 | $request->url($resturl); |
---|
| 233 | $request->header('stationKey'=>$config{'key'}); |
---|
[224] | 234 | my $content = compress($records); |
---|
[278] | 235 | logit(DEBUG, exact_time() . " put on wire: " . $content); |
---|
[224] | 236 | $request->content($content); |
---|
| 237 | |
---|
[130] | 238 | # run the request |
---|
[278] | 239 | logit(DEBUG, exact_time() . " start server push"); |
---|
[130] | 240 | my $response = $ua->request($request); |
---|
[278] | 241 | logit(DEBUG, exact_time() . " end server push"); |
---|
[224] | 242 | |
---|
[130] | 243 | # analyze the response |
---|
| 244 | my $code = $response->code; |
---|
[278] | 245 | $response->code == 200 || logit(DEBUG, "error processing records (" . $response->code . ") records=[" . $records . "]"); |
---|
| 246 | logit(DEBUG, exact_time() . " end flushing data"); |
---|
[130] | 247 | } |
---|
| 248 | |
---|
[223] | 249 | # remove all unused records, debug information, etc. |
---|
| 250 | sub compress { |
---|
| 251 | my ($records) = @_; |
---|
| 252 | my $on_wire; |
---|
| 253 | foreach my $record (split(';', $records)) { |
---|
| 254 | if ($record =~ /^\$GPGGA,/ || $record =~ /^\$PFLAA,/) { |
---|
| 255 | $on_wire = (defined($on_wire)) ? $on_wire . ";" . $record : $record; |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | return $on_wire; |
---|
| 259 | } |
---|
| 260 | |
---|
[278] | 261 | # |
---|
| 262 | # here starts our main program |
---|
| 263 | # |
---|
[261] | 264 | |
---|
[130] | 265 | # parse options |
---|
[251] | 266 | getopts('c:di:j:f:th', \%options); |
---|
[130] | 267 | |
---|
[278] | 268 | my $cfile; |
---|
[130] | 269 | if (defined($options{'c'})) { |
---|
| 270 | $cfile = $options{'c'}; |
---|
[278] | 271 | } elsif (-f "/etc/flarmclient.conf") { |
---|
| 272 | $cfile = "/etc/flarmclient.conf"; |
---|
| 273 | } else { |
---|
| 274 | # last resort, search for a config file in the local directory |
---|
| 275 | $cfile = "./flarmclient.conf"; |
---|
[130] | 276 | } |
---|
[278] | 277 | |
---|
| 278 | # read config file |
---|
[283] | 279 | die ("no config file found") unless (-f "$cfile"); |
---|
[278] | 280 | readconfig($cfile); |
---|
| 281 | |
---|
[224] | 282 | if (defined($options{'d'})) { |
---|
[278] | 283 | $log_debug = 1; |
---|
[224] | 284 | } |
---|
[212] | 285 | if (defined($options{'i'})) { |
---|
| 286 | $interval = $options{'i'}; |
---|
| 287 | } |
---|
[251] | 288 | if (defined($options{'j'})) { |
---|
| 289 | $skip = $options{'j'} |
---|
| 290 | } |
---|
[130] | 291 | if (defined($options{'h'})) { |
---|
| 292 | usage(); |
---|
| 293 | } |
---|
[224] | 294 | if (defined($options{'t'})) { |
---|
[278] | 295 | $log_trace = 1; |
---|
[224] | 296 | } |
---|
[204] | 297 | |
---|
[130] | 298 | # validation: key must be present in config file |
---|
[251] | 299 | die("no key found in config file " . $cfile . " (option: key)") unless defined($config{'key'}); |
---|
[130] | 300 | |
---|
| 301 | # force a flush right away and after every write or print |
---|
| 302 | local $| = 1; |
---|
| 303 | |
---|
[278] | 304 | # ok, everything is setup |
---|
| 305 | logit(OPS, "start client, connect to " . $config{'url'}); |
---|
[257] | 306 | |
---|
[278] | 307 | # create UserAgent object |
---|
| 308 | my $ua = new LWP::UserAgent; |
---|
[130] | 309 | |
---|
| 310 | my $buf; |
---|
[212] | 311 | my $i = 0; |
---|
[289] | 312 | |
---|
[288] | 313 | while(my $record = readdata()) { |
---|
| 314 | |
---|
| 315 | # send only n-th sequence to the server (option -s). A sequence is terminated with a GPGGA-record |
---|
| 316 | if ($i % $skip == 0) { |
---|
[278] | 317 | |
---|
[288] | 318 | logit(TRACE, $record); |
---|
| 319 | $buf = (defined($buf)) ? "$buf;$record" : $record; |
---|
| 320 | } |
---|
[251] | 321 | |
---|
[288] | 322 | # a GPGGA record terminates the sequence |
---|
| 323 | if ($record =~ /^\$GPGGA,/) { |
---|
| 324 | if ($i % ($interval * $skip) == 0) { |
---|
| 325 | flush($buf, $config{'url'}, $ua) ; |
---|
| 326 | $buf = undef; |
---|
| 327 | sleep($interval * $skip) if (defined($options{'f'})); |
---|
[278] | 328 | } |
---|
| 329 | |
---|
[288] | 330 | $i++; |
---|
[130] | 331 | } |
---|
| 332 | } |
---|
| 333 | exit 0; |
---|
| 334 | |
---|
[278] | 335 | END { |
---|
| 336 | logit(OPS, "client is shutting down"); |
---|
| 337 | if (exists($config{"log"})) { |
---|
| 338 | close($log); |
---|
| 339 | } |
---|
| 340 | undef $ua; |
---|
| 341 | undef $flarm; |
---|
| 342 | } |
---|
| 343 | |
---|