# OPC UA device Use `OpcUaDevice` to manage a configured OPC UA connection and check that the server can resolve the browse paths your application expects. Save the configuration as JSON, pass a dict, or construct an `OpcUaDeviceConfig` in Python. ```json { "connection": { "endpoint_url": "opc.tcp://localhost:4840", "request_timeout_s": 5 }, "nodes": { "setpoint": "/Objects/2:Setpoint" } } ``` Save the configuration as `line_1.json`, then open the device to read the setpoint. ```python from instro.opcua import OpcUaDevice with OpcUaDevice("line_1", "line_1.json") as device: measurement = device.read("/Objects/2:Setpoint") print(measurement.channel_data) ``` By default, construction validates the configuration without connecting. Opening the device connects and resolves browse paths from RootFolder in configuration order. The client splits translation requests to fit its decoded array limit. If the server returns `BadTooManyOperations`, configure fewer nodes on the device. The names in `nodes` identify config entries. Use the browse path on `device.client` to get a node handle. The `client` property is available while the device is open. Use it to browse, read raw samples, or manage subscriptions directly. If path resolution fails, opening the device disconnects the client and reports the error. The `nodes` field accepts string paths. A path such as `/Objects/12:Setpoint` can pass validation with `12:Setpoint` interpreted as a name in namespace 0. For namespace indexes of 10 or higher, omit that entry from `nodes` and pass a [structured path](/library/protocols/opcua.md#construct-paths-with-multi-digit-namespace-indexes) to `device.client.get_node()` after opening the device. `device.read(path)` reads Value, publishes a measurement, and returns it. It raises `ValueError` if the sample status is non-Good. `device.write(path, value)` publishes a command before it writes Value. Pass your configured [publishers](/library/publishers.md) to the device constructor. ```python from instro.lib import FilePublisher from instro.opcua import OpcUaDevice publisher = FilePublisher(directory="telemetry", format="jsonl") with OpcUaDevice("line_1", "line_1.json", publishers=[publisher]) as device: measurement = device.read("/Objects/2:Setpoint") device.write("/Objects/2:Setpoint", 42.0) ``` Nodes with the same rooted browse path share a publication channel even if their NodeIds differ. ## Subscription settings Add a `sampling` object alongside `connection` and `nodes` to publish samples from the Value attribute of all configured nodes. Opening the device starts the subscription. Pass `autostart=True` to open and subscribe during construction. If background publication stops unexpectedly, call `open()` again to reconnect and resume sampling with the same publishers. ```json { "sampling": { "kind": "subscription", "sampling_interval_ms": 100 } } ``` `publishing_interval_ms` defaults to the sampling interval. Zero or negative values request the fastest supported publishing interval. The device requests `queue_size=2` per monitored item. The server can grant a smaller queue. `poll_interval_ms` defaults to 1000 ms for nodes with no recent notification. Set it to `null` to disable fallback reads. ## Connection settings The default connection uses anonymous authentication. To authenticate with a username, set `authentication` to an object with `mode` set to `username` and supply `username` and `password_env`. The device reads that environment variable when it opens. Username authentication requires `security_mode` set to `SignAndEncrypt` and a matching `security_policy`, such as `Basic256Sha256`. Use `pki_dir` for the application's PKI directory. When specifying an application certificate, provide both `certificate_path` and `private_key_path`. Relative certificate and key paths resolve under `pki_dir`. For a secure connection, [trust the server certificate](/library/protocols/opcua.md#trust-the-server-certificate) and configure the server to trust the application certificate. Use `urn:nominal:opcua-client` as the URI in the application certificate. Set `request_timeout_s` to a positive number of seconds to override the native request timeout. ## API reference ```{eval-rst} .. autosummary:: :toctree: . :nosignatures: ~instro.opcua.OpcUaDevice ~instro.opcua.OpcUaDeviceConfig ~instro.opcua.OpcUaConnectionConfig ~instro.opcua.OpcUaAnonymousAuth ~instro.opcua.OpcUaUsernameAuth ```