Streaming History Read
Overview
C has no IAsyncEnumerable / Stream; the SDK uses an iterator handle pattern: DarraUa_HistoryStream_New creates the stream and hands back a handle, DarraUa_HistoryStream_Next pulls one record at a time, and DarraUa_HistoryStream_Free releases it when done. The business logic can process records as they arrive (push them to a chart, say) instead of loading the whole range into an array first.
Corresponding section of the standard: OPC UA Part 11(Historical Access) §6.5.
API
| Function | Category | Reading and Writing | Note |
|---|---|---|---|
| DarraUa_HistoryStream_New | History | Read | Create an iterator; output the handle |
| DarraUa_HistoryStream_Next | History | Read | Select the next one DataValue |
| DarraUa_HistoryStream_IsEnd | History | Read | Has the stream ended? |
| DarraUa_HistoryStream_Free | History | — | Release the iterator (including the unconsumed buffer) |
typedef uint32_t DarraUa_HistoryStreamHandle;
#define UA_INVALID_HISTORY_STREAM_HANDLE 0u
DarraUa_Status DarraUa_HistoryStream_New(
DarraUa_SessionHandle session,
const char* node_id,
DarraUa_DateTime start_utc,
DarraUa_DateTime end_utc,
uint32_t batch_size,
DarraUa_HistoryStreamHandle* out_handle);
DarraUa_Status DarraUa_HistoryStream_Next(
DarraUa_HistoryStreamHandle h,
DarraUa_DataValue** out_dv);
int32_t DarraUa_HistoryStream_IsEnd(DarraUa_HistoryStreamHandle h);
void DarraUa_HistoryStream_Free(DarraUa_HistoryStreamHandle h);
| Parameters | Default | Note |
|---|---|---|
| session | — | Session Handle (DarraUa_SessionHandle) |
| node_id | — | Historical Milestones NodeId String |
| start_utc | — | Interval start (UTC, 100ns since 1601, i.e., DarraUa_DateTime) |
| end_utc | — | Range stop; if <= start_utc, the iteration ends immediately |
| batch_size | 0 → 1000 | Maximum RPC Points Per Time |
| out_handle | — | Parameter, iterator handle |
Return Values and End of Stream: New Returns UA_STATUS_GOOD / UA_STATUS_INVALID_ARGUMENT / UA_STATUS_OUT_OF_MEMORY; Next Returns UA_STATUS_GOOD (including end-of-stream) or pass through the Stack error code; when the stream ends, *out_dv == NULL; IsEnd return 1 = ended, 0 = not ended.
Life Cycle: New It does not release anything on its own RPC; it is triggered Next only the first time. Each item retrieved is released DarraUa_DataValue_Delete DarraUa_DataValue* by the caller.Free Releases the iterator and any internal, unconsumed buffer DataValue; NULL-safe.
Code Example
#include <opcua/opcua.h>
#include <opcua_history_stream.h>
#include <stdio.h>
#include <time.h>
/* DarraUa_DateTime = UTC 100ns since 1601-01-01 */
static DarraUa_DateTime utc_now_ft(void) {
return (DarraUa_DateTime)((int64_t)time(NULL) * 10000000LL + 116444736000000000LL);
}
int main(void)
{
DarraUa_SessionHandle h = 0;
/* ... DarraUa_Session_Create + (omitted DarraUa_Session_Connect ) ... */
DarraUa_DateTime t1 = utc_now_ft();
DarraUa_DateTime t0 = t1 - (DarraUa_DateTime)24 * 3600 * 10000000LL;
DarraUa_HistoryStreamHandle it = UA_INVALID_HISTORY_STREAM_HANDLE;
if (DarraUa_HistoryStream_New(h, "ns=2;s=Temp", t0, t1, /*batch_size=*/1000, &it)
!= UA_STATUS_GOOD) {
return 1;
}
DarraUa_DataValue* dv = NULL;
while (DarraUa_HistoryStream_Next(it, &dv) == UA_STATUS_GOOD && dv != NULL) {
/* Process as you go (for example, by feeding the data into a trend chart); there's no need to load the entire segment into an array first. */
printf("src=%lld status=0x%08X\n",
(long long)DarraUa_DataValue_GetSourceTimestamp(dv),
DarraUa_DataValue_GetStatus(dv));
DarraUa_DataValue_Delete(dv); /* Each retrieved value is released by the caller. */
dv = NULL;
}
DarraUa_HistoryStream_Free(it); /* Release iterator + unconsumed buffer, safe NULL */
/* ... (omitted DarraUa_Session_Close(h) ) ... */
return 0;
}
Early Exit (Cancel): When the loop stops immediately Next after the adjustment DarraUa_HistoryStream_Free, the unused DataValue buffer is released Free all at once; there is no need to exit line by line. To check whether the stream has been fully processed from outside the loop, use DarraUa_HistoryStream_IsEnd.
Implementation Constraints
- Currently, the underlying ReadHistoryRaw is not exposed ContinuationPoint; it fetches the entire segment at once and RPC returns all points within the range in a single call.
- For large-range operations, the business side performs multiple self-slicing operations
- batch_size is merely a hint indicating "the maximum number of points the caller can retrieve at a time"; internally, Stack still returns all points at once.
Best Practices
- DataValue Use each piece as soon as you take it out
DarraUa_DataValue_Delete; don't save them up. - For time intervals greater than 1 hour, it is recommended to use outer slices.
- UI Render Throttling: Refresh after 100 entries have accumulated
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| ReadHistoryStreamAsync | read_history_stream | readHistoryStream | ReadHistoryStream | read_history_stream | DarraUa_HistoryStream_New/_Next/_IsEnd/_Free |