Setting up a model-view display for the airframe list.

This commit is contained in:
Nick Foster 2012-07-05 23:52:00 -07:00
parent 8cd551689b
commit b4cafe0384
5 changed files with 494 additions and 449 deletions

View File

@ -8,13 +8,13 @@ import air_modes
from air_modes.modes_exceptions import * from air_modes.modes_exceptions import *
from air_modes.modes_rx_ui import Ui_MainWindow from air_modes.modes_rx_ui import Ui_MainWindow
import csv import csv
import sqlite3
class mainwindow(QtGui.QMainWindow): class mainwindow(QtGui.QMainWindow):
def __init__(self): def __init__(self):
QtGui.QMainWindow.__init__(self) QtGui.QMainWindow.__init__(self)
self.ui = Ui_MainWindow() self.ui = Ui_MainWindow()
self.ui.setupUi(self) self.ui.setupUi(self)
self.setObjectName("gr-air-modes Mode S receiver")
#set defaults #set defaults
#add file, RTL, UHD sources #add file, RTL, UHD sources
@ -53,6 +53,13 @@ class mainwindow(QtGui.QMainWindow):
self.updates = [] self.updates = []
self.output_handler = None self.output_handler = None
self.kmlgen = None #necessary bc we stop its thread in shutdown self.kmlgen = None #necessary bc we stop its thread in shutdown
self.dbinput = None
self.dbname = 'air_modes.db'
self.datamodel = modes_datamodel(self.dbname)
self.ui.list_aircraft.setModel(self.datamodel)
self.ui.list_aircraft.show() #TODO remove
#goes and gets valid antenna, sample rate options from the device and grays out appropriate things #goes and gets valid antenna, sample rate options from the device and grays out appropriate things
def populate_source_options(self): def populate_source_options(self):
@ -111,6 +118,8 @@ class mainwindow(QtGui.QMainWindow):
if self.runner is not None: if self.runner is not None:
self.output_handler.done = True self.output_handler.done = True
self.output_handler = None self.output_handler = None
self.outputs = []
self.updates = []
self.fg.stop() self.fg.stop()
self.runner = None self.runner = None
self.fg = None self.fg = None
@ -120,6 +129,9 @@ class mainwindow(QtGui.QMainWindow):
#self.kmlgen.join() #self.kmlgen.join()
#self.kmlgen = None #self.kmlgen = None
if self.dbinput is not None: self.dbinput.close()
self.dbinput = None
self.ui.button_start.setText("Start") self.ui.button_start.setText("Start")
else: #we aren't already running, let's get this party started else: #we aren't already running, let's get this party started
@ -145,8 +157,7 @@ class mainwindow(QtGui.QMainWindow):
#output options to populate outputs, updates #output options to populate outputs, updates
if self.ui.check_kml.checkState(): if self.ui.check_kml.checkState():
#we spawn a thread to run every 30 seconds (or whatever) to generate KML #we spawn a thread to run every 30 seconds (or whatever) to generate KML
self.kmlgen = air_modes.modes_kml(self.ui.line_kmlfilename.text(), my_position) #create a KML generating thread self.kmlgen = air_modes.modes_kml(self.ui.line_kmlfilename.text(), self.dbname, my_position) #create a KML generating thread
self.outputs.append(self.kmlgen.output)
if self.ui.check_sbs1.checkState(): if self.ui.check_sbs1.checkState():
sbs1port = int(self.ui.line_sbs1port.text()) sbs1port = int(self.ui.line_sbs1port.text())
@ -166,19 +177,57 @@ class mainwindow(QtGui.QMainWindow):
self.updates.append(rawport.add_pending_conns) self.updates.append(rawport.add_pending_conns)
self.livedata = air_modes.modes_output_print(my_position) self.livedata = air_modes.modes_output_print(my_position)
#add output for live data box #add output for live data box
self.outputs.append(self.output_live_data) self.outputs.append(self.output_live_data)
#create output handler #create SQL database for KML and dashboard displays
self.dbwriter = air_modes.modes_output_sql(my_position, self.dbname)
self.outputs.append(self.dbwriter.output) #now the db will update itself
#create output handler thread
self.output_handler = output_handler(self.outputs, self.updates, self.queue) self.output_handler = output_handler(self.outputs, self.updates, self.queue)
self.ui.button_start.setText("Stop") #modify button text
self.ui.button_start.setText("Stop")
def output_live_data(self, msg): def output_live_data(self, msg):
msgstr = self.livedata.parse(msg) msgstr = self.livedata.parse(msg)
if msgstr is not None: if msgstr is not None:
self.ui.text_livedata.append(msgstr) self.ui.text_livedata.append(msgstr)
self.ui.text_livedata.verticalScrollBar().setSliderPosition(self.ui.text_livedata.verticalScrollBar().maximum())
def on_list_aircraft_clicked(self, index):
icao = long(str(index.data().toString()), 16)
#ok now let's fetch the info for this icao and update the display
print icao
#eventually: set up a QTimer or whatever and have it self-update and emit dataChanged()
#better yet just have the SQL interface yell and say hey that line changed.
#on selected aircraft or on update for visible aircraft, emit signal to update current dashboard display.
class modes_datamodel(QtCore.QAbstractListModel):
def __init__(self, dbname):
QtCore.QAbstractListModel.__init__(self)
self.db = sqlite3.connect(dbname)
def rowCount(self, parent=QtCore.QModelIndex()):
icaoquery = "select count(distinct icao) from positions"
cursor = self.db.cursor()
cursor.execute(icaoquery)
icaolist = cursor.fetchall()
cursor.close()
return icaolist[0][0]
def data(self, index, role=QtCore.Qt.DisplayRole):
if not index.isValid():
return QtCore.QVariant()
if index.row() >= self.rowCount():
return QtCore.QVariant()
if role != QtCore.Qt.DisplayRole:
return QtCore.QVariant()
icaoquery = "select distinct icao from positions order by icao"
cursor = self.db.cursor()
cursor.execute(icaoquery)
icaolist = cursor.fetchall()
cursor.close()
return "%06x" % icaolist[index.row()][0]
class output_handler(threading.Thread): class output_handler(threading.Thread):
def __init__(self, outputs, updates, queue): def __init__(self, outputs, updates, queue):
@ -205,6 +254,9 @@ class output_handler(threading.Thread):
time.sleep(0.1) time.sleep(0.1)
self.done = True self.done = True
self.outputs = None
self.updates = None
self.queue = None
class top_block_runner(_threading.Thread): class top_block_runner(_threading.Thread):
@ -274,17 +326,11 @@ class adsb_rx_block (gr.top_block):
self.connect(self.avg, (self.preamble, 1)) self.connect(self.avg, (self.preamble, 1))
self.connect(self.preamble, self.slicer) self.connect(self.preamble, self.slicer)
class wat_block(gr.top_block):
def __init__(self, options, queue):
gr.top_block.__init__(self)
self.src = gr.file_source(gr.sizeof_gr_complex, options["filename"])
self.sink = gr.null_sink(gr.sizeof_gr_complex)
self.connect(self.src, self.sink)
if __name__ == '__main__': if __name__ == '__main__':
app = QtGui.QApplication(sys.argv) app = QtGui.QApplication(sys.argv)
window = mainwindow() window = mainwindow()
window.setWindowTitle("Mode S/ADS-B receiver")
window.show() window.show()
sys.exit(app.exec_()) sys.exit(app.exec_())

View File

@ -190,8 +190,10 @@ if __name__ == '__main__':
if options.kml is not None: if options.kml is not None:
#we spawn a thread to run every 30 seconds (or whatever) to generate KML #we spawn a thread to run every 30 seconds (or whatever) to generate KML
kmlgen = air_modes.modes_kml(options.kml, my_position) #create a KML generating thread dbname = 'adsb.db'
outputs.append(kmlgen.output) sqldb = air_modes.modes_output_sql(my_position, dbname) #input into the db
kmlgen = air_modes.modes_kml(options.kml, dbname, my_position) #create a KML generating thread to read from the db
outputs.append(sqldb.output)
if options.sbs1 is True: if options.sbs1 is True:
sbs1port = air_modes.modes_output_sbs1(my_position, 30003) sbs1port = air_modes.modes_output_sbs1(my_position, 30003)

View File

@ -21,13 +21,11 @@
import sqlite3 import sqlite3
import string, math, threading, time import string, math, threading, time
from air_modes.modes_sql import modes_output_sql
class modes_kml(threading.Thread, modes_output_sql): class modes_kml(threading.Thread):
def __init__(self, filename, localpos, timeout=5): def __init__(self, filename, dbname, localpos, timeout=5):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self._dbname = 'adsb.db' self._dbname = dbname
modes_output_sql.__init__(self, localpos, self._dbname) #write to the db
self._filename = filename self._filename = filename
self.my_coords = localpos self.my_coords = localpos
self._timeout = timeout self._timeout = timeout
@ -43,6 +41,8 @@ class modes_kml(threading.Thread, modes_output_sql):
time.sleep(self._timeout) time.sleep(self._timeout)
self.done = True self.done = True
self._db.close()
self._db = None
def writekml(self): def writekml(self):
kmlstr = self.genkml() kmlstr = self.genkml()

View File

@ -29,6 +29,7 @@ class modes_output_sql(modes_parse.modes_parse):
def __init__(self, mypos, filename): def __init__(self, mypos, filename):
modes_parse.modes_parse.__init__(self, mypos) modes_parse.modes_parse.__init__(self, mypos)
#create the database #create the database
self.filename = filename
self.db = sqlite3.connect(filename) self.db = sqlite3.connect(filename)
#now execute a schema to create the tables you need #now execute a schema to create the tables you need
c = self.db.cursor() c = self.db.cursor()
@ -54,18 +55,28 @@ class modes_output_sql(modes_parse.modes_parse):
);""" );"""
c.execute(query) c.execute(query)
c.close() c.close()
#we close the db conn now to reopen it in the output() thread context.
self.db.close()
self.db = None
def __del__(self): def __del__(self):
self.db.close() self.db = None
def output(self, message): def output(self, message):
try: try:
#we're checking to see if the db is empty, and creating the db object
#if it is. the reason for this is so that the db writing is done within
#the thread context of output(), rather than the thread context of the
#constructor. that way you can spawn a thread to do output().
if self.db is None:
self.db = sqlite3.connect(self.filename)
query = self.make_insert_query(message) query = self.make_insert_query(message)
if query is not None: if query is not None:
c = self.db.cursor() c = self.db.cursor()
c.execute(query) c.execute(query)
c.close() c.close()
self.db.commit() self.db.commit() #don't know if this is necessary
except ADSBError: except ADSBError:
pass pass

View File

@ -6,29 +6,35 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>877</width> <width>686</width>
<height>618</height> <height>413</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>MainWindow</string> <string>MainWindow</string>
</property> </property>
<widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="centralwidget">
<widget class="QListView" name="list_aircraft"> <widget class="QTabWidget" name="tab_dataview">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>10</x> <x>130</x>
<y>240</y> <y>30</y>
<width>91</width> <width>551</width>
<height>281</height> <height>301</height>
</rect> </rect>
</property> </property>
</widget> <property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Setup</string>
</attribute>
<widget class="QGroupBox" name="group_input"> <widget class="QGroupBox" name="group_input">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>10</x> <x>10</x>
<y>10</y> <y>20</y>
<width>221</width> <width>221</width>
<height>191</height> <height>191</height>
</rect> </rect>
@ -234,8 +240,8 @@
<widget class="QGroupBox" name="group_output"> <widget class="QGroupBox" name="group_output">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>260</x> <x>270</x>
<y>10</y> <y>20</y>
<width>281</width> <width>281</width>
<height>151</height> <height>151</height>
</rect> </rect>
@ -387,45 +393,74 @@
<string>FlightGear</string> <string>FlightGear</string>
</property> </property>
</widget> </widget>
<zorder>check_kml</zorder>
<zorder>check_kml</zorder>
<zorder>label_6</zorder>
<zorder>label_7</zorder>
<zorder>check_sbs1</zorder>
<zorder>label_8</zorder>
<zorder>check_raw</zorder>
<zorder>line_kmlfilename</zorder>
<zorder>line_sbs1port</zorder>
<zorder>line_rawport</zorder>
<zorder>line_fgfsport</zorder>
<zorder>label_9</zorder>
<zorder>check_fgfs</zorder>
</widget> </widget>
<widget class="QScrollBar" name="verticalScrollBar"> <widget class="QGroupBox" name="groupBox">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>100</x> <x>280</x>
<y>240</y> <y>170</y>
<width>16</width> <width>211</width>
<height>281</height> <height>111</height>
</rect> </rect>
</property> </property>
<property name="orientation"> <property name="title">
<enum>Qt::Vertical</enum> <string>RX position</string>
</property> </property>
</widget> <property name="alignment">
<widget class="QTabWidget" name="tab_dataview"> <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<widget class="QLabel" name="label_29">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>130</x> <x>20</x>
<y>220</y> <y>60</y>
<width>741</width> <width>71</width>
<height>301</height> <height>20</height>
</rect> </rect>
</property> </property>
<property name="currentIndex"> <property name="text">
<number>2</number> <string>Longitude</string>
</property> </property>
</widget>
<widget class="QLineEdit" name="line_my_lon">
<property name="geometry">
<rect>
<x>90</x>
<y>60</y>
<width>113</width>
<height>27</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="line_my_lat">
<property name="geometry">
<rect>
<x>90</x>
<y>30</y>
<width>113</width>
<height>27</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_28">
<property name="geometry">
<rect>
<x>30</x>
<y>40</y>
<width>61</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>Latitude</string>
</property>
</widget>
</widget>
<zorder>group_input</zorder>
<zorder>group_output</zorder>
<zorder>groupBox</zorder>
<zorder>groupBox</zorder>
</widget>
<widget class="QWidget" name="dashboard"> <widget class="QWidget" name="dashboard">
<attribute name="title"> <attribute name="title">
<string>Dashboard</string> <string>Dashboard</string>
@ -716,12 +751,12 @@
<attribute name="title"> <attribute name="title">
<string>Live data</string> <string>Live data</string>
</attribute> </attribute>
<widget class="QTextBrowser" name="text_livedata"> <widget class="QTextEdit" name="text_livedata">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>5</x> <x>5</x>
<y>11</y> <y>11</y>
<width>721</width> <width>531</width>
<height>251</height> <height>251</height>
</rect> </rect>
</property> </property>
@ -732,7 +767,7 @@
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>10</x> <x>10</x>
<y>220</y> <y>39</y>
<width>101</width> <width>101</width>
<height>17</height> <height>17</height>
</rect> </rect>
@ -744,8 +779,8 @@
<widget class="QPushButton" name="button_start"> <widget class="QPushButton" name="button_start">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>670</x> <x>580</x>
<y>30</y> <y>340</y>
<width>98</width> <width>98</width>
<height>27</height> <height>27</height>
</rect> </rect>
@ -758,7 +793,7 @@
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>10</x> <x>10</x>
<y>540</y> <y>340</y>
<width>271</width> <width>271</width>
<height>22</height> <height>22</height>
</rect> </rect>
@ -767,75 +802,26 @@
<string>Show ADS-B-equipped aircraft only</string> <string>Show ADS-B-equipped aircraft only</string>
</property> </property>
</widget> </widget>
<widget class="QGroupBox" name="groupBox"> <widget class="QListView" name="list_aircraft">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>570</x> <x>15</x>
<y>90</y>
<width>211</width>
<height>111</height>
</rect>
</property>
<property name="title">
<string>RX position</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<widget class="QLabel" name="label_29">
<property name="geometry">
<rect>
<x>20</x>
<y>60</y> <y>60</y>
<width>71</width> <width>101</width>
<height>20</height> <height>271</height>
</rect> </rect>
</property> </property>
<property name="text"> <property name="layoutMode">
<string>Longitude</string> <enum>QListView::SinglePass</enum>
</property> </property>
</widget> </widget>
<widget class="QLineEdit" name="line_my_lon">
<property name="geometry">
<rect>
<x>90</x>
<y>60</y>
<width>113</width>
<height>27</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="line_my_lat">
<property name="geometry">
<rect>
<x>90</x>
<y>30</y>
<width>113</width>
<height>27</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_28">
<property name="geometry">
<rect>
<x>30</x>
<y>40</y>
<width>61</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>Latitude</string>
</property>
</widget>
</widget>
</widget> </widget>
<widget class="QMenuBar" name="menubar"> <widget class="QMenuBar" name="menubar">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>877</width> <width>686</width>
<height>25</height> <height>25</height>
</rect> </rect>
</property> </property>