update_history
Write data to the server-side Historian (rather than the "real-time values" from the current read/write operations):
HistoryUpdate: These are disabled by default on most production servers and must be explicitly enabled by an administrator, along with compliance auditing. See the server requirements for details.
Signature
pub fn Session::update_history(
&self,
node_id: &str,
update_type: HistoryUpdateType,
values: &[DataValue],
) -> Result<Vec<StatusCode>, OpcUaError>;
Returns Vec<StatusCode> — The write status of each value, in the same values order as.
HistoryUpdateType
pub enum HistoryUpdateType {
Insert = 1, // Insert only if the timestamp does not exist
Replace = 2, // Replace only if the timestamp exists
Update = 3, // Insert or Replace (either is acceptable; recommended)
Remove = 4, // Delete by time (use only the SourceTimestamp field; ignore the value)
}
Usage
Backfill Historical Data
use darra_opcua::HistoryUpdateType;
// Most users retrieve data from the history table and immediately DataValue convert it to an UPDATE statement.
let dvs = s.read_history(
"ns=2;s=Temperature_Backup",
yesterday, now, 1000,
)?;
let results = s.update_history("ns=2;s=Temperature", HistoryUpdateType::Update, &dvs)?;
for (i, st) in results.iter().enumerate() {
if !st.is_good() {
eprintln!("values[{}] failed: {}", i, st);
}
}
Insert only (do not overwrite existing entries)
let results = s.update_history("ns=2;s=Temperature", HistoryUpdateType::Insert, &dvs)?;
Delete at a Specified Time
Remove mode only considers each DataValue's source_timestamp; the value field is ignored by the server.
For simple range-based deletions, we recommend using delete_history_range More intuitive.
Permission Requirements
- Node
AccessLevelbit 8 (HistoryWrite) must be set - Users must have HistoryUpdate permissions
- This feature is disabled by default on most production servers and must be explicitly enabled by an administrator.
Error
| StatusCode | Reason |
|---|---|
| BAD_USER_ACCESS_DENIED | No permission |
Protocol Code BadHistoryOperationInvalid | This node does not support HistoryUpdate |
Protocol Code BadEntryExists (Insert Mode) | That time point already exists. |
Protocol Code BadNoEntryExists (Replace / Remove Mode) | That time does not exist. |
The three protocol codes in the table above SDKNot yet Definition: StatusCode Constants (for a complete list of constants, see " StatusCode Related Constants"),
Do not simply copy and paste from " StatusCode::X Comparison"; use status code values or helper functions to determine:
e.status.is_bad() / e.status.raw() / darra_opcua::status_name(e.status).
Structure DataValue (Advanced)
Currently, the public builder is not directly exposed DataValue by the interface SDK Rust
(DataValue::from_raw / wrap_owned is internal to the crate API). To manually construct historical values, the recommended approach is:
- Retrieve a template
Vec<DataValue>from a real history read, modify the timestamp, and write it back. - Data is generated on the PLC or device side and automatically archived by the server-side Historian through real-time
writewriting.
If the business logic must be constructed programmatically, please SDK open an issue; the public DataValue::new(value, status, source_ts) constructor will be extended in a future version.
Error Handling
Overall failure RPC returns Err. A single value failing passes results[i] the check:
let results = s.update_history(node_id, HistoryUpdateType::Update, &dvs)?;
let bad = results.iter().filter(|st| !st.is_good()).count();
if bad > 0 {
eprintln!("{}/{} values failed to write", bad, results.len());
}