Skip to content

Commit

Permalink
Add methods for building ua::DataValue with timestamps (#188)
Browse files Browse the repository at this point in the history
## Description

This adds missing methods to build `ua::DataValue` instances with source
and server timestamps. This also renames and deprecates methods
`status_code()` that do not match the expected names from open62541.

---------

Co-authored-by: Uwe Klotz <[email protected]>
  • Loading branch information
sgoll and uklotzde authored Dec 19, 2024
1 parent 3b02aa6 commit fbbd6a5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### Added

- Zeroize memory held by `PrivateKey` when dropped.
- Add methods `ua::DataValue::with_source_timestamp()`, `ua::DataValue::with_server_timestamp()`,
`ua::DataValue::with_source_picoseconds()`, `ua::DataValue::with_server_picoseconds()`.

## Changed

- Rename methods `ua::DataValue::status_code()` to `status()`, `ua::DataValue::with_status_code()`
to `with_status()`. Deprecate the former methods.

## [0.7.0] - 2024-12-13

Expand Down
2 changes: 1 addition & 1 deletion src/data_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl<T: DataType> DataValue<T> {
pub(crate) fn new(data_value: &ua::DataValue) -> Result<Self> {
// Verify that data value is valid before accessing value. The OPC UA specification requires
// us to do so. The status code may be omitted, in which case it is treated as valid data.
Error::verify_good(&data_value.status_code().unwrap_or(ua::StatusCode::GOOD))?;
Error::verify_good(&data_value.status().unwrap_or(ua::StatusCode::GOOD))?;

// When the status code indicates a good data value, the value is expected to be set.
let value = data_value
Expand Down
46 changes: 43 additions & 3 deletions src/ua/data_types/data_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,46 @@ impl DataValue {
}

#[must_use]
pub fn with_status_code(mut self, status_code: &ua::StatusCode) -> Self {
status_code.clone_into_raw(&mut self.0.status);
pub fn with_source_timestamp(mut self, source_timestamp: &ua::DateTime) -> Self {
source_timestamp.clone_into_raw(&mut self.0.sourceTimestamp);
self.0.set_hasSourceTimestamp(true);
self
}

#[must_use]
pub fn with_server_timestamp(mut self, server_timestamp: &ua::DateTime) -> Self {
server_timestamp.clone_into_raw(&mut self.0.serverTimestamp);
self.0.set_hasServerTimestamp(true);
self
}

#[must_use]
pub fn with_source_picoseconds(mut self, source_picoseconds: u16) -> Self {
self.0.sourcePicoseconds = source_picoseconds;
self.0.set_hasSourcePicoseconds(true);
self
}

#[must_use]
pub fn with_server_picoseconds(mut self, server_picoseconds: u16) -> Self {
self.0.serverPicoseconds = server_picoseconds;
self.0.set_hasServerPicoseconds(true);
self
}

#[must_use]
pub fn with_status(mut self, status: &ua::StatusCode) -> Self {
status.clone_into_raw(&mut self.0.status);
self.0.set_hasStatus(true);
self
}

#[deprecated = "use Self::with_status() instead"]
#[must_use]
pub fn with_status_code(self, status_code: &ua::StatusCode) -> Self {
self.with_status(status_code)
}

/// Gets value.
///
/// This returns the value as [`ua::Variant`] if it is set. Returns `None` when the `DataValue`
Expand Down Expand Up @@ -70,12 +104,18 @@ impl DataValue {
}

#[must_use]
pub fn status_code(&self) -> Option<ua::StatusCode> {
pub fn status(&self) -> Option<ua::StatusCode> {
self.0
.hasStatus()
.then(|| ua::StatusCode::new(self.0.status))
}

#[deprecated = "use Self::status() instead"]
#[must_use]
pub fn status_code(&self) -> Option<ua::StatusCode> {
self.status()
}

pub(crate) fn to_generic<T: DataType>(&self) -> Result<crate::DataValue<T>> {
crate::DataValue::new(self)
}
Expand Down

0 comments on commit fbbd6a5

Please sign in to comment.