Create a Subscription
def create_subscription(self, publishing_interval_ms: float = 500.0) -> Subscription: ...
- Add MonitoredItem Please see Add MonitoredItem.
- See the "data_changed" field data_changed event.
Parameters
| Parameters | Default | Note |
|---|---|---|
| publishing_interval_ms | 500.0 | The minimum interval for the server to push data; the server may revise (adjust) it. |
Examples
with ua.create_subscription(publishing_interval_ms=500) as sub:
print(f"Subscription created with id={sub.subscription_id}")
print(f"Requested interval = {sub.publishing_interval_ms}ms")
sub.publishing_interval_ms returns the value passed at creation time. The actual interval after a server "revise" can only be obtained by calling sub.modify(...) (ModifySubscriptionResult.revised_publishing_interval_ms); at the same time, modify writes the revised value back to sub.publishing_interval_ms.
Multiple Subscription Channels
If an app requires both 100 ms high-frequency subscriptions (fitness data) and 5000 ms low-frequency subscriptions (ambient temperature), create 2 separate subscription branches; do not combine them into a single one:
with ua.create_subscription(100) as fast, ua.create_subscription(5000) as slow:
fast.add_node("ns=2;s=AxisPosition")
slow.add_node("ns=2;s=AmbientTemperature")
# ...
Each time it runs create_subscription, a separate subscription is DarraUa_Subscription_Create created, each with its own publishing interval configuration; changes to one do not affect the other.
OpcUaSubscription Public Attributes
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Identifiers | subscription_id | int | R | Local approximate ID = handle & 0x0FFFFFFF (not server-side SubscriptionId; cannot be passed directly to transfer_subscriptions) |
handle | int | R | Handle value; generally not used directly | |
session | OpcUaSession | R | Session | |
| Configuration | publishing_interval_ms | float | R | The publishing interval provided at creation; it is overwritten by the server-side value only after a successful modify() call |
| MonitoredItems | monitored_node_ids | List[str] | R | Snapshot of all current MI NodeIds |
len(sub) | int | R | MI Length (using Python's built-in len()) | |
| Events (List[Callable]) | data_changed | List[Callable[[DataChangeEventArgs], None]] | RW | List of data change callbacks |
Iteration Support: for mi_handle, node_id in sub: ...
OpcUaSubscription Public Methods
| Methods | Note |
|---|---|
| add_node(node_id, callback=None, attribute=Value, sampling_interval_ms=-1.0, mode=Reporting, queue_size=1, discard_oldest=True, filter=None) | Add Order MI, Returns int Handle |
| add_many(node_ids, callback=None, attribute=Value) | Add in Bulk, Returns List[Tuple[int, StatusCode]] |
| remove(mi_handle) | Removal Order MI |
| remove_by_node_id(node_id) | Press NodeId to remove all matching items MI; returns int the removal count. |
| set_publishing_enabled(enabled) | Turn on/off posts for the entire subscription, Returns StatusCode |
| modify(publishing_interval_ms, lifetime_count, ...) | Change Subscription Settings |
| set_monitoring_mode(mode, mi_handles) | Batch Cut mode, return List[StatusCode] |
| set_mode(handle, mode) | Cut a single item MI mode, return StatusCode |
| modify_monitored_items(requests) | Batch Change Sampling |
| modify_item(handle, sampling_interval_ms, queue_size=1, discard_oldest=True) | Amendment MI |
| republish(retransmit_sequence_number) | Request to resend, returned StatusCode |
| add_triggering_links(trigger_mi_id, linked_mi_ids) | SetTriggering Add Linkage (Currently Returns Constant BadNotSupported) |
| remove_triggering_links(trigger_mi_id, linked_mi_ids) | SetTriggering Unlink (Returns the current constant BadNotSupported) |
| dispose() / close() | Release, Server-side DeleteSubscription |
sub.subscription_id is a local approximation of handle & 0x0FFFFFFF, whereas transfer_subscriptions needs the SubscriptionId from the server's perspective. If you want to use transfer, you must keep track of the server-side id yourself; do not use this property to make the call.
Event
sub.data_changed: List[Callable[[DataChangeEventArgs], None]]
For more information, see data_changed event.