2024-01-25 14:17:38 +00:00
|
|
|
# models.py
|
|
|
|
|
2024-02-03 09:57:56 +00:00
|
|
|
from sqlalchemy import Index, Column, String, Integer, JSON, ForeignKey, DateTime
|
2024-01-25 14:17:38 +00:00
|
|
|
from sqlalchemy.orm import declarative_base, relationship
|
|
|
|
|
|
|
|
Base = declarative_base()
|
|
|
|
|
2024-02-03 09:57:56 +00:00
|
|
|
class Beacon(Base):
|
|
|
|
__tablename__ = 'beacon'
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
timestamp = Column(DateTime)
|
|
|
|
snr = Column(Integer)
|
|
|
|
callsign = Column(String, ForeignKey('station.callsign'))
|
|
|
|
station = relationship("Station", back_populates="beacons")
|
|
|
|
|
|
|
|
Index('idx_beacon_callsign', 'callsign')
|
|
|
|
|
2024-01-25 14:17:38 +00:00
|
|
|
class Station(Base):
|
|
|
|
__tablename__ = 'station'
|
|
|
|
callsign = Column(String, primary_key=True)
|
2024-01-30 20:22:04 +00:00
|
|
|
checksum = Column(String, nullable=True)
|
2024-01-27 11:07:07 +00:00
|
|
|
location = Column(JSON, nullable=True)
|
|
|
|
info = Column(JSON, nullable=True)
|
2024-02-03 09:57:56 +00:00
|
|
|
beacons = relationship("Beacon", order_by="Beacon.id", back_populates="station")
|
|
|
|
|
|
|
|
Index('idx_station_callsign_checksum', 'callsign', 'checksum')
|
|
|
|
|
2024-01-30 20:22:04 +00:00
|
|
|
def to_dict(self):
|
|
|
|
return {
|
|
|
|
'callsign': self.callsign,
|
|
|
|
'checksum': self.checksum,
|
|
|
|
'location': self.location,
|
|
|
|
'info': self.info,
|
2024-01-25 14:17:38 +00:00
|
|
|
|
2024-01-30 20:22:04 +00:00
|
|
|
}
|
2024-01-25 14:17:38 +00:00
|
|
|
class Status(Base):
|
|
|
|
__tablename__ = 'status'
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
name = Column(String, unique=True)
|
|
|
|
|
|
|
|
class P2PMessage(Base):
|
|
|
|
__tablename__ = 'p2p_message'
|
|
|
|
id = Column(String, primary_key=True)
|
|
|
|
origin_callsign = Column(String, ForeignKey('station.callsign'))
|
2024-01-27 11:07:07 +00:00
|
|
|
via_callsign = Column(String, ForeignKey('station.callsign'), nullable=True)
|
2024-01-25 14:17:38 +00:00
|
|
|
destination_callsign = Column(String, ForeignKey('station.callsign'))
|
2024-01-27 11:07:07 +00:00
|
|
|
body = Column(String, nullable=True)
|
2024-01-25 14:17:38 +00:00
|
|
|
attachments = relationship('Attachment', backref='p2p_message')
|
|
|
|
timestamp = Column(DateTime)
|
|
|
|
status_id = Column(Integer, ForeignKey('status.id'), nullable=True)
|
|
|
|
status = relationship('Status', backref='p2p_messages')
|
2024-01-27 11:07:07 +00:00
|
|
|
direction = Column(String)
|
2024-01-25 14:17:38 +00:00
|
|
|
statistics = Column(JSON, nullable=True)
|
|
|
|
|
2024-02-03 09:57:56 +00:00
|
|
|
Index('idx_p2p_message_origin_timestamp', 'origin_callsign', 'via_callsign', 'destination_callsign', 'timestamp', 'attachments')
|
|
|
|
|
2024-01-25 14:17:38 +00:00
|
|
|
def to_dict(self):
|
|
|
|
return {
|
|
|
|
'id': self.id,
|
|
|
|
'timestamp': self.timestamp.isoformat() if self.timestamp else None,
|
|
|
|
'origin': self.origin_callsign,
|
2024-01-27 11:07:07 +00:00
|
|
|
'via': self.via_callsign,
|
2024-01-25 14:17:38 +00:00
|
|
|
'destination': self.destination_callsign,
|
|
|
|
'direction': self.direction,
|
|
|
|
'body': self.body,
|
|
|
|
'attachments': [attachment.to_dict() for attachment in self.attachments],
|
|
|
|
'status': self.status.name if self.status else None,
|
|
|
|
'statistics': self.statistics
|
|
|
|
}
|
|
|
|
|
|
|
|
class Attachment(Base):
|
|
|
|
__tablename__ = 'attachment'
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
name = Column(String)
|
|
|
|
data_type = Column(String)
|
|
|
|
data = Column(String)
|
|
|
|
message_id = Column(String, ForeignKey('p2p_message.id'))
|
|
|
|
|
2024-02-03 09:57:56 +00:00
|
|
|
Index('idx_attachments_id_message_id', 'id', 'message_id')
|
|
|
|
|
2024-01-25 14:17:38 +00:00
|
|
|
def to_dict(self):
|
|
|
|
return {
|
|
|
|
'id': self.id,
|
|
|
|
'name': self.name,
|
|
|
|
'data_type': self.data_type,
|
|
|
|
'data': self.data # Be cautious with large binary data
|
|
|
|
}
|