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
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
28ce9c9d82
commit
40c0ac5144
|
@ -12,13 +12,12 @@ use tracing::debug;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
auth::OpenIDConfig,
|
auth::OpenIDConfig,
|
||||||
debug_bridge::DebugBridgeConfig,
|
devices::{
|
||||||
devices::{AudioSetup, ContactSensor, Device, IkeaOutlet, KasaOutlet, WakeOnLAN},
|
AudioSetup, ContactSensor, DebugBridgeConfig, Device, HueBridgeConfig, IkeaOutlet,
|
||||||
|
KasaOutlet, LightSensorConfig, PresenceConfig, WakeOnLAN,
|
||||||
|
},
|
||||||
error::{ConfigParseError, CreateDeviceError, MissingEnv},
|
error::{ConfigParseError, CreateDeviceError, MissingEnv},
|
||||||
event::EventChannel,
|
event::EventChannel,
|
||||||
hue_bridge::HueBridgeConfig,
|
|
||||||
light_sensor::LightSensorConfig,
|
|
||||||
presence::PresenceConfig,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
|
|
|
@ -1,13 +1,21 @@
|
||||||
mod audio_setup;
|
mod audio_setup;
|
||||||
mod contact_sensor;
|
mod contact_sensor;
|
||||||
|
mod debug_bridge;
|
||||||
|
mod hue_bridge;
|
||||||
mod ikea_outlet;
|
mod ikea_outlet;
|
||||||
mod kasa_outlet;
|
mod kasa_outlet;
|
||||||
|
mod light_sensor;
|
||||||
|
mod presence;
|
||||||
mod wake_on_lan;
|
mod wake_on_lan;
|
||||||
|
|
||||||
pub use self::audio_setup::AudioSetup;
|
pub use self::audio_setup::AudioSetup;
|
||||||
pub use self::contact_sensor::ContactSensor;
|
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::ikea_outlet::IkeaOutlet;
|
||||||
pub use self::kasa_outlet::KasaOutlet;
|
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;
|
pub use self::wake_on_lan::WakeOnLAN;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
@ -20,11 +28,11 @@ use tokio::sync::{mpsc, oneshot};
|
||||||
use tracing::{debug, error, instrument, trace};
|
use tracing::{debug, error, instrument, trace};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
event::OnDarkness,
|
||||||
|
event::OnMqtt,
|
||||||
|
event::OnNotification,
|
||||||
|
event::OnPresence,
|
||||||
event::{Event, EventChannel},
|
event::{Event, EventChannel},
|
||||||
traits::OnDarkness,
|
|
||||||
traits::OnMqtt,
|
|
||||||
traits::OnNotification,
|
|
||||||
traits::OnPresence,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[impl_cast::device(As: OnMqtt + OnPresence + OnDarkness + OnNotification + GoogleHomeDevice + OnOff)]
|
#[impl_cast::device(As: OnMqtt + OnPresence + OnDarkness + OnNotification + GoogleHomeDevice + OnOff)]
|
||||||
|
|
|
@ -8,9 +8,9 @@ use crate::{
|
||||||
config::{self, CreateDevice, MqttDeviceConfig},
|
config::{self, CreateDevice, MqttDeviceConfig},
|
||||||
error::CreateDeviceError,
|
error::CreateDeviceError,
|
||||||
event::EventChannel,
|
event::EventChannel,
|
||||||
|
event::OnMqtt,
|
||||||
|
event::OnPresence,
|
||||||
messages::{RemoteAction, RemoteMessage},
|
messages::{RemoteAction, RemoteMessage},
|
||||||
traits::OnMqtt,
|
|
||||||
traits::OnPresence,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{As, Device};
|
use super::{As, Device};
|
||||||
|
|
|
@ -8,12 +8,12 @@ use tracing::{debug, error, trace, warn};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
config::{CreateDevice, MqttDeviceConfig},
|
config::{CreateDevice, MqttDeviceConfig},
|
||||||
|
devices::DEFAULT_PRESENCE,
|
||||||
error::{CreateDeviceError, MissingWildcard},
|
error::{CreateDeviceError, MissingWildcard},
|
||||||
event::EventChannel,
|
event::EventChannel,
|
||||||
|
event::OnMqtt,
|
||||||
|
event::OnPresence,
|
||||||
messages::{ContactMessage, PresenceMessage},
|
messages::{ContactMessage, PresenceMessage},
|
||||||
presence,
|
|
||||||
traits::OnMqtt,
|
|
||||||
traits::OnPresence,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::Device;
|
use super::Device;
|
||||||
|
@ -94,7 +94,7 @@ impl CreateDevice for ContactSensor {
|
||||||
mqtt: config.mqtt,
|
mqtt: config.mqtt,
|
||||||
presence,
|
presence,
|
||||||
client: client.clone(),
|
client: client.clone(),
|
||||||
overall_presence: presence::DEFAULT,
|
overall_presence: DEFAULT_PRESENCE,
|
||||||
is_closed: true,
|
is_closed: true,
|
||||||
handle: None,
|
handle: None,
|
||||||
})
|
})
|
||||||
|
|
|
@ -3,12 +3,12 @@ use rumqttc::AsyncClient;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use tracing::warn;
|
use tracing::warn;
|
||||||
|
|
||||||
|
use crate::devices::Device;
|
||||||
|
use crate::event::OnDarkness;
|
||||||
|
use crate::event::OnPresence;
|
||||||
use crate::{
|
use crate::{
|
||||||
config::MqttDeviceConfig,
|
config::MqttDeviceConfig,
|
||||||
devices::Device,
|
|
||||||
messages::{DarknessMessage, PresenceMessage},
|
messages::{DarknessMessage, PresenceMessage},
|
||||||
traits::OnDarkness,
|
|
||||||
traits::OnPresence,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
|
@ -4,7 +4,7 @@ use async_trait::async_trait;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tracing::{error, trace, warn};
|
use tracing::{error, trace, warn};
|
||||||
|
|
||||||
use crate::{devices::Device, traits::OnDarkness, traits::OnPresence};
|
use crate::{devices::Device, event::OnDarkness, event::OnPresence};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Flag {
|
pub enum Flag {
|
|
@ -17,9 +17,9 @@ use crate::config::{CreateDevice, InfoConfig, MqttDeviceConfig};
|
||||||
use crate::devices::Device;
|
use crate::devices::Device;
|
||||||
use crate::error::CreateDeviceError;
|
use crate::error::CreateDeviceError;
|
||||||
use crate::event::EventChannel;
|
use crate::event::EventChannel;
|
||||||
|
use crate::event::OnMqtt;
|
||||||
|
use crate::event::OnPresence;
|
||||||
use crate::messages::OnOffMessage;
|
use crate::messages::OnOffMessage;
|
||||||
use crate::traits::OnMqtt;
|
|
||||||
use crate::traits::OnPresence;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, PartialEq, Eq, Copy)]
|
#[derive(Debug, Clone, Deserialize, PartialEq, Eq, Copy)]
|
||||||
pub enum OutletType {
|
pub enum OutletType {
|
||||||
|
|
|
@ -6,9 +6,9 @@ use tracing::{debug, trace, warn};
|
||||||
use crate::{
|
use crate::{
|
||||||
config::MqttDeviceConfig,
|
config::MqttDeviceConfig,
|
||||||
devices::Device,
|
devices::Device,
|
||||||
|
event::OnMqtt,
|
||||||
event::{self, Event, EventChannel},
|
event::{self, Event, EventChannel},
|
||||||
messages::BrightnessMessage,
|
messages::BrightnessMessage,
|
||||||
traits::OnMqtt,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
|
@ -8,9 +8,9 @@ use tracing::{debug, warn};
|
||||||
use crate::{
|
use crate::{
|
||||||
config::MqttDeviceConfig,
|
config::MqttDeviceConfig,
|
||||||
devices::Device,
|
devices::Device,
|
||||||
|
event::OnMqtt,
|
||||||
event::{self, Event, EventChannel},
|
event::{self, Event, EventChannel},
|
||||||
messages::PresenceMessage,
|
messages::PresenceMessage,
|
||||||
traits::OnMqtt,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
|
@ -19,7 +19,7 @@ pub struct PresenceConfig {
|
||||||
pub mqtt: MqttDeviceConfig,
|
pub mqtt: MqttDeviceConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const DEFAULT: bool = false;
|
pub const DEFAULT_PRESENCE: bool = false;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Presence {
|
pub struct Presence {
|
||||||
|
@ -35,7 +35,7 @@ impl Presence {
|
||||||
tx: event_channel.get_tx(),
|
tx: event_channel.get_tx(),
|
||||||
mqtt: config.mqtt,
|
mqtt: config.mqtt,
|
||||||
devices: HashMap::new(),
|
devices: HashMap::new(),
|
||||||
current_overall_presence: DEFAULT,
|
current_overall_presence: DEFAULT_PRESENCE,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -17,8 +17,8 @@ use crate::{
|
||||||
config::{CreateDevice, InfoConfig, MqttDeviceConfig},
|
config::{CreateDevice, InfoConfig, MqttDeviceConfig},
|
||||||
error::CreateDeviceError,
|
error::CreateDeviceError,
|
||||||
event::EventChannel,
|
event::EventChannel,
|
||||||
|
event::OnMqtt,
|
||||||
messages::ActivateMessage,
|
messages::ActivateMessage,
|
||||||
traits::OnMqtt,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::Device;
|
use super::Device;
|
||||||
|
|
29
src/event.rs
29
src/event.rs
|
@ -1,7 +1,11 @@
|
||||||
|
use async_trait::async_trait;
|
||||||
use rumqttc::Publish;
|
use rumqttc::Publish;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
|
use impl_cast::device_trait;
|
||||||
|
|
||||||
use crate::ntfy;
|
use crate::ntfy;
|
||||||
|
use crate::ntfy::Notification;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
|
@ -28,3 +32,28 @@ impl EventChannel {
|
||||||
self.0.clone()
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -2,14 +2,9 @@
|
||||||
#![feature(specialization)]
|
#![feature(specialization)]
|
||||||
pub mod auth;
|
pub mod auth;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod debug_bridge;
|
|
||||||
pub mod devices;
|
pub mod devices;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod event;
|
pub mod event;
|
||||||
pub mod hue_bridge;
|
|
||||||
pub mod light_sensor;
|
|
||||||
pub mod messages;
|
pub mod messages;
|
||||||
pub mod mqtt;
|
pub mod mqtt;
|
||||||
pub mod ntfy;
|
pub mod ntfy;
|
||||||
pub mod presence;
|
|
||||||
pub mod traits;
|
|
||||||
|
|
|
@ -8,14 +8,11 @@ use axum::{
|
||||||
use automation::{
|
use automation::{
|
||||||
auth::{OpenIDConfig, User},
|
auth::{OpenIDConfig, User},
|
||||||
config::Config,
|
config::Config,
|
||||||
debug_bridge::DebugBridge,
|
|
||||||
devices,
|
devices,
|
||||||
|
devices::{DebugBridge, HueBridge, LightSensor, Presence},
|
||||||
error::ApiError,
|
error::ApiError,
|
||||||
hue_bridge::HueBridge,
|
|
||||||
light_sensor::LightSensor,
|
|
||||||
mqtt,
|
mqtt,
|
||||||
ntfy::Ntfy,
|
ntfy::Ntfy,
|
||||||
presence::Presence,
|
|
||||||
};
|
};
|
||||||
use dotenvy::dotenv;
|
use dotenvy::dotenv;
|
||||||
use futures::future::join_all;
|
use futures::future::join_all;
|
||||||
|
|
|
@ -10,7 +10,7 @@ use crate::{
|
||||||
config::NtfyConfig,
|
config::NtfyConfig,
|
||||||
devices::Device,
|
devices::Device,
|
||||||
event::{self, Event, EventChannel},
|
event::{self, Event, EventChannel},
|
||||||
traits::{OnNotification, OnPresence},
|
event::{OnNotification, OnPresence},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type Sender = mpsc::Sender<Notification>;
|
pub type Sender = mpsc::Sender<Notification>;
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user