mirror of
https://github.com/ferdzo/iotDashboard.git
synced 2026-04-05 09:06:26 +00:00
Functioning mqtt ingestion and db write, formating changes, device manager initiated
This commit is contained in:
@@ -7,42 +7,61 @@ To modify schema:
|
||||
3. Review the generated migration in alembic/versions/
|
||||
4. Run: alembic upgrade head
|
||||
"""
|
||||
from sqlalchemy import Boolean, Column, Float, ForeignKey, Index,Text, DateTime
|
||||
|
||||
from sqlalchemy import Boolean, Column, Float, ForeignKey, Index, Text, DateTime
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.sql import func
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
class Device(Base):
|
||||
"""IoT devices registered in the system."""
|
||||
__tablename__ = 'devices'
|
||||
|
||||
|
||||
__tablename__ = "devices"
|
||||
|
||||
id = Column(Text, primary_key=True)
|
||||
name = Column(Text, nullable=False)
|
||||
location = Column(Text)
|
||||
is_active = Column(Boolean, default=True)
|
||||
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Device(id={self.id}, name={self.name})>"
|
||||
|
||||
|
||||
class DeviceCertificate(Base):
|
||||
"""X.509 certificates issued to devices for mTLS authentication."""
|
||||
|
||||
__tablename__ = "device_certificates"
|
||||
|
||||
device_id = Column(
|
||||
Text, ForeignKey("devices.id", ondelete="CASCADE"), primary_key=True
|
||||
)
|
||||
certificate_pem = Column(Text, nullable=False)
|
||||
private_key_pem = Column(Text) # Optional: for backup/escrow
|
||||
issued_at = Column(DateTime(timezone=True), nullable=False)
|
||||
expires_at = Column(DateTime(timezone=True), nullable=False)
|
||||
revoked_at = Column(DateTime(timezone=True))
|
||||
|
||||
def __repr__(self):
|
||||
return f"<DeviceCertificate(device_id={self.device_id}, expires={self.expires_at})>"
|
||||
|
||||
|
||||
class Telemetry(Base):
|
||||
"""
|
||||
Time-series telemetry data from devices.
|
||||
This will be converted to a TimescaleDB hypertable.
|
||||
"""
|
||||
__tablename__ = 'telemetry'
|
||||
|
||||
|
||||
__tablename__ = "telemetry"
|
||||
|
||||
time = Column(DateTime(timezone=True), primary_key=True, nullable=False)
|
||||
device_id = Column(Text, ForeignKey('devices.id'), primary_key=True, nullable=False)
|
||||
metric = Column(Text, primary_key=True, nullable=False) # e.g., 'light', 'temperature'
|
||||
device_id = Column(Text, ForeignKey("devices.id"), primary_key=True, nullable=False)
|
||||
metric = Column(Text, primary_key=True, nullable=False)
|
||||
value = Column(Float, nullable=False)
|
||||
unit = Column(Text)
|
||||
|
||||
__table_args__ = (
|
||||
Index('idx_telemetry_device_time', 'device_id', 'time'),
|
||||
)
|
||||
|
||||
|
||||
__table_args__ = (Index("idx_telemetry_device_time", "device_id", "time"),)
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Telemetry(device={self.device_id}, metric={self.metric}, value={self.value})>"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user