#!/usr/bin/perl #------------------------------------------------------------------------------- # This file is part of the FLARMĀ®-Radar Project. # # Copyright 2012-2014 Simon Moser # Copyright 2013-2014 Dominic Spreitz # # 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; # constants use constant { OPS => 0b001, DEBUG => 0b010, TRACE => 0b100 }; # FIXME: increas that value my $MAX_READ_OP = 10000; my $MAX_CMD_WAIT = 3; # variables my %config; my %options; my $log; my $log_initialized = 0; my $flarm_initialized = 0; my $read_cnt = 0; my $flarm; # defaults my $log_ops = 1; my $log_debug = 0; 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 <> $config{'log'}") || die("Failed to open log file $config{'log'}: $!"); select($log); $| = 1; } 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); } # send a command to the flarm, optionally wait for the expected answer sub send_flarm_cmd { my ($cmd, $expected_answer) = @_; if (defined($flarm)) { logit(OPS, "write to flarm cmd=" . $cmd); my $count_out = $flarm->write($cmd . "\r\n"); warn "write failed\n" unless ($count_out); my $read_start = exact_time("%s"); my $record; if (defined $expected_answer) { while (1) { until($record = $flarm->lookfor()) { logit(DEBUG, exact_time() . " wait for data from flarm device"); sleep(1); } logit(DEBUG, "read from flarm=" . $record); if ($record =~ /$expected_answer/) { logit(OPS, "command accepted: " . $expected_answer); last; } my $elapsed = exact_time("%s") - $read_start; if ($elapsed > $MAX_CMD_WAIT) { logit(OPS, "WARN: did not receive expected answer in time (" . $MAX_CMD_WAIT . " s)"); last; } } } } } # intialize the flarm device sub initialize_flarm_device { logit(OPS, "initialize flarm device"); # ACFT: type of aircraft, default 'unknown' if (defined($config{'acft_type'})) { send_flarm_cmd("\$PFLAC,S,ACFT," . $config{'acft_type'}, "PFLAC,A,ACFT"); } # NMEAOUT: which sentences are sent to the data port, default '1', all incl. propriatery if (defined($config{'nmeaout'})) { send_flarm_cmd("\$PFLAC,S,NMEAOUT," . $config{'nmeaout'}, "PFLAC,A,NMEAOUT"); } # RANGE_ detection range of aircraft, default 25500, max value 25500 if (defined($config{'range'})) { send_flarm_cmd("\$PFLAC,S,RANGE," . $config{'range'}, "PFLAC,A,RANGE"); } # THRE: threshold for aircraft on ground, default '2' if (defined($config{'threshold'})) { send_flarm_cmd("\$PFLAC,S,THRE," . $config{'threshold'}, "PFLAC,A,THRE"); } # UI: visual and output, default '3' (disable buzzer, enable led warnings) if (defined($config{'visual'})) { send_flarm_cmd("\$PFLAC,S,UI," . $config{'visual'}, "PFLAC,A,UI"); } # no idea how it would behave if it was set to stealth, better switch off send_flarm_cmd("\$PFLAC,S,PRIV,0", "PFLAC,A,PRIV"); # not documented but this removes the vertical restriction send_flarm_cmd("\$PFLAC,S,DEBUG,9", "PFLAC,S,DEBUG,9"); $flarm_initialized = 1; logit(OPS, "device initialization completed"); } # abstraction for reading data (from flarm device or from file) sub readdata { unless (defined($flarm)) { # initialize our flarm object if (defined($options{'f'})) { logit(OPS, "read flarm data from file " . $options{'f'}); open($flarm, "< $options{'f'}") || die("failed to open flarm data file for reading: $!"); } else { # check that we have the required configuration options die("missing option 'serial_device' in configuration file") unless (defined($config{'serial_device'})); die("device does not exist: " . $config{'serial_device'}) unless (-c $config{'serial_device'}); die("missing option 'baud' in configuration file") unless (defined($config{'baud'})); # create serial device object logit(OPS, "initialize serial port reader on device=" . $config{'serial_device'} . " with baud=" . $config{'baud'}); $flarm = new Device::SerialPort ($config{'serial_device'}) || die "Can't open $config{'serial_device'}!\n"; $flarm->baudrate($config{'baud'}); $flarm->parity("none"); $flarm->databits(8); $flarm->stopbits(1); $flarm->are_match("\r\n"); if ($config{'do_device_config'}) { initialize_flarm_device() unless ($flarm_initialized); } } } # read data my $record = undef; if (defined($options{'f'})) { $record = <$flarm>; } else { my $read_start = exact_time("%s"); until($record = $flarm->lookfor()) { logit(DEBUG, exact_time() . " wait for data from flarm device"); sleep(1); $read_start = exact_time("%s"); } my $read_duration = exact_time("%s") - $read_start; if ($read_duration > 0.8) { logit(OPS, "trigger re-initialization of serial port, long read duration=" . $read_duration); $flarm = undef; } # pro-active workaround to prevent failures during continuous operation $read_cnt++; if ($read_cnt >= $MAX_READ_OP) { logit(DEBUG, exact_time() . " reset serial port reader"); $flarm = undef; $read_cnt = 0; } } chomp($record); return $record; } # as the name says sub exact_time { my ($format) = @_; $format = "%H:%M:%S" unless defined($format); return strftime($format, 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 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); my $date= sprintf("%04d", ($year + 1900))."/".sprintf("%02d", ($mon + 1))."/".sprintf("%02d", $mday); 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 ("no config file found") unless (-f "$cfile"); 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; my $buf; my $i = 0; while(my $record = readdata()) { # send only n-th sequence to the server (option -s). A sequence is terminated with a GPGGA-record if ($i % $skip == 0) { 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; if (defined($options{'f'})) { sleep($interval * $skip) } elsif ($i > $MAX_READ_OP) { # force a re-initialization $i = 0; $flarm = undef; } } else { $i++; } } } exit 0; END { logit(OPS, "client is shutting down"); if (exists($config{"log"})) { close($log); } undef $ua; undef $flarm; }