Debian Codegraph @

mlat/client/options.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # -*- mode: python; indent-tabs-mode: nil -*-
3
4 # Part of mlat-client - an ADS-B multilateration client.
5 # Copyright 2015, Oliver Jowett <[email protected]>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 import argparse
21 import functools
22 import time
23
24 import _modes
25 from mlat.client.receiver import ReceiverConnection
26 from mlat.client.output import OutputListener, OutputConnector
27 from mlat.client.output import BasestationConnection, ExtBasestationConnection, BeastConnection
28 from mlat.client.util import log
29
30 _receiver_types = {
31 # input type -> decoder mode, server clock type
32 # the server clock type is used by the server to set
33 # the clock jitter etc; clock frequency and
34 # epoch are provided by the client.
35
36 'auto': (None, 'unknown'),
37 'dump1090': (_modes.BEAST, 'dump1090'),
38 'beast': (_modes.BEAST, 'beast'),
39 'radarcape_12mhz': (_modes.BEAST, 'radarcape_12mhz'), # compat
40 'radarcape_gps': (_modes.RADARCAPE, 'radarcape_gps'), # compat
41 'radarcape': (_modes.RADARCAPE, 'radarcape_gps'), # autodetects gps if present (doesn't work with intermediary decoder between mlat-cleint and radarcape, assume radarcape_gps)
42 'sbs': (_modes.SBS, 'sbs'),
43 'avrmlat': (_modes.AVRMLAT, 'unknown'),
44 }
45
46
47 def latitude(s):
48 lat = float(s)
49 if lat < -90 or lat > 90:
50 raise argparse.ArgumentTypeError('Latitude %s must be in the range -90 to 90' % s)
51 return lat
52
53
54 def longitude(s):
55 lon = float(s)
56 if lon < -180 or lon > 360:
57 raise argparse.ArgumentTypeError('Longitude %s must be in the range -180 to 360' % s)
58 if lon > 180:
59 lon -= 360
60 return lon
61
62
63 def altitude(s):
64 if s.endswith('m'):
65 alt = float(s[:-1])
66 elif s.endswith('ft'):
67 alt = float(s[:-2]) * 0.3048
68 else:
69 alt = float(s)
70
71 # Wikipedia to the rescue!
72 # "The lowest point on dry land is the shore of the Dead Sea [...]
73 # 418m below sea level". Perhaps not the best spot for a receiver?
74 # La Rinconada, Peru, pop. 30,000, is at 5100m.
75 if s == '60440ft':
76 log('<3>Altitude not configured, please configure and reboot')
77 time.sleep(3600)
78 raise SystemExit
79
80 if alt < -420 or alt > 5100:
81 raise argparse.ArgumentTypeError('Altitude %s must be in the range -420m to 6000m' % s)
82 return alt
83
84
85 def port(s):
86 port = int(s)
87 if port < 1 or port > 65535:
88 raise argparse.ArgumentTypeError('Port %s must be in the range 1 to 65535' % s)
89 return port
90
91
92 def hostport(s):
93 parts = s.split(':')
94 if len(parts) != 2:
95 raise argparse.ArgumentTypeError("{} should be in 'host:port' format".format(s))
96 return (parts[0], int(parts[1]))
97
98
99 def make_inputs_group(parser):
100 inputs = parser.add_argument_group('Mode S receiver input connection')
101 inputs.add_argument('--input-type',
102 help="Sets the input receiver type.",
103 choices=_receiver_types.keys(),
104 default='dump1090')
105 inputs.add_argument('--input-connect',
106 help="host:port to connect to for Mode S traffic. Required.",
107 required=True,
108 type=hostport,
109 default=('localhost', 30005))
110
111
112 def clock_frequency(args):
113 return _modes.Reader(_receiver_types[args.input_type][0]).frequency
114
115
116 def clock_epoch(args):
117 return _modes.Reader(_receiver_types[args.input_type][0]).epoch
118
119
120 def clock_type(args):
121 return _receiver_types[args.input_type][1]
122
123
124 def connection_mode(args):
125 return _receiver_types[args.input_type][0]
126
127
128 def make_results_group(parser):
129 results = parser.add_argument_group('Results output')
130 results.add_argument('--results',
131 help="""
132 <protocol>,connect,host:port or <protocol>,listen,port.
133 Protocol may be 'basestation', 'ext_basestation', or 'beast'. Can be specified multiple times.""",
134 action='append',
135 default=[])
136 results.add_argument("--no-anon-results",
137 help="Do not generate results for anonymized aircraft",
138 action='store_false',
139 dest='allow_anon_results',
140 default=True)
141 results.add_argument("--no-modeac-results",
142 help="Do not generate results for Mode A/C tracks",
143 action='store_false',
144 dest='allow_modeac_results',
145 default=True)
146
147 return results
148
149
150 def output_factory(s):
151 parts = s.split(',')
152 if len(parts) != 3:
153 raise ValueError('exactly three comma-separated values are needed (see help)')
154
155 ctype, cmode, addr = parts
156
157 connections = {
158 'basestation': BasestationConnection,
159 'ext_basestation': ExtBasestationConnection,
160 'beast': BeastConnection
161 }
162
163 c = connections.get(ctype)
164 if c is None:
165 raise ValueError("connection type '{0}' is not supported; options are: '{1}'".format(
166 ctype, "','".join(connections.keys())))
167
168 if cmode == 'listen':
169 return functools.partial(OutputListener, port=int(addr), connection_factory=c)
170 elif cmode == 'connect':
171 return functools.partial(OutputConnector, addr=hostport(addr), connection_factory=c)
172 else:
173 raise ValueError("connection mode '{0}' is not supported; options are: 'connect','listen'".format(cmode))
174
175
176 def build_outputs(args):
177 outputs = []
178 for s in args.results:
179 try:
180 factory = output_factory(s)
181 except ValueError as e:
182 log("Warning: Ignoring bad results output option '{0}': {1}",
183 s, str(e))
184 continue
185
186 try:
187 output = factory()
188 except Exception as e:
189 log("Warning: Could not create results output '{0}': {1}",
190 s, str(e))
191 continue
192
193 outputs.append(output)
194
195 return outputs
196
197
198 def build_receiver_connection(args):
199 return ReceiverConnection(host=args.input_connect[0],
200 port=args.input_connect[1],
201 mode=connection_mode(args))