Metadata-Version: 2.4
Name: instro-opcua
Version: 1.0.0
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering
Classifier: Typing :: Typed
Requires-Dist: instro>=1.15.0
Requires-Dist: pydantic>=2.0
License-File: LICENSE
Summary: Synchronous OPC UA client primitives with typed attribute values
Keywords: opcua,opc-ua,industrial-automation,instrument-control
License-Expression: Apache-2.0
Requires-Python: >=3.11, <3.15
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Changelog, https://github.com/nominal-io/instro/blob/main/packages/instro-opcua/CHANGELOG.md
Project-URL: Documentation, https://instro.nominal.io
Project-URL: Homepage, https://github.com/nominal-io/instro
Project-URL: Issues, https://github.com/nominal-io/instro/issues
Project-URL: Repository, https://github.com/nominal-io/instro

# instro-opcua

Synchronous OPC UA client primitives for [instro](https://github.com/nominal-io/instro).
Python 3.11 through 3.14 are supported. The API lives under `instro.opcua`.

Use `instro.opcua` to discover endpoints and connect to a server.
Connections use anonymous authentication by default. Pass an `IdentityToken`
to authenticate with a username or certificate.
Username authentication requires password encryption through the channel or the
server's username token policy by default. Set `allow_plaintext_creds=True` to
permit a plaintext password.

Discovery and connection use `instro/opcua` under your local data directory for the certificate store.
On Linux the base directory is `$XDG_DATA_HOME` or `~/.local/share`.
On macOS it is `~/Library/Application Support`, and on Windows it is `%LOCALAPPDATA%`.
If the operating system provides no local data directory, the client uses `./pki`.
Pass `pki_dir` to `get_endpoints()` to use a different certificate store for discovery.

Install the standalone package from PyPI with `uv add instro-opcua` or `pip install instro-opcua`.
The package is also available through the `instro[opcua]` extra and is included in `instro[all]`.

```bash
pip install "instro[opcua]"
```

```python
from instro.opcua import OpcUaClient

with OpcUaClient.connect("opc.tcp://localhost:4840") as client:
    clock = client.get_node("/Objects/Server/ServerStatus/CurrentTime")
    sample = clock.value
    if sample.status.is_good:
        print(sample.value, sample.source_unix_timestamp)
    else:
        print(sample.status.name())
```

The [client guide](https://instro.nominal.io/library/protocols/opcua/) covers
authentication, navigation, reads, writes, and subscriptions. See the
[API reference](https://instro.nominal.io/ref/protocols/opcua/) for supported
types and connection settings. If the client recreates subscriptions during
reconnect, see the [known cleanup defect](https://instro.nominal.io/library/protocols/opcua/#recovery-and-shutdown).

## Sample and command a configured device

Pass publishers to `OpcUaDevice`, then call `read()` or `write()` to publish the operation.
Both methods accept a browse path from RootFolder or a Node from the device's client.
Reuse a Node to skip path translation on repeated calls.

```python
from instro.lib import FilePublisher
from instro.opcua import OpcUaDevice

publisher = FilePublisher(format="jsonl", directory="telemetry")
with OpcUaDevice("line_1", "line_1.json", publishers=[publisher]) as device:
    setpoint = device.client.get_node("/Objects/2:Setpoint")
    sample = device.read(setpoint)
    print(sample.latest)
    device.write(setpoint, 42.0)
```

The channel name uses the device name and the rooted browse path with explicit
namespace indexes. Nodes with the same rooted path share a channel. Commands add
the `.cmd` suffix. A command records intent before submission to the server,
so a rejected write can still have a command record. These methods publish
booleans, strings, floats, and integers that can be represented exactly as floats.
Use the native Node API for structured values or access to the complete OPC UA sample.

To publish background samples, add a `sampling` block alongside `connection`
and `nodes` in the device configuration:

```json
{
  "connection": {"endpoint_url": "opc.tcp://localhost:4840"},
  "nodes": {"setpoint": "/Objects/2:Setpoint"},
  "sampling": {
    "kind": "subscription",
    "sampling_interval_ms": 100
  }
}
```

Opening the device subscribes to the Value attribute of every configured node.
Pass `autostart=True` to open the connection and start the configured
subscription during construction. Call `close()` when you are done.
The publishing interval uses the sampling interval by default. A zero or negative
publishing interval requests the fastest supported publishing interval. The device
requests two queue entries per monitored item by default.
By default, the device reads nodes that haven't sent a notification during the
past second. Set `poll_interval_ms` in the sampling block to change that
period, or set it to `null` to disable fallback reads. Closing the device
finishes publishing retained samples before closing its publishers.

Development setup and checks are documented in
[CONTRIBUTING.md](https://github.com/nominal-io/instro/blob/main/CONTRIBUTING.md).

