read_history_processed (Aggregate)
The most powerful mode HistoryRead, used in conjunction with standard aggregation functions. Given an interval, an aggregation function, and the width of each subinterval, the server returns the aggregated result for each subinterval.
Preliminary / Supporting
- To pull all original points, please use ReadRaw.
- For interpolation at a specific point in time, please use ReadAtTime.
Signature
pub fn Session::read_history_processed(
&self,
node_id: &str,
start: SystemTime,
end: SystemTime,
processing_interval_ms: f64,
aggregate_node_id: &str,
) -> Result<Vec<DataValue>, OpcUaError>;
Standard Aggregate Function Constants
darra_opcua::history_aggregate Module:
| Constants | NodeId | Meaning |
|---|---|---|
| AVERAGE | i=2342 | Arithmetic Mean |
| TIME_AVERAGE | i=11285 | Time-Weighted Average |
| TOTAL | i=2348 | Cumulative (Algebraic Sum) |
| MINIMUM | i=2346 | Minimum Value |
| MAXIMUM | i=2347 | Maximum value |
| COUNT | i=2352 | Number of "Good" status points within the interval |
| STANDARD_DEVIATION_SAMPLE | i=11427 | Sample standard deviation |
| RANGE | i=2353 | Max − Min |
| DELTA | i=11286 | End of Interval − Start of Interval |
For a complete list, see the OPC UA Aggregate Functions Standard.
Usage
Hourly average of the 24-hour temperature
use std::time::{Duration, SystemTime};
use darra_opcua::history_aggregate;
let now = SystemTime::now();
let yesterday = now - Duration::from_secs(86400);
let values = s.read_history_processed(
"ns=2;s=Temperature",
yesterday, now,
/*processing_interval_ms*/ 3_600_000.0, // 1 hour
history_aggregate::AVERAGE,
)?;
// Returns 24 values DataValue, each representing the average for that hour
for v in &values {
println!("avg = {}", v.variant().try_get_f64().unwrap_or(0.0));
}
Highest Temperature Each Day of the Week
let week_ago = now - Duration::from_secs(7 * 86400);
let maxes = s.read_history_processed(
"ns=2;s=Temperature",
week_ago, now,
86_400_000.0, // 1 day
history_aggregate::MAXIMUM,
)?;
Total traffic in one hour
let one_hour_ago = now - Duration::from_secs(3600);
let totals = s.read_history_processed(
"ns=2;s=FlowRate",
one_hour_ago, now,
3_600_000.0,
history_aggregate::TOTAL,
)?;
Performance Advantages
- Avoid fetching millions of data points and processing them on the client side
- Servers often index historical data, enabling extremely fast aggregation.
- Standard Solutions for Cross-Day and Cross-Week Reports
Server Requirements
- Historian must implement this aggregate function (the supported functions are listed in the ServerCapabilities/AggregateFunctions column on the server).
- The server returns a protocol code for unsupported aggregations
BadAggregateNotSupported— SDK No constantStatusCodehas been defined for this code (For a complete list of constants, seeStatusCodeAssociated Constants); when making a determination, do not simply copy fromStatusCode::X, but use status code values / help functions:e.status.is_bad()/e.status.raw()/darra_opcua::status_name(e.status)
Parameter Validation
Internal API validation:
aggregate_node_idCannot be emptyprocessing_interval_msMust be > 0
Otherwise, return Err(OpcUaError::with_message(StatusCode::INVALID_ARGUMENT, ...)).
Custom Aggregations NodeId
If the server has a non-standard aggregate, simply pass its string NodeId:
s.read_history_processed(
"ns=2;s=Temperature",
yesterday, now,
60_000.0, // 1 minute
"ns=2;s=Custom_RMS", // Custom RMS Aggregation
)?;