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