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