Moved quasi-devices into the devices module and made event related device traits part of the event module
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Dreaded_X 2023-04-24 04:28:17 +02:00
parent 28ce9c9d82
commit 40c0ac5144
Signed by: Dreaded_X
GPG Key ID: FA5F485356B0D2D4
15 changed files with 64 additions and 66 deletions

View File

@ -12,13 +12,12 @@ use tracing::debug;
use crate::{
auth::OpenIDConfig,
debug_bridge::DebugBridgeConfig,
devices::{AudioSetup, ContactSensor, Device, IkeaOutlet, KasaOutlet, WakeOnLAN},
devices::{
AudioSetup, ContactSensor, DebugBridgeConfig, Device, HueBridgeConfig, IkeaOutlet,
KasaOutlet, LightSensorConfig, PresenceConfig, WakeOnLAN,
},
error::{ConfigParseError, CreateDeviceError, MissingEnv},
event::EventChannel,
hue_bridge::HueBridgeConfig,
light_sensor::LightSensorConfig,
presence::PresenceConfig,
};
#[derive(Debug, Deserialize)]

View File

@ -1,13 +1,21 @@
mod audio_setup;
mod contact_sensor;
mod debug_bridge;
mod hue_bridge;
mod ikea_outlet;
mod kasa_outlet;
mod light_sensor;
mod presence;
mod wake_on_lan;
pub use self::audio_setup::AudioSetup;
pub use self::contact_sensor::ContactSensor;
pub use self::debug_bridge::{DebugBridge, DebugBridgeConfig};
pub use self::hue_bridge::{HueBridge, HueBridgeConfig};
pub use self::ikea_outlet::IkeaOutlet;
pub use self::kasa_outlet::KasaOutlet;
pub use self::light_sensor::{LightSensor, LightSensorConfig};
pub use self::presence::{Presence, PresenceConfig, DEFAULT_PRESENCE};
pub use self::wake_on_lan::WakeOnLAN;
use std::collections::HashMap;
@ -20,11 +28,11 @@ use tokio::sync::{mpsc, oneshot};
use tracing::{debug, error, instrument, trace};
use crate::{
event::OnDarkness,
event::OnMqtt,
event::OnNotification,
event::OnPresence,
event::{Event, EventChannel},
traits::OnDarkness,
traits::OnMqtt,
traits::OnNotification,
traits::OnPresence,
};
#[impl_cast::device(As: OnMqtt + OnPresence + OnDarkness + OnNotification + GoogleHomeDevice + OnOff)]

View File

@ -8,9 +8,9 @@ use crate::{
config::{self, CreateDevice, MqttDeviceConfig},
error::CreateDeviceError,
event::EventChannel,
event::OnMqtt,
event::OnPresence,
messages::{RemoteAction, RemoteMessage},
traits::OnMqtt,
traits::OnPresence,
};
use super::{As, Device};

View File

@ -8,12 +8,12 @@ use tracing::{debug, error, trace, warn};
use crate::{
config::{CreateDevice, MqttDeviceConfig},
devices::DEFAULT_PRESENCE,
error::{CreateDeviceError, MissingWildcard},
event::EventChannel,
event::OnMqtt,
event::OnPresence,
messages::{ContactMessage, PresenceMessage},
presence,
traits::OnMqtt,
traits::OnPresence,
};
use super::Device;
@ -94,7 +94,7 @@ impl CreateDevice for ContactSensor {
mqtt: config.mqtt,
presence,
client: client.clone(),
overall_presence: presence::DEFAULT,
overall_presence: DEFAULT_PRESENCE,
is_closed: true,
handle: None,
})

View File

@ -3,12 +3,12 @@ use rumqttc::AsyncClient;
use serde::Deserialize;
use tracing::warn;
use crate::devices::Device;
use crate::event::OnDarkness;
use crate::event::OnPresence;
use crate::{
config::MqttDeviceConfig,
devices::Device,
messages::{DarknessMessage, PresenceMessage},
traits::OnDarkness,
traits::OnPresence,
};
#[derive(Debug, Deserialize)]

View File

@ -4,7 +4,7 @@ use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use tracing::{error, trace, warn};
use crate::{devices::Device, traits::OnDarkness, traits::OnPresence};
use crate::{devices::Device, event::OnDarkness, event::OnPresence};
#[derive(Debug)]
pub enum Flag {

View File

@ -17,9 +17,9 @@ use crate::config::{CreateDevice, InfoConfig, MqttDeviceConfig};
use crate::devices::Device;
use crate::error::CreateDeviceError;
use crate::event::EventChannel;
use crate::event::OnMqtt;
use crate::event::OnPresence;
use crate::messages::OnOffMessage;
use crate::traits::OnMqtt;
use crate::traits::OnPresence;
#[derive(Debug, Clone, Deserialize, PartialEq, Eq, Copy)]
pub enum OutletType {

View File

@ -6,9 +6,9 @@ use tracing::{debug, trace, warn};
use crate::{
config::MqttDeviceConfig,
devices::Device,
event::OnMqtt,
event::{self, Event, EventChannel},
messages::BrightnessMessage,
traits::OnMqtt,
};
#[derive(Debug, Clone, Deserialize)]

View File

@ -8,9 +8,9 @@ use tracing::{debug, warn};
use crate::{
config::MqttDeviceConfig,
devices::Device,
event::OnMqtt,
event::{self, Event, EventChannel},
messages::PresenceMessage,
traits::OnMqtt,
};
#[derive(Debug, Deserialize)]
@ -19,7 +19,7 @@ pub struct PresenceConfig {
pub mqtt: MqttDeviceConfig,
}
pub const DEFAULT: bool = false;
pub const DEFAULT_PRESENCE: bool = false;
#[derive(Debug)]
pub struct Presence {
@ -35,7 +35,7 @@ impl Presence {
tx: event_channel.get_tx(),
mqtt: config.mqtt,
devices: HashMap::new(),
current_overall_presence: DEFAULT,
current_overall_presence: DEFAULT_PRESENCE,
}
}
}

View File

@ -17,8 +17,8 @@ use crate::{
config::{CreateDevice, InfoConfig, MqttDeviceConfig},
error::CreateDeviceError,
event::EventChannel,
event::OnMqtt,
messages::ActivateMessage,
traits::OnMqtt,
};
use super::Device;

View File

@ -1,7 +1,11 @@
use async_trait::async_trait;
use rumqttc::Publish;
use tokio::sync::mpsc;
use impl_cast::device_trait;
use crate::ntfy;
use crate::ntfy::Notification;
#[derive(Debug, Clone)]
pub enum Event {
@ -28,3 +32,28 @@ impl EventChannel {
self.0.clone()
}
}
#[async_trait]
#[device_trait]
pub trait OnMqtt {
fn topics(&self) -> Vec<&str>;
async fn on_mqtt(&mut self, message: Publish);
}
#[async_trait]
#[device_trait]
pub trait OnPresence {
async fn on_presence(&mut self, presence: bool);
}
#[async_trait]
#[device_trait]
pub trait OnDarkness {
async fn on_darkness(&mut self, dark: bool);
}
#[async_trait]
#[device_trait]
pub trait OnNotification {
async fn on_notification(&mut self, notification: Notification);
}

View File

@ -2,14 +2,9 @@
#![feature(specialization)]
pub mod auth;
pub mod config;
pub mod debug_bridge;
pub mod devices;
pub mod error;
pub mod event;
pub mod hue_bridge;
pub mod light_sensor;
pub mod messages;
pub mod mqtt;
pub mod ntfy;
pub mod presence;
pub mod traits;

View File

@ -8,14 +8,11 @@ use axum::{
use automation::{
auth::{OpenIDConfig, User},
config::Config,
debug_bridge::DebugBridge,
devices,
devices::{DebugBridge, HueBridge, LightSensor, Presence},
error::ApiError,
hue_bridge::HueBridge,
light_sensor::LightSensor,
mqtt,
ntfy::Ntfy,
presence::Presence,
};
use dotenvy::dotenv;
use futures::future::join_all;

View File

@ -10,7 +10,7 @@ use crate::{
config::NtfyConfig,
devices::Device,
event::{self, Event, EventChannel},
traits::{OnNotification, OnPresence},
event::{OnNotification, OnPresence},
};
pub type Sender = mpsc::Sender<Notification>;

View File

@ -1,30 +0,0 @@
use async_trait::async_trait;
use impl_cast::device_trait;
use rumqttc::Publish;
use crate::ntfy::Notification;
#[async_trait]
#[device_trait]
pub trait OnMqtt {
fn topics(&self) -> Vec<&str>;
async fn on_mqtt(&mut self, message: Publish);
}
#[async_trait]
#[device_trait]
pub trait OnPresence {
async fn on_presence(&mut self, presence: bool);
}
#[async_trait]
#[device_trait]
pub trait OnDarkness {
async fn on_darkness(&mut self, dark: bool);
}
#[async_trait]
#[device_trait]
pub trait OnNotification {
async fn on_notification(&mut self, notification: Notification);
}