ads_client/
ads_delete_device_notification.rs

1use bytes::{Bytes, BytesMut};
2use log::info;
3use crate::{Client, AdsCommand, AdsError, AdsErrorCode, HEADER_SIZE, LEN_DEL_DEV_NOT, Result, misc::HandleData};
4
5impl Client {
6
7    fn pre_delete_device_notification(&self, handle : u32, invoke_id : u32) -> Bytes {
8        let ams_header = self.c_init_ams_header(invoke_id, Some(LEN_DEL_DEV_NOT as u32), AdsCommand::DeleteDeviceNotification);
9
10        let del_not_header  : [u8; LEN_DEL_DEV_NOT] = [
11            u32_lw_lb!(handle),
12            u32_lw_hb!(handle),
13            u32_hw_lb!(handle),
14            u32_hw_hb!(handle), 
15        ];
16
17        let iter_ams_header = ams_header.into_iter();
18        let iter_del_not    = del_not_header.into_iter();
19
20        let mut _del_not_req = BytesMut::with_capacity(HEADER_SIZE + LEN_DEL_DEV_NOT);
21        _del_not_req = iter_ams_header.chain(iter_del_not).collect();
22
23        _del_not_req.freeze()
24    }
25
26    fn post_delete_device_notification(del_not_response : HandleData) -> Result<()>{
27        Client::eval_ams_error(del_not_response.ams_err)?;
28
29        del_not_response.payload
30            .map(|p| Client::eval_return_code(p.as_ref()))
31            .ok_or_else(|| AdsError{n_error : AdsErrorCode::ADSERR_DEVICE_INVALIDDATA.into(), s_msg : String::from("Invalid data values")})??;
32
33        Ok(())
34    }
35
36    /// Submit an asynchronous [ADS Delete Device Notification](https://infosys.beckhoff.com/content/1033/tc3_ads_intro/115881995.html?id=6216061301016726131) request.
37    /// 
38    /// Checkout the extensive examples [notification](https://github.com/hANSIc99/ads_client/blob/main/examples/notification.rs) 
39    /// and [notification_async](https://github.com/hANSIc99/ads_client/blob/main/examples/notification_async.rs).
40    pub async fn delete_device_notification(&self, handle: u32 ) -> Result<()>{
41        // Prepare delete device notification request
42        let invoke_id = self.create_invoke_id();
43        let _del_not_req = self.pre_delete_device_notification(handle, invoke_id);
44
45        info!("Submit Delete Notification Request: Invoke ID: {}", invoke_id);
46
47        // Create handle for request
48        self.register_command_handle(invoke_id, AdsCommand::DeleteDeviceNotification);
49
50        // Launch the CommandManager future
51        let cmd_man_future = self.create_cmd_man_future(invoke_id);
52
53        // Launch socket future
54        let socket_future = self.socket_write(&_del_not_req);
55
56        tokio::try_join!(cmd_man_future, socket_future).and_then(| (del_not_response, _)| {
57            Client::post_delete_device_notification(del_not_response)
58        }) 
59    }
60}