OpcUaDevice#

class OpcUaDevice(
name: str,
config: OpcUaDeviceConfig | dict[str, object] | Path | str,
publishers: list[Publisher] | None = None,
*,
autostart: bool = False,
)[source]#

Bases: Instrument

Own a configured OPC UA client and publish samples from a configured subscription.

Validate a config and initialize the device.

Parameters:
  • name (str)

    Instrument name.

  • config (OpcUaDeviceConfig | dict[str, object] | Path | str)

    Config model, mapping, or JSON file path.

  • publishers (list[Publisher] | None)

    Publishers owned by the instrument.

  • autostart (bool)

    Open the connection and start any configured subscription during construction.

Examples

>>> with OpcUaDevice("line_1", "line_1.json") as device:
...     sample = device.client.get_node("/Objects/2:Setpoint").value

Attributes

background_interval

Seconds between background daemon iterations (0 = no wait).

background_running

Whether the background daemon thread is running.

channel_names

Return an insertion-ordered snapshot of all published channel names.

client

Return the client for the open device.

Methods

add_background_daemon_function

Append method to the daemon's call list.

add_publisher

Register a publisher to receive this instrument's Measurement/Command data.

close

Drain subscription samples, close publishers, and disconnect the client.

define_background_daemon

Replace all daemon functions with a single method (called with the given args).

get_channel

Return the most recent length samples for channel_name from the in-memory buffer.

get_single_channel_value

Return the most recent sample for channel_name from the in-memory buffer.

open

Connect and start publishing data, if configured.

publish

Fan data out to every configured publisher; kwargs pass through.

read

Read a node's Value attribute and publish one measurement.

start

Start the background daemon thread.

stop

Signal the background daemon to stop and join it.

write

Publish a command and write the node's Value attribute.

property client: OpcUaClient#

Return the client for the open device.

Raises:

RuntimeError – The device has not opened or has closed.

open() → None[source]#

Connect and start publishing data, if configured.

Browse paths are resolved in configuration order, with translation requests split as needed to fit the client’s decoded array limit.

add_background_daemon_function(
method: Callable,
*args,
**kwargs,
)#

Append method to the daemon’s call list. Use define_background_daemon to replace instead.

add_publisher(
publisher: Publisher,
)#

Register a publisher to receive this instrument’s Measurement/Command data.

property background_interval#

Seconds between background daemon iterations (0 = no wait).

property background_running: bool#

Whether the background daemon thread is running.

property channel_names: tuple[str, ...]#

Return an insertion-ordered snapshot of all published channel names.

Names are complete after one full daemon iteration: {name}.loop_time publishes every iteration, so call get_channel(“loop_time”, wait_for_new_samples=True) then read channel_names to ensure the complete set has arrived.

Raises:

RuntimeError – No background buffer; start() was not called.

define_background_daemon(
method: Callable,
*args,
**kwargs,
)#

Replace all daemon functions with a single method (called with the given args).

get_channel(
channel_name: str,
length: int = 1,
wait_for_new_samples: bool = False,
timeout: float = 10.0,
) → Measurement#

Return the most recent length samples for channel_name from the in-memory buffer.

If the channel does not exist yet, or no sample is available, the code will always block until timeout expires.

Parameters:
  • channel_name

    Name of the channel to retrieve.

  • length

    Number of trailing samples to return.

  • wait_for_new_samples

    Block until at least length new values arrive.

  • timeout

    Seconds to wait when insufficient data exists or wait_for_new_samples=True.

Raises:
  • RuntimeError – No background buffer; start() was not called.

  • ChannelNotFoundError – channel had no values and no data appeared before timeout. wait_for_new_samples=True and channel did not appear within timeout.

  • ChannelValueTimeoutError – wait_for_new_samples=True and values did not arrive within timeout.

get_single_channel_value(
channel_name: str,
) → float | str | None#

Return the most recent sample for channel_name from the in-memory buffer.

This will not wait, if data is not available then None is returned.

Parameters:

channel_name

Name of the channel to retrieve.

Raises:

RuntimeError – No background buffer; start() was not called.

publish(
data: Measurement | Command,
**kwargs,
)#

Fan data out to every configured publisher; kwargs pass through.

read(
node_or_path: str | RelativePath | Node,
) → Measurement[source]#

Read a node’s Value attribute and publish one measurement.

Use the source timestamp when available, then the server timestamp, then the local receipt time.

Parameters:

node_or_path (str | RelativePath | Node)

Rooted browse path or node from this client.

Returns:

Measurement – The published sample with a Unix timestamp in nanoseconds.

Raises:
  • ValueError – The node belongs to another client, the sample has non-Good quality, or the integer value would lose precision.

  • TypeError – The sample value cannot be published as a scalar.

start()#

Start the background daemon thread. No-op if already running.

stop()#

Signal the background daemon to stop and join it. No-op if not running.

write(
node_or_path: str | RelativePath | Node,
value: bool | int | float | str,
*,
dtype: DataType | None = None,
) → None[source]#

Publish a command and write the node’s Value attribute.

Publication records intent before submission. A rejected write still raises, and a publisher error prevents submission.

Parameters:
  • node_or_path (str | RelativePath | Node)

    Rooted browse path or node from this client.

  • value (bool | int | float | str)

    Scalar value to write and publish.

  • dtype (DataType | None)

    Wire type override for the write.

Raises:
  • ValueError – The node belongs to another client or the integer value would lose precision.

  • TypeError – The value cannot be published as a scalar.

  • OpcUaError – The server rejects the write or the session fails.

close() → None[source]#

Drain subscription samples, close publishers, and disconnect the client.