kopia lustrzana https://github.com/jprochazka/adsb-receiver
Merge 2.1.0 branch into master.
commit
b27602baa5
|
@ -2,6 +2,14 @@
|
|||
|
||||
The following is a history of the changes made to this project.
|
||||
|
||||
## v2.0.3 *(May 19th, 2016)*
|
||||
|
||||
* Added password confirmation for MySQL database user to bash script. *(advanced)*
|
||||
* Changes applied to image configuration script including fixes for bugs and wording.
|
||||
* The device's IP address is now properly displayed at end of the bash setup process.
|
||||
* MySQL upgrades failed to detect local or remote installs properly. *(advanced)*
|
||||
* Changes to resolve PHP errors after running the PHP portal installer. *(advanced)*
|
||||
|
||||
## v2.0.2 *(May 12th, 2016)*
|
||||
|
||||
* When reinstalling dump1090-mutability the user is no longer asked for LAT and LON.
|
||||
|
|
|
@ -144,7 +144,7 @@ echo "You have been warned."
|
|||
echo -e "\033[37m"
|
||||
read -p "Use portal with advanced features? [y/N] " ADVANCED
|
||||
|
||||
## ASK IF ADVANCED FEATURES ARE TO BE USED
|
||||
## ASK IF ADVANCED FEATURES ARE TO BE USED
|
||||
|
||||
if [[ $ADVANCED =~ ^[yY]$ ]]; then
|
||||
echo -e "\033[31m"
|
||||
|
@ -211,24 +211,26 @@ if [[ $ADVANCED =~ ^[yY]$ ]]; then
|
|||
echo -e "\033[37m"
|
||||
|
||||
DATABASEHOST="localhost"
|
||||
if [[ $LOCALDATABASE == 1 ]]; then
|
||||
read -p "MySQL user login: [root] " DATABASEUSER
|
||||
read -p "Password for MySQL user: " DATABASEPASSWORD
|
||||
if [[ $LOCALDATABASE != 2 ]]; then
|
||||
read -p "MySQL user login: [root] " DATABASEADMINUSER
|
||||
read -p "Password for MySQL user: " DATABASEADMINPASSWORD
|
||||
if [[ $LOCALDATABASE == "" ]]; then
|
||||
DATABASEUSER="root"
|
||||
DATABASEADMINUSER="root"
|
||||
fi
|
||||
|
||||
# Check that the supplied password is correct.
|
||||
while ! mysql -u${DATABASEUSER} -p${DATABASEPASSWORD} -e ";" ; do
|
||||
while ! mysql -u${DATABASEADMINUSER} -p${DATABASEADMINPASSWORD} -e ";" ; do
|
||||
echo ""
|
||||
echo -e "\033[31m"
|
||||
echo -e "Unable to connect to the MySQL server using the supplied login and password.\033[37m"
|
||||
read -p "MySQL user login: [root] " DATABASEUSER
|
||||
read -p "Password for MySQL user: " DATABASEPASSWORD
|
||||
read -p "MySQL user login: [root] " DATABASEADMINUSER
|
||||
read -p "Password for MySQL user: " DATABASEADMINPASSWORD
|
||||
if [[ $LOCALDATABASE == "" ]]; then
|
||||
DATABASEUSER="root"
|
||||
DATABASEADMINUSER="root"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
echo ""
|
||||
|
||||
if [[ $LOCALDATABASE == 2 ]]; then
|
||||
# Ask for remote MySQL address if the database is hosted remotely.
|
||||
|
@ -237,6 +239,13 @@ if [[ $ADVANCED =~ ^[yY]$ ]]; then
|
|||
read -p "Database Name: " DATABASENAME
|
||||
read -p "Database User Name: " DATABASEUSER
|
||||
read -p "Database User Password: " DATABASEPASSWORD
|
||||
read -p "Confirm Database User Password: " CONFIRMDATABASEPASSWORD
|
||||
while [ $DATABASEPASSWORD != $CONFIRMDATABASEPASSWORD ]; do
|
||||
echo -e "\033[31m"
|
||||
echo -e "The supplied database user passwords did not match.\033[37m"
|
||||
read -p "Database User Password: " DATABASEPASSWORD
|
||||
read -p "Confirm Database User Password: " CONFIRMDATABASEPASSWORD
|
||||
done
|
||||
|
||||
if [[ $LOCALDATABASE == 2 ]]; then
|
||||
# Check the connection to the remote MySQL server.
|
||||
|
@ -251,10 +260,10 @@ if [[ $ADVANCED =~ ^[yY]$ ]]; then
|
|||
# Create the database and user if running MySQL locally.
|
||||
echo -e "\033[33m"
|
||||
echo -e "Creating MySQL database and user...\033[37m"
|
||||
mysql -u${DATABASEUSER} -p${DATABASEPASSWORD} -e "CREATE DATABASE ${DATABASENAME};"
|
||||
mysql -u${DATABASEUSER} -p${DATABASEPASSWORD} -e "CREATE USER '${DATABASEUSER}'@'localhost' IDENTIFIED BY \"${DATABASEPASSWORD}\";";
|
||||
mysql -u${DATABASEUSER} -p${DATABASEPASSWORD} -e "GRANT ALL PRIVILEGES ON ${DATABASENAME}.* TO '${DATABASEUSER}'@'localhost';"
|
||||
mysql -u${DATABASEUSER} -p${DATABASEPASSWORD} -e "FLUSH PRIVILEGES;"
|
||||
mysql -u${DATABASEADMINUSER} -p${DATABASEADMINPASSWORD} -e "CREATE DATABASE ${DATABASENAME};"
|
||||
mysql -u${DATABASEADMINUSER} -p${DATABASEADMINPASSWORD} -e "CREATE USER '${DATABASEUSER}'@'localhost' IDENTIFIED BY \"${DATABASEPASSWORD}\";";
|
||||
mysql -u${DATABASEADMINUSER} -p${DATABASEADMINPASSWORD} -e "GRANT ALL PRIVILEGES ON ${DATABASENAME}.* TO '${DATABASEUSER}'@'localhost';"
|
||||
mysql -u${DATABASEADMINUSER} -p${DATABASEADMINPASSWORD} -e "FLUSH PRIVILEGES;"
|
||||
fi
|
||||
|
||||
echo -e "\033[31m"
|
||||
|
@ -269,7 +278,7 @@ if [[ $ADVANCED =~ ^[yY]$ ]]; then
|
|||
echo -e "\033[37m"
|
||||
read -p "Press enter to continue..." CONTINUE
|
||||
fi
|
||||
|
||||
|
||||
if [[ $DATABASEENGINE == 2 ]]; then
|
||||
# Create and empty SQLite databse and set the proper permissions on it.
|
||||
sudo sqlite3 ${DOCUMENTROOT}/data/portal.sqlite ""
|
||||
|
|
|
@ -88,7 +88,7 @@ if [ -f $DOCUMENTROOT/classes/settings.class.php ]; then
|
|||
DATABASEENGINE=1
|
||||
fi
|
||||
|
||||
if [ $HOST == "localhost" ] || [ $HOST == "127.0.0.1" ]; then
|
||||
if [[ $HOST == "localhost" ]] || [[ $HOST == "127.0.0.1" ]]; then
|
||||
LOCALDATABASE=1
|
||||
else
|
||||
LOCALDATABASE=2
|
||||
|
@ -156,13 +156,16 @@ if [ -f /etc/lsb-release ]; then
|
|||
CheckPackage php7.0-xml
|
||||
else
|
||||
CheckPackage php5-cgi
|
||||
CheckPackage php5-json
|
||||
fi
|
||||
else
|
||||
CheckPackage php5-cgi
|
||||
CheckPackage php5-json
|
||||
fi
|
||||
|
||||
CheckPackage libpython2.7
|
||||
if [[ $ADVANCED =~ ^[yY]$ ]]; then
|
||||
CheckPackage python-pyinotify
|
||||
if [[ $DATABASEENGINE == 2 ]]; then
|
||||
CheckPackage sqlite3
|
||||
|
||||
|
@ -228,24 +231,26 @@ if [[ $INSTALLED == "n" ]]; then
|
|||
echo -e "\033[37m"
|
||||
|
||||
DATABASEHOST="localhost"
|
||||
if [[ $LOCALDATABASE == 1 ]]; then
|
||||
read -p "MySQL user login: [root] " DATABASEUSER
|
||||
read -p "Password for MySQL user: " DATABASEPASSWORD
|
||||
if [[ $LOCALDATABASE != 2 ]]; then
|
||||
read -p "MySQL user login: [root] " DATABASEADMINUSER
|
||||
read -p "Password for MySQL user: " DATABASEADMINPASSWORD
|
||||
if [[ $LOCALDATABASE == "" ]]; then
|
||||
DATABASEUSER="root"
|
||||
DATABASEADMINUSER="root"
|
||||
fi
|
||||
|
||||
# Check that the supplied password is correct.
|
||||
while ! mysql -u${DATABASEUSER} -p${DATABASEPASSWORD} -e ";" ; do
|
||||
while ! mysql -u${DATABASEADMINUSER} -p${DATABASEADMINPASSWORD} -e ";" ; do
|
||||
echo ""
|
||||
echo -e "\033[31m"
|
||||
echo -e "Unable to connect to the MySQL server using the supplied login and password.\033[37m"
|
||||
read -p "MySQL user login: [root] " DATABASEUSER
|
||||
read -p "Password for MySQL user: " DATABASEPASSWORD
|
||||
read -p "MySQL user login: [root] " DATABASEADMINUSER
|
||||
read -p "Password for MySQL user: " DATABASEADMINPASSWORD
|
||||
if [[ $LOCALDATABASE == "" ]]; then
|
||||
DATABASEUSER="root"
|
||||
DATABASEADMINUSER="root"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
echo ""
|
||||
|
||||
if [[ $LOCALDATABASE == 2 ]]; then
|
||||
# Ask for remote MySQL address if the database is hosted remotely.
|
||||
|
@ -254,6 +259,13 @@ if [[ $INSTALLED == "n" ]]; then
|
|||
read -p "Database Name: " DATABASENAME
|
||||
read -p "Database User Name: " DATABASEUSER
|
||||
read -p "Database User Password: " DATABASEPASSWORD
|
||||
read -p "Confirm Database User Password: " CONFIRMDATABASEPASSWORD
|
||||
while [ $DATABASEPASSWORD != $CONFIRMDATABASEPASSWORD ]; do
|
||||
echo -e "\033[31m"
|
||||
echo -e "The supplied database user passwords did not match.\033[37m"
|
||||
read -p "Database User Password: " DATABASEPASSWORD
|
||||
read -p "Confirm Database User Password: " CONFIRMDATABASEPASSWORD
|
||||
done
|
||||
|
||||
if [[ $LOCALDATABASE == 2 ]]; then
|
||||
# Check the connection to the remote MySQL server.
|
||||
|
@ -261,17 +273,17 @@ if [[ $INSTALLED == "n" ]]; then
|
|||
echo -e "\033[31m"
|
||||
echo -e "Unable to connect to the MySQL server using the supplied login and password.\033[37m"
|
||||
read -p "MySQL Server Address: " DATABASEHOST
|
||||
read -p "Database user Name: " DATABASEUSER
|
||||
read -p "Database User Name: " DATABASEUSER
|
||||
read -p "Database User Password: " DATABASEPASSWORD
|
||||
done
|
||||
else
|
||||
# Create the database and user if running MySQL locally.
|
||||
echo -e "\033[33m"
|
||||
echo -e "Creating MySQL database and user...\033[37m"
|
||||
mysql -u${DATABASEUSER} -p${DATABASEPASSWORD} -e "CREATE DATABASE ${DATABASENAME};"
|
||||
mysql -u${DATABASEUSER} -p${DATABASEPASSWORD} -e "CREATE USER '${DATABASEUSER}'@'localhost' IDENTIFIED BY \"${DATABASEPASSWORD}\";";
|
||||
mysql -u${DATABASEUSER} -p${DATABASEPASSWORD} -e "GRANT ALL PRIVILEGES ON ${DATABASENAME}.* TO '${DATABASEUSER}'@'localhost';"
|
||||
mysql -u${DATABASEUSER} -p${DATABASEPASSWORD} -e "FLUSH PRIVILEGES;"
|
||||
mysql -u${DATABASEADMINUSER} -p${DATABASEADMINPASSWORD} -e "CREATE DATABASE ${DATABASENAME};"
|
||||
mysql -u${DATABASEADMINUSER} -p${DATABASEADMINPASSWORD} -e "CREATE USER '${DATABASEUSER}'@'localhost' IDENTIFIED BY \"${DATABASEPASSWORD}\";";
|
||||
mysql -u${DATABASEADMINUSER} -p${DATABASEADMINPASSWORD} -e "GRANT ALL PRIVILEGES ON ${DATABASENAME}.* TO '${DATABASEUSER}'@'localhost';"
|
||||
mysql -u${DATABASEADMINUSER} -p${DATABASEADMINPASSWORD} -e "FLUSH PRIVILEGES;"
|
||||
fi
|
||||
|
||||
echo -e "\033[31m"
|
||||
|
|
|
@ -34,12 +34,12 @@
|
|||
## SOFTWARE VERSIONS
|
||||
|
||||
# FlightAware PiAware
|
||||
PIAWAREVERSION="2.1-5"
|
||||
PIAWAREBRANCH="5c77c4924"
|
||||
PIAWAREVERSION="3.0.3"
|
||||
PIAWAREBRANCH="master"
|
||||
|
||||
# PlaneFinder ADS-B Client
|
||||
PFCLIENTVERSIONARM="3.4.30"
|
||||
PFCLIENTVERSIONI386="3.4.30"
|
||||
PFCLIENTVERSIONARM="3.4.61"
|
||||
PFCLIENTVERSIONI386="3.4.61"
|
||||
|
||||
# Flightradar24 Linux Debian package
|
||||
FR24CLIENTVERSIONI386="1.0.18-5"
|
||||
|
|
|
@ -205,6 +205,20 @@
|
|||
chmod($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."data".DIRECTORY_SEPARATOR."portal.sqlite", 0666);
|
||||
}
|
||||
|
||||
$common->updateSetting("version", "2.0.2");
|
||||
$common->updateSetting("patch", "");
|
||||
} catch(Exception $e) {
|
||||
$error = TRUE;
|
||||
$errorMessage = $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
// UPGRADE RO V2.0.3
|
||||
///////////////////////
|
||||
|
||||
if ($common->getSetting("version") == "2.0.2") {
|
||||
try {
|
||||
$common->updateSetting("version", $thisVersion);
|
||||
$common->updateSetting("patch", "");
|
||||
} catch(Exception $e) {
|
||||
|
@ -213,6 +227,7 @@
|
|||
}
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
///////////////////////
|
||||
// UPGRADE TO V2.1.0
|
||||
///////////////////////
|
||||
|
@ -324,6 +339,8 @@
|
|||
}
|
||||
}
|
||||
|
||||
=======
|
||||
>>>>>>> refs/remotes/origin/master
|
||||
|
||||
require_once('../admin/includes/header.inc.php');
|
||||
|
||||
|
|
|
@ -41,144 +41,153 @@ import time
|
|||
import os
|
||||
#import urllib2
|
||||
|
||||
while True:
|
||||
def log(string):
|
||||
#print(string) # uncomment to enable debug logging
|
||||
return
|
||||
|
||||
# Read the configuration file.
|
||||
with open(os.path.dirname(os.path.realpath(__file__)) + '/config.json') as config_file:
|
||||
config = json.load(config_file)
|
||||
# Read the configuration file.
|
||||
with open(os.path.dirname(os.path.realpath(__file__)) + '/config.json') as config_file:
|
||||
config = json.load(config_file)
|
||||
|
||||
# Import the needed database library.
|
||||
if config["database"]["type"] == "mysql":
|
||||
import MySQLdb
|
||||
if config["database"]["type"] == "sqlite":
|
||||
import sqlite3
|
||||
# Import the needed database library.
|
||||
if config["database"]["type"] == "mysql":
|
||||
import MySQLdb
|
||||
else:
|
||||
import sqlite3
|
||||
|
||||
# Read dump1090-mutability's aircraft.json.
|
||||
with open('/run/dump1090-mutability/aircraft.json') as data_file:
|
||||
data = json.load(data_file)
|
||||
# For testing using a remote JSON feed.
|
||||
#response = urllib2.urlopen('http://192.168.254.2/dump1090/data/aircraft.json')
|
||||
#data = json.load(response)
|
||||
class FlightsProcessor(object):
|
||||
def __init__(self, config):
|
||||
self.config = config
|
||||
self.dbType = config["database"]["type"]
|
||||
# List of required keys for position data entries
|
||||
self.position_keys = ('lat', 'lon', 'altitude', 'speed', 'track', 'vert_rate')
|
||||
|
||||
if config["database"]["type"] == "sqlite":
|
||||
## Connect to a SQLite database.
|
||||
db = sqlite3.connect(config["database"]["db"])
|
||||
else:
|
||||
## Connect to a MySQL database.
|
||||
db = MySQLdb.connect(host=config["database"]["host"], user=config["database"]["user"], passwd=config["database"]["passwd"], db=config["database"]["db"])
|
||||
def setupDBStatements(self, formatSymbol):
|
||||
if hasattr(self, 'STMTS'):
|
||||
return
|
||||
mapping = { "s": formatSymbol }
|
||||
self.STMTS = {
|
||||
'select_aircraft_count':"SELECT COUNT(*) FROM adsb_aircraft WHERE icao = %(s)s" % mapping,
|
||||
'select_aircraft_id': "SELECT id FROM adsb_aircraft WHERE icao = %(s)s" % mapping,
|
||||
'select_flight_count': "SELECT COUNT(*) FROM adsb_flights WHERE flight = %(s)s" % mapping,
|
||||
'select_flight_id': "SELECT id FROM adsb_flights WHERE flight = %(s)s" % mapping,
|
||||
'select_position': "SELECT message FROM adsb_positions WHERE flight = %(s)s AND message = %(s)s ORDER BY time DESC LIMIT 1" % mapping,
|
||||
'insert_aircraft': "INSERT INTO adsb_aircraft (icao, firstSeen, lastSeen) VALUES (%(s)s, %(s)s, %(s)s)" % mapping,
|
||||
'insert_flight': "INSERT INTO adsb_flights (aircraft, flight, firstSeen, lastSeen) VALUES (%(s)s, %(s)s, %(s)s, %(s)s)" % mapping,
|
||||
'insert_position_sqwk': "INSERT INTO adsb_positions (flight, time, message, squawk, latitude, longitude, track, altitude, verticleRate, speed) VALUES (%(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s)" % mapping,
|
||||
'insert_position': "INSERT INTO adsb_positions (flight, time, message, latitude, longitude, track, altitude, verticleRate, speed) VALUES (%(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s, %(s)s)" % mapping,
|
||||
'update_aircraft_seen': "UPDATE adsb_aircraft SET lastSeen = %(s)s WHERE icao = %(s)s" % mapping,
|
||||
'update_flight_seen': "UPDATE adsb_flights SET aircraft = %(s)s, lastSeen = %(s)s WHERE flight = %(s)s" % mapping
|
||||
}
|
||||
|
||||
# Assign the time to a variable.
|
||||
time_now = datetime.datetime.utcnow().strftime("%Y/%m/%d %H:%M:%S")
|
||||
def connectDB(self):
|
||||
if self.dbType == "sqlite": ## Connect to a SQLite database.
|
||||
self.setupDBStatements("?")
|
||||
return sqlite3.connect(self.config["database"]["db"])
|
||||
else: ## Connect to a MySQL database.
|
||||
self.setupDBStatements("%s")
|
||||
return MySQLdb.connect(host=self.config["database"]["host"],
|
||||
user=self.config["database"]["user"],
|
||||
passwd=self.config["database"]["passwd"],
|
||||
db=self.config["database"]["db"])
|
||||
|
||||
cursor = db.cursor()
|
||||
for aircraft in data["aircraft"]:
|
||||
def processAircraftList(self, aircraftList):
|
||||
db = self.connectDB()
|
||||
# Get Database cursor handle
|
||||
self.cursor = db.cursor()
|
||||
# Assign the time to a variable.
|
||||
self.time_now = datetime.datetime.utcnow().strftime("%Y/%m/%d %H:%M:%S")
|
||||
|
||||
for aircraft in aircraftList:
|
||||
self.processAircraft(aircraft)
|
||||
|
||||
# Close the database connection.
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
def processAircraft(self, aircraft):
|
||||
hexcode = aircraft["hex"]
|
||||
# Check if this aircraft was already seen.
|
||||
if config["database"]["type"] == "sqlite":
|
||||
params = (aircraft["hex"],)
|
||||
cursor.execute("SELECT COUNT(*) FROM adsb_aircraft WHERE icao = ?", params)
|
||||
else:
|
||||
cursor.execute("SELECT COUNT(*) FROM adsb_aircraft WHERE icao = %s", aircraft["hex"])
|
||||
row_count = cursor.fetchone()
|
||||
self.cursor.execute(self.STMTS['select_aircraft_count'], (hexcode,))
|
||||
row_count = self.cursor.fetchone()
|
||||
if row_count[0] == 0:
|
||||
# Insert the new aircraft.
|
||||
#print("Added Aircraft: " + aircraft["hex"])
|
||||
if config["database"]["type"] == "sqlite":
|
||||
params = (aircraft["hex"], time_now, time_now,)
|
||||
cursor.execute("INSERT INTO adsb_aircraft (icao, firstSeen, lastSeen) VALUES (?, ?, ?)", params)
|
||||
else:
|
||||
cursor.execute("INSERT INTO adsb_aircraft (icao, firstSeen, lastSeen) VALUES (%s, %s, %s)", (aircraft["hex"], time_now, time_now))
|
||||
log("Added Aircraft: " + hexcode)
|
||||
self.cursor.execute(self.STMTS['insert_aircraft'], (hexcode, self.time_now, self.time_now,))
|
||||
else:
|
||||
# Update the existing aircraft.
|
||||
if config["database"]["type"] == "sqlite":
|
||||
params = (time_now, aircraft["hex"],)
|
||||
cursor.execute("UPDATE adsb_aircraft SET lastSeen = ? WHERE icao = ?", params)
|
||||
else:
|
||||
cursor.execute("UPDATE adsb_aircraft SET lastSeen = %s WHERE icao = %s", (time_now, aircraft["hex"]))
|
||||
self.cursor.execute(self.STMTS['update_aircraft_seen'], (self.time_now, hexcode,))
|
||||
log("Updating Aircraft: " + hexcode)
|
||||
# Get the ID of this aircraft.
|
||||
if config["database"]["type"] == "sqlite":
|
||||
params = (aircraft["hex"],)
|
||||
cursor.execute("SELECT id FROM adsb_aircraft WHERE icao = ?", params)
|
||||
else:
|
||||
cursor.execute("SELECT id FROM adsb_aircraft WHERE icao = %s", aircraft["hex"])
|
||||
rows = cursor.fetchall()
|
||||
for row in rows:
|
||||
aircraft_id = row[0]
|
||||
self.cursor.execute(self.STMTS['select_aircraft_id'], (hexcode,))
|
||||
row = self.cursor.fetchone()
|
||||
aircraft_id = row[0]
|
||||
log("\tFound Aircraft ID: " + str(aircraft_id))
|
||||
|
||||
# Check that a flight is tied to this track.
|
||||
if aircraft.has_key('flight'):
|
||||
# Check to see if the flight already exists in the database.
|
||||
if config["database"]["type"] == "sqlite":
|
||||
params = (aircraft["flight"].strip(),)
|
||||
cursor.execute("SELECT COUNT(*) FROM adsb_flights WHERE flight = ?", params)
|
||||
else:
|
||||
cursor.execute("SELECT COUNT(*) FROM adsb_flights WHERE flight = %s", aircraft["flight"].strip())
|
||||
row_count = cursor.fetchone()
|
||||
if row_count[0] == 0:
|
||||
# If the flight does not exist in the database add it.
|
||||
if config["database"]["type"] == "sqlite":
|
||||
params = (aircraft_id, aircraft["flight"].strip(), time_now, time_now,)
|
||||
cursor.execute("INSERT INTO adsb_flights (aircraft, flight, firstSeen, lastSeen) VALUES (?, ?, ?, ?)", params)
|
||||
else:
|
||||
cursor.execute("INSERT INTO adsb_flights (aircraft, flight, firstSeen, lastSeen) VALUES (%s, %s, %s, %s)", (aircraft_id, aircraft["flight"].strip(), time_now, time_now))
|
||||
#print("Added Flight: " + aircraft["flight"].strip())
|
||||
else:
|
||||
# If it already exists pdate the time it was last seen.
|
||||
if config["database"]["type"] == "sqlite":
|
||||
params =(aircraft_id, time_now, aircraft["flight"].strip(),)
|
||||
cursor.execute("UPDATE adsb_flights SET aircraft = ?, lastSeen = ? WHERE flight = ?", params)
|
||||
else:
|
||||
cursor.execute("UPDATE adsb_flights SET aircraft = %s, lastSeen = %s WHERE flight = %s", (aircraft_id, time_now, aircraft["flight"].strip()))
|
||||
self.processFlight(aircraft_id, aircraft)
|
||||
|
||||
# Get the ID of this flight.
|
||||
if config["database"]["type"] == "sqlite":
|
||||
params = (aircraft["flight"].strip(),)
|
||||
cursor.execute("SELECT id FROM adsb_flights WHERE flight = ?", params)
|
||||
def processFlight(self, aircraft_id, aircraft):
|
||||
flight = aircraft["flight"].strip()
|
||||
# Check to see if the flight already exists in the database.
|
||||
self.cursor.execute(self.STMTS['select_flight_count'], (flight,))
|
||||
row_count = self.cursor.fetchone()
|
||||
if row_count[0] == 0:
|
||||
# If the flight does not exist in the database add it.
|
||||
params = (aircraft_id, flight, self.time_now, self.time_now,)
|
||||
self.cursor.execute(self.STMTS['insert_flight'], params)
|
||||
log("\t\tAdded Flight: " + flight)
|
||||
else:
|
||||
# If it already exists pdate the time it was last seen.
|
||||
params = (aircraft_id, self.time_now, flight,)
|
||||
self.cursor.execute(self.STMTS['update_flight_seen'], params)
|
||||
log("\t\tUpdated Flight: " + flight)
|
||||
# Get the ID of this flight.
|
||||
self.cursor.execute(self.STMTS['select_flight_id'], (flight,))
|
||||
row = self.cursor.fetchone()
|
||||
flight_id = row[0]
|
||||
|
||||
# Check if position data is available.
|
||||
if (all (k in aircraft for k in self.position_keys) and aircraft["altitude"] != "ground"):
|
||||
self.processPositions(flight_id, aircraft)
|
||||
|
||||
def processPositions(self, flight_id, aircraft):
|
||||
# Check that this message has not already been added to the database.
|
||||
params = (flight_id, aircraft["messages"],)
|
||||
self.cursor.execute(self.STMTS['select_position'], params)
|
||||
row = self.cursor.fetchone()
|
||||
|
||||
if row == None or row[0] != aircraft["messages"]:
|
||||
# Add this position to the database.
|
||||
if aircraft.has_key('squawk'):
|
||||
params = (flight_id, self.time_now, aircraft["messages"], aircraft["squawk"],
|
||||
aircraft["lat"], aircraft["lon"], aircraft["track"],
|
||||
aircraft["altitude"], aircraft["vert_rate"], aircraft["speed"],)
|
||||
self.cursor.execute(self.STMTS['insert_position_sqwk'], params)
|
||||
log("\t\t\tInserted position w/ Squawk " + repr(params))
|
||||
else:
|
||||
cursor.execute("SELECT id FROM adsb_flights WHERE flight = %s", aircraft["flight"].strip())
|
||||
rows = cursor.fetchall()
|
||||
for row in rows:
|
||||
flight_id = row[0]
|
||||
# Get the ID of this aircraft.
|
||||
if config["database"]["type"] == "sqlite":
|
||||
params = (aircraft["hex"].strip(),)
|
||||
cursor.execute("SELECT id FROM adsb_aircraft WHERE icao = ?", params)
|
||||
else:
|
||||
cursor.execute("SELECT id FROM adsb_aircraft WHERE icao = %s", aircraft["hex"].strip())
|
||||
rows = cursor.fetchall()
|
||||
for row in rows:
|
||||
flight_id = row[0]
|
||||
params = (flight_id, self.time_now, aircraft["messages"], aircraft["lat"], aircraft["lon"],
|
||||
aircraft["track"], aircraft["altitude"], aircraft["vert_rate"], aircraft["speed"],)
|
||||
self.cursor.execute(self.STMTS['insert_position'], params)
|
||||
log("\t\t\tInserted position w/o Squawk " + repr(params))
|
||||
else:
|
||||
log("\t\t\tMessage is the same")
|
||||
|
||||
# Check if position data is available.
|
||||
if aircraft.has_key('lat') and aircraft.has_key('lon') and aircraft.has_key('altitude') and aircraft.has_key('speed') and aircraft.has_key('track') and aircraft.has_key('vert_rate') and aircraft["altitude"] != "ground":
|
||||
# Check that this message has not already been added to the database.
|
||||
if config["database"]["type"] == "sqlite":
|
||||
params = (flight_id, aircraft["messages"],)
|
||||
cursor.execute("SELECT message FROM adsb_positions WHERE flight = ? AND message = ? ORDER BY time DESC", params)
|
||||
else:
|
||||
cursor.execute("SELECT message FROM adsb_positions WHERE flight = %s AND message = %s ORDER BY time DESC", (flight_id, aircraft["messages"]))
|
||||
rows = cursor.fetchall()
|
||||
row_count = cursor.rowcount
|
||||
last_message = 0
|
||||
for row in rows:
|
||||
last_message = row[0]
|
||||
if row_count == 0 or last_message != aircraft["messages"]:
|
||||
# Add this position to the database.
|
||||
if aircraft.has_key('squawk'):
|
||||
if config["database"]["type"] == "sqlite":
|
||||
params = (flight_id, aircraft_id, time_now, aircraft["messages"], aircraft["squawk"], aircraft["lat"], aircraft["lon"], aircraft["track"], aircraft["altitude"], aircraft["vert_rate"], aircraft["speed"],)
|
||||
cursor.execute("INSERT INTO adsb_positions (flight, aircraft, time, message, squawk, latitude, longitude, track, altitude, verticleRate, speed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", params)
|
||||
else:
|
||||
cursor.execute("INSERT INTO adsb_positions (flight, aircraft, time, message, squawk, latitude, longitude, track, altitude, verticleRate, speed) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", (flight_id, aircraft_id, time_now, aircraft["messages"], aircraft["squawk"], aircraft["lat"], aircraft["lon"], aircraft["track"], aircraft["altitude"], aircraft["vert_rate"], aircraft["speed"]))
|
||||
else:
|
||||
if config["database"]["type"] == "sqlite":
|
||||
params = (flight_id, aircraft_id, time_now, aircraft["messages"], aircraft["lat"], aircraft["lon"], aircraft["track"], aircraft["altitude"], aircraft["vert_rate"], aircraft["speed"],)
|
||||
cursor.execute("INSERT INTO adsb_positions (flight, aircraft, time, message, latitude, longitude, track, altitude, verticleRate, speed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", params)
|
||||
else:
|
||||
cursor.execute("INSERT INTO adsb_positions (flight, aircraft, time, message, latitude, longitude, track, altitude, verticleRate, speed) VALUES (%s, $s, %s, %s, %s, %s, %s, %s, %s, %s)", (flight_id, aircraft_id, time_now, aircraft["messages"], aircraft["lat"], aircraft["lon"], aircraft["track"], aircraft["altitude"], aircraft["vert_rate"], aircraft["speed"]))
|
||||
|
||||
# Close the database connection.
|
||||
db.commit()
|
||||
db.close()
|
||||
if __name__ == "__main__":
|
||||
processor = FlightsProcessor(config)
|
||||
|
||||
#print("Last Run: " + datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"))
|
||||
time.sleep(15)
|
||||
# Main run loop
|
||||
while True:
|
||||
# Read dump1090-mutability's aircraft.json.
|
||||
with open('/run/dump1090-mutability/aircraft.json') as data_file:
|
||||
data = json.load(data_file)
|
||||
# For testing using a remote JSON feed.
|
||||
#response = urllib2.urlopen('http://192.168.254.2/dump1090/data/aircraft.json')
|
||||
#data = json.load(response)
|
||||
|
||||
processor.processAircraftList(data["aircraft"])
|
||||
|
||||
log("Last Run: " + datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"))
|
||||
time.sleep(15)
|
Ładowanie…
Reference in New Issue