source: core/trunk/client/data/getpath.pl @ 296

Last change on this file since 296 was 296, checked in by smoser, 11 years ago

Update Copyright, fix encoding issue

File size: 4.1 KB
Line 
1#-------------------------------------------------------------------------------
2# This file is part of the FLARM®-Radar Project.
3#   
4#   Copyright 2012-2014 Simon Moser
5#   Copyright 2013-2014 Dominic Spreitz
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#!/usr/bin/perl
23
24use strict;
25use Math::Trig;
26
27my $r = 6371000.785;
28my $maptype = "satellite";
29my $weight = 3;
30my $zoom = 14;
31my $size = "600x600";
32my $sensor = "false";
33my $url = "https://maps.googleapis.com/maps/api/staticmap";
34my $known_flarms = {
35        "4B0CF5" => "Robin HB-EXP",
36        "4B1307" => "Bravo HB-HFJ"
37};
38my $step_time = 3;
39my $timeout = 45;
40
41my ($from, $until, $before, $now) = (undef, undef, undef, undef);
42my ($own_lat, $own_long);
43my $flarm = undef;
44my $seen;
45
46sub fstr {
47  my ($value,$precision) = @_;
48  $precision ||= 5;
49  my $s = sprintf("%.${precision}f", $value);
50  $s =~ s/\.?0*$//;
51  return $s;
52}
53
54
55sub parse_GPGGA {
56        my ($line) = @_;
57        my ($msg,$time,$lat,$northsouth,$long,$eastwest,$quality,$satelites,$dhop,$alt) = split(/,/, $line);
58        my ($g_lat, $g_long) = nmeaToGoogle($lat,$northsouth,$long,$eastwest);
59        print "nmea: " . $lat . $northsouth . "," . $long . $eastwest . "\n";
60        print "google: " . $g_lat . "," . $g_long . "\n";
61        $now = $time;
62        $own_lat = $g_lat;
63        $own_long = $g_long;
64}
65
66sub parse_PFLAA {
67        my ($line) = @_;
68        my ($msg,$alarm,$rel_north,$rel_east,$rel_vert,$id_type,$id,$track,$turnrate,$groundspeed,$climb,$type) = split(/,/, $line);
69        return unless (defined($now));
70        return if (defined($from) && $now < $from);
71        return if (defined($until) && $now > $until);
72        return if (defined($flarm) && $id ne $flarm);
73        return unless (defined($own_lat));
74        return unless (defined($own_long));
75        return unless ($groundspeed > 10 || $rel_vert > 20);
76       
77        print $now . ": " . $line . " " . ($own_lat+distToLat($rel_north)) . "," . ($own_long+distToLong($rel_east)) . "\n";
78        store($now, $id, $own_lat + distToLat($rel_north), $own_long + distToLong($rel_east));
79}
80
81sub store {
82        my ($time, $id, $g_lat, $g_long) = @_;
83       
84        if (!exists($seen->{$id})) {
85                $seen->{$id} = ();
86        }
87        if (!defined($before) || $now  >= $before+$step_time) {
88                $before = $now;
89                push(@{$seen->{$id}}, [$time, $g_lat, $g_long]);
90        }       
91}
92
93sub distToLat {
94        my ($dist) = @_;
95        return 180*$dist/($r*pi);
96}
97
98sub distToLong {
99        my ($dist) = @_;
100        return 180*$dist/(cos($own_lat*pi/180)*$r*pi);
101}
102
103sub nmeaToGoogle {
104        my ($lat,$northsouth,$long,$eastwest) = @_;
105        my $lat_sign = ($northsouth =~ /N/) ? 1 : -1;
106        $lat =~ /^(\d{2})(\d{2})\.(\d{5})$/;
107        my $g_lat = ($1 + ($2 + $3*1e-5)/60) * $lat_sign;
108       
109        my $long_sign = ($eastwest =~ /E/) ? 1 : -1;
110        $long =~ /^(\d{3})(\d{2})\.(\d{5})$/;
111        my $g_long = ($1 + ($2 + $3*1e-5)/60) * $long_sign;
112        return ($g_lat, $g_long);       
113}
114
115while (my $line = <>) {
116        chomp($line);
117        ($line =~ /^\$GPGGA/) && parse_GPGGA($line);
118        ($line =~ /^\$PFLAA/) && parse_PFLAA($line);
119}
120
121foreach my $flarm_id (keys(%{$seen})) {
122        my $plane = exists($known_flarms->{$flarm_id}) ? $known_flarms->{$flarm_id} : "???";
123        print "==== Flarm-ID: $flarm_id ($plane) ====\n";
124
125        my $last_record;
126        foreach my $record (@{$seen->{$flarm_id}}) {
127                my ($time, $g_lat, $g_long) = @{$record};
128               
129                if (!defined($last_record) || $time >= $last_record + $timeout) {
130                        print "\n";
131                        print $url 
132                                . "?maptype=" . $maptype 
133                                . "&zoom=" . $zoom
134                                . "&size=" . $size
135                                . "&sensor=" . $sensor
136                                . "&center=" . fstr($own_lat, 5) . "," . fstr($own_long, 5)
137                                . "&path=color:red|weight:3";
138                }
139                $last_record = $time;
140                print "|".fstr($g_lat,5) . "," . fstr($g_long, 5);
141        }
142        $last_record = undef;
143        print "\n\n";
144}
145
146
Note: See TracBrowser for help on using the repository browser.