Moved traits into seperate module
This commit is contained in:
parent
b7329b58ee
commit
1a9d99fed9
|
@ -6,9 +6,9 @@ use tracing::warn;
|
|||
use crate::{
|
||||
config::MqttDeviceConfig,
|
||||
devices::Device,
|
||||
light_sensor::OnDarkness,
|
||||
mqtt::{DarknessMessage, PresenceMessage},
|
||||
presence::OnPresence,
|
||||
traits::OnDarkness,
|
||||
traits::OnPresence,
|
||||
};
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
|
|
|
@ -21,10 +21,10 @@ use tracing::{debug, error, instrument, trace};
|
|||
|
||||
use crate::{
|
||||
event::{Event, EventChannel},
|
||||
light_sensor::OnDarkness,
|
||||
mqtt::OnMqtt,
|
||||
ntfy::OnNotification,
|
||||
presence::OnPresence,
|
||||
traits::OnDarkness,
|
||||
traits::OnMqtt,
|
||||
traits::OnNotification,
|
||||
traits::OnPresence,
|
||||
};
|
||||
|
||||
#[impl_cast::device(As: OnMqtt + OnPresence + OnDarkness + OnNotification + GoogleHomeDevice + OnOff)]
|
||||
|
|
|
@ -4,11 +4,14 @@ use rumqttc::AsyncClient;
|
|||
use serde::Deserialize;
|
||||
use tracing::{debug, error, trace, warn};
|
||||
|
||||
use crate::config::{self, CreateDevice, MqttDeviceConfig};
|
||||
use crate::error::CreateDeviceError;
|
||||
use crate::event::EventChannel;
|
||||
use crate::mqtt::{OnMqtt, RemoteAction, RemoteMessage};
|
||||
use crate::presence::OnPresence;
|
||||
use crate::{
|
||||
config::{self, CreateDevice, MqttDeviceConfig},
|
||||
error::CreateDeviceError,
|
||||
event::EventChannel,
|
||||
mqtt::{RemoteAction, RemoteMessage},
|
||||
traits::OnMqtt,
|
||||
traits::OnPresence,
|
||||
};
|
||||
|
||||
use super::{As, Device};
|
||||
|
||||
|
|
|
@ -10,8 +10,10 @@ use crate::{
|
|||
config::{CreateDevice, MqttDeviceConfig},
|
||||
error::{CreateDeviceError, MissingWildcard},
|
||||
event::EventChannel,
|
||||
mqtt::{ContactMessage, OnMqtt, PresenceMessage},
|
||||
presence::{self, OnPresence},
|
||||
mqtt::{ContactMessage, PresenceMessage},
|
||||
presence,
|
||||
traits::OnMqtt,
|
||||
traits::OnPresence,
|
||||
};
|
||||
|
||||
use super::Device;
|
||||
|
|
|
@ -17,8 +17,9 @@ use crate::config::{CreateDevice, InfoConfig, MqttDeviceConfig};
|
|||
use crate::devices::Device;
|
||||
use crate::error::CreateDeviceError;
|
||||
use crate::event::EventChannel;
|
||||
use crate::mqtt::{OnMqtt, OnOffMessage};
|
||||
use crate::presence::OnPresence;
|
||||
use crate::mqtt::OnOffMessage;
|
||||
use crate::traits::OnMqtt;
|
||||
use crate::traits::OnPresence;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq, Eq, Copy)]
|
||||
pub enum OutletType {
|
||||
|
|
|
@ -17,7 +17,8 @@ use crate::{
|
|||
config::{CreateDevice, InfoConfig, MqttDeviceConfig},
|
||||
error::CreateDeviceError,
|
||||
event::EventChannel,
|
||||
mqtt::{ActivateMessage, OnMqtt},
|
||||
mqtt::ActivateMessage,
|
||||
traits::OnMqtt,
|
||||
};
|
||||
|
||||
use super::Device;
|
||||
|
|
|
@ -14,6 +14,7 @@ pub enum Event {
|
|||
pub type Sender = mpsc::Sender<Event>;
|
||||
pub type Receiver = mpsc::Receiver<Event>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct EventChannel(Sender);
|
||||
|
||||
impl EventChannel {
|
||||
|
|
|
@ -4,7 +4,7 @@ use async_trait::async_trait;
|
|||
use serde::{Deserialize, Serialize};
|
||||
use tracing::{error, trace, warn};
|
||||
|
||||
use crate::{devices::Device, light_sensor::OnDarkness, presence::OnPresence};
|
||||
use crate::{devices::Device, traits::OnDarkness, traits::OnPresence};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Flag {
|
||||
|
|
|
@ -11,3 +11,4 @@ pub mod light_sensor;
|
|||
pub mod mqtt;
|
||||
pub mod ntfy;
|
||||
pub mod presence;
|
||||
pub mod traits;
|
||||
|
|
|
@ -7,14 +7,10 @@ use crate::{
|
|||
config::MqttDeviceConfig,
|
||||
devices::Device,
|
||||
event::{self, Event, EventChannel},
|
||||
mqtt::{BrightnessMessage, OnMqtt},
|
||||
mqtt::BrightnessMessage,
|
||||
traits::OnMqtt,
|
||||
};
|
||||
|
||||
#[async_trait]
|
||||
pub trait OnDarkness: Sync + Send + 'static {
|
||||
async fn on_darkness(&mut self, dark: bool);
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct LightSensorConfig {
|
||||
#[serde(flatten)]
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use bytes::Bytes;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use thiserror::Error;
|
||||
|
@ -10,13 +9,6 @@ use rumqttc::{Event, EventLoop, Incoming, Publish};
|
|||
|
||||
use crate::event::{self, EventChannel};
|
||||
|
||||
#[async_trait]
|
||||
#[impl_cast::device_trait]
|
||||
pub trait OnMqtt {
|
||||
fn topics(&self) -> Vec<&str>;
|
||||
async fn on_mqtt(&mut self, message: Publish);
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ParseError {
|
||||
#[error("Invalid message payload received: {0:?}")]
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use impl_cast::device_trait;
|
||||
use serde::Serialize;
|
||||
use serde_repr::*;
|
||||
use tokio::sync::mpsc;
|
||||
|
@ -11,18 +10,12 @@ use crate::{
|
|||
config::NtfyConfig,
|
||||
devices::Device,
|
||||
event::{self, Event, EventChannel},
|
||||
presence::OnPresence,
|
||||
traits::{OnNotification, OnPresence},
|
||||
};
|
||||
|
||||
pub type Sender = mpsc::Sender<Notification>;
|
||||
pub type Receiver = mpsc::Receiver<Notification>;
|
||||
|
||||
#[async_trait]
|
||||
#[device_trait]
|
||||
pub trait OnNotification {
|
||||
async fn on_notification(&mut self, notification: Notification);
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Ntfy {
|
||||
base_url: String,
|
||||
|
|
|
@ -9,14 +9,10 @@ use crate::{
|
|||
config::MqttDeviceConfig,
|
||||
devices::Device,
|
||||
event::{self, Event, EventChannel},
|
||||
mqtt::{OnMqtt, PresenceMessage},
|
||||
mqtt::PresenceMessage,
|
||||
traits::OnMqtt,
|
||||
};
|
||||
|
||||
#[async_trait]
|
||||
pub trait OnPresence: Sync + Send + 'static {
|
||||
async fn on_presence(&mut self, presence: bool);
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct PresenceConfig {
|
||||
#[serde(flatten)]
|
||||
|
|
30
src/traits.rs
Normal file
30
src/traits.rs
Normal file
|
@ -0,0 +1,30 @@
|
|||
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