Skip to main content

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

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。

Next Step​