OpcUaDataValue
OpcUaDataValue All information returned by a single read operation: value + status + timestamp.
Implementation AutoCloseable — Hold the native handle, Must close.
- For the internal value container, please see OpcUaVariant.
- For standard node constants, see WellKnownNodes.
Public Methods
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Value | variant() | OpcUaVariant | R | Actual value (not held; do not close) |
getStatusEnum() | Enums.StatusCode | R | Data quality (enumeration format): Good / Bad* / Uncertain* | |
getStatusCode() | int | R | Raw 32-bit StatusCode | |
isGood() | boolean | R | Equivalent to (code & 0xC0000000) == 0 | |
| Timestamps | sourceTimestamp() | long | R | Data source FileTime (100ns since 1601 UTC), 0 = null |
serverTimestamp() | long | R | Server-side FileTime, 0 = null | |
| Internal | handle() | Pointer | R | Internal native handle (for debugging) |
| Release | close() | 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
| Value | Meaning |
|---|---|
| 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.BadCommunicationError | Communication Failed |
| Enums.StatusCode.BadNodeIdUnknown | The node does not exist |
| Enums.StatusCode.BadNotReadable | This 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:
| Source | owns | Should I close it? |
|---|---|---|
| ua.read(...) returns | true | Yes |
| Each element returned by ua.readMany(...) | true | Yes |
| Each element returned by ua.readHistoryXxx(...) | true | Yes |
| node.getValue() returns | true | Yes |
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).