SIENTIAPDE-1110

Refactor OPC activity and OpcRepository to include metadata parameter in write_data methods, improving error handling and logging capabilities for better traceability.
This commit is contained in:
vitor-aignosi
2025-06-27 11:05:47 -03:00
parent f692e23a28
commit 119decac16
2 changed files with 14 additions and 12 deletions

View File

@@ -10,6 +10,8 @@ from regex import F
from sientia_do.notifications.handlers import NotificationHandler
from sientia_do.notifications.models import NotificationLevel
from laborious.activities.opc import OPC
data_type_map = {
'float': {
'converter': float,
@@ -192,7 +194,8 @@ class OpcRepository():
return True
def write_data(self, node: str, value: Any, data_type: str) -> bool:
def write_data(self, node: str, value: Any, data_type: str,
logger: OPC, metadata: dict[str, Any]) -> bool:
"""
Writes data to the OPC server.
If the connection is not established, it attempts to reconnect.
@@ -216,7 +219,7 @@ class OpcRepository():
level=NotificationLevel.ERROR,
attachment_content=trace
)
self.logger.error(trace)
logger.error(trace, metadata)
self.error_count += 1
return False
@@ -227,10 +230,11 @@ class OpcRepository():
block="opc_repository",
level=NotificationLevel.ERROR
)
logger.error(f"Unsupported data type: {data_type}", metadata)
return False
data = data_type_map[data_type]['converter'](value)
self.logger.info(f'Writing {data} - {type(data)} to {node}')
logger.info(f'Writing {data} - {type(data)} to {node}', metadata)
ua_data = DataValue(
Variant(data, data_type_map[data_type]['opc_type']))
@@ -245,7 +249,7 @@ class OpcRepository():
level=NotificationLevel.ERROR,
attachment_content=trace
)
self.logger.error(trace)
logger.error(trace, metadata)
self.error_count += 1
return False
self.error_count = 0