feat(parity): implement Bloques 1, 2 y 3 complete feature parity v0.7.0-beta
This commit is contained in:
@@ -252,3 +252,116 @@ class HardwareService:
|
||||
"trama_ticket": trama_ticket,
|
||||
"latencia_ms": round(latencia, 2),
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def encolar_cierre(cls, modulo_id: int, ticket_id: int = 0) -> Tuple[bool, Dict[str, Any]]:
|
||||
"""
|
||||
Encola un comando de cierre forzado de barrera/molinete en la tabla `pdtmdl`
|
||||
con tramas C0..C6 según tipo_acceso y firmware (PASS vs PARK).
|
||||
Paridad 1:1 con pct_repository.py:encolar_comando_cierre_modulo (v1.2.3).
|
||||
"""
|
||||
t_start = time.perf_counter()
|
||||
mdl = cls.obtener_info_modulo(modulo_id)
|
||||
if not mdl:
|
||||
return False, {"error": f"Módulo {modulo_id} no encontrado en base de datos"}
|
||||
|
||||
cat_map = {}
|
||||
try:
|
||||
with db_session() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"SELECT CategoriaID, ValorID FROM MdlCatVal WHERE ModuloID = %s",
|
||||
(modulo_id,),
|
||||
)
|
||||
cat_map = {row[0]: row[1] for row in cursor.fetchall()}
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
tipo_modulo_id = cat_map.get(2, 0)
|
||||
sentido_id = cat_map.get(4, 0)
|
||||
nombre_upper = (mdl.get("Nombre") or "").upper()
|
||||
|
||||
target_id = modulo_id
|
||||
target_mdl = mdl
|
||||
|
||||
if tipo_modulo_id == 6 or mdl.get("Address", 0) >= 90:
|
||||
if (sentido_id == 1 or "ENTRADA" in nombre_upper or "INGRESO" in nombre_upper) and mdl.get("ModuloEntradaID"):
|
||||
target_id = mdl["ModuloEntradaID"]
|
||||
elif (sentido_id == 2 or "SALIDA" in nombre_upper or "EGRESO" in nombre_upper) and mdl.get("ModuloSalidaID"):
|
||||
target_id = mdl["ModuloSalidaID"]
|
||||
|
||||
if target_id != modulo_id:
|
||||
fisico = cls.obtener_info_modulo(target_id)
|
||||
if fisico:
|
||||
target_mdl = fisico
|
||||
|
||||
target_cat_map = {}
|
||||
if target_id != modulo_id:
|
||||
try:
|
||||
with db_session() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"SELECT CategoriaID, ValorID FROM MdlCatVal WHERE ModuloID = %s",
|
||||
(target_id,),
|
||||
)
|
||||
target_cat_map = {row[0]: row[1] for row in cursor.fetchall()}
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
target_cat_map = cat_map
|
||||
|
||||
address = target_mdl.get("Address", 1)
|
||||
is_pass = (target_cat_map.get(2, 0) == 1)
|
||||
tipo_acceso = target_cat_map.get(15) or cat_map.get(15, 3)
|
||||
|
||||
if is_pass:
|
||||
if tipo_acceso == 1:
|
||||
payload = "C4"
|
||||
elif tipo_acceso in (2, 7):
|
||||
payload = "C2"
|
||||
elif tipo_acceso in (3, 4):
|
||||
payload = "C1"
|
||||
elif tipo_acceso == 6:
|
||||
payload = "C0"
|
||||
else:
|
||||
payload = "C3"
|
||||
else:
|
||||
if tipo_acceso == 1:
|
||||
payload = "C6"
|
||||
elif tipo_acceso in (2, 7):
|
||||
payload = "C5"
|
||||
elif tipo_acceso in (3, 4):
|
||||
payload = "C1"
|
||||
elif tipo_acceso == 6:
|
||||
payload = "C0"
|
||||
else:
|
||||
payload = "C3"
|
||||
|
||||
trama_comando = formatear_comando_hardware(address, payload)
|
||||
|
||||
sql_insert = """
|
||||
INSERT INTO pdtmdl (PdtMdlID, ModuloID, Comando, Inmediato, Extra)
|
||||
VALUES ((SELECT COALESCE(MAX(PdtMdlID), 0) + 1 FROM pdtmdl), %s, %s, 1, %s)
|
||||
"""
|
||||
extra_info = str(ticket_id or 0)[:20]
|
||||
|
||||
with db_session() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(sql_insert, (target_id, trama_comando, extra_info))
|
||||
if target_id != modulo_id:
|
||||
cursor.execute(sql_insert, (modulo_id, trama_comando, extra_info))
|
||||
|
||||
latencia = (time.perf_counter() - t_start) * 1000
|
||||
|
||||
return True, {
|
||||
"modulo_id": modulo_id,
|
||||
"modulo_solicitado": modulo_id,
|
||||
"modulo_fisico_target": target_id,
|
||||
"address": address,
|
||||
"address_fisico": address,
|
||||
"tipo_acceso": tipo_acceso,
|
||||
"is_pass": is_pass,
|
||||
"payload": payload,
|
||||
"comando_trama": trama_comando,
|
||||
"latencia_ms": round(latencia, 2),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user