Sistema completo para monitoreo y gestion de flotas de vehiculos con: - Backend FastAPI con PostgreSQL/TimescaleDB - Frontend React con TypeScript y TailwindCSS - App movil React Native con Expo - Soporte para dispositivos GPS, Meshtastic y celulares - Video streaming en vivo con MediaMTX - Geocercas, alertas, viajes y reportes - Autenticacion JWT y WebSockets en tiempo real Documentacion completa y guias de usuario incluidas.
112 lines
3.2 KiB
Python
112 lines
3.2 KiB
Python
"""
|
|
Modelo de Parada para registrar detenciones durante viajes.
|
|
"""
|
|
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import (
|
|
Boolean,
|
|
DateTime,
|
|
Float,
|
|
ForeignKey,
|
|
Index,
|
|
Integer,
|
|
String,
|
|
Text,
|
|
)
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.core.database import Base
|
|
|
|
|
|
class Parada(Base):
|
|
"""
|
|
Modelo de parada/detención de un vehículo durante un viaje.
|
|
|
|
Se registra cuando el vehículo permanece detenido por más
|
|
de un tiempo mínimo configurado (ej: 2 minutos).
|
|
"""
|
|
|
|
__tablename__ = "paradas"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
|
|
# Relaciones
|
|
viaje_id: Mapped[int | None] = mapped_column(
|
|
ForeignKey("viajes.id", ondelete="CASCADE"),
|
|
nullable=True,
|
|
index=True,
|
|
)
|
|
vehiculo_id: Mapped[int] = mapped_column(
|
|
ForeignKey("vehiculos.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
)
|
|
|
|
# Tiempo
|
|
inicio_tiempo: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
|
fin_tiempo: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
duracion_segundos: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
|
|
|
# Ubicación
|
|
lat: Mapped[float] = mapped_column(Float, nullable=False)
|
|
lng: Mapped[float] = mapped_column(Float, nullable=False)
|
|
direccion: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
|
|
|
# Clasificación
|
|
tipo: Mapped[str] = mapped_column(
|
|
String(50),
|
|
default="desconocido",
|
|
nullable=False,
|
|
) # desconocido, entrega, carga, descanso, trafico, cliente, otro
|
|
|
|
# Estado del vehículo durante la parada
|
|
motor_apagado: Mapped[bool | None] = mapped_column(Boolean, nullable=True)
|
|
|
|
# POI asociado (si aplica)
|
|
poi_id: Mapped[int | None] = mapped_column(
|
|
ForeignKey("pois.id", ondelete="SET NULL"),
|
|
nullable=True,
|
|
)
|
|
|
|
# Geocerca asociada (si aplica)
|
|
geocerca_id: Mapped[int | None] = mapped_column(
|
|
ForeignKey("geocercas.id", ondelete="SET NULL"),
|
|
nullable=True,
|
|
)
|
|
|
|
# Estado (para paradas en curso)
|
|
en_curso: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
|
|
|
|
# Notas
|
|
notas: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
|
|
# Relaciones ORM
|
|
viaje: Mapped["Viaje | None"] = relationship(
|
|
"Viaje",
|
|
back_populates="paradas",
|
|
lazy="selectin",
|
|
)
|
|
|
|
# Índices
|
|
__table_args__ = (
|
|
Index("idx_paradas_vehiculo_inicio", "vehiculo_id", "inicio_tiempo"),
|
|
Index("idx_paradas_en_curso", "en_curso"),
|
|
)
|
|
|
|
@property
|
|
def duracion_formateada(self) -> str:
|
|
"""Retorna la duración en formato legible."""
|
|
if not self.duracion_segundos:
|
|
if self.en_curso:
|
|
return "En curso"
|
|
return "N/A"
|
|
horas = self.duracion_segundos // 3600
|
|
minutos = (self.duracion_segundos % 3600) // 60
|
|
if horas > 0:
|
|
return f"{horas}h {minutos}m"
|
|
return f"{minutos}m"
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<Parada(id={self.id}, vehiculo_id={self.vehiculo_id}, tipo='{self.tipo}')>"
|