gr-air-modes/apps/modes_rx

117 lines
4.4 KiB
Python
Executable File

#!/usr/bin/env python
# Copyright 2010, 2013 Nick Foster
#
# This file is part of gr-air-modes
#
# gr-air-modes is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# gr-air-modes is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with gr-air-modes; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
from gnuradio.eng_option import eng_option
from gnuradio.gr.pubsub import pubsub
from optparse import OptionParser
import time, os, sys, threading, math
from string import split, join
import air_modes
from air_modes.types import *
from air_modes.exceptions import *
import zmq
#todo: maybe move plugins to separate programs (flightgear, SBS1, etc.)
def main():
my_position = None
usage = "%prog: [options]"
optparser = OptionParser(option_class=eng_option, usage=usage)
air_modes.modes_radio.add_radio_options(optparser)
optparser.add_option("-l","--location", type="string", default=None,
help="GPS coordinates of receiving station in format xx.xxxxx,xx.xxxxx")
#data source options
optparser.add_option("-a","--remote", type="string", default=None,
help="specify additional servers from which to take data in format tcp://x.x.x.x:y,tcp://....")
optparser.add_option("-n","--no-print", action="store_true", default=False,
help="disable printing decoded packets to stdout")
#output plugins
optparser.add_option("-K","--kml", type="string", default=None,
help="filename for Google Earth KML output")
optparser.add_option("-P","--sbs1", action="store_true", default=False,
help="open an SBS-1-compatible server on port 30003")
optparser.add_option("-m","--multiplayer", type="string", default=None,
help="FlightGear server to send aircraft data, in format host:port")
(options, args) = optparser.parse_args()
#construct the radio
context = zmq.Context(1)
tb = air_modes.modes_radio(options, context)
servers = ["inproc://modes-radio-pub"]
if options.remote is not None:
servers += options.remote.split(",")
relay = air_modes.zmq_pubsub_iface(context, subaddr=servers, pubaddr=None)
#ok now relay is gonna get all those tasty strings
#internally we want to distribute parsed data instead, lighten the load
#TODO obviously this should go somewhere besides the main app. But where?
publisher = pubsub()
def make_report(pub, message):
[data, ecc, reference, timestamp] = message.split()
try:
ret = air_modes.modes_report(air_modes.modes_reply(int(data, 16)),
int(ecc, 16),
20.0*math.log10(float(reference)),
air_modes.stamp(0, float(timestamp)))
pub["modes_dl"] = ret
pub["type%i_dl" % ret.data.get_type()] = ret
except ADSBError:
pass
send = lambda msg: make_report(publisher, msg)
relay.subscribe("dl_data", send)
if options.location is not None:
my_position = [float(n) for n in options.location.split(",")]
#CPR decoder obj to handle getting position from BDS0,5 and BDS0,6 pkts
cpr_dec = air_modes.cpr.cpr_decoder(my_position)
if options.kml is not None:
dbname = 'adsb.db'
lock = threading.Lock()
sqldb = air_modes.output_sql(cpr_dec, dbname, lock, publisher) #input into the db
kmlgen = air_modes.output_kml(options.kml, dbname, my_position, lock) #create a KML generating thread to read from the db
if options.no_print is not True:
printer = air_modes.output_print(cpr_dec, publisher)
if options.multiplayer is not None:
[fghost, fgport] = options.multiplayer.split(':')
fgout = air_modes.output_flightgear(cpr_dec, fghost, int(fgport), publisher)
if options.sbs1 is True:
sbs1port = air_modes.output_sbs1(cpr_dec, 30003, publisher)
tb.run()
tb.close()
relay.close()
if options.kml is not None:
kmlgen.close()
if __name__ == '__main__':
main()