tapo/api/child_devices/
t110_handler.rs1use 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, T110Result};
9use crate::responses::{T110Log, TriggerLogsResult};
10
11pub struct T110Handler {
13 client: Arc<RwLock<ApiClient>>,
14 device_id: String,
15}
16
17impl T110Handler {
18 pub(crate) fn new(client: Arc<RwLock<ApiClient>>, device_id: String) -> Self {
19 Self { client, device_id }
20 }
21
22 pub async fn get_device_info(&self) -> Result<T110Result, Error> {
25 let request = TapoRequest::GetDeviceInfo(TapoParams::new(EmptyParams));
26
27 self.client
28 .read()
29 .await
30 .control_child::<T110Result>(self.device_id.clone(), request)
31 .await?
32 .ok_or_else(|| Error::Tapo(TapoResponseError::EmptyResult))
33 .map(|result| result.decode())?
34 }
35
36 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 pub async fn get_trigger_logs(
58 &self,
59 page_size: u64,
60 start_id: u64,
61 ) -> Result<TriggerLogsResult<T110Log>, 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}