OpcUaDevice#
- class OpcUaDevice(
- name: str,
- config: OpcUaDeviceConfig | dict[str, object] | Path | str,
- publishers: list[Publisher] | None = None,
- *,
- autostart: bool = False,
Bases:
InstrumentOwn a configured OPC UA client and publish samples from a configured subscription.
Validate a config and initialize the device.
- Parameters:
Examples
>>> with OpcUaDevice("line_1", "line_1.json") as device: ... sample = device.client.get_node("/Objects/2:Setpoint").value
Attributes
Seconds between background daemon iterations (0 = no wait).
Whether the background daemon thread is running.
Return an insertion-ordered snapshot of all published channel names.
Return the client for the open device.
Methods
Append
methodto the daemon's call list.Register a publisher to receive this instrument's Measurement/Command data.
Drain subscription samples, close publishers, and disconnect the client.
Replace all daemon functions with a single
method(called with the given args).Return the most recent
lengthsamples forchannel_namefrom the in-memory buffer.Return the most recent sample for
channel_namefrom the in-memory buffer.Connect and start publishing data, if configured.
Fan
dataout to every configured publisher;kwargspass through.Read a node's Value attribute and publish one measurement.
Start the background daemon thread.
Signal the background daemon to stop and join it.
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
methodto the daemon’s call list. Usedefine_background_daemonto 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 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( ) Measurement#
Return the most recent
lengthsamples forchannel_namefrom the in-memory buffer.If the channel does not exist yet, or no sample is available, the code will always block until
timeoutexpires.- Parameters:
channel_name
Name of the channel to retrieve.
length
Number of trailing samples to return.
wait_for_new_samples
Block until at least
lengthnew 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=Trueand channel did not appear withintimeout.ChannelValueTimeoutError –
wait_for_new_samples=Trueand values did not arrive withintimeout.
- get_single_channel_value(
- channel_name: str,
Return the most recent sample for
channel_namefrom the in-memory buffer.This will not wait, if data is not available then
Noneis 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
dataout to every configured publisher;kwargspass through.
- read(
- node_or_path: str | RelativePath | Node,
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,
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:
- 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.