use std::sync::Arc;
use reqwest::Method;
use serde_json::{Map, Value, json};
use url::Url;
use crate::endpoints::blacklist::BlacklistApi;
use crate::envelope::Envelope;
use crate::error::Error;
use crate::routes;
use crate::transport::{RequestBody, Transport};
#[derive(Debug)]
pub struct DevicesApi {
transport: Arc<Transport>,
}
impl DevicesApi {
#[must_use]
pub fn new(transport: Arc<Transport>) -> Self {
Self { transport }
}
pub async fn get_devices(
&self,
network_id: &str,
thread: Option<bool>,
proxied_node: Option<bool>,
parent: Option<&Value>,
) -> Result<Envelope, Error> {
let mut query = Vec::new();
if let Some(thread) = thread {
query.push(("thread", bool_param(thread)));
}
if let Some(proxied_node) = proxied_node {
query.push(("proxied_node", bool_param(proxied_node)));
}
self.transport
.resource(
&routes::devices::V8_GET_DEVICES,
network_id,
parent,
&query,
RequestBody::None,
)
.await
}
pub async fn get_device(
&self,
network_id: &str,
mac: &str,
parent: Option<&Value>,
) -> Result<Envelope, Error> {
let url = self.resolve_device_url(network_id, mac, parent)?;
self.transport
.request(Method::GET, url, &[], RequestBody::None)
.await
}
fn resolve_device_url(
&self,
network_id: &str,
mac: &str,
parent: Option<&Value>,
) -> Result<Url, Error> {
if let Some(parent) = parent
&& let Some(url) = crate::links::self_url(self.transport.api_host(), parent)?
{
return Ok(url);
}
routes::devices::V8_GET_DEVICE.resolve(self.transport.api_host(), network_id, mac, None)
}
pub async fn set_device_nickname(
&self,
network_id: &str,
mac: &str,
nickname: &str,
) -> Result<Envelope, Error> {
let normalized_mac = crate::util::id_from_url(mac)?;
let url = routes::devices::V8_SET_DEVICE_NICKNAME.resolve(
self.transport.api_host(),
network_id,
&normalized_mac,
None,
)?;
self.transport
.request(
Method::PUT,
url,
&[],
RequestBody::Json(json!({ "nickname": nickname })),
)
.await
}
pub async fn pause_device(
&self,
network_id: &str,
mac: &str,
paused: bool,
) -> Result<Envelope, Error> {
let normalized_mac = crate::util::id_from_url(mac)?;
let url = routes::devices::V8_PAUSE_DEVICE.resolve(
self.transport.api_host(),
network_id,
&normalized_mac,
None,
)?;
self.transport
.request(
Method::PUT,
url,
&[],
RequestBody::Json(json!({ "paused": paused })),
)
.await
}
pub async fn update_device_via_link(
&self,
network_id: &str,
mac: &str,
nickname: Option<&str>,
paused: Option<bool>,
profile: Option<&str>,
parent: Option<&Value>,
) -> Result<Envelope, Error> {
let mut body = Map::new();
if let Some(nickname) = nickname {
body.insert("nickname".to_owned(), json!(nickname));
}
if let Some(paused) = paused {
body.insert("paused".to_owned(), json!(paused));
}
if let Some(profile) = profile {
body.insert("profile".to_owned(), json!(profile));
}
if body.is_empty() {
return Err(Error::validation(
"device",
"at least one of nickname, paused, profile must be supplied",
));
}
let url = self.resolve_device_url(network_id, mac, parent)?;
crate::links::warn_uncharacterised_write("update_device_via_link");
self.transport
.request(
Method::PUT,
url,
&[],
RequestBody::Json(Value::Object(body)),
)
.await
}
pub async fn set_device_type(
&self,
network_id: &str,
mac: &str,
device_type: &str,
) -> Result<Envelope, Error> {
let url = routes::devices::SET_DEVICE_TYPE.resolve(
self.transport.api_host(),
network_id,
mac,
None,
)?;
self.transport
.request(
Method::PUT,
url,
&[],
RequestBody::Json(json!({ "device_type": device_type })),
)
.await
}
pub async fn get_device_labels(&self, network_id: &str, mac: &str) -> Result<Envelope, Error> {
let url = routes::devices::GET_DEVICE_LABELS.resolve(
self.transport.api_host(),
network_id,
mac,
None,
)?;
self.transport
.request(Method::GET, url, &[], RequestBody::None)
.await
}
#[allow(clippy::too_many_arguments)]
pub async fn set_device_labels(
&self,
network_id: &str,
mac: &str,
make_label: Option<&str>,
model_label: Option<&str>,
version_label: Option<&str>,
type_label: Option<&str>,
) -> Result<Envelope, Error> {
let url = routes::devices::SET_DEVICE_LABELS.resolve(
self.transport.api_host(),
network_id,
mac,
None,
)?;
let mut query = Vec::new();
if let Some(value) = make_label {
query.push(("make_label", value.to_owned()));
}
if let Some(value) = model_label {
query.push(("model_label", value.to_owned()));
}
if let Some(value) = version_label {
query.push(("version_label", value.to_owned()));
}
if let Some(value) = type_label {
query.push(("type_label", value.to_owned()));
}
crate::links::warn_uncharacterised_write("set_device_labels");
self.transport
.request(Method::PUT, url, &query, RequestBody::None)
.await
}
pub async fn block_device(&self, network_id: &str, mac: &str) -> Result<Envelope, Error> {
BlacklistApi::new(Arc::clone(&self.transport))
.add_to_blacklist(network_id, mac, None)
.await
}
pub async fn unblock_device(&self, network_id: &str, mac: &str) -> Result<Envelope, Error> {
BlacklistApi::new(Arc::clone(&self.transport))
.remove_from_blacklist(network_id, mac, None)
.await
}
}
fn bool_param(value: bool) -> String {
if value {
"true".to_owned()
} else {
"false".to_owned()
}
}