Moved traits into seperate module

This commit is contained in:
Dreaded_X 2023-04-14 21:53:52 +02:00
parent b7329b58ee
commit 1a9d99fed9
Signed by: Dreaded_X
GPG Key ID: FA5F485356B0D2D4
14 changed files with 61 additions and 45 deletions

View File

@ -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)]

View File

@ -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)]

View File

@ -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};

View File

@ -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;

View File

@ -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 {

View File

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

View File

@ -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 {

View File

@ -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 {

View File

@ -11,3 +11,4 @@ pub mod light_sensor;
pub mod mqtt;
pub mod ntfy;
pub mod presence;
pub mod traits;

View File

@ -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)]

View File

@ -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:?}")]

View File

@ -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,

View File

@ -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
View 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);
}