feat: backend FastAPI funcional con auth, ETA y simulador
This commit is contained in:
67
backend/simulator.py
Normal file
67
backend/simulator.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import json
|
||||
import os
|
||||
import datetime
|
||||
from sqlalchemy.orm import Session
|
||||
from database import SessionLocal
|
||||
import models
|
||||
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
with open(os.path.join(BASE_DIR, "data", "rutas.json")) as f:
|
||||
RUTAS = json.load(f)
|
||||
|
||||
COLONIAS = {}
|
||||
with open(os.path.join(BASE_DIR, "data", "colonias-rutas.json")) as f:
|
||||
for item in json.load(f):
|
||||
COLONIAS[item["colonia"].lower()] = item
|
||||
|
||||
def init_rutas(db: Session):
|
||||
for ruta in RUTAS:
|
||||
existe = db.query(models.EstadoRuta).filter_by(route_id=ruta["routeId"]).first()
|
||||
if not existe:
|
||||
db.add(models.EstadoRuta(route_id=ruta["routeId"], current_position_id=1))
|
||||
db.commit()
|
||||
|
||||
def avanzar_rutas():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
estados = db.query(models.EstadoRuta).all()
|
||||
for estado in estados:
|
||||
if estado.current_position_id < 8:
|
||||
estado.current_position_id += 1
|
||||
estado.updated_at = datetime.datetime.utcnow()
|
||||
print(f"[SIM] {estado.route_id} → posición {estado.current_position_id}")
|
||||
else:
|
||||
estado.current_position_id = 1
|
||||
db.commit()
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
def get_eta(route_id: str, db: Session):
|
||||
estado = db.query(models.EstadoRuta).filter_by(route_id=route_id).first()
|
||||
if not estado:
|
||||
return None
|
||||
ruta = next((r for r in RUTAS if r["routeId"] == route_id), None)
|
||||
if not ruta:
|
||||
return None
|
||||
pos = estado.current_position_id
|
||||
if pos >= 8:
|
||||
return {"mensaje": "El servicio de hoy ha finalizado.", "evento": "ROUTE_COMPLETED",
|
||||
"ventana_inicio": "--", "ventana_fin": "--"}
|
||||
pos_actual = ruta["positions"][pos - 1]
|
||||
pos_siguiente = ruta["positions"][min(pos, 7)]
|
||||
ts = datetime.datetime.fromisoformat(pos_siguiente["timestamp"].replace("Z", "+00:00"))
|
||||
ventana_fin = ts.strftime("%H:%M")
|
||||
ventana_inicio = (ts - datetime.timedelta(minutes=7)).strftime("%H:%M")
|
||||
if pos == 1:
|
||||
evento = "ROUTE_START"
|
||||
mensaje = "El camión está por iniciar su recorrido. Ten listos tus residuos."
|
||||
elif pos == 4:
|
||||
evento = "TRUCK_PROXIMITY"
|
||||
mensaje = f"El camión está a menos de 15 minutos. Saca tus bolsas a la acera."
|
||||
else:
|
||||
evento = "EN_CAMINO"
|
||||
mensaje = f"El camión llegará a tu zona entre las {ventana_inicio} y {ventana_fin}."
|
||||
return {"mensaje": mensaje, "evento": evento,
|
||||
"ventana_inicio": ventana_inicio, "ventana_fin": ventana_fin,
|
||||
"current_position": pos}
|
||||
Reference in New Issue
Block a user