2010-10-19 00:59:08 +08:00
|
|
|
#
|
|
|
|
# Copyright 2010 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.
|
|
|
|
#
|
|
|
|
|
2012-10-06 23:44:33 +08:00
|
|
|
import time, os, sys, threading
|
2010-09-15 13:01:56 +08:00
|
|
|
from string import split, join
|
2012-07-17 10:27:09 +08:00
|
|
|
import air_modes
|
2010-10-18 13:17:48 +08:00
|
|
|
import sqlite3
|
2012-07-17 10:27:09 +08:00
|
|
|
from air_modes.exceptions import *
|
2013-06-06 06:30:45 +08:00
|
|
|
from gnuradio.gr.pubsub import pubsub
|
2010-10-18 13:17:48 +08:00
|
|
|
|
2013-06-19 09:57:24 +08:00
|
|
|
class output_sql:
|
|
|
|
def __init__(self, cpr, filename, lock, publisher):
|
|
|
|
#pubsub.__init__(self)
|
|
|
|
self._cpr = cpr
|
2013-06-03 21:07:36 +08:00
|
|
|
self._lock = lock;
|
2013-05-30 15:58:03 +08:00
|
|
|
#create the database
|
|
|
|
self.filename = filename
|
|
|
|
self._db = sqlite3.connect(filename)
|
|
|
|
#now execute a schema to create the tables you need
|
|
|
|
c = self._db.cursor()
|
|
|
|
query = """CREATE TABLE IF NOT EXISTS "positions" (
|
|
|
|
"icao" INTEGER KEY NOT NULL,
|
|
|
|
"seen" TEXT NOT NULL,
|
|
|
|
"alt" INTEGER,
|
|
|
|
"lat" REAL,
|
|
|
|
"lon" REAL
|
|
|
|
);"""
|
|
|
|
c.execute(query)
|
|
|
|
query = """CREATE TABLE IF NOT EXISTS "vectors" (
|
|
|
|
"icao" INTEGER KEY NOT NULL,
|
|
|
|
"seen" TEXT NOT NULL,
|
|
|
|
"speed" REAL,
|
|
|
|
"heading" REAL,
|
|
|
|
"vertical" REAL
|
|
|
|
);"""
|
|
|
|
c.execute(query)
|
|
|
|
query = """CREATE TABLE IF NOT EXISTS "ident" (
|
|
|
|
"icao" INTEGER PRIMARY KEY NOT NULL,
|
2013-06-20 02:24:11 +08:00
|
|
|
"ident" TEXT NOT NULL,
|
|
|
|
"type" TEXT NOT NULL
|
2013-05-30 15:58:03 +08:00
|
|
|
);"""
|
|
|
|
c.execute(query)
|
|
|
|
c.close()
|
|
|
|
self._db.commit()
|
|
|
|
#we close the db conn now to reopen it in the output() thread context.
|
|
|
|
self._db.close()
|
|
|
|
self._db = None
|
2013-06-19 09:57:24 +08:00
|
|
|
publisher.subscribe("type17_dl", self.insert)
|
2013-05-30 05:18:15 +08:00
|
|
|
|
|
|
|
def insert(self, message):
|
2012-10-10 00:26:16 +08:00
|
|
|
with self._lock:
|
|
|
|
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
|
2013-05-30 05:18:15 +08:00
|
|
|
#constructor.
|
|
|
|
if self._db is None:
|
|
|
|
self._db = sqlite3.connect(self.filename)
|
2012-07-06 14:52:00 +08:00
|
|
|
|
2012-10-10 00:26:16 +08:00
|
|
|
query = self.make_insert_query(message)
|
|
|
|
if query is not None:
|
2013-05-30 05:18:15 +08:00
|
|
|
c = self._db.cursor()
|
2012-10-10 00:26:16 +08:00
|
|
|
c.execute(query)
|
|
|
|
c.close()
|
2013-06-03 21:07:36 +08:00
|
|
|
# self._db.commit()
|
2012-10-08 08:54:03 +08:00
|
|
|
|
2012-10-10 00:26:16 +08:00
|
|
|
except ADSBError:
|
|
|
|
pass
|
2012-06-20 06:43:43 +08:00
|
|
|
|
2013-06-19 09:57:24 +08:00
|
|
|
def make_insert_query(self, msg):
|
2010-10-18 13:17:48 +08:00
|
|
|
#assembles a SQL query tailored to our database
|
|
|
|
#this version ignores anything that isn't Type 17 for now, because we just don't care
|
|
|
|
query = None
|
2013-06-19 09:57:24 +08:00
|
|
|
msgtype = msg.data["df"]
|
2010-10-18 13:17:48 +08:00
|
|
|
if msgtype == 17:
|
2013-06-19 09:57:24 +08:00
|
|
|
query = self.sql17(msg.data)
|
|
|
|
#self["new_adsb"] = data["aa"] #publish change notification
|
2010-10-18 13:17:48 +08:00
|
|
|
|
|
|
|
return query
|
|
|
|
|
2013-06-10 20:52:28 +08:00
|
|
|
#TODO: if there's a way to publish selective reports on upsert to distinguish,
|
|
|
|
#for instance, between a new ICAO that's just been heard, and a refresh of an
|
|
|
|
#existing ICAO, both of those would be useful publishers for the GUI model.
|
|
|
|
#otherwise, worst-case you can just refresh everything every time a report
|
|
|
|
#comes in, but it's going to use more CPU. Not likely a problem if you're only
|
|
|
|
#looking at ADS-B (no mode S) data.
|
|
|
|
#It's probably time to look back at the Qt SQL table model and see if it can be
|
|
|
|
#bent into shape for you.
|
2012-07-14 00:43:22 +08:00
|
|
|
def sql17(self, data):
|
2012-06-27 14:27:58 +08:00
|
|
|
icao24 = data["aa"]
|
2012-07-17 00:16:48 +08:00
|
|
|
bdsreg = data["me"].get_type()
|
2013-06-19 09:57:24 +08:00
|
|
|
#self["bds%.2i" % bdsreg] = icao24 #publish under "bds08", "bds06", etc.
|
2012-07-14 00:43:22 +08:00
|
|
|
|
2012-07-17 00:16:48 +08:00
|
|
|
if bdsreg == 0x08:
|
2013-06-19 09:57:24 +08:00
|
|
|
(msg, typename) = air_modes.parseBDS08(data)
|
2013-06-19 10:09:09 +08:00
|
|
|
return "INSERT OR REPLACE INTO ident (icao, ident, type) VALUES (" + "%i" % icao24 + ", '" + msg + "', '" + typename + "')"
|
2012-07-17 00:16:48 +08:00
|
|
|
elif bdsreg == 0x06:
|
2013-06-19 09:57:24 +08:00
|
|
|
[ground_track, decoded_lat, decoded_lon, rnge, bearing] = air_modes.parseBDS06(data, self._cpr)
|
2012-06-27 14:27:58 +08:00
|
|
|
altitude = 0
|
2010-10-18 13:17:48 +08:00
|
|
|
if decoded_lat is None: #no unambiguously valid position available
|
2013-05-30 16:06:53 +08:00
|
|
|
raise CPRNoPositionError
|
2010-10-18 13:17:48 +08:00
|
|
|
else:
|
2013-05-30 16:06:53 +08:00
|
|
|
return "INSERT INTO positions (icao, seen, alt, lat, lon) VALUES (" + "%i" % icao24 + ", datetime('now'), " + str(altitude) + ", " + "%.6f" % decoded_lat + ", " + "%.6f" % decoded_lon + ")"
|
2012-07-17 00:16:48 +08:00
|
|
|
elif bdsreg == 0x05:
|
2013-06-19 09:57:24 +08:00
|
|
|
[altitude, decoded_lat, decoded_lon, rnge, bearing] = air_modes.parseBDS05(data, self._cpr)
|
2010-10-18 13:17:48 +08:00
|
|
|
if decoded_lat is None: #no unambiguously valid position available
|
2013-05-30 16:06:53 +08:00
|
|
|
raise CPRNoPositionError
|
2010-10-18 13:17:48 +08:00
|
|
|
else:
|
2013-05-30 16:06:53 +08:00
|
|
|
return "INSERT INTO positions (icao, seen, alt, lat, lon) VALUES (" + "%i" % icao24 + ", datetime('now'), " + str(altitude) + ", " + "%.6f" % decoded_lat + ", " + "%.6f" % decoded_lon + ")"
|
2012-07-17 00:16:48 +08:00
|
|
|
elif bdsreg == 0x09:
|
|
|
|
subtype = data["bds09"].get_type()
|
2012-07-22 02:12:19 +08:00
|
|
|
if subtype == 0:
|
2013-06-19 09:57:24 +08:00
|
|
|
[velocity, heading, vert_spd, turnrate] = air_modes.parseBDS09_0(data)
|
2013-05-30 16:06:53 +08:00
|
|
|
return "INSERT INTO vectors (icao, seen, speed, heading, vertical) VALUES (" + "%i" % icao24 + ", datetime('now'), " + "%.0f" % velocity + ", " + "%.0f" % heading + ", " + "%.0f" % vert_spd + ")"
|
2012-07-22 02:12:19 +08:00
|
|
|
elif subtype == 1:
|
2013-06-19 09:57:24 +08:00
|
|
|
[velocity, heading, vert_spd] = air_modes.parseBDS09_1(data)
|
2013-05-30 16:06:53 +08:00
|
|
|
return "INSERT INTO vectors (icao, seen, speed, heading, vertical) VALUES (" + "%i" % icao24 + ", datetime('now'), " + "%.0f" % velocity + ", " + "%.0f" % heading + ", " + "%.0f" % vert_spd + ")"
|
2012-07-22 02:12:19 +08:00
|
|
|
else:
|
2013-05-30 16:06:53 +08:00
|
|
|
raise NoHandlerError
|