Co-authored-by: MENDOZA BALLARDO GAEL RICARDO <gael-meb123@users.noreply.github.com>
Co-authored-by: Azareth-Tr <Azareth-Tr@users.noreply.github.com> Co-authored-by: eddgranados12 <eddgranados12@users.noreply.github.com> primeras vistas para el frontend, configuracion para firebase
This commit is contained in:
@@ -1,49 +1,43 @@
|
||||
import os
|
||||
from typing import Dict
|
||||
import firebase_admin
|
||||
from firebase_admin import credentials, messaging
|
||||
|
||||
_initialized = False
|
||||
_use_mock = True
|
||||
_firebase_initialized = False
|
||||
|
||||
def init_firebase(credentials_path: str = None):
|
||||
global _initialized, _use_mock
|
||||
try:
|
||||
import firebase_admin
|
||||
from firebase_admin import credentials, messaging
|
||||
except Exception:
|
||||
_use_mock = True
|
||||
return
|
||||
def init_firebase(cred_path: str):
|
||||
"""Inicializa la conexión con Firebase usando el Service Account (JSON)."""
|
||||
global _firebase_initialized
|
||||
|
||||
if os.path.exists(cred_path):
|
||||
try:
|
||||
cred = credentials.Certificate(cred_path)
|
||||
firebase_admin.initialize_app(cred)
|
||||
_firebase_initialized = True
|
||||
print(f"Firebase Admin SDK inicializado correctamente desde: {cred_path}")
|
||||
except ValueError:
|
||||
# Si el entorno se recarga, Firebase podría quejarse de que ya se inicializó
|
||||
_firebase_initialized = True
|
||||
except Exception as e:
|
||||
print(f"Error al inicializar Firebase: {e}")
|
||||
else:
|
||||
print(f"ADVERTENCIA: Credenciales no encontradas en '{cred_path}'.")
|
||||
print("Las notificaciones se ejecutarán en modo SIMULADO (solo consola).")
|
||||
|
||||
if credentials_path is None:
|
||||
credentials_path = os.environ.get('FIREBASE_CREDENTIALS_PATH', 'backend/secrets/firebase-adminsdk.json')
|
||||
|
||||
if not os.path.exists(credentials_path):
|
||||
_use_mock = True
|
||||
def send_to_topic(topic: str, title: str, body: str):
|
||||
"""Envía una notificación push a todos los dispositivos suscritos a un topic (ej. RUTA-01)."""
|
||||
if not _firebase_initialized:
|
||||
print(f"[MOCK PUSH] -> Topic: {topic} | Título: '{title}' | Mensaje: '{body}'")
|
||||
return
|
||||
|
||||
try:
|
||||
cred = credentials.Certificate(credentials_path)
|
||||
firebase_admin.initialize_app(cred)
|
||||
_initialized = True
|
||||
_use_mock = False
|
||||
except Exception:
|
||||
_use_mock = True
|
||||
|
||||
|
||||
def send_to_topic(topic: str, payload: Dict):
|
||||
"""Sends a push to an FCM topic. Falls back to mock (prints) if not configured."""
|
||||
global _use_mock
|
||||
if _use_mock:
|
||||
print(f"[MOCK PUSH] topic={topic} payload={payload}")
|
||||
return {"mock": True, "topic": topic, "payload": payload}
|
||||
|
||||
try:
|
||||
from firebase_admin import messaging
|
||||
message = messaging.Message(
|
||||
notification=messaging.Notification(title=payload.get('title'), body=payload.get('body')),
|
||||
notification=messaging.Notification(
|
||||
title=title,
|
||||
body=body,
|
||||
),
|
||||
topic=topic,
|
||||
)
|
||||
resp = messaging.send(message)
|
||||
return {"result": resp}
|
||||
response = messaging.send(message)
|
||||
print(f"Push enviado al topic '{topic}' exitosamente. MessageID: {response}")
|
||||
except Exception as e:
|
||||
print(f"[PUSH ERROR] {e}")
|
||||
return {"error": str(e)}
|
||||
print(f"Error al enviar push al topic '{topic}': {e}")
|
||||
@@ -1,20 +1,39 @@
|
||||
from contextlib import asynccontextmanager
|
||||
from fastapi import FastAPI
|
||||
import os
|
||||
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||
|
||||
# Aquí se importarán los routers en el futuro
|
||||
# from app.api.routers import auth, addresses, routes, eta
|
||||
from app.api.eta import router as eta_router
|
||||
from app.services import simulation, notifications
|
||||
|
||||
scheduler = AsyncIOScheduler()
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""
|
||||
Maneja el ciclo de vida de la aplicación.
|
||||
Ideal para arrancar el cron job de simulación (APScheduler).
|
||||
"""
|
||||
print("Iniciando aplicación: Backend Sistema de Recolección...")
|
||||
# TODO: Inicializar APScheduler aquí para avanzar current_position_id (1-8)
|
||||
|
||||
# 1. Cargar datos de simulación
|
||||
simulation.load_data()
|
||||
simulation.start_simulation_state()
|
||||
|
||||
# 2. Inicializar Firebase (o Mock si no hay credenciales)
|
||||
# Ruta relativa correcta cuando se ejecuta desde la carpeta /backend
|
||||
cred_path = os.environ.get("FIREBASE_CREDENTIALS_PATH", "secrets/firebase-adminsdk.json")
|
||||
notifications.init_firebase(cred_path)
|
||||
|
||||
# 3. Arrancar el scheduler de simulación
|
||||
tick_seconds = int(os.environ.get("SIMULATION_TICK_SECONDS", 15))
|
||||
scheduler.add_job(simulation.tick, 'interval', seconds=tick_seconds, id='simulation_tick')
|
||||
scheduler.start()
|
||||
print(f"Simulador de rutas iniciado. Avanzando cada {tick_seconds} segundos.")
|
||||
|
||||
yield
|
||||
|
||||
print("Apagando aplicación y deteniendo simulador...")
|
||||
# TODO: Apagar APScheduler
|
||||
scheduler.shutdown()
|
||||
|
||||
app = FastAPI(
|
||||
title="API - Recolección Inteligente y Privada",
|
||||
@@ -23,6 +42,9 @@ app = FastAPI(
|
||||
lifespan=lifespan
|
||||
)
|
||||
|
||||
# Incluir routers de la API
|
||||
app.include_router(eta_router)
|
||||
|
||||
# Endpoints de prueba base
|
||||
@app.get("/")
|
||||
def read_root():
|
||||
|
||||
Reference in New Issue
Block a user