1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! Standard OPC UA Part 9 methods for a retained state or a specific condition event.
use super::{CallOutcome, Control, ControlError, Events, ua};
use crate::NodeId;
const UA: &str = "http://opcfoundation.org/UA/";
fn standard(id: &str) -> NodeId {
NodeId::new(UA, id).expect("standard numeric NodeId")
}
/// Optional localized operator comment applied to the state identified by an EventId.
/// Pass `None` to preserve the existing comment, or a locale with empty text to clear it.
#[derive(Clone, Copy, Debug)]
pub struct ConditionComment<'a> {
/// Locale identifier of the supplied text, for example `"en-US"`.
pub locale: &'a str,
/// Operator comment in the supplied locale.
pub text: &'a str,
}
/// A condition method could not be prepared or its Call response could not be obtained.
/// An operation-level rejection from the server is returned in [`CallOutcome::status`].
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ConditionError {
/// A standard condition type was supplied where the method requires a condition instance.
#[error("use the ConditionId of the reported condition instance")]
InvalidCondition,
/// EventIds are opaque and must contain one through 65,536 bytes.
#[error("a condition EventId must contain 1 to 65536 bytes")]
InvalidEventId,
/// The encoded comment strings contain NUL or exceed the combined 16 KiB limit.
#[error("the comment must be NUL-free and its locale and text must fit in 16 KiB")]
InvalidComment,
/// The event subscription has already failed. Recreate it before requesting refresh.
#[error("the event stream has ended")]
Ended,
/// The standard Call failed locally or has an indeterminate remote outcome.
#[error(transparent)]
Call(#[from] ControlError),
}
impl Control<'_> {
/// Acknowledge the reported branch state by its condition instance and opaque EventId.
/// Supply the ConditionId and EventId from the same notification on this server where
/// AckedState/Id was false. The separate BranchId identifies a retained branch.
/// A server can reject an unknown EventId or an already acknowledged state.
///
/// ```
/// use opcua_kit::{Client, ConditionComment, ConditionError, Control, NodeId};
///
/// async fn acknowledge(client: &Client, condition: &NodeId, event_id: &[u8])
/// -> Result<u32, ConditionError>
/// {
/// let comment = ConditionComment { locale: "en", text: "Operator reviewed" };
/// let result = Control::new(client)
/// .acknowledge_condition(condition, event_id, Some(comment))
/// .await?;
/// Ok(result.status)
/// }
/// ```
pub async fn acknowledge_condition(
&self,
condition: &NodeId,
event_id: &[u8],
comment: Option<ConditionComment<'_>>,
) -> Result<CallOutcome, ConditionError> {
self.condition_method("i=9111", condition, event_id, comment)
.await
}
/// Confirm the reported branch state by its condition instance and opaque EventId.
/// Use the EventId for the state whose ConfirmedState/Id was false, including when the
/// server issued a new EventId after acknowledgement. Confirm requires server support.
pub async fn confirm_condition(
&self,
condition: &NodeId,
event_id: &[u8],
comment: Option<ConditionComment<'_>>,
) -> Result<CallOutcome, ConditionError> {
self.condition_method("i=9113", condition, event_id, comment)
.await
}
async fn condition_method(
&self,
method: &str,
condition: &NodeId,
event_id: &[u8],
comment: Option<ConditionComment<'_>>,
) -> Result<CallOutcome, ConditionError> {
if condition.namespace() == UA
&& matches!(condition.identifier(), "i=2782" | "i=2881" | "i=2915")
{
return Err(ConditionError::InvalidCondition);
}
if !(1..=65536).contains(&event_id.len()) {
return Err(ConditionError::InvalidEventId);
}
let text = match comment {
Some(c) => {
if c.locale.len().saturating_add(c.text.len()) > 16 * 1024
|| c.locale.contains('\0')
|| c.text.contains('\0')
{
return Err(ConditionError::InvalidComment);
}
ua::LocalizedText::new(c.locale, c.text)
}
None => ua::LocalizedText::default(),
};
let arguments = [
ua::Variant::ByteString(ua::ByteString::from(event_id.to_vec())),
ua::Variant::LocalizedText(Box::new(text)),
];
Ok(self
.call_method(condition, &standard(method), &arguments)
.await?)
}
}
impl Events {
/// Request retained condition events for this subscription on its own authenticated session.
/// It can run while an outstanding Publish waits for a notification or keepalive.
/// A Good method result accepts the request. Observe RefreshStart and RefreshEnd events and
/// retained notifications through [`Events::next`] to complete a refresh.
pub async fn refresh_conditions(&mut self) -> Result<CallOutcome, ConditionError> {
let (client, subscription) = self.condition_session().ok_or(ConditionError::Ended)?;
let args = [ua::Variant::UInt32(subscription)];
Ok(Control::new(client)
.call_method(&standard("i=2782"), &standard("i=3875"), &args)
.await?)
}
}