SIENTIAPDE-988 Implement core functionality for OPC ingestor, including main execution flow, IngestorManager methods, and OPC server management. Add unit tests for IngestorManager and OPCManager.
This commit is contained in:
@@ -24,9 +24,23 @@ class OpcManager():
|
||||
self.private_key_path = private_key_path
|
||||
self.server_cert_path = server_cert_path
|
||||
self.nodes = {}
|
||||
self.subscription = None
|
||||
self.subscriptions = {}
|
||||
self.data_manager = data_manager
|
||||
|
||||
def initialize_from_config(self, server_config: dict, data_manager: DataManager, logger: Logger):
|
||||
"""
|
||||
Initializes the OpcManager instance using a server configuration dictionary.
|
||||
Args:
|
||||
server_config (dict): A dictionary containing server configuration details.
|
||||
data_manager (DataManager): An instance of DataManager for data handling.
|
||||
logger (Logger): Logger instance for logging information.
|
||||
"""
|
||||
self.__init__(
|
||||
server_config['name'], server_config['url'], data_manager, logger, server_config['server_uri'],
|
||||
server_config.get('cert_path'), server_config.get(
|
||||
'private_key_path'), server_config.get('server_cert_path')
|
||||
)
|
||||
|
||||
def set_security(self):
|
||||
"""
|
||||
Configures the security settings for the OPC UA client.
|
||||
@@ -82,7 +96,7 @@ class OpcManager():
|
||||
self.logger.info('Starting connection...')
|
||||
self.client.connect()
|
||||
|
||||
def create_subscription(self, period: int = 500):
|
||||
def create_subscription(self, name: str, period: int = 500):
|
||||
"""
|
||||
Creates a subscription with the specified monitoring period.
|
||||
This method establishes a subscription to monitor data changes or events
|
||||
@@ -101,12 +115,11 @@ class OpcManager():
|
||||
raise ValueError("Client not connected. Call connect first.")
|
||||
|
||||
p = period if period != None else 500
|
||||
self.period = p
|
||||
self.subscription = self.client.create_subscription(
|
||||
self.subscriptions[name] = self.client.create_subscription(
|
||||
p, self)
|
||||
self.logger.info('Subscription created.')
|
||||
|
||||
def subscribe(self, nodes: dict, collect_period: int):
|
||||
def subscribe(self, subscription: str, nodes: dict, collect_period: int):
|
||||
"""
|
||||
Subscribes to a set of OPC UA nodes for data change notifications.
|
||||
This method adds the specified nodes to the subscription and configures
|
||||
@@ -123,7 +136,7 @@ class OpcManager():
|
||||
`create_subscription` prior to this method.
|
||||
"""
|
||||
|
||||
if not self.subscription:
|
||||
if not self.subscriptions.get(subscription):
|
||||
raise ValueError(
|
||||
"Subscription not created. Call create_subscription first.")
|
||||
|
||||
@@ -138,7 +151,16 @@ class OpcManager():
|
||||
'cycle_count': 0
|
||||
}
|
||||
|
||||
self.subscription.subscribe_data_change(self.addr_nodes)
|
||||
self.subscriptions[subscription].subscribe_data_change(self.addr_nodes)
|
||||
|
||||
def unsubscribe(self, subscription: str):
|
||||
|
||||
if not self.subscriptions.get(subscription):
|
||||
raise ValueError(
|
||||
"Subscription not created. Call create_subscription first.")
|
||||
self.subscriptions[subscription].delete()
|
||||
del self.subscriptions[subscription]
|
||||
self.logger.info(f"Unsubscribed from {subscription}.")
|
||||
|
||||
def __del__(self):
|
||||
self.disconnect()
|
||||
@@ -156,33 +178,13 @@ class OpcManager():
|
||||
|
||||
self.logger.warning('Disconnecting from OPC server')
|
||||
try:
|
||||
self.subscription.delete()
|
||||
[self.subscriptions[sub].delete() for sub in self.subscriptions]
|
||||
self.logger.warning("Deleted all subscriptions.")
|
||||
self.client.disconnect()
|
||||
self.logger.warning("Disconnected from OPC UA server.")
|
||||
except Exception as sub_error:
|
||||
self.logger.error(f"Failed to clean up subscription: {sub_error}")
|
||||
|
||||
def init_collector(self, nodes: dict, collect_period: int, period: int):
|
||||
"""
|
||||
Initializes the OPC data collector by connecting to the server, creating a subscription,
|
||||
and subscribing to the specified nodes.
|
||||
Args:
|
||||
nodes (dict): A dictionary where keys are node identifiers and values are the node details
|
||||
to be subscribed to.
|
||||
collect_period (int): The interval (in milliseconds) at which data should be collected
|
||||
from the subscribed nodes.
|
||||
period (int): The publishing interval (in milliseconds) for the subscription.
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
|
||||
self.connect()
|
||||
self.create_subscription(period)
|
||||
self.subscribe(nodes, collect_period)
|
||||
|
||||
self.logger.info(
|
||||
f"Connected to OPC server {self.name} at {self.url}.")
|
||||
|
||||
def datachange_notification(self, node, _val, data):
|
||||
"""
|
||||
Handles data change notifications for monitored OPC UA nodes.
|
||||
@@ -263,7 +265,7 @@ class OpcManager():
|
||||
Monitors the OPC connection and triggers events based on the number of cycles
|
||||
without receiving data from the OPC server.
|
||||
Args:
|
||||
handle_listen_events (Callable[[str, str, NotificationLevel], None]):
|
||||
handle_listen_events (Callable[[str, str, NotificationLevel], None]):
|
||||
A callback function to handle notification events. It takes three arguments:
|
||||
- A message string describing the event.
|
||||
- An event code string.
|
||||
|
||||
Reference in New Issue
Block a user