Skip to main content

Crate opcua_tag_browser

Crate opcua_tag_browser 

Source
Expand description

An OPC UA client for PLC data.

Point a Collector at a PLC and it discovers every tag, subscribes to as many as the server will monitor, polls the rest, reconnects when the link drops, and streams every change to a sink you choose. TagClient reads and writes individual values on demand.

use opcua_tag_browser::{Collector, TagChange};

Collector::new("line1", "opc.tcp://192.168.201.2:4840")
    .insecure()
    .sink(|e: &TagChange<'_>| println!("{} = {}", e.path(), e.value))
    .run()

§Selecting what to collect

A PLC may expose thousands of nodes, and servers cap how many can be monitored at once. Selecting fewer than the ceiling means everything arrives by subscription and polling never engages.

c().only(["Machine/Axis1/Speed", "Machine/Axis1/Position"]);
c().matching("Machine/Axis*/Speed");
c().select(|tag| tag.path.starts_with("Machine/") && !tag.path.contains("Diag"));

§Writing values

use opcua_tag_browser::Collector;

let plc = Collector::new("line1", "opc.tcp://192.168.201.2:4840")
    .insecure()
    .client()?;

plc.set("Machine/Axis1/Setpoint", 1500.0)?;
plc.set("Machine/Enable", true)?;

println!("{}", plc.get("Machine/Axis1/Speed")?);

TagClient is Clone and Send, so it can be moved into another thread while a collector’s event loop runs.

§Security

ConnectOptions::default signs and encrypts with Basic256Sha256 and rejects untrusted server certificates. That fails against a server configured for plaintext, which is deliberate: calling Collector::insecure records the decision in your source instead of inheriting it from a default.

use opcua_tag_browser::{ConnectOptions, Security};
use opcua_tag_browser::opcua::client::prelude::SecurityPolicy;

let secure = ConnectOptions::default()
    .security(Security::SignAndEncrypt(SecurityPolicy::Basic256Sha256))
    .user_name("collector", "hunter2");

Sending a username over an unencrypted channel returns Error::InsecureCredentials rather than transmitting it, and Credentials redacts passwords in Debug output so they cannot reach a log by accident.

§Sinks

TagSink is the seam between the crate and your application; the crate decides nothing about where data goes. A closure implements it, and sinks::LogSink and sinks::JsonlSink ship ready to use.

use opcua_tag_browser::{TagChange, TagSink};

struct Threshold;

impl TagSink for Threshold {
    fn tag_changed(&self, event: &TagChange<'_>) {
        if event.as_f64().is_some_and(|v| v > 100.0) {
            println!("{} is high: {}", event.path(), event.value);
        }
    }
}

§Shutting down

The crate never calls process::exit. In a binary, Collector::handle_ctrl_c wires up a handler; inside a larger application, take a CollectorHandle and stop it yourself.

Closing sessions on exit matters more than it looks. A server does not learn an abandoned session is gone until it times out, and until then its monitored items still count against the budget.

§Lower-level pieces

Collector composes parts that stay public. Reach for them when you want the address space but not the monitoring:

TypeRole
connectopen a session
PlcSessionsession operations, with opcua panics contained
NodeBrowserlist a node’s children, pagination handled
NodeFilterreject server furniture
TreeScannerwalk the tree into PlcTag values
TagSetindex tags for lookup by path, name, or glob
TagRepositorycache the result

§Feature flags

FeatureDefaultEffect
monitoringyesCollector, subscriptions, polling
writeyesTagClient, Collector::client, PlcSession::write, OpcUaSession::inner: everything that can change a PLC
json-cacheyesJsonFileTagRepository and tag caching
jsonl-sinknosinks::JsonlSink and Collector::jsonl
ctrl-cnoCollector::handle_ctrl_c
fullnoall of the above

§Version compatibility

This crate re-exports the opcua crate it was built against. Use that path rather than your own opcua dependency, or a version mismatch produces type errors that look unrelated to their cause.

opcua-tag-browseropcua
0.20.12
0.10.12

§Minimum supported Rust version

1.75. Raising it is a breaking change.

Re-exports§

pub use opcua;

Modules§

sinks
Ready-made TagSink implementations.

Structs§

AcceptAll
Keeps every node.
BrowsedNode
A child node discovered while browsing.
ClientCertificate
This client’s own certificate, as the server sees it.
Collector
Connects to one PLC, discovers its tags, and streams every change to a sink.
CollectorHandle
Stops a running Collector and closes its sessions.
ConnectOptions
Options for opening a client session.
ConnectionChange
A connection state transition.
ConnectionInfo
What a session was opened with, for showing next to the server’s own diagnostics.
DefaultNodeFilter
Drops nodes that are server furniture rather than process data.
JsonFileTagRepository
A TagRepository backed by a pretty-printed JSON file.
MonitorOptions
Subscription and polling tuning.
OpcUaNodeBrowser
A NodeBrowser backed by a live session.
OpcUaSession
A PlcSession backed by a live opcua client session.
PlcTag
A single variable node discovered on an OPC UA server.
ScanOptions
Tuning knobs for a scan.
ScanProgress
Where a running scan is, for showing it while it runs.
ScanReport
The outcome of a scan, including the subtrees that could not be read.
TagChange
A tag whose value or quality changed.
TagClient
Reads and writes individual tag values on demand.
TagSet
A scanned tag list, indexed for lookup by browse path and display name.
TreeScanner
Recursively walks an address space, collecting variable nodes as tags.

Enums§

Credentials
How the client identifies itself to the server.
Error
Errors produced while connecting to, browsing, monitoring, or writing to an OPC UA server.
Security
Message security applied to a session.
Source
Which mechanism produced a value.

Traits§

NodeBrowser
Lists the children of a node.
NodeFilter
Decides whether a node should be kept during a scan.
PlcSession
The OPC UA session operations this crate needs.
TagRepository
Stores and retrieves a cached tag list.
TagSink
Receives everything a Collector observes.

Functions§

catch_panic
Runs a closure, converting any panic into StatusCode::BadUnexpectedError.
client_certificate
Reads the client certificate kept in pki_dir, if one was made already.
connect
Connects to an endpoint using the supplied options.
discover
Scans an endpoint and returns its tags.
format_quality
Renders the status code attached to a DataValue as a short quality label.
format_variant
Formats an OPC UA Variant into a compact display string.
panic_message
Extracts a readable message from a panic payload.
variant_as_bool
Interprets a Variant as a boolean, if it holds one.
variant_as_f64
Interprets a Variant as a float, if it holds a number.
variant_as_i64
Interprets a Variant as a signed integer, if it holds an integral value.

Type Aliases§

Result
Convenience alias for results returned by this crate.