DataValue
darra::opcua::DataValue This is the complete result of a single read: value + status + timestamp. Move-only, It is automatically released when it goes out of scope; no manual release is required.
- For information on value types, see Variant.
- For standard node constants, see WellKnownNodes.
Public Properties (Accessors)
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Value | Value() | Variant | R | Actual value (borrowed, follows the same lifetime DataValue) |
StatusCode() | uint32_t | R | Data quality, raw 32-bit code | |
StatusEnum() | Status | R | Data quality, strongly typed enumeration (Good / Bad* / Uncertain*) | |
IsGood() | bool | R | Equivalent to StatusCode() == 0 | |
| Timestamps (FileTime, 100ns since 1601) | SourceTimestamp() | int64_t | R | Timestamp applied by the data source |
ServerTimestamp() | int64_t | R | Timestamp applied when the server received the request | |
| Resource Handles | Handle() | DarraUa_DataValue* | R | Raw handle; used only when interoperating with the C interface |
Structure
Usually returned s.Read / s.ReadHistory / s.ReadMany by. When you need to construct it yourself (for example, HistoryUpdate), usingResource Handle Constructor (DataValue(DarraUa_DataValue*, bool owns)):
DataValue dv(handle, /*owns=*/true);
Usage
auto dv = s.Read("ns=2;s=Temperature");
// Quality Inspection
if (!dv.IsGood()) {
std::cerr << "Bad value: 0x" << std::hex << dv.StatusCode() << "\n";
return;
}
// Value
double temp = 0.0;
if (!dv.Value().TryGetDouble(temp)) {
std::cerr << "Type mismatch\n";
return;
}
// Timestamp (FileTime → time_t)
constexpr int64_t WIN_EPOCH_OFFSET = 116444736000000000LL;
int64_t ft = dv.SourceTimestamp();
time_t t = (ft - WIN_EPOCH_OFFSET) / 10000000;
std::cout << "T = " << temp << " degC @ " << t << "\n";
Key Values in the Status Enumeration
| Value | Meaning |
|---|---|
| Status::Good (0x00000000) | Everything is normal |
| Status::Uncertain (0x40000000) | Uncertain but available |
| Status::Bad (0x80000000) | Invalid data |
| Status::BadCommunicationError | Communication Failed |
| Status::BadNodeIdUnknown | The node does not exist |
| Status::BadNotReadable | This attribute is not readable. |
Business logic must check or IsGood() explicitly compare enumerations; it must not assume "Good."
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.
FileTime ↔ std::chrono Conversion
constexpr int64_t WIN_EPOCH_OFFSET = 116444736000000000LL;
// FileTime → std::chrono::system_clock::time_point
auto FromFileTime(int64_t ft) {
return std::chrono::system_clock::time_point(
std::chrono::nanoseconds((ft - WIN_EPOCH_OFFSET) * 100));
}
// std::chrono::system_clock::time_point → FileTime
int64_t ToFileTime(std::chrono::system_clock::time_point tp) {
auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
tp.time_since_epoch()).count();
return WIN_EPOCH_OFFSET + ns / 100;
}
Life Cycle and Release
DataValue Automatically released when it goes out of scope; no manual release is required (RAII).
{
auto dv = s.Read("ns=2;s=T1");
// Using a DV ...
} // When leaving the scope, `dv` is automatically destroyed.
// Batch
auto dvs = s.ReadMany(node_ids);
for (auto const& dv : dvs) {
// Use dv (reference / borrow)
} // When leaving the scope, the DVS is destroyed as a whole, and each element is released one by one DataValue .
Note: The returned dv.Value() object's Variant lifetime follows that of dv. Reusing dv after it has been deallocated Variant constitutes a "use-after-free":
auto dv = s.Read("ns=2;s=T1");
auto v = dv.Value(); // borrow
double d = 0; v.TryGetDouble(d);
// v Cannot escape the scope of dv