from __future__ import annotations from datetime import datetime from sqlalchemy import Boolean, DateTime, Float, ForeignKey, Integer, JSON, String, Text from sqlalchemy.orm import Mapped, mapped_column from app.db.base import Base, TimestampMixin, UUIDPrimaryKeyMixin class DataSource(UUIDPrimaryKeyMixin, TimestampMixin, Base): __tablename__ = "data_sources" mine_id: Mapped[str | None] = mapped_column( String(32), ForeignKey("mines.id", ondelete="CASCADE"), nullable=True, index=True ) code: Mapped[str] = mapped_column(String(64), unique=True, index=True) name: Mapped[str] = mapped_column(String(128)) source_type: Mapped[str] = mapped_column(String(32), index=True) protocol: Mapped[str] = mapped_column(String(64), default="REST") endpoint: Mapped[str] = mapped_column(String(512), default="") status: Mapped[str] = mapped_column(String(32), default="offline", index=True) last_sync_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) point_count: Mapped[int] = mapped_column(Integer, default=0) latency_ms: Mapped[int | None] = mapped_column(Integer, nullable=True) config: Mapped[dict] = mapped_column(JSON, default=dict) class Sensor(UUIDPrimaryKeyMixin, TimestampMixin, Base): __tablename__ = "sensors" mine_id: Mapped[str] = mapped_column( String(32), ForeignKey("mines.id", ondelete="CASCADE"), index=True ) area_id: Mapped[str | None] = mapped_column( String(32), ForeignKey("mine_areas.id", ondelete="SET NULL"), nullable=True ) data_source_id: Mapped[str | None] = mapped_column( String(32), ForeignKey("data_sources.id", ondelete="SET NULL"), nullable=True, index=True ) code: Mapped[str] = mapped_column(String(64), index=True) name: Mapped[str] = mapped_column(String(128)) sensor_type: Mapped[str] = mapped_column(String(32), index=True) unit: Mapped[str] = mapped_column(String(32), default="") location: Mapped[str] = mapped_column(String(255), default="") status: Mapped[str] = mapped_column(String(32), default="online", index=True) warning_threshold: Mapped[float | None] = mapped_column(Float, nullable=True) alarm_threshold: Mapped[float | None] = mapped_column(Float, nullable=True) upper_bound: Mapped[float | None] = mapped_column(Float, nullable=True) lower_bound: Mapped[float | None] = mapped_column(Float, nullable=True) last_value: Mapped[float | None] = mapped_column(Float, nullable=True) last_seen_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) properties: Mapped[dict] = mapped_column(JSON, default=dict) class SensorReading(UUIDPrimaryKeyMixin, Base): __tablename__ = "sensor_readings" sensor_id: Mapped[str] = mapped_column( String(32), ForeignKey("sensors.id", ondelete="CASCADE"), index=True ) measured_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), index=True) value: Mapped[float] = mapped_column(Float) quality: Mapped[int] = mapped_column(Integer, default=100) is_alarm: Mapped[bool] = mapped_column(Boolean, default=False, index=True) source: Mapped[str] = mapped_column(String(64), default="") meta: Mapped[dict] = mapped_column(JSON, default=dict) class VentilationFacility(UUIDPrimaryKeyMixin, TimestampMixin, Base): __tablename__ = "ventilation_facilities" mine_id: Mapped[str] = mapped_column( String(32), ForeignKey("mines.id", ondelete="CASCADE"), index=True ) area_id: Mapped[str | None] = mapped_column( String(32), ForeignKey("mine_areas.id", ondelete="SET NULL"), nullable=True ) roadway_id: Mapped[str | None] = mapped_column( String(32), ForeignKey("roadways.id", ondelete="SET NULL"), nullable=True ) code: Mapped[str] = mapped_column(String(64), index=True) name: Mapped[str] = mapped_column(String(128)) facility_type: Mapped[str] = mapped_column(String(32), index=True) location: Mapped[str] = mapped_column(String(255), default="") status: Mapped[str] = mapped_column(String(32), default="normal", index=True) open_state: Mapped[str] = mapped_column(String(32), default="") sensor_value: Mapped[float | None] = mapped_column(Float, nullable=True) video_confidence: Mapped[float | None] = mapped_column(Float, nullable=True) last_checked_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) properties: Mapped[dict] = mapped_column(JSON, default=dict)