Subscription
OpcUaSubscription It is a subscription container that manages a set of MonitoredItem (MI) and provides data_changed events (List[Callable]).
The actual class name is Subscription; OpcUaSubscription is a reserved legacy API compatibility alias; both refer to the same class.
Subpage Redirect
- To create a subscription, please refer to Create a Subscription.
- Add / Add in Bulk MonitoredItem Please refer to Add MonitoredItem.
- To change publishing_interval, lifetime, or sampling, please refer to Modify.
- For "Disabled / Sampling / Reporting," please refer to SetMonitoringMode.
- To request a resend for a specific SeqNo, please refer to Republish.
- For information on the
data_changedfield and the threading model, please refer to data_changed event. - For dead zone filtering, please refer to DataChangeFilter; For information on selecting event fields, please refer to EventFilter.
- For OPC UA alarms and event subscriptions, see Event Subscription; for Triggering see Triggering Links.
- For information on restoring a subscription after a connection loss, please refer to Subscription Resumes Automatically.
One-Minute Overview
from darra_opcua import MonitoringMode
# Create
with ua.create_subscription(publishing_interval_ms=500) as sub:
# Data Change Callback (list[callable]; append directly when there are multiple callbacks)
sub.data_changed.append(
lambda e: print(f"{e.node_id} = {e.value_string} ({e.status.name})"))
# Add MI
h1 = sub.add_node("ns=2;s=Counter")
h2 = sub.add_node("ns=2;s=Temperature")
# Add in Bulk
nodes = [f"ns=2;s=T{i}" for i in range(1, 101)]
results = sub.add_many(nodes)
# Change the sampling interval MI for a single item
sub.modify_item(h1, sampling_interval_ms=100)
# Pause / Resume Collection
sub.set_mode(h1, MonitoringMode.Disabled)
sub.set_mode(h1, MonitoringMode.Reporting)
# Remove
sub.remove(h1)
# with automatic `dispose(DeleteSubscription)` on exit
Subscribe to Lifecycle Events
from darra_opcua import OpcUaEventCategory
def on_any(e):
if e.category == OpcUaEventCategory.SubscriptionCreated: pass
if e.category == OpcUaEventCategory.MonitoredItemAdded: pass
if e.category == OpcUaEventCategory.SubscriptionLost: pass
ua.events.any.append(on_any)
These categories go through the unified ua.events.any channel (not the subscription_lost strongly typed event channel); see Session Events。