fix(hardware): replicar reglas exactas de puesto de control de escritorio para apertura de molinete club y comando T9
This commit is contained in:
@@ -43,11 +43,14 @@ class HardwareService:
|
||||
return cursor.fetchone()
|
||||
|
||||
@classmethod
|
||||
def encolar_apertura(cls, modulo_id: int, ticket_id: int = 0) -> Tuple[bool, Dict[str, Any]]:
|
||||
def encolar_apertura(
|
||||
cls, modulo_id: int, ticket_id: int = 0, sin_ticket: bool = False
|
||||
) -> Tuple[bool, Dict[str, Any]]:
|
||||
"""
|
||||
Encola un comando de apertura de barrera/molinete en la tabla `pdtmdl`
|
||||
para que OnEverWPC lo despache al bus RS-485 físico de inmediato.
|
||||
Resuelve automáticamente módulos virtuales (98, 97) hacia sus placas físicas (20, 21).
|
||||
Aplica exactamente las mismas reglas de vinculación virtual, tipo de acceso,
|
||||
firmware (PARK vs PASS) y despacho de ticket T9 que el puesto de control de escritorio.
|
||||
"""
|
||||
t_start = time.perf_counter()
|
||||
mdl = cls.obtener_info_modulo(modulo_id)
|
||||
@@ -86,11 +89,60 @@ class HardwareService:
|
||||
if fisico:
|
||||
target_mdl = fisico
|
||||
|
||||
# 3. Categorías del módulo destino físico
|
||||
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)
|
||||
duracion = target_mdl.get("duracion_pulso") or 500
|
||||
|
||||
# 3. Comando universal de apertura por pulso de barrera (O3)
|
||||
payload = "O3"
|
||||
# CategoriaID 2: 1 = PASS (Access), 5 = PARK
|
||||
is_pass = (target_cat_map.get(2, 0) == 1)
|
||||
|
||||
# CategoriaID 15: Tipo Acceso (1: Puerta, 2/7: Molinete / Molinete Club, 3: Barrera Estado, 5: Barrera Pulso, 6: Barrera Doble Pulso)
|
||||
tipo_acceso = target_cat_map.get(15) or cat_map.get(15, 3)
|
||||
|
||||
# CategoriaID 3: Tipo Identificación (1: Proximidad / Tótem Ticket)
|
||||
tipo_identificacion = cat_map.get(3, 0) or target_cat_map.get(3, 0)
|
||||
|
||||
# 4. Mapeo de comando según firmware y tipo de acceso (idéntico a pct_repository.py de OemspotPySYS)
|
||||
if is_pass:
|
||||
if tipo_acceso == 1: # Puerta
|
||||
dur_ds = max(1, duracion // 100)
|
||||
payload = f"V6{dur_ds:02d}"
|
||||
elif tipo_acceso in (2, 7): # Molinete / Molinete Club
|
||||
payload = "V5"
|
||||
elif tipo_acceso == 3: # Barrera Estado
|
||||
payload = "V9"
|
||||
elif tipo_acceso == 6: # Barrera Doble Pulso
|
||||
payload = "V8"
|
||||
else: # Barrera Pulso (5) o Controlado (4) o Default
|
||||
payload = "V3"
|
||||
else:
|
||||
# ST1660 PARK (o Default)
|
||||
if tipo_acceso == 1: # Puerta
|
||||
payload = "O6"
|
||||
elif tipo_acceso in (2, 7): # Molinete / Molinete Club
|
||||
payload = "O5"
|
||||
elif tipo_acceso == 3: # Barrera Estado
|
||||
payload = "O1"
|
||||
elif tipo_acceso == 6: # Barrera Doble Pulso
|
||||
payload = "O8"
|
||||
else: # Barrera Pulso (5) o Controlado (4) o Default
|
||||
payload = "O3"
|
||||
|
||||
trama_comando = formatear_comando_hardware(address, payload)
|
||||
|
||||
sql_insert = """
|
||||
@@ -98,17 +150,22 @@ class HardwareService:
|
||||
VALUES ((SELECT COALESCE(MAX(PdtMdlID), 0) + 1 FROM pdtmdl), %s, %s, 1, %s)
|
||||
"""
|
||||
extra_info = str(ticket_id or 0)[:20]
|
||||
trama_ticket = None
|
||||
|
||||
with db_session() as conn:
|
||||
cursor = conn.cursor()
|
||||
# Encolar para la placa física real (ej: Modulo 100009 con Addr 20 o Modulo 100013 con Addr 21)
|
||||
# Encolar para la placa física real (ej: Modulo 100009 con Addr 20)
|
||||
cursor.execute(sql_insert, (target_id, trama_comando, extra_info))
|
||||
|
||||
# Si el módulo llamado era virtual, encolar también una entrada con el ID virtual
|
||||
# pero con la trama física, garantizando compatibilidad con cualquier hilo de sondeo
|
||||
if target_id != modulo_id:
|
||||
cursor.execute(sql_insert, (modulo_id, trama_comando, extra_info))
|
||||
|
||||
# Tótem de tickets / código de barras si corresponde y no es sin_ticket (regla exacta de OemspotPySYS)
|
||||
if not sin_ticket and tipo_identificacion == 1 and ticket_id > 0:
|
||||
trama_ticket = formatear_comando_hardware(address, "T9")
|
||||
cursor.execute(sql_insert, (target_id, trama_ticket, extra_info))
|
||||
|
||||
latencia = (time.perf_counter() - t_start) * 1000
|
||||
|
||||
return True, {
|
||||
@@ -117,7 +174,10 @@ class HardwareService:
|
||||
"modulo_fisico_target": target_id,
|
||||
"address": address,
|
||||
"address_fisico": address,
|
||||
"tipo_acceso": tipo_acceso,
|
||||
"is_pass": is_pass,
|
||||
"payload": payload,
|
||||
"comando_trama": trama_comando,
|
||||
"trama_ticket": trama_ticket,
|
||||
"latencia_ms": round(latencia, 2),
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import pytest
|
||||
from app.services.hardware_service import HardwareService, formatear_comando_hardware, calcular_checksum
|
||||
|
||||
|
||||
def test_checksum_and_formatting():
|
||||
assert calcular_checksum("\x0220O5") == "18"
|
||||
assert formatear_comando_hardware(20, "O5") == "\x0220O518\x03"
|
||||
assert formatear_comando_hardware(20, "T9") == "\x0220T90F\x03"
|
||||
|
||||
|
||||
def test_encolar_apertura_virtual_module_molinete_club():
|
||||
"""
|
||||
Verifica que el Módulo Virtual 100004 (ENTRADA VISITAS):
|
||||
1. Se resuelva a la placa física 100009 (Address 20).
|
||||
2. Aplique payload O5 correspondiente a Molinete Club (tipo_acceso 7).
|
||||
3. Genere la trama de apertura \x0220O518\x03.
|
||||
4. Genere la trama T9 para impresión de ticket \x0220T90F\x03 cuando tipo_identificacion == 1.
|
||||
"""
|
||||
ok, res = HardwareService.encolar_apertura(modulo_id=100004, ticket_id=99999999)
|
||||
assert ok is True
|
||||
assert res["modulo_solicitado"] == 100004
|
||||
assert res["modulo_fisico_target"] == 100009
|
||||
assert res["address"] == 20
|
||||
assert res["tipo_acceso"] == 7
|
||||
assert res["is_pass"] is False
|
||||
assert res["payload"] == "O5"
|
||||
assert res["comando_trama"] == "\x0220O518\x03"
|
||||
assert res["trama_ticket"] == "\x0220T90F\x03"
|
||||
Reference in New Issue
Block a user