Skip to content

AutoID client: read a device's status without browsing - #4854

Merged
marcschier merged 3 commits into
OPCFoundation:masterfrom
biancode:spec/autoid-status-refresh
Oct 21, 2026
Merged

marcschier merged 3 commits into
OPCFoundation:masterfrom
biancode:spec/autoid-status-refresh

Conversation

@biancode

Copy link
Copy Markdown
Collaborator

Description

A follow-up to #4825. It adds AutoIdClient.RefreshDeviceAsync(AutoIdDeviceSnapshot, CancellationToken), which reads the variables of a reader device that change at runtime again through the NodeIds a snapshot already carries. It makes one Read and no Browse.

Why

ReadDeviceAsync browses the children of the device on every call. OPC 30010 makes most device variables and methods optional, so the browse is how the client finds out what this device publishes. AutoIdReadOptions.StatusOnly only narrows the Read. This is the point the Copilot review raised on #4825 (discussion). 1458af9 documented the behaviour, but a client that polls the status of N readers still pays N Browse and N Read requests per cycle. One Read per reader would do.

AutoIdDeviceSnapshot already carries what a poll needs: DeviceStatusId, LastScanDataId, LastScanTimestampId, DeviceLocationId and Methods, all taken from that browse. RefreshDeviceAsync uses them.

Change

  • RefreshDeviceAsync(snapshot) reads DeviceStatus and, when the device publishes them, LastScanData, LastScanTimestamp and DeviceLocation. That is one Read of one to four variables and no Browse. It returns a copy of the snapshot (with), and every other member, including Methods and the NodeIds, is unchanged.

  • Stateless. The client keeps no cache. The caller holds the snapshot, as with the other companion clients (PumpsClient.ReadPumpAsync, ScalesClient.ReadSnapshotAsync, and Robotics' RefreshAsync on its handles). A poller calls ReadDeviceAsync once and then passes the snapshot to RefreshDeviceAsync on every cycle.

  • Stale snapshot. The snapshot is not validated against the server. If it no longer matches the device (the device or a variable was deleted, or the server assigned other NodeIds after a restart), the status of the Read surfaces:

    • a bad status for DeviceStatus (for example BadNodeIdUnknown) is thrown as ServiceResultException with that status;
    • a bad status for LastScanData is returned in LastScanStatus with a null value;
    • a bad LastScanTimestamp or DeviceLocation leaves that member at its default.

    In each case the caller reads the device again with ReadDeviceAsync. That also picks up optional variables and methods the device published after the snapshot was taken.

  • Arguments. A null snapshot throws ArgumentNullException. A snapshot without a DeviceStatus NodeId (not obtained from ReadDeviceAsync) throws ArgumentException. Neither one makes a service call.

  • Docs. Covered in the XML docs of ReadDeviceAsync, RefreshDeviceAsync, AutoIdReadOptions and AutoIdDeviceSnapshot, in docs/AutoID.md and in the client NugetREADME.md. The wording maintainers gave AutoIdReadOptions in 1458af9 stays as it is, with one added sentence that points to RefreshDeviceAsync. docs/AutoID.md states the cost of each call:

Call Network cost
ReadDeviceAsync(reader) One Browse of the device's children (BrowseNext only if the server pages the result), one Read of every published variable
ReadDeviceAsync(reader, AutoIdReadOptions.StatusOnly) One Browse, one Read of two variables (DeviceStatus, DeviceName)
RefreshDeviceAsync(snapshot) One Read of up to four variables, no Browse

The change is additive. No existing member changes behaviour, and there are no new dependencies. The MCP autoid_read_device tool still uses ReadDeviceAsync, because a single MCP tool call has no snapshot to refresh.

Tests

tests/Opc.Ua.AutoID.Tests:

  • AutoIdRefreshDeviceTests (new, 7 tests) uses a MockBehavior.Strict ISession with VerifyNoOtherCalls, so any Browse, BrowseNext or second Read fails the test:
    • one Read of exactly the four snapshot NodeIds (Value attribute, maxAge 0), with the values mapped and the other members (Methods, DeviceName, NodeIds) unchanged;
    • a device without the optional variables is refreshed with a one-variable Read;
    • a stale DeviceStatus NodeId throws with the Read's status (BadNodeIdUnknown);
    • a stale LastScanData surfaces BadNodeIdUnknown in LastScanStatus, and a bad timestamp or location resets those members;
    • a result-count mismatch throws BadUnexpectedError;
    • null and invalid snapshots are rejected without a service call;
    • cancellation reaches the Read.
  • AutoIdReaderEndToEndTests (2 new tests against the hosted AutoID server over opc.tcp):
    • ARefreshedSnapshotCarriesTheCurrentStatusAsync: a reader is read once, then faulted on the server. The refreshed snapshot shows Error and the methods from the original browse.
    • ARefreshOfAStaleSnapshotSurfacesTheStatusOfTheReadAsync: snapshots whose NodeIds the server does not know (as after a deletion). LastScanStatus carries BadNodeIdUnknown, and an unknown DeviceStatus NodeId throws BadNodeIdUnknown.
  • Fail-without (mutation checks):
    • With a Browse of the device's children added to RefreshDeviceAsync, every strict-mock test that reaches the Read fails.
    • With a bad LastScanData status masked to BadNotFound (the ReadDeviceAsync convention for optional variables), AStaleOptionalVariableSurfacesItsStatusWithoutThrowingAsync and the end-to-end stale-snapshot test fail.
    • Without the method, the tests do not compile.

Verification (local, macOS arm64, SDK 10.0.402 from global.json)

Head 2118c24 on OPCFoundation/master ac24123 (#4852), which includes the NUnit 5 migration (#4840). The new tests follow the NUnit 5 conventions in docs/DeveloperGuide.md: Assert.ThrowsAsync and Assert.MultipleAsync are awaited, and the enclosing test returns Task.

Related Issues

Checklist

  • I have signed the CLA and read the CONTRIBUTING doc.
  • I have added tests that prove my fix is effective or that my feature works and increased code coverage.
  • I have added all necessary documentation.
  • I have verified that my changes do not introduce (new) build or analyzer warnings. (Local build of the changed projects: 0 warnings, 0 errors.)
  • I ran all tests locally using the UA.slnx solution against at least .net framework and .net 10, and all passed. (Locally the AutoID test project on net10.0; net48 is built but runs only on Windows CI.)
  • I fixed all failing and flaky tests in the CI pipelines and all CodeQL warnings. (Pending the upstream checks; see the CI note above.)
  • I have addressed all PR feedback received.

biancode and others added 2 commits October 20, 2026 08:38
ReadDeviceAsync browses the children of the device on every call, so
polling the status of many readers costs a Browse and a Read per reader
and cycle; AutoIdReadOptions.StatusOnly only narrows the Read.

Add AutoIdClient.RefreshDeviceAsync(snapshot), which reads DeviceStatus
and, when published, LastScanData, LastScanTimestamp and DeviceLocation
again through the NodeIds the snapshot carries: one Read, no Browse,
no client-side state. A stale snapshot surfaces the status of the Read:
a bad DeviceStatus throws, a bad LastScanData is returned in
LastScanStatus; the caller then reads the device again.

Document the network cost of both calls in the XML docs, docs/AutoID.md
and the client NugetREADME, and add strict-mock tests (no Browse issued)
and end-to-end tests against the hosted AutoID server.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A new integration assertion assumes initial state on a shared reader, making it dependent on test execution order.

1 open issue
What changed in this PR

Adds snapshot-based AutoID polling, avoiding repeated discovery browses.

Changes:

  • Adds RefreshDeviceAsync to refresh runtime values with one Read and no Browse.
  • Documents polling costs and stale-snapshot behavior.
  • Adds strict-mock and hosted-server tests.
File Description
tests/​Opc.Ua.AutoID.Tests/​AutoIdRefreshDeviceTests.cs Tests request shape, validation, errors and cancellation.
tests/​Opc.Ua.AutoID.Tests/​AutoIdReaderEndToEndTests.cs Tests live refresh and stale NodeIds.
src/​Opc.Ua.AutoID.Client/​NugetREADME.md Describes browse-free refresh.
src/​Opc.Ua.AutoID.Client/​AutoIdReadOptions.cs Links read options to refresh.
src/​Opc.Ua.AutoID.Client/​AutoIdDeviceSnapshot.cs Documents snapshot reuse.
src/​Opc.Ua.AutoID.Client/​AutoIdClient.Read.cs Implements snapshot-based refresh.
docs/​AutoID.md Explains polling costs and recovery.

Review effort: Balanced

ARefreshedSnapshotCarriesTheCurrentStatusAsync expected LastScanStatus
to be UncertainInitialValue, which holds only while no other test of
the fixture has scanned the optical reader. The fixture shares one host
through OneTimeSetUp, and AScanThatEndsOnItsOwnLeavesTheReaderIdleAsync
and OcrAndOpticalReadersScanTheirCodesAsync scan that reader and leave
LastScanData Good; neither clearing the provider nor the fault resets
it. The test passed only because NUnit runs it first alphabetically.

Compare the refreshed status with the StatusCode the hosted
LastScanData variable holds, so the result no longer depends on the
order the tests run in.
@marcschier
marcschier enabled auto-merge (squash) October 21, 2026 04:26
@marcschier
marcschier merged commit a51f08a into OPCFoundation:master Oct 21, 2026
217 of 219 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants