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