opcua_kit/client/events/
sequenced_data.rs1use super::{MAX_WAIT, PublishError, Sequenced, publish, wire};
3use crate::client::{ua, wire::variant_size};
4use crate::{
5 CertificateRollover, Client, ConnectionConfig, ConnectionOptions, DataChange, MonitoredNode,
6 NodeId, RecoveringClient, SubscriptionError,
7};
8use std::time::Duration;
9
10pub struct SequencedDataChanges {
27 stream: publish::Stream<DataChange>,
28 items: Vec<MonitoredNode>,
29}
30impl SequencedDataChanges {
31 pub async fn connect(
33 config: ConnectionConfig<'_>,
34 options: ConnectionOptions<'_>,
35 nodes: &[NodeId],
36 interval: Duration,
37 ) -> Result<Self, PublishError> {
38 Self::open(RecoveringClient::new(config, options), nodes, interval).await
39 }
40 pub async fn connect_with_rollover(
42 config: ConnectionConfig<'_>,
43 options: ConnectionOptions<'_>,
44 rollover: CertificateRollover<'_>,
45 nodes: &[NodeId],
46 interval: Duration,
47 ) -> Result<Self, PublishError> {
48 Self::open(
49 RecoveringClient::new_with_rollover(config, options, rollover),
50 nodes,
51 interval,
52 )
53 .await
54 }
55 async fn open(
56 owner: RecoveringClient<'_>,
57 nodes: &[NodeId],
58 interval: Duration,
59 ) -> Result<Self, PublishError> {
60 if !(1..=32).contains(&nodes.len()) {
61 return Err(SubscriptionError::ItemCount.into());
62 }
63 if !(Duration::from_millis(10)..=MAX_WAIT).contains(&interval) {
64 return Err(SubscriptionError::InvalidInterval.into());
65 }
66 let client = owner.into_client().await?;
67 let (sub, timeout, items) = match setup(&client, nodes, interval).await {
68 Ok(v) => v,
69 Err(e) => {
70 client.disconnect().await;
71 return Err(e);
72 }
73 };
74 Ok(Self {
75 stream: publish::Stream::new(client, sub, timeout, nodes.len(), decode),
76 items,
77 })
78 }
79 pub fn items(&self) -> &[MonitoredNode] {
81 &self.items
82 }
83 pub async fn next(&mut self, wait: Duration) -> Result<Sequenced<DataChange>, PublishError> {
85 self.stream.next(wait).await
86 }
87 pub async fn disconnect(self) {
89 self.stream.disconnect().await
90 }
91}
92async fn setup(
93 client: &Client,
94 nodes: &[NodeId],
95 interval: Duration,
96) -> Result<(u32, u32, Vec<MonitoredNode>), PublishError> {
97 let (mut session, metadata) = client.read_session().await?;
98 let mut requests = Vec::with_capacity(nodes.len());
99 for (index, node) in nodes.iter().enumerate() {
100 let node = metadata.namespaces.wire_node(node, index)?;
101 requests.push(wire::item(
102 node,
103 13,
104 (index + 1) as u32,
105 interval,
106 Default::default(),
107 ));
108 }
109 let sub = wire::create_subscription(client, &mut session, interval).await?;
110 let results = wire::create_items(client, &mut session, sub.id, requests).await?;
111 Ok((sub.id, sub.timeout_ms, wire::revisions(nodes, &results)?))
112}
113fn decode(
114 note: &ua::ExtensionObject,
115 count: usize,
116 budget: &mut usize,
117 values: &mut Vec<DataChange>,
118) -> Result<(), PublishError> {
119 let list = note
120 .inner_as::<ua::DataChangeNotification>()
121 .ok_or(PublishError::InvalidNotification)?;
122 let changes = list.monitored_items.as_deref().unwrap_or_default();
123 if changes.len() > 32 - values.len() {
124 return Err(PublishError::InvalidNotification);
125 }
126 for change in changes {
127 let index = change
128 .client_handle
129 .checked_sub(1)
130 .ok_or(PublishError::InvalidNotification)? as usize;
131 if index >= count {
132 return Err(PublishError::InvalidNotification);
133 }
134 let raw = &change.value;
135 let status = raw.status.unwrap_or_default().bits();
136 if status & 0x0c80 == 0x0480 {
137 return Err(SubscriptionError::ItemOverflow { index, status }.into());
138 }
139 if let Some(value) = &raw.value {
140 let bytes = variant_size(value)
141 .filter(|n| *n <= 128 * 1024)
142 .ok_or(SubscriptionError::ItemValueLimit { index })?;
143 *budget = budget
144 .checked_sub(bytes)
145 .ok_or(PublishError::InvalidNotification)?;
146 }
147 values.push(DataChange {
148 index,
149 value: raw.clone(),
150 });
151 }
152 Ok(())
153}