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:
@@ -23,7 +23,6 @@ class OPC(BaseActivity):
|
||||
self.opc_servers = opc_servers
|
||||
|
||||
self.opc_repository = {}
|
||||
self.logger.debug(f"OPC servers: {opc_servers}")
|
||||
for id, server in opc_servers.items():
|
||||
self.opc_repository[id] = OpcRepository(
|
||||
id=server['id'],
|
||||
@@ -41,7 +40,7 @@ class OPC(BaseActivity):
|
||||
BaseActivity.__init__(self, logger, notification_handler)
|
||||
|
||||
def write_data(self, server_id: str, tag: str, data: Any,
|
||||
data_type: str, tag_type: str) -> bool:
|
||||
data_type: str, tag_type: str, metadata: dict[str, Any]) -> bool:
|
||||
"""
|
||||
Write data to OPC server.
|
||||
|
||||
@@ -58,8 +57,7 @@ class OPC(BaseActivity):
|
||||
|
||||
try:
|
||||
return self.opc_repository[server_id].write_data(
|
||||
tag, data, data_type)
|
||||
self.logger.debug(f"Wrote {tag_type} to {tag}")
|
||||
tag, data, data_type, self, metadata)
|
||||
except Exception as e:
|
||||
trace = traceback.format_exc()
|
||||
self.notification_handler.build_and_send_notification(
|
||||
@@ -98,8 +96,6 @@ class OPC(BaseActivity):
|
||||
|
||||
success = True
|
||||
|
||||
self.logger.debug(f"OPC output config: {opc_output_config}")
|
||||
self.logger.debug(f"OPC servers: {self.opc_repository}")
|
||||
for server_id, config in opc_output_config.items():
|
||||
if self.opc_repository.get(server_id) is None:
|
||||
self.error(f"OPC server {server_id} not found", metadata)
|
||||
@@ -112,7 +108,8 @@ class OPC(BaseActivity):
|
||||
tag=tag,
|
||||
data=data.head(1)['prediction'].values[0],
|
||||
data_type=tag_config['data_type'],
|
||||
tag_type='prediction'
|
||||
tag_type='prediction',
|
||||
metadata=metadata
|
||||
)
|
||||
|
||||
if 'confidence_tags' in config:
|
||||
@@ -122,7 +119,8 @@ class OPC(BaseActivity):
|
||||
tag=tag,
|
||||
data=data.head(1)['prediction_confidence'].values[0],
|
||||
data_type=tag_config['data_type'],
|
||||
tag_type='confidence'
|
||||
tag_type='confidence',
|
||||
metadata=metadata
|
||||
)
|
||||
|
||||
return self.process_confidence(data, success, metadata)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user