tapo/api/child_devices/
t100_handler.rs

1use std::sync::Arc;
2
3use tokio::sync::RwLock;
4
5use crate::api::ApiClient;
6use crate::error::{Error, TapoResponseError};
7use crate::requests::{EmptyParams, GetTriggerLogsParams, TapoParams, TapoRequest};
8use crate::responses::{DecodableResultExt, T100Result};
9use crate::responses::{T100Log, TriggerLogsResult};
10
11/// Handler for the [T100](https://www.tapo.com/en/search/?q=T100) devices.
12pub struct T100Handler {
13    client: Arc<RwLock<ApiClient>>,
14    device_id: String,
15}
16
17impl T100Handler {
18    pub(crate) fn new(client: Arc<RwLock<ApiClient>>, device_id: String) -> Self {
19        Self { client, device_id }
20    }
21
22    /// Returns *device info* as [`T100Result`].
23    /// It is not guaranteed to contain all the properties returned from the Tapo API.
24    pub async fn get_device_info(&self) -> Result<T100Result, Error> {
25        let request = TapoRequest::GetDeviceInfo(TapoParams::new(EmptyParams));
26
27        self.client
28            .read()
29            .await
30            .control_child::<T100Result>(self.device_id.clone(), request)
31            .await?
32            .ok_or_else(|| Error::Tapo(TapoResponseError::EmptyResult))
33            .map(|result| result.decode())?
34    }
35
36    /// Returns *device info* as [`serde_json::Value`].
37    /// It contains all the properties returned from the Tapo API.
38    pub async fn get_device_info_json(&self) -> Result<serde_json::Value, Error> {
39        let request = TapoRequest::GetDeviceInfo(TapoParams::new(EmptyParams));
40
41        self.client
42            .read()
43            .await
44            .control_child::<serde_json::Value>(self.device_id.clone(), request)
45            .await?
46            .ok_or_else(|| Error::Tapo(TapoResponseError::EmptyResult))
47    }
48
49    /// Returns a list of *trigger logs*.
50    ///
51    /// # Arguments
52    ///
53    /// * `page_size` - the maximum number of log items to return
54    /// * `start_id` - the log item `id` from which to start returning results in reverse chronological order (newest first)
55    ///
56    /// Use a `start_id` of `0` to get the most recent X logs, where X is capped by `page_size`.
57    pub async fn get_trigger_logs(
58        &self,
59        page_size: u64,
60        start_id: u64,
61    ) -> Result<TriggerLogsResult<T100Log>, Error> {
62        let child_params = GetTriggerLogsParams::new(page_size, start_id);
63        let child_request = TapoRequest::GetTriggerLogs(Box::new(TapoParams::new(child_params)));
64
65        self.client
66            .read()
67            .await
68            .control_child(self.device_id.clone(), child_request)
69            .await?
70            .ok_or_else(|| Error::Tapo(TapoResponseError::EmptyResult))
71    }
72}