tinkerforge_async/bindings/
remote_switch_bricklet.rs

1/* ***********************************************************
2 * This file was automatically generated on 2024-02-16.      *
3 *                                                           *
4 * Rust Bindings Version 2.0.20                              *
5 *                                                           *
6 * If you have a bugfix for this file and want to commit it, *
7 * please fix the bug in the generator. You can find a link  *
8 * to the generators git repository on tinkerforge.com       *
9 *************************************************************/
10
11//! Controls remote mains switches.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/RemoteSwitch_Bricklet_Rust.html).
14#[allow(unused_imports)]
15use crate::{
16    base58::Uid, byte_converter::*, device::*, error::TinkerforgeError, ip_connection::async_io::AsyncIpConnection,
17    low_level_traits::LowLevelRead,
18};
19#[allow(unused_imports)]
20use futures_core::Stream;
21#[allow(unused_imports)]
22use tokio_stream::StreamExt;
23pub enum RemoteSwitchBrickletFunction {
24    SwitchSocket,
25    GetSwitchingState,
26    SetRepeats,
27    GetRepeats,
28    SwitchSocketA,
29    SwitchSocketB,
30    DimSocketB,
31    SwitchSocketC,
32    GetIdentity,
33    CallbackSwitchingDone,
34}
35impl From<RemoteSwitchBrickletFunction> for u8 {
36    fn from(fun: RemoteSwitchBrickletFunction) -> Self {
37        match fun {
38            RemoteSwitchBrickletFunction::SwitchSocket => 1,
39            RemoteSwitchBrickletFunction::GetSwitchingState => 2,
40            RemoteSwitchBrickletFunction::SetRepeats => 4,
41            RemoteSwitchBrickletFunction::GetRepeats => 5,
42            RemoteSwitchBrickletFunction::SwitchSocketA => 6,
43            RemoteSwitchBrickletFunction::SwitchSocketB => 7,
44            RemoteSwitchBrickletFunction::DimSocketB => 8,
45            RemoteSwitchBrickletFunction::SwitchSocketC => 9,
46            RemoteSwitchBrickletFunction::GetIdentity => 255,
47            RemoteSwitchBrickletFunction::CallbackSwitchingDone => 3,
48        }
49    }
50}
51pub const REMOTE_SWITCH_BRICKLET_SWITCH_TO_OFF: u8 = 0;
52pub const REMOTE_SWITCH_BRICKLET_SWITCH_TO_ON: u8 = 1;
53pub const REMOTE_SWITCH_BRICKLET_SWITCHING_STATE_READY: u8 = 0;
54pub const REMOTE_SWITCH_BRICKLET_SWITCHING_STATE_BUSY: u8 = 1;
55
56#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
57pub struct Identity {
58    pub uid: String,
59    pub connected_uid: String,
60    pub position: char,
61    pub hardware_version: [u8; 3],
62    pub firmware_version: [u8; 3],
63    pub device_identifier: u16,
64}
65impl FromByteSlice for Identity {
66    fn bytes_expected() -> usize {
67        25
68    }
69    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
70        Identity {
71            uid: <String>::from_le_byte_slice(&bytes[0..8]),
72            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
73            position: <char>::from_le_byte_slice(&bytes[16..17]),
74            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
75            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
76            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
77        }
78    }
79}
80
81/// Controls remote mains switches
82#[derive(Clone)]
83pub struct RemoteSwitchBricklet {
84    device: Device,
85}
86impl RemoteSwitchBricklet {
87    pub const DEVICE_IDENTIFIER: u16 = 235;
88    pub const DEVICE_DISPLAY_NAME: &'static str = "Remote Switch Bricklet";
89    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
90    pub fn new(uid: Uid, connection: AsyncIpConnection) -> RemoteSwitchBricklet {
91        let mut result = RemoteSwitchBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
92        result.device.response_expected[u8::from(RemoteSwitchBrickletFunction::SwitchSocket) as usize] = ResponseExpectedFlag::False;
93        result.device.response_expected[u8::from(RemoteSwitchBrickletFunction::GetSwitchingState) as usize] =
94            ResponseExpectedFlag::AlwaysTrue;
95        result.device.response_expected[u8::from(RemoteSwitchBrickletFunction::SetRepeats) as usize] = ResponseExpectedFlag::False;
96        result.device.response_expected[u8::from(RemoteSwitchBrickletFunction::GetRepeats) as usize] = ResponseExpectedFlag::AlwaysTrue;
97        result.device.response_expected[u8::from(RemoteSwitchBrickletFunction::SwitchSocketA) as usize] = ResponseExpectedFlag::False;
98        result.device.response_expected[u8::from(RemoteSwitchBrickletFunction::SwitchSocketB) as usize] = ResponseExpectedFlag::False;
99        result.device.response_expected[u8::from(RemoteSwitchBrickletFunction::DimSocketB) as usize] = ResponseExpectedFlag::False;
100        result.device.response_expected[u8::from(RemoteSwitchBrickletFunction::SwitchSocketC) as usize] = ResponseExpectedFlag::False;
101        result.device.response_expected[u8::from(RemoteSwitchBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
102        result
103    }
104
105    /// Returns the response expected flag for the function specified by the function ID parameter.
106    /// It is true if the function is expected to send a response, false otherwise.
107    ///
108    /// For getter functions this is enabled by default and cannot be disabled, because those
109    /// functions will always send a response. For callback configuration functions it is enabled
110    /// by default too, but can be disabled by [`set_response_expected`](crate::remote_switch_bricklet::RemoteSwitchBricklet::set_response_expected).
111    /// For setter functions it is disabled by default and can be enabled.
112    ///
113    /// Enabling the response expected flag for a setter function allows to detect timeouts
114    /// and other error conditions calls of this setter as well. The device will then send a response
115    /// for this purpose. If this flag is disabled for a setter function then no response is sent
116    /// and errors are silently ignored, because they cannot be detected.
117    ///
118    /// See [`set_response_expected`](crate::remote_switch_bricklet::RemoteSwitchBricklet::set_response_expected) for the list of function ID constants available for this function.
119    pub fn get_response_expected(&mut self, fun: RemoteSwitchBrickletFunction) -> Result<bool, GetResponseExpectedError> {
120        self.device.get_response_expected(u8::from(fun))
121    }
122
123    /// Changes the response expected flag of the function specified by the function ID parameter.
124    /// This flag can only be changed for setter (default value: false) and callback configuration
125    /// functions (default value: true). For getter functions it is always enabled.
126    ///
127    /// Enabling the response expected flag for a setter function allows to detect timeouts and
128    /// other error conditions calls of this setter as well. The device will then send a response
129    /// for this purpose. If this flag is disabled for a setter function then no response is sent
130    /// and errors are silently ignored, because they cannot be detected.
131    pub fn set_response_expected(
132        &mut self,
133        fun: RemoteSwitchBrickletFunction,
134        response_expected: bool,
135    ) -> Result<(), SetResponseExpectedError> {
136        self.device.set_response_expected(u8::from(fun), response_expected)
137    }
138
139    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
140    pub fn set_response_expected_all(&mut self, response_expected: bool) {
141        self.device.set_response_expected_all(response_expected)
142    }
143
144    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
145    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
146    pub fn get_api_version(&self) -> [u8; 3] {
147        self.device.api_version
148    }
149
150    /// This receiver is triggered whenever the switching state changes
151    /// from busy to ready, see [`get_switching_state`].
152    ///
153    /// [`get_switching_state`]: #method.get_switching_state
154    pub async fn get_switching_done_callback_receiver(&mut self) -> impl Stream<Item = ()> {
155        self.device.get_callback_receiver(u8::from(RemoteSwitchBrickletFunction::CallbackSwitchingDone)).await.map(|_p| ())
156    }
157
158    /// This function is deprecated, use [`switch_socket_a`] instead.
159    ///
160    /// Associated constants:
161    /// * REMOTE_SWITCH_BRICKLET_SWITCH_TO_OFF
162    ///	* REMOTE_SWITCH_BRICKLET_SWITCH_TO_ON
163    pub async fn switch_socket(&mut self, house_code: u8, receiver_code: u8, switch_to: u8) -> Result<(), TinkerforgeError> {
164        let mut payload = [0; 3];
165        house_code.write_to_slice(&mut payload[0..1]);
166        receiver_code.write_to_slice(&mut payload[1..2]);
167        switch_to.write_to_slice(&mut payload[2..3]);
168
169        #[allow(unused_variables)]
170        let result = self.device.set(u8::from(RemoteSwitchBrickletFunction::SwitchSocket), &payload).await?;
171        Ok(())
172    }
173
174    /// Returns the current switching state. If the current state is busy, the
175    /// Bricklet is currently sending a code to switch a socket. It will not
176    /// accept any requests to switch sockets until the state changes to ready.
177    ///
178    /// How long the switching takes is dependent on the number of repeats, see
179    /// [`set_repeats`].
180    ///
181    /// Associated constants:
182    /// * REMOTE_SWITCH_BRICKLET_SWITCHING_STATE_READY
183    ///	* REMOTE_SWITCH_BRICKLET_SWITCHING_STATE_BUSY
184    pub async fn get_switching_state(&mut self) -> Result<u8, TinkerforgeError> {
185        let payload = [0; 0];
186
187        #[allow(unused_variables)]
188        let result = self.device.get(u8::from(RemoteSwitchBrickletFunction::GetSwitchingState), &payload).await?;
189        Ok(u8::from_le_byte_slice(result.body()))
190    }
191
192    /// Sets the number of times the code is sent when one of the switch socket
193    /// functions is called. The repeats basically correspond to the amount of time
194    /// that a button of the remote is pressed.
195    ///
196    /// Some dimmers are controlled by the length of a button pressed,
197    /// this can be simulated by increasing the repeats.
198    pub async fn set_repeats(&mut self, repeats: u8) -> Result<(), TinkerforgeError> {
199        let mut payload = [0; 1];
200        repeats.write_to_slice(&mut payload[0..1]);
201
202        #[allow(unused_variables)]
203        let result = self.device.set(u8::from(RemoteSwitchBrickletFunction::SetRepeats), &payload).await?;
204        Ok(())
205    }
206
207    /// Returns the number of repeats as set by [`set_repeats`].
208    pub async fn get_repeats(&mut self) -> Result<u8, TinkerforgeError> {
209        let payload = [0; 0];
210
211        #[allow(unused_variables)]
212        let result = self.device.get(u8::from(RemoteSwitchBrickletFunction::GetRepeats), &payload).await?;
213        Ok(u8::from_le_byte_slice(result.body()))
214    }
215
216    /// To switch a type A socket you have to give the house code, receiver code and the
217    /// state (on or off) you want to switch to.
218    ///
219    /// A detailed description on how you can figure out the house and receiver code
220    /// can be found [here](remote_switch_bricklet_type_a_house_and_receiver_code).
221    ///
222    ///
223    /// .. versionadded:: 2.0.1$nbsp;(Plugin)
224    ///
225    /// Associated constants:
226    /// * REMOTE_SWITCH_BRICKLET_SWITCH_TO_OFF
227    ///	* REMOTE_SWITCH_BRICKLET_SWITCH_TO_ON
228    pub async fn switch_socket_a(&mut self, house_code: u8, receiver_code: u8, switch_to: u8) -> Result<(), TinkerforgeError> {
229        let mut payload = [0; 3];
230        house_code.write_to_slice(&mut payload[0..1]);
231        receiver_code.write_to_slice(&mut payload[1..2]);
232        switch_to.write_to_slice(&mut payload[2..3]);
233
234        #[allow(unused_variables)]
235        let result = self.device.set(u8::from(RemoteSwitchBrickletFunction::SwitchSocketA), &payload).await?;
236        Ok(())
237    }
238
239    /// To switch a type B socket you have to give the address, unit and the state
240    /// (on or off) you want to switch to.
241    ///
242    /// To switch all devices with the same address use 255 for the unit.
243    ///
244    /// A detailed description on how you can teach a socket the address and unit can
245    /// be found [here](remote_switch_bricklet_type_b_address_and_unit).
246    ///
247    ///
248    /// .. versionadded:: 2.0.1$nbsp;(Plugin)
249    ///
250    /// Associated constants:
251    /// * REMOTE_SWITCH_BRICKLET_SWITCH_TO_OFF
252    ///	* REMOTE_SWITCH_BRICKLET_SWITCH_TO_ON
253    pub async fn switch_socket_b(&mut self, address: u32, unit: u8, switch_to: u8) -> Result<(), TinkerforgeError> {
254        let mut payload = [0; 6];
255        address.write_to_slice(&mut payload[0..4]);
256        unit.write_to_slice(&mut payload[4..5]);
257        switch_to.write_to_slice(&mut payload[5..6]);
258
259        #[allow(unused_variables)]
260        let result = self.device.set(u8::from(RemoteSwitchBrickletFunction::SwitchSocketB), &payload).await?;
261        Ok(())
262    }
263
264    /// To control a type B dimmer you have to give the address, unit and the
265    /// dim value you want to set the dimmer to.
266    ///
267    /// A detailed description on how you can teach a dimmer the address and unit can
268    /// be found [here](remote_switch_bricklet_type_b_address_and_unit).
269    ///
270    ///
271    /// .. versionadded:: 2.0.1$nbsp;(Plugin)
272    pub async fn dim_socket_b(&mut self, address: u32, unit: u8, dim_value: u8) -> Result<(), TinkerforgeError> {
273        let mut payload = [0; 6];
274        address.write_to_slice(&mut payload[0..4]);
275        unit.write_to_slice(&mut payload[4..5]);
276        dim_value.write_to_slice(&mut payload[5..6]);
277
278        #[allow(unused_variables)]
279        let result = self.device.set(u8::from(RemoteSwitchBrickletFunction::DimSocketB), &payload).await?;
280        Ok(())
281    }
282
283    /// To switch a type C socket you have to give the system code, device code and the
284    /// state (on or off) you want to switch to.
285    ///
286    /// A detailed description on how you can figure out the system and device code
287    /// can be found [here](remote_switch_bricklet_type_c_system_and_device_code).
288    ///
289    ///
290    /// .. versionadded:: 2.0.1$nbsp;(Plugin)
291    ///
292    /// Associated constants:
293    /// * REMOTE_SWITCH_BRICKLET_SWITCH_TO_OFF
294    ///	* REMOTE_SWITCH_BRICKLET_SWITCH_TO_ON
295    pub async fn switch_socket_c(&mut self, system_code: char, device_code: u8, switch_to: u8) -> Result<(), TinkerforgeError> {
296        let mut payload = [0; 3];
297        system_code.write_to_slice(&mut payload[0..1]);
298        device_code.write_to_slice(&mut payload[1..2]);
299        switch_to.write_to_slice(&mut payload[2..3]);
300
301        #[allow(unused_variables)]
302        let result = self.device.set(u8::from(RemoteSwitchBrickletFunction::SwitchSocketC), &payload).await?;
303        Ok(())
304    }
305
306    /// Returns the UID, the UID where the Bricklet is connected to,
307    /// the position, the hardware and firmware version as well as the
308    /// device identifier.
309    ///
310    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
311    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
312    /// position 'z'.
313    ///
314    /// The device identifier numbers can be found [here](device_identifier).
315    /// |device_identifier_constant|
316    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
317        let payload = [0; 0];
318
319        #[allow(unused_variables)]
320        let result = self.device.get(u8::from(RemoteSwitchBrickletFunction::GetIdentity), &payload).await?;
321        Ok(Identity::from_le_byte_slice(result.body()))
322    }
323}