@pytest_asyncio.fixture
async def opc_client(request):
"""
Фикстура OPC UA клиента для чтения выходных сигналов СОУ.
Подключается к серверу, указанному в OPC_URL (opc.tcp://host:port).
"""
from clients.opc_ua_client import OpcUaClient
stand_manager = request.config.group_state.get("stand_manager")
opc_url = stand_manager.opc_tunnel_url if stand_manager else None
if not opc_url:
opc_url = os.environ.get(EnvKeyConstants.OPC_URL)
if not opc_url:
pytest.fail("OPC_URL не задан в переменных окружения")
client = OpcUaClient(opc_url)
try:
await client.connect()
except Exception as error:
pytest.fail(f"Не удалось подключиться к OPC UA серверу ({opc_url}): {error}")
try:
yield client
finally:
try:
await client.disconnect()
except Exception as error:
logger.warning("[OPC] [WARNING] Ошибка при отключении OPC UA клиента: %s", error)