Skip to main content

bacnet_client/client/
summaries.rs

1//! GetEventInformation, GetAlarmSummary and GetEnrollmentSummary, sent and
2//! decoded (#2077).
3
4use super::*;
5
6use bacnet_services::alarm_event::{GetEventInformationAck, GetEventInformationRequest};
7use bacnet_services::alarm_summary::GetAlarmSummaryAck;
8use bacnet_services::enrollment_summary::{GetEnrollmentSummaryAck, GetEnrollmentSummaryRequest};
9use bacnet_types::primitives::ObjectIdentifier;
10
11impl<T: TransportPort + 'static> BACnetClient<T> {
12    /// GetEventInformation to a device on this network, its answer decoded:
13    /// the objects after `last_received_object_identifier` (from the first
14    /// when `None`) with an active event or an unacknowledged transition.
15    /// While the answer's `more_events` is set, ask again from its last
16    /// object.
17    pub async fn get_event_information(
18        &self,
19        destination_mac: &[u8],
20        last_received_object_identifier: Option<ObjectIdentifier>,
21    ) -> Result<GetEventInformationAck, Error> {
22        let buf = event_information_request(last_received_object_identifier);
23        let ack = self
24            .confirmed_request(
25                destination_mac,
26                ConfirmedServiceChoice::GET_EVENT_INFORMATION,
27                &buf,
28            )
29            .await?;
30        GetEventInformationAck::decode(&ack)
31    }
32
33    /// [`Self::get_event_information`] to a discovered device, routed when
34    /// it is behind a router. A device not in the table fails before
35    /// sending.
36    pub async fn get_event_information_from_device(
37        &self,
38        device_instance: u32,
39        last_received_object_identifier: Option<ObjectIdentifier>,
40    ) -> Result<GetEventInformationAck, Error> {
41        let buf = event_information_request(last_received_object_identifier);
42        let ack = self
43            .confirmed_request_to_device(
44                device_instance,
45                ConfirmedServiceChoice::GET_EVENT_INFORMATION,
46                &buf,
47            )
48            .await?;
49        GetEventInformationAck::decode(&ack)
50    }
51
52    /// GetAlarmSummary to a device on this network, its answer decoded.
53    pub async fn get_alarm_summary(
54        &self,
55        destination_mac: &[u8],
56    ) -> Result<GetAlarmSummaryAck, Error> {
57        let ack = self
58            .confirmed_request(
59                destination_mac,
60                ConfirmedServiceChoice::GET_ALARM_SUMMARY,
61                &[],
62            )
63            .await?;
64        GetAlarmSummaryAck::decode(&ack)
65    }
66
67    /// [`Self::get_alarm_summary`] to a discovered device, routed when it
68    /// is behind a router. A device not in the table fails before sending.
69    pub async fn get_alarm_summary_from_device(
70        &self,
71        device_instance: u32,
72    ) -> Result<GetAlarmSummaryAck, Error> {
73        let ack = self
74            .confirmed_request_to_device(
75                device_instance,
76                ConfirmedServiceChoice::GET_ALARM_SUMMARY,
77                &[],
78            )
79            .await?;
80        GetAlarmSummaryAck::decode(&ack)
81    }
82
83    /// GetEnrollmentSummary to a device on this network, its answer
84    /// decoded. A filter out of its range fails before sending.
85    pub async fn get_enrollment_summary(
86        &self,
87        destination_mac: &[u8],
88        request: &GetEnrollmentSummaryRequest,
89    ) -> Result<GetEnrollmentSummaryAck, Error> {
90        let mut buf = BytesMut::new();
91        request.try_encode(&mut buf)?;
92        let ack = self
93            .confirmed_request(
94                destination_mac,
95                ConfirmedServiceChoice::GET_ENROLLMENT_SUMMARY,
96                &buf,
97            )
98            .await?;
99        GetEnrollmentSummaryAck::decode(&ack)
100    }
101
102    /// [`Self::get_enrollment_summary`] to a discovered device, routed when
103    /// it is behind a router. A device not in the table fails before
104    /// sending.
105    pub async fn get_enrollment_summary_from_device(
106        &self,
107        device_instance: u32,
108        request: &GetEnrollmentSummaryRequest,
109    ) -> Result<GetEnrollmentSummaryAck, Error> {
110        let mut buf = BytesMut::new();
111        request.try_encode(&mut buf)?;
112        let ack = self
113            .confirmed_request_to_device(
114                device_instance,
115                ConfirmedServiceChoice::GET_ENROLLMENT_SUMMARY,
116                &buf,
117            )
118            .await?;
119        GetEnrollmentSummaryAck::decode(&ack)
120    }
121
122    /// A confirmed request to a discovered device: through its router when
123    /// the table holds one, and otherwise to its MAC.
124    async fn confirmed_request_to_device(
125        &self,
126        device_instance: u32,
127        service_choice: ConfirmedServiceChoice,
128        service_data: &[u8],
129    ) -> Result<Bytes, Error> {
130        let (mac, routing) = self.resolve_device(device_instance).await?;
131        match routing {
132            Some((network, address)) => {
133                self.confirmed_request_routed(&mac, network, &address, service_choice, service_data)
134                    .await
135            }
136            None => {
137                self.confirmed_request(&mac, service_choice, service_data)
138                    .await
139            }
140        }
141    }
142}
143
144fn event_information_request(
145    last_received_object_identifier: Option<ObjectIdentifier>,
146) -> BytesMut {
147    let mut buf = BytesMut::new();
148    GetEventInformationRequest {
149        last_received_object_identifier,
150    }
151    .encode(&mut buf);
152    buf
153}