Browse Source

Switch sqlalchemy without flask

pull/68/head
squeaky otter 1 year ago
parent
commit
10ea41acd6
Signed by: dashie GPG Key ID: C2D57B325840B755
3 changed files with 66 additions and 124 deletions
  1. +3
    -2
      airwaves_adsb_client.py
  2. +10
    -2
      config.py.sample
  3. +53
    -120
      models.py

+ 3
- 2
airwaves_adsb_client.py View File

@ -10,7 +10,7 @@ import time
import datetime
from libPyAirwaves.adsb import is_valid_adsb_message
from libPyAirwaves.structs import AdsbType
from models import db, Aircrafts, AircraftModes, AircraftOwner, AircraftRegistration
from models import Session, Aircrafts, AircraftModes, AircraftOwner, AircraftRegistration
import redis
import json
@ -20,6 +20,7 @@ max_failed_connection = 10
redis = redis.from_url(config.REDIS_URL)
pubsub = redis.pubsub()
pubsub.subscribe("room:vehicles")
session = Session()
if __name__ == "__main__":
while count_failed_connection < max_failed_connection:
@ -98,7 +99,7 @@ if __name__ == "__main__":
# Get more datas from SQL
sqlcraft = (
db.session.query(
session.query(
AircraftModes.icao_type_code,
Aircrafts.type,
Aircrafts.manufacturer,


+ 10
- 2
config.py.sample View File

@ -4,6 +4,14 @@ SQLALCHEMY_DATABASE_URI = \
# Redis configuration for the ingesters<>backend pubsub
REDIS_URL = "redis://127.0.0.1:6379/3"
# Hostname on where this server is running
PYAW_HOSTNAME = 'potato'
# Sources for ADS-B and AIS, PLEASE NOTE that currently only one source is handled for each !
# Port for ADSB (dump1090) needs to be 30003 (SBS BaseStation)
# Port for AIS (rtl-ais) needs to be 10110
ADSB_SOURCE = {"host": "patate", "port": 30003, "name": "patate", "reconnect_delay": 5, "thread_timeout": 30}
AIS_SOURCE = {'host': 'patate', 'port': 10110, "name": "patate", "reconnect_delay": 5, "thread_timeout": 30}
# Do not touch that
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_RECORD_QUERIES = True
SQLALCHEMY_RECORD_QUERIES = False

+ 53
- 120
models.py View File

@ -1,145 +1,78 @@
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy_searchable import make_searchable
import datetime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, DateTime, Boolean
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from config import SQLALCHEMY_DATABASE_URI, SQLALCHEMY_RECORD_QUERIES
db = SQLAlchemy()
make_searchable(db.metadata)
Base = declarative_base()
engine = create_engine(SQLALCHEMY_DATABASE_URI, echo=SQLALCHEMY_RECORD_QUERIES)
Session = sessionmaker(bind=engine)
class Translation(db.Model):
class Translation(Base):
"""Translation table for ACARSD"""
__tablename__ = "translation"
id = db.Column(db.Integer, primary_key=True)
reg = db.Column(db.String(20))
reg_correct = db.Column(db.String(20))
operator = db.Column(db.String(20))
operator_correct = db.Column(db.String(20))
source = db.Column(db.String(255))
date_added = db.Column(db.DateTime(timezone=False), default=datetime.datetime.utcnow)
date_modified = db.Column(
db.DateTime(timezone=False), default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow
id = Column(Integer, primary_key=True)
reg = Column(String(20))
reg_correct = Column(String(20))
operator = Column(String(20))
operator_correct = Column(String(20))
source = Column(String(255))
date_added = Column(DateTime(timezone=False), default=datetime.datetime.utcnow)
date_modified = Column(
DateTime(timezone=False), default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow
)
class Aircrafts(db.Model):
class Aircrafts(Base):
__tablename__ = "aircraft"
id = db.Column(db.Integer, primary_key=True)
icao = db.Column(db.String(10), nullable=False, unique=True)
type = db.Column(db.String(255), nullable=False)
manufacturer = db.Column(db.String(255), nullable=False)
official_page = db.Column(db.String(255), nullable=False)
aircraft_shadow = db.Column(db.String(255), default=None)
aircraft_description = db.Column(db.String(255), default=None)
engine_type = db.Column(db.String(255), default=None)
engine_count = db.Column(db.Integer, default=None)
wake_category = db.Column(db.String(10), default=None)
mfr = db.Column(db.String(255), default=None)
id = Column(Integer, primary_key=True)
icao = Column(String(10), nullable=False, unique=True)
type = Column(String(255), nullable=False)
manufacturer = Column(String(255), nullable=False)
official_page = Column(String(255), nullable=False)
aircraft_shadow = Column(String(255), default=None)
aircraft_description = Column(String(255), default=None)
engine_type = Column(String(255), default=None)
engine_count = Column(Integer, default=None)
wake_category = Column(String(10), default=None)
mfr = Column(String(255), default=None)
class AircraftModes(db.Model):
class AircraftModes(Base):
__tablename__ = "aircraft_mode"
id = db.Column(db.Integer, primary_key=True)
first_created = db.Column(db.DateTime(timezone=False), default=datetime.datetime.utcnow)
last_modified = db.Column(db.DateTime(timezone=False))
mode_s = db.Column(db.String(6), nullable=False, index=True)
mode_s_country = db.Column(db.String(40), index=True) # originally 24 chars
registration = db.Column(db.String(20), index=True)
icao_type_code = db.Column(db.String(4), index=True)
type_flight = db.Column(db.String(50))
source = db.Column(db.String(255))
source_type = db.Column(db.String(255), default="modes")
id = Column(Integer, primary_key=True)
first_created = Column(DateTime(timezone=False), default=datetime.datetime.utcnow)
last_modified = Column(DateTime(timezone=False))
mode_s = Column(String(6), nullable=False, index=True)
mode_s_country = Column(String(40), index=True) # originally 24 chars
registration = Column(String(20), index=True)
icao_type_code = Column(String(4), index=True)
type_flight = Column(String(50))
source = Column(String(255))
source_type = Column(String(255), default="modes")
class AircraftOwner(db.Model):
class AircraftOwner(Base):
__tablename__ = "aircraft_owner"
id = db.Column(db.Integer, primary_key=True)
registration = db.Column(db.String(255), nullable=False, index=True)
base = db.Column(db.String(255), default=None)
owner = db.Column(db.String(255))
date_first_reg = db.Column(db.DateTime(timezone=False), default=None)
source = db.Column(db.String(255), nullable=False)
is_private = db.Column(db.Boolean, default=False)
id = Column(Integer, primary_key=True)
registration = Column(String(255), nullable=False, index=True)
base = Column(String(255), default=None)
owner = Column(String(255))
date_first_reg = Column(DateTime(timezone=False), default=None)
source = Column(String(255), nullable=False)
is_private = Column(Boolean, default=False)
class AircraftRegistration(db.Model):
class AircraftRegistration(Base):
__tablename__ = "aircraft_registration"
id = db.Column(db.Integer, primary_key=True)
country = db.Column(db.String(255), nullable=False, index=True)
prefix = db.Column(db.String(10), nullable=False)
class ArchiveAdsbMessage(db.Model):
__tablename__ = "archive_adsb_message"
id = db.Column(db.Integer, primary_key=True)
# Common part of ADS-B message
message_type = db.Column(db.String(5), nullable=False)
transmission_type = db.Column(db.Integer, nullable=False)
session_id = db.Column(db.String(20))
aircraft_id = db.Column(db.String(20))
hex_ident = db.Column(db.String(20)) # ModeS ident
flight_id = db.Column(db.String(20))
date_msg_gen = db.Column(db.String(20))
time_msg_gen = db.Column(db.String(20))
msg_gen = db.Column(db.DateTime(timezone=False))
date_msg_log = db.Column(db.String(20))
time_msg_log = db.Column(db.String(20))
msg_log = db.Column(db.DateTime(timezone=False))
# Other part
callsign = db.Column(db.String(20))
altitude = db.Column(db.Integer)
ground_speed = db.Column(db.Integer)
track = db.Column(db.String(20))
latitude = db.Column(db.Float)
longitude = db.Column(db.Float)
vertical_rate = db.Column(db.Integer)
squawk = db.Column(db.Integer)
alert = db.Column(db.String(20))
emergency = db.Column(db.String(20))
spi_ident = db.Column(db.String(20))
is_on_ground = db.Column(db.Boolean, default=False)
# More things not included in a RAW ADS-B SBS BaseStation message
src = db.Column(db.String(255))
client_name = db.Column(db.String(255))
data_origin = db.Column(db.String(255))
entry_point = db.Column(db.String(255))
def raw(self):
return ",".join(
[
self.message_type,
self.transmission_type,
self.session_id,
self.aircraft_id,
self.hex_ident,
self.flight_id,
self.date_msg_gen,
self.time_msg_gen,
self.date_msg_log,
self.time_msg_log,
self.callsign,
self.altitude,
self.ground_speed,
self.track,
self.latitude,
self.longitude,
self.vertical_rate,
self.squawk,
self.alert,
self.emergency,
self.spi_ident,
self.is_on_ground,
]
)
def vert_stat(self):
return "air" if self.is_on_ground else "gnd"
id = Column(Integer, primary_key=True)
country = Column(String(255), nullable=False, index=True)
prefix = Column(String(10), nullable=False)

Loading…
Cancel
Save