Skip to main content

OpcUaDataValue

OpcUaDataValue All information returned by a single read operation: value + status + timestamp.

Implementation AutoCloseable — Hold the native handle, Must close.

Supporting

Public Methods​

CategoryPropertyTypeAccessDescription
Valuevariant()OpcUaVariantRActual value (not held; do not close)
getStatusEnum()Enums.StatusCodeRData quality (enumeration format): Good / Bad* / Uncertain*
getStatusCode()intRRaw 32-bit StatusCode
isGood()booleanREquivalent to (code & 0xC0000000) == 0
TimestampssourceTimestamp()longRData source FileTime (100ns since 1601 UTC), 0 = null
serverTimestamp()longRServer-side FileTime, 0 = null
Internalhandle()PointerRInternal native handle (for debugging)
Releaseclose()void—Releases native memory (when owns=true)

Usage​

try (OpcUaDataValue dv = ua.read("ns=2;s=Temperature")) {

// Quality Inspection
if (!dv.isGood()) {
System.err.println("Bad value: " + dv.getStatusEnum());
return;
}

// Value
OpcUaVariant v = dv.variant();
Double temp = v.asDouble();
if (temp == null) {
System.err.println("Type mismatch");
return;
}

// Get the timestamp
long ft = dv.sourceTimestamp();
Instant t = (ft > 0)
? Instant.ofEpochMilli((ft - 116444736000000000L) / 10000L)
: Instant.now();

System.out.printf("%s: %.2f °C%n", t, temp);
}

StatusCode Key Value​

ValueMeaning
Enums.StatusCode.Good (0x00000000)Everything is normal
Enums.StatusCode.Uncertain (0x40000000)Uncertain but usable (e.g., sensor out of range)
Enums.StatusCode.Bad (0x80000000)Invalid data
Enums.StatusCode.BadCommunicationErrorCommunication Failed
Enums.StatusCode.BadNodeIdUnknownThe node does not exist
Enums.StatusCode.BadNotReadableThis attribute is not readable.

The business logic must check for / getStatusEnum() isGood(); it cannot make assumptions. Good.

Enums.StatusCode Provides isGood() / isUncertain() / isBad() bitwise comparison methods.

Meaning of Timestamps​

  • sourceTimestamp() — Actual Time the Data Was Generated (Sensor sampling time / Calculation time). Use this for history, trends, and synchronization.
  • serverTimestamp() — Server Receive/Processing Time. Used to diagnose network latency. Generally not used for business purposes.

If the server does not have a SourceTimestamp set, it is a copy of ServerTimestamp.

A return value of 0 indicates that the timestamp is null.

FileTime ↔ Instant Conversion​

import java.time.Instant;

// FileTime → Instant
long ft = dv.sourceTimestamp();
Instant t = (ft > 0)
? Instant.ofEpochMilli((ft - 116444736000000000L) / 10000L)
: Instant.now();

// Instant → FileTime
long ft2 = t.toEpochMilli() * 10000L + 116444736000000000L;

Memory Ownership​

OpcUaDataValue Hold a native handle; implementation AutoCloseable:

SourceownsShould I close it?
ua.read(...) returnstrueYes
Each element returned by ua.readMany(...)trueYes
Each element returned by ua.readHistoryXxx(...)trueYes
node.getValue() returnstrueYes
Consequences of Forgetting to Close Resources

Failing to close resources will cause native memory leaks (memory usage will increase after running the code 100k+ times). Always use the try-with-resources or try-finally blocks.

Best Practices​

// Single
try (OpcUaDataValue dv = ua.read("ns=2;s=T1")) {
System.out.println(dv.variant());
}

// Batch
List<OpcUaDataValue> dvs = ua.readMany(nodes, Enums.AttributeId.Value);
try {
for (OpcUaDataValue dv : dvs) {
if (dv != null) System.out.println(dv.variant());
}
} finally {
for (OpcUaDataValue dv : dvs) if (dv != null) dv.close();
}

Construction (for HistoryUpdate)​

Typically defined by its internal SDK structure. The use cases for HistoryUpdate which users employ it currently need to be derived DataValue from existing implementations (future versions will expose a public constructor).

Next Step​