84 lines
3.1 KiB
Python
84 lines
3.1 KiB
Python
# ==============================================================================
|
|
# Servicio de Gestión de Estaciones de Trabajo (Workstations)
|
|
# ==============================================================================
|
|
from typing import List, Dict, Any
|
|
from app.repositories.workstation_repository import WorkstationRepository
|
|
from app.schemas.workstation_schema import (
|
|
WorkstationStatusResponse,
|
|
WorkstationConfigResponse,
|
|
WorkstationItemDTO,
|
|
LaneModuleDTO,
|
|
)
|
|
|
|
|
|
class WorkstationService:
|
|
@staticmethod
|
|
def obtener_config_workstation(name: str) -> WorkstationConfigResponse:
|
|
"""Retorna la configuración esperada por el cliente web: id, name, entry_modules, exit_modules."""
|
|
wks_row = WorkstationRepository.obtener_workstation_por_nombre(name)
|
|
if not wks_row:
|
|
return WorkstationConfigResponse(
|
|
id=0,
|
|
name=name,
|
|
entry_modules=[],
|
|
exit_modules=[],
|
|
)
|
|
|
|
wks_id = wks_row["WorkstationID"]
|
|
nombre_oficial = wks_row["Nombre"].strip()
|
|
modulos_rows = WorkstationRepository.listar_modulos_por_workstation(wks_id)
|
|
|
|
entry_modules: List[LaneModuleDTO] = []
|
|
exit_modules: List[LaneModuleDTO] = []
|
|
|
|
for m in modulos_rows:
|
|
nom = m["Nombre"].strip()
|
|
nom_upper = nom.upper()
|
|
if "SALIDA" in nom_upper or "EGRESO" in nom_upper:
|
|
es_entrada = False
|
|
elif "ENTRADA" in nom_upper or "INGRESO" in nom_upper:
|
|
es_entrada = True
|
|
else:
|
|
es_entrada = not bool(m.get("ModuloEntradaID"))
|
|
dto = LaneModuleDTO(
|
|
id=m["ModuloID"],
|
|
name=nom,
|
|
address=m["Address"],
|
|
type="ENTRY" if es_entrada else "EXIT",
|
|
grupo_modulos=m.get("GrupoModulos"),
|
|
es_entrada=es_entrada,
|
|
duracion_pulso=m.get("duracion_pulso"),
|
|
)
|
|
if es_entrada:
|
|
entry_modules.append(dto)
|
|
else:
|
|
exit_modules.append(dto)
|
|
|
|
return WorkstationConfigResponse(
|
|
id=wks_id,
|
|
name=nombre_oficial,
|
|
entry_modules=entry_modules,
|
|
exit_modules=exit_modules,
|
|
)
|
|
|
|
@staticmethod
|
|
def obtener_estado_workstation(name: str) -> WorkstationStatusResponse:
|
|
"""Determina si la tablet/workstation está registrada y qué carriles tiene asignados."""
|
|
cfg = WorkstationService.obtener_config_workstation(name)
|
|
all_modules = cfg.entry_modules + cfg.exit_modules
|
|
return WorkstationStatusResponse(
|
|
workstation_name=cfg.name,
|
|
is_control_post=len(all_modules) > 0,
|
|
workstation_id=cfg.id,
|
|
modules=all_modules,
|
|
)
|
|
|
|
@staticmethod
|
|
def listar_estaciones_disponibles() -> List[WorkstationItemDTO]:
|
|
"""Retorna el listado de todas las workstations reales desde la tabla wks de SQL Server."""
|
|
rows = WorkstationRepository.listar_todas()
|
|
return [
|
|
WorkstationItemDTO(id=r["WorkstationID"], name=r["Nombre"].strip())
|
|
for r in rows
|
|
]
|