locka99 / opcua

A client and server implementation of the OPC UA specification written in Rust
Mozilla Public License 2.0
475 stars 128 forks source link

Broken docs.rs documentation. How to do a simple one time read without subscription? #295

Open kwinz opened 8 months ago

kwinz commented 8 months ago

Hi,

https://docs.rs/opcua/0.11.0/opcua/client/index.html shows how to start a subscription, but now how to one time poll/read/write a variable. Neither does the code in https://github.com/locka99/opcua/tree/master/samples

https://docs.rs/opcua/0.11.0/opcua/client/index.html links to

Both of those links are broken. Probably the info how to read a variable was somewhere in that Session documentation?

I also tried https://docs.rs/opcua/0.10.0/opcua/client/session/struct.Session.html but that doesn't work either.

Ironically the only breadcrumb of an example how to do a one off read I found in an old closed issue https://github.com/locka99/opcua/issues/93 (although that code is outdated and doesn't compile any more with the current library) and obviously reading the source code helps a bit.

kwinz commented 8 months ago

Besides the broken link in the README.md and not finding Session documentation. I also created a question in discussions: https://github.com/locka99/opcua/discussions/296#discussion-5793457

mfwre commented 8 months ago

You can find the Session documentation here. It is indeed broken if you click the link from the opcua::client description. You can reach it following this route: client > prelude > Session.

In the Session documentation you'll find the AttributeService trait which gives you the fn read method.

To do a simple read you can then:

let mut client = Client::new(config);
let session = client.connect_to_endpoint(..);
let values = session.read(&[ReadValueId], TimestampsToReturn::Both, 0f64);

Of course this is pseudo-code. Create an array of NodeId you want to get the value from, convert it into an array of ReadValueId (it implements Into) and pass it to the read method with some other parameters.

I hope this was useful.

kwinz commented 8 months ago

Thank you @mfwre, much appreciated! I am not sure if you have time to also look at my question in the discussions https://github.com/locka99/opcua/discussions/296. Specifically how to list all available variable names/paths/ReadValueIds on a OPC UA server with this crate as client. I found the browse function in the Session documentation that you linked, but I am afraid I am unsure how to use it to query all existing variables/nodes.

mfwre commented 7 months ago

Hi, this is a more tricky answer.

The function you listed, browse, is the correct one. If you have no clue on which nodes to start browsing on simply call the method NodeId::root_node_id() to get the server's root node.

As for what parameters to use in the function (#296: I'm referring to the \\ ???? comments in your linked question) you'll find a detailed explanation in the OPC-UA specification; part 4 - section 5.8.2 and 5.8.3.

You still need to implement a "recursive" browsing yourself by repeating the browse call with different NodeIds.

I'll attach a screenshot of the page here for convenience. 20231109_13h14m48s_grim