Switched to channels for communication between different parts of the code

This commit is contained in:
2023-01-09 23:50:50 +01:00
parent cf88768c15
commit 5b9d24e82f
18 changed files with 285 additions and 251 deletions

View File

@@ -1,3 +1,4 @@
use async_trait::async_trait;
use google_home::traits;
use rumqttc::{AsyncClient, matches};
use tracing::{error, warn, debug};
@@ -11,6 +12,7 @@ use super::Device;
// @TODO Ideally we store am Arc to the childern devices,
// that way they hook into everything just like all other devices
#[derive(Debug)]
pub struct AudioSetup {
identifier: String,
mqtt: MqttDeviceConfig,
@@ -71,8 +73,9 @@ impl OnMqtt for AudioSetup {
}
}
#[async_trait]
impl OnPresence for AudioSetup {
fn on_presence(&mut self, presence: bool) {
async fn on_presence(&mut self, presence: bool) {
// Turn off the audio setup when we leave the house
if !presence {
debug!(id = self.identifier, "Turning devices off");

View File

@@ -1,5 +1,6 @@
use std::time::Duration;
use async_trait::async_trait;
use pollster::FutureExt;
use rumqttc::{AsyncClient, matches};
use tokio::task::JoinHandle;
@@ -9,6 +10,7 @@ use crate::{config::{MqttDeviceConfig, PresenceDeviceConfig}, mqtt::{OnMqtt, Con
use super::Device;
#[derive(Debug)]
pub struct ContactSensor {
identifier: String,
mqtt: MqttDeviceConfig,
@@ -42,8 +44,9 @@ impl Device for ContactSensor {
}
}
#[async_trait]
impl OnPresence for ContactSensor {
fn on_presence(&mut self, presence: bool) {
async fn on_presence(&mut self, presence: bool) {
self.overall_presence = presence;
}
}

View File

@@ -1,5 +1,6 @@
use std::time::Duration;
use async_trait::async_trait;
use google_home::errors::ErrorCode;
use google_home::{GoogleHomeDevice, device, types::Type, traits};
use rumqttc::{AsyncClient, Publish, matches};
@@ -12,6 +13,7 @@ use crate::devices::Device;
use crate::mqtt::{OnMqtt, OnOffMessage};
use crate::presence::OnPresence;
#[derive(Debug)]
pub struct IkeaOutlet {
identifier: String,
info: InfoConfig,
@@ -108,12 +110,13 @@ impl OnMqtt for IkeaOutlet {
}
}
#[async_trait]
impl OnPresence for IkeaOutlet {
fn on_presence(&mut self, presence: bool) {
async fn on_presence(&mut self, presence: bool) {
// Turn off the outlet when we leave the house
if !presence {
debug!(id = self.identifier, "Turning device off");
set_on(self.client.clone(), self.mqtt.topic.clone(), false).block_on();
set_on(self.client.clone(), self.mqtt.topic.clone(), false).await;
}
}
}

View File

@@ -6,6 +6,7 @@ use serde::{Serialize, Deserialize};
use super::Device;
#[derive(Debug)]
pub struct KasaOutlet {
identifier: String,
addr: SocketAddr,

View File

@@ -7,6 +7,7 @@ use crate::{config::{InfoConfig, MqttDeviceConfig}, mqtt::{OnMqtt, ActivateMessa
use super::Device;
#[derive(Debug)]
pub struct WakeOnLAN {
identifier: String,
info: InfoConfig,