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:
@@ -99,6 +99,55 @@ class OPC(BaseActivity):
|
|||||||
)
|
)
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
def validate_server(self, server_id: str, metadata: dict[str, Any]) -> bool:
|
||||||
|
if self.opc_repository.get(server_id) is None:
|
||||||
|
message = f"OPC server {server_id} not found to perform write operation."
|
||||||
|
self.send_notification(
|
||||||
|
metadata=metadata,
|
||||||
|
notification_id="OPC_SERVER_NOT_FOUND",
|
||||||
|
message=message,
|
||||||
|
block="write_opc_data",
|
||||||
|
level=NotificationLevel.ERROR,
|
||||||
|
attachment_content=f"OPC servers: {list(self.opc_repository.keys())}"
|
||||||
|
)
|
||||||
|
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:
|
||||||
|
for tag, tag_config in config['prediction_tags'].items():
|
||||||
|
local_success = self.write_data(
|
||||||
|
server_id=server_id,
|
||||||
|
tag=tag,
|
||||||
|
data=data.head(1)['prediction'].values[0],
|
||||||
|
data_type=tag_config['data_type'],
|
||||||
|
tag_type='prediction',
|
||||||
|
metadata=metadata
|
||||||
|
)
|
||||||
|
if local_success:
|
||||||
|
self.info(
|
||||||
|
f"Prediction data written to OPC server {server_id} for tag {tag}.", metadata)
|
||||||
|
success = success and local_success
|
||||||
|
|
||||||
|
if 'confidence_tags' in config:
|
||||||
|
for tag, tag_config in config['confidence_tags'].items():
|
||||||
|
local_success = self.write_data(
|
||||||
|
server_id=server_id,
|
||||||
|
tag=tag,
|
||||||
|
data=data.head(1)['prediction_confidence'].values[0],
|
||||||
|
data_type=tag_config['data_type'],
|
||||||
|
tag_type='confidence',
|
||||||
|
metadata=metadata
|
||||||
|
)
|
||||||
|
if local_success:
|
||||||
|
self.info(
|
||||||
|
f"Confidence data written to OPC server {server_id} for tag {tag}.", metadata)
|
||||||
|
success = success and local_success
|
||||||
|
|
||||||
|
return success
|
||||||
|
|
||||||
@activity.defn(name='write_opc_data')
|
@activity.defn(name='write_opc_data')
|
||||||
async def write_opc_data(self, input_data: dict[str, Any]) -> dict[Any, Any]:
|
async def write_opc_data(self, input_data: dict[str, Any]) -> dict[Any, Any]:
|
||||||
"""
|
"""
|
||||||
@@ -127,47 +176,13 @@ class OPC(BaseActivity):
|
|||||||
success = True
|
success = True
|
||||||
|
|
||||||
for server_id, config in opc_output_config.items():
|
for server_id, config in opc_output_config.items():
|
||||||
if self.opc_repository.get(server_id) is None:
|
|
||||||
message = f"OPC server {server_id} not found to perform write operation."
|
if not self.validate_server(server_id, metadata):
|
||||||
self.send_notification(
|
|
||||||
metadata=metadata,
|
|
||||||
notification_id="OPC_SERVER_NOT_FOUND",
|
|
||||||
message=message,
|
|
||||||
block="write_opc_data",
|
|
||||||
level=NotificationLevel.ERROR,
|
|
||||||
attachment_content=f"OPC servers: {list(self.opc_repository.keys())}"
|
|
||||||
)
|
|
||||||
success = False
|
success = False
|
||||||
|
continue
|
||||||
|
|
||||||
if 'prediction_tags' in config:
|
success = success and self.manage_output_tags(
|
||||||
for tag, tag_config in config['prediction_tags'].items():
|
server_id, config, data, metadata, success)
|
||||||
local_success = self.write_data(
|
|
||||||
server_id=server_id,
|
|
||||||
tag=tag,
|
|
||||||
data=data.head(1)['prediction'].values[0],
|
|
||||||
data_type=tag_config['data_type'],
|
|
||||||
tag_type='prediction',
|
|
||||||
metadata=metadata
|
|
||||||
)
|
|
||||||
if local_success:
|
|
||||||
self.info(
|
|
||||||
f"Prediction data written to OPC server {server_id} for tag {tag}.", metadata)
|
|
||||||
success = success and local_success
|
|
||||||
|
|
||||||
if 'confidence_tags' in config:
|
|
||||||
for tag, tag_config in config['confidence_tags'].items():
|
|
||||||
local_success = self.write_data(
|
|
||||||
server_id=server_id,
|
|
||||||
tag=tag,
|
|
||||||
data=data.head(1)['prediction_confidence'].values[0],
|
|
||||||
data_type=tag_config['data_type'],
|
|
||||||
tag_type='confidence',
|
|
||||||
metadata=metadata
|
|
||||||
)
|
|
||||||
if local_success:
|
|
||||||
self.info(
|
|
||||||
f"Confidence data written to OPC server {server_id} for tag {tag}.", metadata)
|
|
||||||
success = success and local_success
|
|
||||||
|
|
||||||
return self.process_confidence(data, success, metadata)
|
return self.process_confidence(data, success, metadata)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user