Streaming History Read
Overview
IDE When loading historical data into a trend chart, loading ten thousand data points all at once Vec can cause the process to hang OOM.read_history_stream By returning an iterator HistoryStream, it implements the standard interface Iterator<Item = DataValue>, allowing the business side to retrieve data one item at a time for dv in ... while also directly benefiting from all iterator adapters (filter / take / map / count).
Corresponding section of the standard: OPC UA Part 11(Historical Access) §6.5.
API
| Methods | Category | Reading and Writing | Note |
|---|---|---|---|
| ua.read_history_stream(node_id, start, end, batch_size) | History | Read | Returns HistoryStream (Iterator<Item = DataValue>) |
| Parameters | Default | Note |
|---|---|---|
| node_id | — | Historical Milestones (&str) |
| start | — | Start SystemTime |
| end | — | End SystemTime |
| batch_size | — | Maximum RPC Points Per Time |
HistoryStream Exported from the darra_opcua top level (same level Session as ).
Code Example
use darra_opcua::Session;
use std::time::{Duration, SystemTime};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut ua = Session::new("opc.tcp://localhost:4840")?;
ua.connect()?;
let t1 = SystemTime::now();
let t0 = t1 - Duration::from_secs(24 * 3600);
// 1) Render as you go — Standard for loop
for dv in ua.read_history_stream("ns=2;s=Temp", t0, t1, 1000) {
if let Some(ts) = dv.source_timestamp() {
println!("{:?} = {:?}", ts, dv.variant().as_f64());
}
}
// 2) Early exit (cancel) — just use `break`; the rest will be released by `Drop`
let mut cancel = false;
for dv in ua.read_history_stream("ns=2;s=Temp", t0, t1, 1000) {
if cancel { break; }
println!("{}", dv.is_good());
cancel = true; // Illustration: The business side receives a cancellation signal
}
// 3) Iterator Adapter: Counting Bad Values
let bad = ua
.read_history_stream("ns=2;s=Temp", t0, t1, 1000)
.filter(|dv| !dv.is_good())
.count();
println!("24h 内 {} 个坏值", bad);
Ok(())
}
Implementation Constraints
- Currently ContinuationPoint, read_history is not exposed; it RPC returns all points within the specified range in a single call (subject to the batch_size limit).
- Business units in large regions can slice their own data
- Returns a null iterator when a read operation at the lower level fails; no error is reported to the caller.
Best Practices
- Use break to cancel; the remainder is released by DataValue Drop.
- For time intervals greater than 1 hour, it is recommended to use outer slices.
- UI Render Throttling: Refresh every 100 entries
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| ReadHistoryStreamAsync | read_history_stream | readHistoryStream | ReadHistoryStream | read_history_stream | DarraUa_HistoryStream_New/_Next/_IsEnd/_Free |