#!/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 optparse import OptionParser
import time, os, sys, threading
from string import split, join
import air_modes
from air_modes.exceptions import *
import zmq

class screen_printer(threading.Thread):
    def __init__(self, position, context, addr=None, port=None):
        threading.Thread.__init__(self)
        self._subscriber = context.socket(zmq.SUB)
        if addr is not None:
            self._subscriber.connect("tcp://%s:%i" % (addr, port))
        else:
            self._subscriber.connect("inproc://modes-radio-pub")
        self._subscriber.setsockopt(zmq.SUBSCRIBE, "dl_data")

        self._printer = air_modes.output_print(position)
        self.done = threading.Event()
        self.finished = threading.Event()
        self.setDaemon(True)
        self.start()

    def run(self):
        while not self.done.is_set():
            [address, msg] = self._subscriber.recv_multipart() #blocking
            try:
                self._printer.output(msg)
            except ADSBError:
                pass

        self._subscriber.close()
        self.finished.set()

#todo: maybe move plugins to separate programs (flightgear, SBS1, etc.)
def main():
  my_position = None
  usage = "%prog: [options]"
  parser = OptionParser(option_class=eng_option, usage=usage)
  air_modes.modes_radio.add_radio_options(parser)
  parser.add_option("-K","--kml", type="string", default=None,
                      help="filename for Google Earth KML output")
  parser.add_option("-P","--sbs1", action="store_true", default=False,
                      help="open an SBS-1-compatible server on port 30003")
  parser.add_option("-s","--servers", type="string", default=None,
                      help="specify additional servers from which to take data in format tcp://x.x.x.x:y,tcp://....")
  parser.add_option("-n","--no-print", action="store_true", default=False,
                      help="disable printing decoded packets to stdout")
  parser.add_option("-l","--location", type="string", default=None,
                      help="GPS coordinates of receiving station in format xx.xxxxx,xx.xxxxx")
  parser.add_option("-u","--udp", type="int", default=None,
                      help="Use UDP source on specified port")
  parser.add_option("-m","--multiplayer", type="string", default=None,
                      help="FlightGear server to send aircraft data, in format host:port")
  parser.add_option("-t","--tcp", type="int", default=None,
                      help="Open a TCP server on this port to publish reports")

  (options, args) = parser.parse_args()

  #construct the radio
  context = zmq.Context(1)
  tb = air_modes.modes_radio(options, context)
  servers = ["inproc://modes-radio-pub"]
  if options.servers is not None:
      servers += options.servers.split(",")
  relay = air_modes.zmq_pubsub_iface(context, subaddr=servers, pubaddr=None)

  if options.location is not None:
    my_position = [float(n) for n in options.location.split(",")]

  if options.kml is not None:
    dbname = 'adsb.db'
    lock = threading.Lock()
    sqldb = air_modes.output_sql(my_position, dbname, lock) #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
    relay.subscribe("dl_data", sqldb.insert)

  printer = None
  if options.no_print is not True:
    relay.subscribe("dl_data", air_modes.output_print(my_position).output)

  if options.multiplayer is not None:
    [fghost, fgport] = options.multiplayer.split(':')
    fgout = air_modes.output_flightgear(my_position, fghost, int(fgport))
    relay.subscribe("dl_data", fgout.output)

#  if options.sbs1 is True:
#    sbs1port = air_modes.output_sbs1(my_position, 30003)
#    relay.subscribe("dl_data", sbs1port.output)
    #updates.append(sbs1port.add_pending_conns) #TODO FIXME
  tb.run()
  tb.cleanup()

  relay.shutdown.set()
  relay.finished.wait(0.2)

  if options.kml is not None:
    kmlgen.close()
    

if __name__ == '__main__':
  main()
