Skip to main content

Session — OpcUaSession

OpcUaSession This is the main Client class; one instance corresponds to one OPC UA Session. All operations (read / write / browse / subscribe / call / history) originate from this object.

DarraOpcUa It is a compatible alias OpcUaSession for (DarraOpcUa = OpcUaSession), and the two are equivalent.

Subpage Redirect
  • For structural and connection specifications, please refer to Constructor.
  • For connecting/disconnecting, please refer to Connection Management.
  • For single-item and batch read/write operations, please refer to read / write.
  • For the unified channel for internal events, please refer to Events.
  • For namespace lookup, please refer to Namespaces.
  • For information on heartbeat and session keep-alive, please refer to KeepAlive.

Public Attributes​

CategoryPropertyTypeAccessDescription
Connectionsendpoint_urlstrRThe endpoint URL specified at construction
security_modeMessageSecurityModeRSecurity Mode (NONE / Sign / SignAndEncrypt)
StatusstateSessionStateRCurrent session status (Disconnected / Connecting / Connected / Reconnecting / Closing / Failed)
is_connectedboolRWhether the user is connected (= state == SessionState.Connected)
handleintRC-layer native session handle (internal)
Child ObjectsnodesNodeCollectionRLazy Load Node Accessor
namespacesNamespaceCollectionRNamespace URI ↔ Index Conversion
EventseventsOpcUaEventsRCollection of session-level events: state_changed / connected / disconnected / reconnecting / reconnected / keep_alive / communication_error / subscription_lost / node_removed / node_access_denied / node_type_changed / session_lost / server_state_changed / subscription_restored / any (each is a List[Callable], registered using .append)
Configurationkeep_alive_interval_msintRWKeepAlive heartbeat interval (default 10000 ms, <= 0 = disabled)
auto_publishboolRWEnables or disables the background publish thread (default value: False; the constructor parameter auto_publish_on_connect defaults to True and is automatically set to True in `connect()`)

Public Methods​

Connection Management​

  • connect() — Establish a connection (Hello → SecureChannel → Session → Activate)
  • disconnect() — Connection terminated by the user; the instance can reconnect
  • dispose() / close() — Stop the heart + Close the native session + Release stack references (with automatically called upon exit dispose)

Heartbeat and Release​

  • publish(timeout_ms = 2000) — Manually trigger a Publish once; returns StatusCode (usually does not need to be called)

Read / Write​

Browse​

Subscribe​

Method Call​

History​

Control​

  • cancel(request_handle) — Cancels pending requests in the session and returns the actual number of requests canceled by the server (int)

Safe variant (never throws an exception)​

Advanced Search / Subintervals​

  • browse_filtered(nid, node_class_filter, direction, reference_type_id, include_subtypes) — Directional + reference type filtering (currently reduced to browse + client-side NodeClass filtering)
  • read_range(node_id, range_, attribute) / write_range(node_id, range_, value, attribute) — Subarray read/write (NumericRange); Forward when the entire segment is involved read / write, Throw an exception when the current segment is not the entire segment NotImplementedError

Historical Streams​

Server Information​

Alarms and Conditions​

  • acknowledge / confirm / add_comment / enable_condition / disable_condition / condition_refresh / condition_refresh2 / one_shot_shelve / timed_shelve / unshelve — See Alarm and Confirmation

Complete Example​

from darra_opcua import OpcUaSession, OpcUaVariant   # Module Name darra_opcua
import time

with OpcUaSession("opc.tcp://localhost:4840") as ua:
ua.events.connected.append(
lambda e: print(f"Connected to {e.endpoint_url}"))
ua.events.disconnected.append(
lambda e: print(f"Disconnected: {e.message}"))
ua.connect()

# Read
with ua.read("ns=2;s=Temperature") as dv:
print(f"T = {dv.value} °C")

# Write
st = ua.write("ns=2;s=Setpoint", OpcUaVariant().set_double(42.0))
print(f"Write status = {st.name}")

# Subscribe
with ua.create_subscription(500) as sub:
sub.data_changed.append(
lambda e: print(f"{e.node_id} = {e.value_string} ({e.status.name})"))
sub.add_node("ns=2;s=Counter")

# Methods
outputs = ua.call(
"ns=2;s=Calc", "ns=2;s=Calc.Add",
[OpcUaVariant().set_int32(3), OpcUaVariant().set_int32(4)])
for v in outputs:
with v:
print(f"sum = {v.as_int32}")

time.sleep(60)

Next Step​