#!/usr/bin/perl #------------------------------------------------------------------------------- # This file is part of the FLARM¨-Radar Project. # # Copyright 2013 Netzschmiede GmbH (http://www.netzschmiede.ch) # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # Project Website: www.flarmradar.ch # Email: info@flarmradar.ch #------------------------------------------------------------------------------- use strict; use warnings; use Getopt::Std; use File::Basename; use Time::HiRes qw(gettimeofday); use POSIX qw(strftime); use POSIX qw(setsid); use LWP::UserAgent; use Device::SerialPort; use constant { OPS => 0b001, DEBUG => 0b010, TRACE => 0b100 }; my %config; my %options; my $log; my $log_initialized = 0; # default values my $log_ops = 1; my $log_debug = 1; my $log_trace = 0; my $interval = 3; my $skip = 1; # signal handlers # toggle $log_trace with 'kill -USR1 ' $SIG{USR1} = sub { if ($log_trace) { $log_trace = 0; } else { $log_trace = 1; } }; $SIG{INT} = sub { die("terminate program"); }; # display a help page sub usage { print <> $log") || print "Failed to open trace file $log: $!"; } else { $log = *STDOUT; } $log_initialized = 1; } print $log "$msg\n" if (($aspect & OPS) && $log_ops); print $log "$msg\n" if (($aspect & TRACE) && $log_trace); print $log "$msg\n" if (($aspect & DEBUG) && $log_debug); } # read config file sub readconfig { my ($config_file) = @_; open(CONF, "< $config_file") || die("failed to open config file for reading: $!"); while(my $line = ) { chomp($line); next if $line =~ /^\s*#/; next if $line =~ /^\s*$/; if ($line =~ /^\s*(\S*)\s*=\s*(\S*)\s*$/) { $config{$1} = $2; } } close(CONF); } # as the name says sub exact_time { return strftime("%H:%M:%S", localtime()) . "." . (gettimeofday())[1]; } # send the records to the server. We don't make a request for each record for # performance reasons. sub flush { my ($records, $url, $ua) = @_; logit(DEBUG, exact_time() . " start flushing data to server"); my $date = `date -u +%Y/%m/%d`; chomp($date); my $resturl = $url . "/" . $date; logit(DEBUG, exact_time() . " request resource: " . $resturl); # compose the request my $request = HTTP::Request->new('PUT'); $request->url($resturl); $request->header('stationKey'=>$config{'key'}); my $content = compress($records); logit(DEBUG, exact_time() . " put on wire: " . $content); $request->content($content); # run the request logit(DEBUG, exact_time() . " start server push"); my $response = $ua->request($request); logit(DEBUG, exact_time() . " end server push"); # analyze the response my $code = $response->code; $response->code == 200 || logit(DEBUG, "error processing records (" . $response->code . ") records=[" . $records . "]"); logit(DEBUG, exact_time() . " end flushing data"); } # remove all unused records, debug information, etc. sub compress { my ($records) = @_; my $on_wire; foreach my $record (split(';', $records)) { if ($record =~ /^\$GPGGA,/ || $record =~ /^\$PFLAA,/) { $on_wire = (defined($on_wire)) ? $on_wire . ";" . $record : $record; } } return $on_wire; } # # here starts our main program # # parse options getopts('c:di:j:f:th', \%options); my $cfile; if (defined($options{'c'})) { $cfile = $options{'c'}; } elsif (-f "/etc/flarmclient.conf") { $cfile = "/etc/flarmclient.conf"; } else { # last resort, search for a config file in the local directory $cfile = "./flarmclient.conf"; } # read config file die ("config file found") unless (-f "./flarmclient.conf"); readconfig($cfile); if (defined($options{'d'})) { $log_debug = 1; } if (defined($options{'i'})) { $interval = $options{'i'}; } if (defined($options{'j'})) { $skip = $options{'j'} } if (defined($options{'h'})) { usage(); } if (defined($options{'t'})) { $log_trace = 1; } # validation: key must be present in config file die("no key found in config file " . $cfile . " (option: key)") unless defined($config{'key'}); # force a flush right away and after every write or print local $| = 1; # ok, everything is setup logit(OPS, "start client, connect to " . $config{'url'}); # create UserAgent object my $ua = new LWP::UserAgent; # create Flarm object my $flarm = new Device::SerialPort ($config{'serial_device'}); $flarm->baudrate($config{'baud'}); $flarm->parity("none"); $flarm->databits(8); $flarm->stopbits(1); $flarm->are_match("\r\n"); my $buf; my $i = 0; while(1) { my $record = $flarm->lookfor(); if ($record) { chomp($record); # send only n-th record to the server (option -s) if ($i % $skip == 0) { chomp($record); logit(TRACE, $record); $buf = (defined($buf)) ? "$buf;$record" : $record; } # a GPGGA record terminates the sequence if ($record =~ /^\$GPGGA,/) { if ($i % ($interval * $skip) == 0) { flush($buf, $config{'url'}, $ua) ; $buf = undef; sleep($interval * $skip) if (defined($options{'f'})); } $i++; } } else { sleep(1); } } exit 0; END { logit(OPS, "client is shutting down"); if (exists($config{"log"})) { close($log); } undef $ua; undef $flarm; }