SIENTIAPDE-1102

Refactor OPC activity to enhance data writing and confidence processing; update tests accordingly
This commit is contained in:
vitor-aignosi
2025-06-12 14:57:18 -03:00
parent e3ef4853dd
commit 781e3a43bb
5 changed files with 113 additions and 51 deletions

View File

@@ -2,9 +2,11 @@ import traceback
from logging import Logger
from datetime import datetime
from pathlib import Path
from typing import Any
from asyncua.sync import Client
from asyncua.crypto.security_policies import SecurityPolicyBasic256
from asyncua.ua import DataValue, Variant, VariantType
from regex import F
from sientia_do.notifications.handlers import NotificationHandler
from sientia_do.notifications.models import NotificationLevel
@@ -190,7 +192,7 @@ class OpcRepository():
return True
def write_data(self, node, value, data_type):
def write_data(self, node: str, value: Any, data_type: str) -> bool:
"""
Writes data to the OPC server.
If the connection is not established, it attempts to reconnect.
@@ -202,7 +204,7 @@ class OpcRepository():
If the client is connected, it returns True.
"""
if not self.validate_connection():
return
return False
try:
node = self.client.get_node(node)
except Exception as e:
@@ -216,7 +218,7 @@ class OpcRepository():
)
self.logger.error(trace)
self.error_count += 1
return
return False
if data_type not in data_type_map:
self.notification_handler.build_and_send_notification(
@@ -225,7 +227,7 @@ class OpcRepository():
block="opc_repository",
level=NotificationLevel.ERROR
)
return
return False
data = data_type_map[data_type]['converter'](value)
self.logger.info(f'Writing {data} - {type(data)} to {node}')
@@ -245,5 +247,7 @@ class OpcRepository():
)
self.logger.error(trace)
self.error_count += 1
return
return False
self.error_count = 0
return True