use serde_json::json;
use crate::error::Result;
use crate::types::AqaraValueResponse;
use crate::types::networking::{
CloseConnectParams, OpenConnectParams, QueryBindKeyParams, QueryBindParams,
QueryDeviceSupportGatewayParams, QueryPositionSupportGatewayParams,
};
#[cfg(feature = "async")]
use crate::Client;
#[cfg(feature = "blocking")]
use crate::BlockingClient;
#[cfg(feature = "async")]
#[derive(Clone)]
pub struct NetworkingService {
client: Client,
}
#[cfg(feature = "async")]
impl NetworkingService {
pub(crate) fn new(client: Client) -> Self {
Self { client }
}
pub async fn bind_key(&self, params: QueryBindKeyParams) -> Result<AqaraValueResponse> {
let data = json!({
"positionId": params.position_id.unwrap_or_default(),
"connectType": params.connect_type.unwrap_or_else(|| "lumi".to_string()),
});
self.client
.call_json("query.device.bindKey", data, true, true)
.await
}
pub async fn bind(&self, params: QueryBindParams) -> Result<AqaraValueResponse> {
let data = json!({ "bindKey": params.bind_key.expose() });
self.client
.call_json("query.device.bind", data, true, true)
.await
}
pub async fn open_connect(&self, params: OpenConnectParams) -> Result<AqaraValueResponse> {
let data = json!({ "did": params.did });
self.client
.call_json("write.device.openConnect", data, true, false)
.await
}
pub async fn close_connect(&self, params: CloseConnectParams) -> Result<AqaraValueResponse> {
let data = json!({ "did": params.did });
self.client
.call_json("write.device.closeConnect", data, true, false)
.await
}
pub async fn support_gateway(
&self,
params: QueryDeviceSupportGatewayParams,
) -> Result<AqaraValueResponse> {
let data = json!({ "model": params.model });
self.client
.call_json("query.device.supportGateway", data, true, true)
.await
}
pub async fn support_gateway_by_position(
&self,
params: QueryPositionSupportGatewayParams,
) -> Result<AqaraValueResponse> {
let data = json!({
"positionId": params.position_id.unwrap_or_default(),
"model": params.model,
"pageNum": params.page_num.to_string(),
"pageSize": params.page_size.to_string(),
});
self.client
.call_json("query.position.supportGateway", data, true, true)
.await
}
}
#[cfg(feature = "blocking")]
#[derive(Clone)]
pub struct BlockingNetworkingService {
client: BlockingClient,
}
#[cfg(feature = "blocking")]
impl BlockingNetworkingService {
pub(crate) fn new(client: BlockingClient) -> Self {
Self { client }
}
pub fn bind_key(&self, params: QueryBindKeyParams) -> Result<AqaraValueResponse> {
let data = json!({
"positionId": params.position_id.unwrap_or_default(),
"connectType": params.connect_type.unwrap_or_else(|| "lumi".to_string()),
});
self.client
.call_json("query.device.bindKey", data, true, true)
}
pub fn bind(&self, params: QueryBindParams) -> Result<AqaraValueResponse> {
let data = json!({ "bindKey": params.bind_key.expose() });
self.client.call_json("query.device.bind", data, true, true)
}
pub fn open_connect(&self, params: OpenConnectParams) -> Result<AqaraValueResponse> {
let data = json!({ "did": params.did });
self.client
.call_json("write.device.openConnect", data, true, false)
}
pub fn close_connect(&self, params: CloseConnectParams) -> Result<AqaraValueResponse> {
let data = json!({ "did": params.did });
self.client
.call_json("write.device.closeConnect", data, true, false)
}
pub fn support_gateway(
&self,
params: QueryDeviceSupportGatewayParams,
) -> Result<AqaraValueResponse> {
let data = json!({ "model": params.model });
self.client
.call_json("query.device.supportGateway", data, true, true)
}
pub fn support_gateway_by_position(
&self,
params: QueryPositionSupportGatewayParams,
) -> Result<AqaraValueResponse> {
let data = json!({
"positionId": params.position_id.unwrap_or_default(),
"model": params.model,
"pageNum": params.page_num.to_string(),
"pageSize": params.page_size.to_string(),
});
self.client
.call_json("query.position.supportGateway", data, true, true)
}
}