SIENTIAPDE-1169

Implement server validation and output tag management in OPC class

- Added a new method to validate the existence of OPC servers before writing data, improving error handling.
- Introduced a method to manage writing of prediction and confidence tags, streamlining the data writing process.
- Refactored the write_opc_data method to utilize the new validation and management methods for better code organization and clarity.
This commit is contained in:
vitor-aignosi
2025-08-19 09:07:05 -03:00
parent fc6c95c264
commit 3ee076214c

View File

@@ -99,34 +99,7 @@ class OPC(BaseActivity):
) )
raise e raise e
@activity.defn(name='write_opc_data') def validate_server(self, server_id: str, metadata: dict[str, Any]) -> bool:
async def write_opc_data(self, input_data: dict[str, Any]) -> dict[Any, Any]:
"""
Write prediction and confidence data to OPC servers. The two writing
operations are optional and independent of each other.
Args:
- input_data(dict[str, Any]): The input data. Contains the following keys:
- data(dict[str, Any]): The dataframe that contains the data to write
to the OPC servers.
- opc_output_config(dict[str, Any]): The OPC writing configuration.
The keys are the OPC server names and the values contain:
- prediction_tags(dict[str, Any]): The tags to write to the OPC servers.
- confidence_tags(dict[str, Any]): The tags to write to the OPC servers.
Returns:
- dict[Any, Any]: The data that was written to the OPC servers.
"""
metadata = input_data['metadata']
self.debug("Writing data to OPC servers...", metadata)
data = DataFrame(input_data['data'])
opc_output_config = input_data['opc_output_config']
self.debug(data, metadata)
success = True
for server_id, config in opc_output_config.items():
if self.opc_repository.get(server_id) is None: if self.opc_repository.get(server_id) is None:
message = f"OPC server {server_id} not found to perform write operation." message = f"OPC server {server_id} not found to perform write operation."
self.send_notification( self.send_notification(
@@ -137,8 +110,12 @@ class OPC(BaseActivity):
level=NotificationLevel.ERROR, level=NotificationLevel.ERROR,
attachment_content=f"OPC servers: {list(self.opc_repository.keys())}" attachment_content=f"OPC servers: {list(self.opc_repository.keys())}"
) )
success = False return False
return True
def manage_output_tags(
self, server_id: str, config: dict[str, Any], data: DataFrame,
metadata: dict[str, Any], success: bool) -> bool:
if 'prediction_tags' in config: if 'prediction_tags' in config:
for tag, tag_config in config['prediction_tags'].items(): for tag, tag_config in config['prediction_tags'].items():
local_success = self.write_data( local_success = self.write_data(
@@ -169,6 +146,44 @@ class OPC(BaseActivity):
f"Confidence data written to OPC server {server_id} for tag {tag}.", metadata) f"Confidence data written to OPC server {server_id} for tag {tag}.", metadata)
success = success and local_success success = success and local_success
return success
@activity.defn(name='write_opc_data')
async def write_opc_data(self, input_data: dict[str, Any]) -> dict[Any, Any]:
"""
Write prediction and confidence data to OPC servers. The two writing
operations are optional and independent of each other.
Args:
- input_data(dict[str, Any]): The input data. Contains the following keys:
- data(dict[str, Any]): The dataframe that contains the data to write
to the OPC servers.
- opc_output_config(dict[str, Any]): The OPC writing configuration.
The keys are the OPC server names and the values contain:
- prediction_tags(dict[str, Any]): The tags to write to the OPC servers.
- confidence_tags(dict[str, Any]): The tags to write to the OPC servers.
Returns:
- dict[Any, Any]: The data that was written to the OPC servers.
"""
metadata = input_data['metadata']
self.debug("Writing data to OPC servers...", metadata)
data = DataFrame(input_data['data'])
opc_output_config = input_data['opc_output_config']
self.debug(data, metadata)
success = True
for server_id, config in opc_output_config.items():
if not self.validate_server(server_id, metadata):
success = False
continue
success = success and self.manage_output_tags(
server_id, config, data, metadata, success)
return self.process_confidence(data, success, metadata) return self.process_confidence(data, success, metadata)
def process_confidence(self, data: DataFrame, success: bool, metadata: dict[str, Any]) -> dict[Any, Any]: def process_confidence(self, data: DataFrame, success: bool, metadata: dict[str, Any]) -> dict[Any, Any]: