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.
178 lines
5.4 KiB
Python
178 lines
5.4 KiB
Python
"""
|
|
Configuración central de la aplicación.
|
|
|
|
Utiliza Pydantic BaseSettings para cargar variables de entorno
|
|
con validación de tipos y valores por defecto.
|
|
"""
|
|
|
|
from functools import lru_cache
|
|
from typing import Any, List, Optional
|
|
|
|
from pydantic import PostgresDsn, field_validator, model_validator
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
"""Configuración de la aplicación cargada desde variables de entorno."""
|
|
|
|
model_config = SettingsConfigDict(
|
|
env_file=".env",
|
|
env_file_encoding="utf-8",
|
|
case_sensitive=False,
|
|
extra="ignore",
|
|
)
|
|
|
|
# Aplicación
|
|
APP_NAME: str = "Adan Fleet Monitor"
|
|
APP_VERSION: str = "1.0.0"
|
|
DEBUG: bool = False
|
|
ENVIRONMENT: str = "development"
|
|
API_V1_PREFIX: str = "/api/v1"
|
|
|
|
# Servidor
|
|
HOST: str = "0.0.0.0"
|
|
PORT: int = 8000
|
|
WORKERS: int = 4
|
|
|
|
# Base de datos PostgreSQL/TimescaleDB
|
|
POSTGRES_HOST: str = "localhost"
|
|
POSTGRES_PORT: int = 5432
|
|
POSTGRES_USER: str = "adan"
|
|
POSTGRES_PASSWORD: str = "adan_secret"
|
|
POSTGRES_DB: str = "adan_fleet"
|
|
DATABASE_URL: Optional[str] = None
|
|
DATABASE_POOL_SIZE: int = 20
|
|
DATABASE_MAX_OVERFLOW: int = 10
|
|
|
|
@model_validator(mode="after")
|
|
def build_database_url(self) -> "Settings":
|
|
"""Construye la URL de conexión a la base de datos."""
|
|
if not self.DATABASE_URL:
|
|
self.DATABASE_URL = (
|
|
f"postgresql+asyncpg://{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}"
|
|
f"@{self.POSTGRES_HOST}:{self.POSTGRES_PORT}/{self.POSTGRES_DB}"
|
|
)
|
|
return self
|
|
|
|
# Redis
|
|
REDIS_HOST: str = "localhost"
|
|
REDIS_PORT: int = 6379
|
|
REDIS_DB: int = 0
|
|
REDIS_PASSWORD: Optional[str] = None
|
|
REDIS_URL: Optional[str] = None
|
|
|
|
@model_validator(mode="after")
|
|
def build_redis_url(self) -> "Settings":
|
|
"""Construye la URL de conexión a Redis."""
|
|
if not self.REDIS_URL:
|
|
password_part = f":{self.REDIS_PASSWORD}@" if self.REDIS_PASSWORD else ""
|
|
self.REDIS_URL = f"redis://{password_part}{self.REDIS_HOST}:{self.REDIS_PORT}/{self.REDIS_DB}"
|
|
return self
|
|
|
|
# Seguridad JWT
|
|
SECRET_KEY: str = "your-super-secret-key-change-in-production"
|
|
ALGORITHM: str = "HS256"
|
|
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
|
|
REFRESH_TOKEN_EXPIRE_DAYS: int = 7
|
|
|
|
# CORS
|
|
CORS_ORIGINS: List[str] = ["http://localhost:3000", "http://localhost:5173"]
|
|
CORS_ALLOW_CREDENTIALS: bool = True
|
|
CORS_ALLOW_METHODS: List[str] = ["*"]
|
|
CORS_ALLOW_HEADERS: List[str] = ["*"]
|
|
|
|
@field_validator("CORS_ORIGINS", mode="before")
|
|
@classmethod
|
|
def parse_cors_origins(cls, v: Any) -> List[str]:
|
|
"""Parsea los orígenes CORS desde string separado por comas."""
|
|
if isinstance(v, str):
|
|
return [origin.strip() for origin in v.split(",")]
|
|
return v
|
|
|
|
# Traccar Integration
|
|
TRACCAR_HOST: str = "localhost"
|
|
TRACCAR_PORT: int = 5055
|
|
TRACCAR_API_URL: str = "http://localhost:8082/api"
|
|
TRACCAR_USERNAME: Optional[str] = None
|
|
TRACCAR_PASSWORD: Optional[str] = None
|
|
|
|
# MediaMTX Video Server
|
|
MEDIAMTX_HOST: str = "localhost"
|
|
MEDIAMTX_API_PORT: int = 9997
|
|
MEDIAMTX_RTSP_PORT: int = 8554
|
|
MEDIAMTX_WEBRTC_PORT: int = 8889
|
|
|
|
# Meshtastic
|
|
MESHTASTIC_ENABLED: bool = False
|
|
MESHTASTIC_SERIAL_PORT: Optional[str] = None
|
|
MESHTASTIC_TCP_HOST: Optional[str] = None
|
|
MESHTASTIC_TCP_PORT: int = 4403
|
|
|
|
# MQTT (para dispositivos IoT)
|
|
MQTT_ENABLED: bool = False
|
|
MQTT_HOST: str = "localhost"
|
|
MQTT_PORT: int = 1883
|
|
MQTT_USERNAME: Optional[str] = None
|
|
MQTT_PASSWORD: Optional[str] = None
|
|
MQTT_TOPIC_LOCATIONS: str = "adan/locations/#"
|
|
MQTT_TOPIC_ALERTS: str = "adan/alerts/#"
|
|
|
|
# Email (notificaciones)
|
|
SMTP_HOST: str = "localhost"
|
|
SMTP_PORT: int = 587
|
|
SMTP_USER: Optional[str] = None
|
|
SMTP_PASSWORD: Optional[str] = None
|
|
SMTP_FROM_EMAIL: str = "noreply@adan-fleet.com"
|
|
SMTP_FROM_NAME: str = "Adan Fleet Monitor"
|
|
SMTP_TLS: bool = True
|
|
|
|
# Push Notifications (Firebase)
|
|
FIREBASE_CREDENTIALS_PATH: Optional[str] = None
|
|
FIREBASE_ENABLED: bool = False
|
|
|
|
# Almacenamiento de archivos
|
|
UPLOAD_DIR: str = "/var/lib/adan/uploads"
|
|
MAX_UPLOAD_SIZE_MB: int = 100
|
|
ALLOWED_IMAGE_TYPES: List[str] = ["image/jpeg", "image/png", "image/webp"]
|
|
ALLOWED_VIDEO_TYPES: List[str] = ["video/mp4", "video/webm"]
|
|
|
|
# Reportes
|
|
REPORTS_DIR: str = "/var/lib/adan/reports"
|
|
REPORT_RETENTION_DAYS: int = 90
|
|
|
|
# Geocoding
|
|
GEOCODING_PROVIDER: str = "nominatim" # nominatim, google, mapbox
|
|
GOOGLE_MAPS_API_KEY: Optional[str] = None
|
|
MAPBOX_ACCESS_TOKEN: Optional[str] = None
|
|
|
|
# Alertas y umbrales
|
|
ALERT_SPEED_LIMIT_DEFAULT: int = 120 # km/h
|
|
ALERT_IDLE_MINUTES: int = 15
|
|
ALERT_BATTERY_LOW_PERCENT: int = 20
|
|
ALERT_NO_SIGNAL_MINUTES: int = 30
|
|
|
|
# Limpieza de datos
|
|
LOCATION_RETENTION_DAYS: int = 365
|
|
ALERT_RETENTION_DAYS: int = 180
|
|
VIDEO_RETENTION_DAYS: int = 30
|
|
|
|
# Logging
|
|
LOG_LEVEL: str = "INFO"
|
|
LOG_FORMAT: str = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
|
LOG_FILE: Optional[str] = None
|
|
|
|
|
|
@lru_cache()
|
|
def get_settings() -> Settings:
|
|
"""
|
|
Obtiene la instancia de configuración (singleton cacheado).
|
|
|
|
Returns:
|
|
Settings: Instancia de configuración de la aplicación.
|
|
"""
|
|
return Settings()
|
|
|
|
|
|
# Instancia global de configuración
|
|
settings = get_settings()
|