Improved the way devices are instantiated from their respective configs
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-08-16 02:17:21 +02:00
parent ab5e47d1ff
commit b1506f8e63
15 changed files with 530 additions and 364 deletions

View File

@@ -5,14 +5,17 @@ use std::{
use async_trait::async_trait;
use google_home::{errors::ErrorCode, traits::OnOff};
use rumqttc::AsyncClient;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tracing::{debug, error, trace, warn};
use crate::{
config::CreateDevice, device_manager::DeviceManager, devices::Device, error::CreateDeviceError,
event::EventChannel, event::OnDarkness, event::OnPresence, traits::Timeout,
config::{ConfigExternal, DeviceConfig},
devices::Device,
error::DeviceConfigError,
event::OnDarkness,
event::OnPresence,
traits::Timeout,
};
#[derive(Debug)]
@@ -117,8 +120,27 @@ pub struct HueLightConfig {
pub timer_id: isize,
}
#[async_trait]
impl DeviceConfig for HueLightConfig {
async fn create(
self,
identifier: &str,
_ext: &ConfigExternal,
) -> Result<Box<dyn Device>, DeviceConfigError> {
let device = HueLight {
identifier: identifier.to_owned(),
addr: (self.ip, 80).into(),
login: self.login,
light_id: self.light_id,
timer_id: self.timer_id,
};
Ok(Box::new(device))
}
}
#[derive(Debug)]
pub struct HueLight {
struct HueLight {
pub identifier: String,
pub addr: SocketAddr,
pub login: String,
@@ -126,28 +148,6 @@ pub struct HueLight {
pub timer_id: isize,
}
#[async_trait]
impl CreateDevice for HueLight {
type Config = HueLightConfig;
async fn create(
identifier: &str,
config: Self::Config,
_event_channel: &EventChannel,
_client: &AsyncClient,
_presence_topic: &str,
_devices: &DeviceManager,
) -> Result<Self, CreateDeviceError> {
Ok(Self {
identifier: identifier.to_owned(),
addr: (config.ip, 80).into(),
login: config.login,
light_id: config.light_id,
timer_id: config.timer_id,
})
}
}
impl Device for HueLight {
fn get_id(&self) -> &str {
&self.identifier