2017-10-03 11:31:24 +00:00
|
|
|
from geoalchemy2.shape import to_shape
|
|
|
|
from geoalchemy2.types import Geometry
|
2015-11-15 10:59:52 +00:00
|
|
|
|
2016-04-24 17:34:25 +00:00
|
|
|
from .geo import Location
|
2015-11-15 10:59:52 +00:00
|
|
|
|
2019-08-31 08:14:41 +00:00
|
|
|
from app import db
|
2020-10-27 19:46:14 +00:00
|
|
|
|
|
|
|
from .airport import Airport
|
2020-11-24 19:26:28 +00:00
|
|
|
from .receiver_state import ReceiverState
|
2015-11-15 10:59:52 +00:00
|
|
|
|
2019-02-10 12:10:19 +00:00
|
|
|
|
|
|
|
class Receiver(db.Model):
|
2018-01-11 07:35:07 +00:00
|
|
|
__tablename__ = "receivers"
|
2015-11-15 10:59:52 +00:00
|
|
|
|
2020-10-27 19:46:14 +00:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
name = db.Column(db.String(9))
|
|
|
|
|
2019-08-31 08:14:41 +00:00
|
|
|
location_wkt = db.Column("location", Geometry("POINT", srid=4326))
|
2019-02-10 12:25:24 +00:00
|
|
|
altitude = db.Column(db.Float(precision=2))
|
2016-04-24 17:34:25 +00:00
|
|
|
|
2019-02-10 12:25:24 +00:00
|
|
|
firstseen = db.Column(db.DateTime, index=True)
|
|
|
|
lastseen = db.Column(db.DateTime, index=True)
|
2020-05-15 17:01:17 +00:00
|
|
|
timestamp = db.Column(db.DateTime, index=True)
|
2019-02-10 12:25:24 +00:00
|
|
|
version = db.Column(db.String)
|
|
|
|
platform = db.Column(db.String)
|
2020-10-27 19:46:14 +00:00
|
|
|
cpu_temp = db.Column(db.Float(precision=2))
|
|
|
|
rec_input_noise = db.Column(db.Float(precision=2))
|
|
|
|
|
|
|
|
agl = db.Column(db.Float(precision=2))
|
2016-04-24 17:34:25 +00:00
|
|
|
|
2018-12-08 19:04:41 +00:00
|
|
|
# Relations
|
2019-08-31 08:14:41 +00:00
|
|
|
country_id = db.Column(db.Integer, db.ForeignKey("countries.gid", ondelete="SET NULL"), index=True)
|
|
|
|
country = db.relationship("Country", foreign_keys=[country_id], backref=db.backref("receivers", order_by="Receiver.name.asc()"))
|
2018-12-08 19:04:41 +00:00
|
|
|
|
2020-10-27 19:46:14 +00:00
|
|
|
airport_id = db.Column(db.Integer, db.ForeignKey("airports.id", ondelete="CASCADE"), index=True)
|
|
|
|
airport = db.relationship("Airport", foreign_keys=[airport_id], backref=db.backref("receivers", order_by="Receiver.name.asc()"))
|
|
|
|
|
2020-11-20 16:31:03 +00:00
|
|
|
__table_args__ = (db.Index('idx_receivers_name_uc', 'name', unique=True), )
|
2020-10-27 19:46:14 +00:00
|
|
|
|
2016-04-24 17:34:25 +00:00
|
|
|
@property
|
|
|
|
def location(self):
|
|
|
|
if self.location_wkt is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
coords = to_shape(self.location_wkt)
|
|
|
|
return Location(lat=coords.y, lon=coords.x)
|
2020-10-27 19:46:14 +00:00
|
|
|
|
2020-11-24 19:26:28 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
import datetime
|
|
|
|
if datetime.datetime.utcnow() - self.lastseen < datetime.timedelta(minutes=10):
|
|
|
|
return ReceiverState.OK if len(self.statistics) > 0 else ReceiverState.ZOMBIE
|
|
|
|
elif datetime.datetime.utcnow() - self.lastseen < datetime.timedelta(hours=1):
|
|
|
|
return ReceiverState.UNKNOWN
|
|
|
|
else:
|
|
|
|
return ReceiverState.OFFLINE
|
|
|
|
|
2020-10-27 19:46:14 +00:00
|
|
|
def airports_nearby(self):
|
|
|
|
query = (
|
|
|
|
db.session.query(Airport, db.func.st_distance_sphere(self.location_wkt, Airport.location_wkt), db.func.st_azimuth(self.location_wkt, Airport.location_wkt))
|
2020-11-22 07:55:19 +00:00
|
|
|
.filter(db.func.st_contains(db.func.st_buffer(Airport.location_wkt, 1), self.location_wkt))
|
|
|
|
.filter(Airport.style.in_((2, 4, 5)))
|
|
|
|
.order_by(db.func.st_distance_sphere(self.location_wkt, Airport.location_wkt).asc())
|
|
|
|
.limit(5)
|
2020-10-27 19:46:14 +00:00
|
|
|
)
|
2020-11-22 07:55:19 +00:00
|
|
|
airports = [(airport, distance, azimuth) for airport, distance, azimuth in query]
|
2020-10-27 19:46:14 +00:00
|
|
|
return airports
|