Small refactor related to device creation
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
1a9d99fed9
commit
0ad42c029e
|
@ -13,7 +13,7 @@ use tracing::debug;
|
||||||
use crate::{
|
use crate::{
|
||||||
auth::OpenIDConfig,
|
auth::OpenIDConfig,
|
||||||
debug_bridge::DebugBridgeConfig,
|
debug_bridge::DebugBridgeConfig,
|
||||||
devices::{self, AudioSetup, ContactSensor, IkeaOutlet, KasaOutlet, WakeOnLAN},
|
devices::{AudioSetup, ContactSensor, Device, IkeaOutlet, KasaOutlet, WakeOnLAN},
|
||||||
error::{ConfigParseError, CreateDeviceError, MissingEnv},
|
error::{ConfigParseError, CreateDeviceError, MissingEnv},
|
||||||
event::EventChannel,
|
event::EventChannel,
|
||||||
hue_bridge::HueBridgeConfig,
|
hue_bridge::HueBridgeConfig,
|
||||||
|
@ -34,7 +34,7 @@ pub struct Config {
|
||||||
pub hue_bridge: Option<HueBridgeConfig>,
|
pub hue_bridge: Option<HueBridgeConfig>,
|
||||||
pub debug_bridge: Option<DebugBridgeConfig>,
|
pub debug_bridge: Option<DebugBridgeConfig>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub devices: HashMap<String, Device>,
|
pub devices: HashMap<String, DeviceConfig>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
@ -124,12 +124,12 @@ pub struct MqttDeviceConfig {
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub enum Device {
|
pub enum DeviceConfig {
|
||||||
IkeaOutlet(<IkeaOutlet as CreateDevice>::Config),
|
|
||||||
WakeOnLAN(<WakeOnLAN as CreateDevice>::Config),
|
|
||||||
KasaOutlet(<KasaOutlet as CreateDevice>::Config),
|
|
||||||
AudioSetup(<AudioSetup as CreateDevice>::Config),
|
AudioSetup(<AudioSetup as CreateDevice>::Config),
|
||||||
ContactSensor(<ContactSensor as CreateDevice>::Config),
|
ContactSensor(<ContactSensor as CreateDevice>::Config),
|
||||||
|
IkeaOutlet(<IkeaOutlet as CreateDevice>::Config),
|
||||||
|
KasaOutlet(<KasaOutlet as CreateDevice>::Config),
|
||||||
|
WakeOnLAN(<WakeOnLAN as CreateDevice>::Config),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
|
@ -175,37 +175,30 @@ pub trait CreateDevice {
|
||||||
Self: Sized;
|
Self: Sized;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Device {
|
macro_rules! create {
|
||||||
|
(($self:ident, $id:ident, $event_channel:ident, $client:ident, $presence_topic:ident), [ $( $Variant:ident ),* ]) => {
|
||||||
|
match $self {
|
||||||
|
$(DeviceConfig::$Variant(c) => Box::new($Variant::create($id, c, $event_channel, $client, $presence_topic)?),)*
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DeviceConfig {
|
||||||
pub fn create(
|
pub fn create(
|
||||||
self,
|
self,
|
||||||
id: &str,
|
id: &str,
|
||||||
event_channel: &EventChannel,
|
event_channel: &EventChannel,
|
||||||
client: &AsyncClient,
|
client: &AsyncClient,
|
||||||
presence: &str,
|
presence_topic: &str,
|
||||||
) -> Result<Box<dyn devices::Device>, CreateDeviceError> {
|
) -> Result<Box<dyn Device>, CreateDeviceError> {
|
||||||
let device: Box<dyn devices::Device> = match self {
|
Ok(create! {
|
||||||
// TODO: It would be nice if this would be more automatic, not sure how to do that...
|
(self, id, event_channel, client, presence_topic), [
|
||||||
Device::IkeaOutlet(c) => {
|
AudioSetup,
|
||||||
Box::new(IkeaOutlet::create(id, c, event_channel, client, presence)?)
|
ContactSensor,
|
||||||
}
|
IkeaOutlet,
|
||||||
Device::WakeOnLAN(c) => {
|
KasaOutlet,
|
||||||
Box::new(WakeOnLAN::create(id, c, event_channel, client, presence)?)
|
WakeOnLAN
|
||||||
}
|
]
|
||||||
Device::KasaOutlet(c) => {
|
})
|
||||||
Box::new(KasaOutlet::create(id, c, event_channel, client, presence)?)
|
|
||||||
}
|
|
||||||
Device::AudioSetup(c) => {
|
|
||||||
Box::new(AudioSetup::create(id, c, event_channel, client, presence)?)
|
|
||||||
}
|
|
||||||
Device::ContactSensor(c) => Box::new(ContactSensor::create(
|
|
||||||
id,
|
|
||||||
c,
|
|
||||||
event_channel,
|
|
||||||
client,
|
|
||||||
presence,
|
|
||||||
)?),
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(device)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,20 +86,6 @@ impl DevicesHandle {
|
||||||
Ok(rx.await??)
|
Ok(rx.await??)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Finish implementing this
|
|
||||||
// pub fn create_device<T>(&self, identifier: &str, config: T::Config, presence_topic: &str) -> Result<T, CreateDeviceError>
|
|
||||||
// where
|
|
||||||
// T: CreateDevice,
|
|
||||||
// {
|
|
||||||
// T::create(
|
|
||||||
// identifier,
|
|
||||||
// config,
|
|
||||||
// self.event_channel,
|
|
||||||
// self.client,
|
|
||||||
// presence_topic: presence_topic.to_owned(),
|
|
||||||
// )
|
|
||||||
// }
|
|
||||||
|
|
||||||
pub async fn add_device(&self, device: Box<dyn Device>) -> Result<(), DevicesError> {
|
pub async fn add_device(&self, device: Box<dyn Device>) -> Result<(), DevicesError> {
|
||||||
let (tx, rx) = oneshot::channel();
|
let (tx, rx) = oneshot::channel();
|
||||||
self.tx.send(Command::AddDevice { device, tx }).await?;
|
self.tx.send(Command::AddDevice { device, tx }).await?;
|
||||||
|
|
|
@ -19,8 +19,8 @@ use super::{As, Device};
|
||||||
pub struct AudioSetupConfig {
|
pub struct AudioSetupConfig {
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
mqtt: MqttDeviceConfig,
|
mqtt: MqttDeviceConfig,
|
||||||
mixer: Box<config::Device>,
|
mixer: Box<config::DeviceConfig>,
|
||||||
speakers: Box<config::Device>,
|
speakers: Box<config::DeviceConfig>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: We need a better way to store the children devices
|
// TODO: We need a better way to store the children devices
|
||||||
|
|
Loading…
Reference in New Issue
Block a user