use {
crate::{
client::TpLinkClient,
error::Result,
types::{TPLinkDiscoveryData, TPLinkSmartLightData, TPLinkSmartPowerStripRes},
},
serde_json::Value,
std::{net::IpAddr, sync::Arc},
};
#[derive(Clone, Debug)]
pub enum Device {
SmartPlug(SmartPlug),
SmartLight(SmartLight),
SmartDimmer(SmartDimmer),
PowerStrip(PowerStrip),
}
impl Device {
pub fn ip(&self) -> Option<IpAddr> {
match self {
Device::SmartPlug(d) => d.data.ip,
Device::SmartLight(d) => d.data.ip,
Device::SmartDimmer(d) => d.data.ip,
Device::PowerStrip(d) => d.data.ip,
}
}
pub fn ip_string(&self) -> Option<String> {
self.ip().map(|ip| ip.to_string())
}
pub fn alias(&self) -> &str {
match self {
Device::SmartPlug(d) => &d.data.alias,
Device::SmartLight(d) => &d.data.alias,
Device::SmartDimmer(d) => &d.data.alias,
Device::PowerStrip(d) => &d.data.alias,
}
}
pub fn device_id(&self) -> &str {
match self {
Device::SmartPlug(d) => &d.data.device_id,
Device::SmartLight(_) => "light", Device::SmartDimmer(d) => &d.data.device_id,
Device::PowerStrip(d) => &d.data.device_id,
}
}
pub fn mac_address(&self) -> Option<&str> {
match self {
Device::SmartPlug(d) => Some(&d.data.mac),
Device::SmartLight(_) => None, Device::SmartDimmer(d) => Some(&d.data.mac),
Device::PowerStrip(d) => Some(&d.data.mac),
}
}
}
#[derive(Clone, Debug)]
pub struct SmartPlug {
pub data: TPLinkDiscoveryData,
client: Arc<TpLinkClient>,
}
impl SmartPlug {
pub fn new(data: TPLinkDiscoveryData, client: Arc<TpLinkClient>) -> Self {
Self { data, client }
}
pub async fn turn_on(&self) -> Result<()> {
let ip =
self.ip_string()
.ok_or_else(|| crate::error::TpLinkError::DeviceCommunication {
message: "Device has no IP address".to_string(),
})?;
self.client.turn_plug_on(&ip).await
}
pub async fn turn_off(&self) -> Result<()> {
let ip =
self.ip_string()
.ok_or_else(|| crate::error::TpLinkError::DeviceCommunication {
message: "Device has no IP address".to_string(),
})?;
self.client.turn_plug_off(&ip).await
}
pub async fn set_alias(&self, alias: &str) -> Result<()> {
let ip =
self.ip_string()
.ok_or_else(|| crate::error::TpLinkError::DeviceCommunication {
message: "Device has no IP address".to_string(),
})?;
self.client.set_plug_alias(&ip, alias).await
}
pub async fn reboot(&self) -> Result<()> {
let ip =
self.ip_string()
.ok_or_else(|| crate::error::TpLinkError::DeviceCommunication {
message: "Device has no IP address".to_string(),
})?;
self.client.reboot_plug(&ip).await
}
pub fn ip_string(&self) -> Option<String> {
self.data.ip.map(|ip| ip.to_string())
}
pub fn alias(&self) -> &str {
&self.data.alias
}
pub fn device_id(&self) -> &str {
&self.data.device_id
}
}
#[derive(Clone, Debug)]
pub struct SmartLight {
pub data: TPLinkSmartLightData,
client: Arc<TpLinkClient>,
}
impl SmartLight {
pub fn new(data: TPLinkSmartLightData, client: Arc<TpLinkClient>) -> Self {
Self { data, client }
}
pub async fn turn_on_off(&self, on: bool) -> Result<()> {
let ip =
self.ip_string()
.ok_or_else(|| crate::error::TpLinkError::DeviceCommunication {
message: "Device has no IP address".to_string(),
})?;
self.client.turn_light_on_off(&ip, on).await
}
pub async fn turn_on(&self) -> Result<()> {
self.turn_on_off(true).await
}
pub async fn turn_off(&self) -> Result<()> {
self.turn_on_off(false).await
}
pub async fn set_brightness(&self, brightness: u8) -> Result<()> {
let ip =
self.ip_string()
.ok_or_else(|| crate::error::TpLinkError::DeviceCommunication {
message: "Device has no IP address".to_string(),
})?;
self.client.set_light_brightness(&ip, brightness).await
}
pub async fn set_color(&self, color: &str) -> Result<()> {
let ip =
self.ip_string()
.ok_or_else(|| crate::error::TpLinkError::DeviceCommunication {
message: "Device has no IP address".to_string(),
})?;
self.client.set_light_color(&ip, color).await
}
pub fn ip_string(&self) -> Option<String> {
self.data.ip.map(|ip| ip.to_string())
}
pub fn alias(&self) -> &str {
&self.data.alias
}
pub fn is_dimmable(&self) -> bool {
self.data.is_dimmable == 1
}
pub fn is_color(&self) -> bool {
self.data.is_color == 1
}
}
#[derive(Clone, Debug)]
pub struct SmartDimmer {
pub data: TPLinkDiscoveryData,
client: Arc<TpLinkClient>,
}
impl SmartDimmer {
pub fn new(data: TPLinkDiscoveryData, client: Arc<TpLinkClient>) -> Self {
Self { data, client }
}
pub async fn set_brightness(&self, brightness: u8) -> Result<()> {
let ip =
self.ip_string()
.ok_or_else(|| crate::error::TpLinkError::DeviceCommunication {
message: "Device has no IP address".to_string(),
})?;
self.client.set_dimmer_brightness(&ip, brightness).await
}
pub async fn set_inactivity_timeout(&self, timeout_minutes: u8) -> Result<()> {
let ip =
self.ip_string()
.ok_or_else(|| crate::error::TpLinkError::DeviceCommunication {
message: "Device has no IP address".to_string(),
})?;
self.client
.set_dimmer_inactivity_timeout(&ip, timeout_minutes)
.await
}
pub fn ip_string(&self) -> Option<String> {
self.data.ip.map(|ip| ip.to_string())
}
pub fn alias(&self) -> &str {
&self.data.alias
}
pub fn device_id(&self) -> &str {
&self.data.device_id
}
}
#[derive(Clone, Debug)]
pub struct PowerStrip {
pub data: TPLinkSmartPowerStripRes,
client: Arc<TpLinkClient>,
}
impl PowerStrip {
pub fn new(data: TPLinkSmartPowerStripRes, client: Arc<TpLinkClient>) -> Self {
Self { data, client }
}
pub async fn turn_socket_on(&self, socket_id: &str) -> Result<()> {
let ip =
self.ip_string()
.ok_or_else(|| crate::error::TpLinkError::DeviceCommunication {
message: "Device has no IP address".to_string(),
})?;
self.client.turn_power_strip_socket_on(&ip, socket_id).await
}
pub async fn turn_socket_off(&self, socket_id: &str) -> Result<()> {
let ip =
self.ip_string()
.ok_or_else(|| crate::error::TpLinkError::DeviceCommunication {
message: "Device has no IP address".to_string(),
})?;
self.client
.turn_power_strip_socket_off(&ip, socket_id)
.await
}
pub async fn get_energy_usage(&self, socket_id: &str) -> Result<Value> {
let ip =
self.ip_string()
.ok_or_else(|| crate::error::TpLinkError::DeviceCommunication {
message: "Device has no IP address".to_string(),
})?;
self.client
.get_power_strip_energy_usage(&ip, socket_id)
.await
}
pub async fn get_emeter_data(&self, socket_id: &str) -> Result<Value> {
let ip =
self.ip_string()
.ok_or_else(|| crate::error::TpLinkError::DeviceCommunication {
message: "Device has no IP address".to_string(),
})?;
self.client
.get_power_strip_emeter_data(&ip, socket_id)
.await
}
pub fn ip_string(&self) -> Option<String> {
self.data.ip.map(|ip| ip.to_string())
}
pub fn alias(&self) -> &str {
&self.data.alias
}
pub fn device_id(&self) -> &str {
&self.data.device_id
}
pub fn sockets(&self) -> &[crate::types::Child] {
&self.data.children
}
}