SIENTIAPDE-1646

SIENTIAPDE-1646 Update dependencies and refactor OPC integration

- Replaced `opcua` with `asyncua` in `requirements-local.txt` and `requirements.txt` to utilize the async capabilities.
- Updated `values.yaml` to change the GitHub branch from `feature/SIENTIAPDE-1646` to `release/SIENTIAPDE-1646`.
- Refactored `opc.py` and `opc_repository.py` to accommodate the new `asyncua` library, ensuring compatibility with the synchronous API.
- Adjusted tests in `test_opc_repository.py` to reflect changes in the client implementation and maintain functionality.
This commit is contained in:
vitor-aignosi
2026-05-11 12:29:39 -03:00
parent 8d34228d7b
commit e22b0bc7c3
6 changed files with 26 additions and 21 deletions

View File

@@ -1,8 +1,9 @@
"""
Synchronous OPC UA client repository using python-opcua (opcua package).
Synchronous OPC UA client repository using asyncua (opcua-asyncio) ``sync`` API.
Connects to OPC UA servers, optionally configures Basic256 security, validates sessions,
and writes node values with typed variants and Prometheus-compatible metrics.
``asyncua.sync.Client`` runs the asyncio client on a background thread so callers
stay synchronous. Connect/disconnect, optional Basic256 security, session checks,
and typed writes mirror the previous python-opcua integration.
"""
import json
@@ -12,8 +13,9 @@ from datetime import datetime
from pathlib import Path
from typing import Any
from opcua import Client, ua
from opcua.crypto import security_policies
from asyncua import ua
from asyncua.crypto import security_policies
from asyncua.sync import Client
from sientia_do.notifications.handlers import CoreNotificationHandler as NotificationHandler
from sientia_do.notifications.models import NotificationLevel
from sientia_do.observability.logger import Logger
@@ -54,7 +56,7 @@ class OpcRepository(SientiaMonitoring):
url: OPC UA endpoint URL.
id: Server identifier used in metrics and notifications.
server_name: Human-readable server name for labels.
client: Active opcua.Client instance while connected.
client: Active ``asyncua.sync.Client`` while connected.
"""
def __init__(
@@ -115,16 +117,17 @@ class OpcRepository(SientiaMonitoring):
if self.client is None:
raise ValueError('Client must be initialized before setting security')
self.client.application_uri = self.server_uri
self.client.application_uri = self.server_uri or self.client.application_uri
self.logger.custom_info('Setting security...', self.metadata)
self.client.set_security(
security_policies.SecurityPolicyBasic256,
str(cert),
str(private_key),
None,
str(server_cert) if server_cert else None,
)
self.client.secure_channel_timeout = 10000000
self.client.session_timeout = 10000000
self.client.aio_obj.secure_channel_timeout = 10000000
self.client.aio_obj.session_timeout = 10000000
def connect(self) -> tuple[bool, dict[str, Any]]:
"""
@@ -136,11 +139,11 @@ class OpcRepository(SientiaMonitoring):
self.client = Client(self.url, timeout=10)
self.client.name = self.pod_id
self.client.application_name = self.pod_id
self.client.aio_obj.name = self.pod_id
self.client.aio_obj.description = self.pod_id
pod_uri = self.pod_id.replace('-', ':')
self.client.application_uri = pod_uri
self.client.product_uri = pod_uri
self.client.aio_obj.product_uri = pod_uri
if self.cert_path:
self.set_security()
@@ -336,7 +339,7 @@ class OpcRepository(SientiaMonitoring):
Write a typed value to an OPC UA node after validating connectivity.
Args:
node: Node id string accepted by opcua Client.get_node.
node: Node id string accepted by ``Client.get_node``.
value: Scalar value to encode.
data_type: Key into ``data_type_map`` (e.g. float, str).
logger: Caller logger for per-write traces.