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

Last change on this file since 121 was 121, checked in by smoser, 12 years ago

#71

File size: 4.1 KB
Line 
1#-------------------------------------------------------------------------------
2# This file is part of the FLARM¨-Radar Project.
3#   
4#   Copyright 2013 Netzschmiede GmbH (http://www.netzschmiede.ch)
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#   http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18#   Project Website: www.flarmradar.ch
19#   Email: info@flarmradar.ch
20#-------------------------------------------------------------------------------
21#!/usr/bin/perl
22
23use strict;
24use Math::Trig;
25
26my $r = 6371000.785;
27my $maptype = "satellite";
28my $weight = 3;
29my $zoom = 14;
30my $size = "600x600";
31my $sensor = "false";
32my $url = "https://maps.googleapis.com/maps/api/staticmap";
33my $known_flarms = {
34        "4B0CF5" => "Robin HB-EXP",
35        "4B1307" => "Bravo HB-HFJ"
36};
37my $step_time = 3;
38my $timeout = 45;
39
40my ($from, $until, $before, $now) = (undef, undef, undef, undef);
41my ($own_lat, $own_long);
42my $flarm = undef;
43my $seen;
44
45sub fstr {
46  my ($value,$precision) = @_;
47  $precision ||= 5;
48  my $s = sprintf("%.${precision}f", $value);
49  $s =~ s/\.?0*$//;
50  return $s;
51}
52
53
54sub parse_GPGGA {
55        my ($line) = @_;
56        my ($msg,$time,$lat,$northsouth,$long,$eastwest,$quality,$satelites,$dhop,$alt) = split(/,/, $line);
57        my ($g_lat, $g_long) = nmeaToGoogle($lat,$northsouth,$long,$eastwest);
58        print "nmea: " . $lat . $northsouth . "," . $long . $eastwest . "\n";
59        print "google: " . $g_lat . "," . $g_long . "\n";
60        $now = $time;
61        $own_lat = $g_lat;
62        $own_long = $g_long;
63}
64
65sub parse_PFLAA {
66        my ($line) = @_;
67        my ($msg,$alarm,$rel_north,$rel_east,$rel_vert,$id_type,$id,$track,$turnrate,$groundspeed,$climb,$type) = split(/,/, $line);
68        return unless (defined($now));
69        return if (defined($from) && $now < $from);
70        return if (defined($until) && $now > $until);
71        return if (defined($flarm) && $id ne $flarm);
72        return unless (defined($own_lat));
73        return unless (defined($own_long));
74        return unless ($groundspeed > 10 || $rel_vert > 20);
75       
76        print $now . ": " . $line . " " . ($own_lat+distToLat($rel_north)) . "," . ($own_long+distToLong($rel_east)) . "\n";
77        store($now, $id, $own_lat + distToLat($rel_north), $own_long + distToLong($rel_east));
78}
79
80sub store {
81        my ($time, $id, $g_lat, $g_long) = @_;
82       
83        if (!exists($seen->{$id})) {
84                $seen->{$id} = ();
85        }
86        if (!defined($before) || $now  >= $before+$step_time) {
87                $before = $now;
88                push(@{$seen->{$id}}, [$time, $g_lat, $g_long]);
89        }       
90}
91
92sub distToLat {
93        my ($dist) = @_;
94        return 180*$dist/($r*pi);
95}
96
97sub distToLong {
98        my ($dist) = @_;
99        return 180*$dist/(cos($own_lat*pi/180)*$r*pi);
100}
101
102sub nmeaToGoogle {
103        my ($lat,$northsouth,$long,$eastwest) = @_;
104        my $lat_sign = ($northsouth =~ /N/) ? 1 : -1;
105        $lat =~ /^(\d{2})(\d{2})\.(\d{5})$/;
106        my $g_lat = ($1 + ($2 + $3*1e-5)/60) * $lat_sign;
107       
108        my $long_sign = ($eastwest =~ /E/) ? 1 : -1;
109        $long =~ /^(\d{3})(\d{2})\.(\d{5})$/;
110        my $g_long = ($1 + ($2 + $3*1e-5)/60) * $long_sign;
111        return ($g_lat, $g_long);       
112}
113
114while (my $line = <>) {
115        chomp($line);
116        ($line =~ /^\$GPGGA/) && parse_GPGGA($line);
117        ($line =~ /^\$PFLAA/) && parse_PFLAA($line);
118}
119
120foreach my $flarm_id (keys(%{$seen})) {
121        my $plane = exists($known_flarms->{$flarm_id}) ? $known_flarms->{$flarm_id} : "???";
122        print "==== Flarm-ID: $flarm_id ($plane) ====\n";
123
124        my $last_record;
125        foreach my $record (@{$seen->{$flarm_id}}) {
126                my ($time, $g_lat, $g_long) = @{$record};
127               
128                if (!defined($last_record) || $time >= $last_record + $timeout) {
129                        print "\n";
130                        print $url 
131                                . "?maptype=" . $maptype 
132                                . "&zoom=" . $zoom
133                                . "&size=" . $size
134                                . "&sensor=" . $sensor
135                                . "&center=" . fstr($own_lat, 5) . "," . fstr($own_long, 5)
136                                . "&path=color:red|weight:3";
137                }
138                $last_record = $time;
139                print "|".fstr($g_lat,5) . "," . fstr($g_long, 5);
140        }
141        $last_record = undef;
142        print "\n\n";
143}
144
145
Note: See TracBrowser for help on using the repository browser.