Improved the internals of the LuaDeviceConfig macro and improve the
usability of the macro
This commit is contained in:
@@ -22,7 +22,7 @@ pub struct AirFilterConfig {
|
||||
info: InfoConfig,
|
||||
#[device_config(flatten)]
|
||||
mqtt: MqttDeviceConfig,
|
||||
#[device_config(user_data)]
|
||||
#[device_config(from_lua)]
|
||||
client: WrappedAsyncClient,
|
||||
}
|
||||
|
||||
|
||||
@@ -14,9 +14,9 @@ use crate::messages::{RemoteAction, RemoteMessage};
|
||||
pub struct AudioSetupConfig {
|
||||
#[device_config(flatten)]
|
||||
mqtt: MqttDeviceConfig,
|
||||
#[device_config(user_data)]
|
||||
#[device_config(from_lua)]
|
||||
mixer: WrappedDevice,
|
||||
#[device_config(user_data)]
|
||||
#[device_config(from_lua)]
|
||||
speakers: WrappedDevice,
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ use crate::device_manager::{DeviceConfig, WrappedDevice};
|
||||
use crate::devices::DEFAULT_PRESENCE;
|
||||
use crate::error::DeviceConfigError;
|
||||
use crate::event::{OnMqtt, OnPresence};
|
||||
use crate::helper::DurationSeconds;
|
||||
use crate::messages::{ContactMessage, PresenceMessage};
|
||||
use crate::mqtt::WrappedAsyncClient;
|
||||
use crate::traits::Timeout;
|
||||
@@ -23,7 +22,7 @@ use crate::traits::Timeout;
|
||||
pub struct PresenceDeviceConfig {
|
||||
#[device_config(flatten)]
|
||||
pub mqtt: MqttDeviceConfig,
|
||||
#[device_config(with = "DurationSeconds")]
|
||||
#[device_config(with(Duration::from_secs))]
|
||||
pub timeout: Duration,
|
||||
}
|
||||
|
||||
@@ -44,9 +43,9 @@ impl From<TriggerDevicesHelper> for Vec<(WrappedDevice, bool)> {
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
pub struct TriggerConfig {
|
||||
#[device_config(user_data, with = "TriggerDevicesHelper")]
|
||||
#[device_config(from_lua, from(TriggerDevicesHelper))]
|
||||
devices: Vec<(WrappedDevice, bool)>,
|
||||
#[device_config(with = "Option<DurationSeconds>")]
|
||||
#[device_config(default, with(|t: Option<_>| t.map(Duration::from_secs)))]
|
||||
pub timeout: Option<Duration>,
|
||||
}
|
||||
|
||||
@@ -54,11 +53,11 @@ pub struct TriggerConfig {
|
||||
pub struct ContactSensorConfig {
|
||||
#[device_config(flatten)]
|
||||
mqtt: MqttDeviceConfig,
|
||||
#[device_config(user_data)]
|
||||
#[device_config(from_lua)]
|
||||
presence: Option<PresenceDeviceConfig>,
|
||||
#[device_config(user_data)]
|
||||
#[device_config(from_lua)]
|
||||
trigger: Option<TriggerConfig>,
|
||||
#[device_config(user_data)]
|
||||
#[device_config(from_lua)]
|
||||
client: WrappedAsyncClient,
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ use crate::mqtt::WrappedAsyncClient;
|
||||
pub struct DebugBridgeConfig {
|
||||
#[device_config(flatten)]
|
||||
pub mqtt: MqttDeviceConfig,
|
||||
#[device_config(user_data)]
|
||||
#[device_config(from_lua)]
|
||||
client: WrappedAsyncClient,
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ use crate::device_manager::DeviceConfig;
|
||||
use crate::devices::Device;
|
||||
use crate::error::DeviceConfigError;
|
||||
use crate::event::{OnDarkness, OnPresence};
|
||||
use crate::helper::Ipv4SocketAddr;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Flag {
|
||||
@@ -25,7 +24,7 @@ pub struct FlagIDs {
|
||||
|
||||
#[derive(Debug, LuaDeviceConfig, Clone)]
|
||||
pub struct HueBridgeConfig {
|
||||
#[device_config(rename = "ip", with = "Ipv4SocketAddr<80>")]
|
||||
#[device_config(rename("ip"), with(|ip| SocketAddr::new(ip, 80)))]
|
||||
pub addr: SocketAddr,
|
||||
pub login: String,
|
||||
pub flags: FlagIDs,
|
||||
|
||||
@@ -14,13 +14,12 @@ use crate::config::MqttDeviceConfig;
|
||||
use crate::device_manager::DeviceConfig;
|
||||
use crate::error::DeviceConfigError;
|
||||
use crate::event::OnMqtt;
|
||||
use crate::helper::Ipv4SocketAddr;
|
||||
use crate::messages::{RemoteAction, RemoteMessage};
|
||||
use crate::traits::Timeout;
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
pub struct HueGroupConfig {
|
||||
#[device_config(rename = "ip", with = "Ipv4SocketAddr<80>")]
|
||||
#[device_config(rename("ip"), with(|ip| SocketAddr::new(ip, 80)))]
|
||||
pub addr: SocketAddr,
|
||||
pub login: String,
|
||||
pub group_id: isize,
|
||||
|
||||
@@ -17,7 +17,6 @@ use crate::device_manager::DeviceConfig;
|
||||
use crate::devices::Device;
|
||||
use crate::error::DeviceConfigError;
|
||||
use crate::event::{OnMqtt, OnPresence};
|
||||
use crate::helper::DurationSeconds;
|
||||
use crate::messages::{OnOffMessage, RemoteAction, RemoteMessage};
|
||||
use crate::mqtt::WrappedAsyncClient;
|
||||
use crate::traits::Timeout;
|
||||
@@ -36,21 +35,17 @@ pub struct IkeaOutletConfig {
|
||||
info: InfoConfig,
|
||||
#[device_config(flatten)]
|
||||
mqtt: MqttDeviceConfig,
|
||||
#[device_config(default = default_outlet_type)]
|
||||
#[device_config(default(OutletType::Outlet))]
|
||||
outlet_type: OutletType,
|
||||
#[device_config(with = "Option<DurationSeconds>")]
|
||||
#[device_config(default, with(|t: Option<_>| t.map(Duration::from_secs)))]
|
||||
timeout: Option<Duration>,
|
||||
#[device_config(default)]
|
||||
pub remotes: Vec<MqttDeviceConfig>,
|
||||
|
||||
#[device_config(user_data)]
|
||||
#[device_config(from_lua)]
|
||||
client: WrappedAsyncClient,
|
||||
}
|
||||
|
||||
fn default_outlet_type() -> OutletType {
|
||||
OutletType::Outlet
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl DeviceConfig for IkeaOutletConfig {
|
||||
async fn create(&self, identifier: &str) -> Result<Box<dyn Device>, DeviceConfigError> {
|
||||
|
||||
@@ -15,11 +15,10 @@ use tracing::trace;
|
||||
use super::Device;
|
||||
use crate::device_manager::DeviceConfig;
|
||||
use crate::error::DeviceConfigError;
|
||||
use crate::helper::Ipv4SocketAddr;
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
pub struct KasaOutletConfig {
|
||||
#[device_config(rename = "ip", with = "Ipv4SocketAddr<9999>")]
|
||||
#[device_config(rename("ip"), with(|ip| SocketAddr::new(ip, 9999)))]
|
||||
addr: SocketAddr,
|
||||
}
|
||||
|
||||
|
||||
@@ -7,8 +7,7 @@ use crate::config::MqttDeviceConfig;
|
||||
use crate::device_manager::DeviceConfig;
|
||||
use crate::devices::Device;
|
||||
use crate::error::DeviceConfigError;
|
||||
use crate::event::{self, Event, OnMqtt};
|
||||
use crate::helper::TxHelper;
|
||||
use crate::event::{self, Event, EventChannel, OnMqtt};
|
||||
use crate::messages::BrightnessMessage;
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
@@ -17,7 +16,7 @@ pub struct LightSensorConfig {
|
||||
pub mqtt: MqttDeviceConfig,
|
||||
pub min: isize,
|
||||
pub max: isize,
|
||||
#[device_config(rename = "event_channel", user_data, with = "TxHelper")]
|
||||
#[device_config(rename("event_channel"), from_lua, with(|ec: EventChannel| ec.get_tx()))]
|
||||
pub tx: event::Sender,
|
||||
}
|
||||
|
||||
|
||||
@@ -24,14 +24,10 @@ pub struct WakeOnLANConfig {
|
||||
#[device_config(flatten)]
|
||||
mqtt: MqttDeviceConfig,
|
||||
mac_address: MacAddress,
|
||||
#[device_config(default = default_broadcast_ip)]
|
||||
#[device_config(default(Ipv4Addr::new(255, 255, 255, 255)))]
|
||||
broadcast_ip: Ipv4Addr,
|
||||
}
|
||||
|
||||
fn default_broadcast_ip() -> Ipv4Addr {
|
||||
Ipv4Addr::new(255, 255, 255, 255)
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl DeviceConfig for WakeOnLANConfig {
|
||||
async fn create(&self, identifier: &str) -> Result<Box<dyn Device>, DeviceConfigError> {
|
||||
|
||||
@@ -8,8 +8,7 @@ use super::{Device, Notification};
|
||||
use crate::config::MqttDeviceConfig;
|
||||
use crate::device_manager::DeviceConfig;
|
||||
use crate::error::DeviceConfigError;
|
||||
use crate::event::{self, Event, OnMqtt};
|
||||
use crate::helper::TxHelper;
|
||||
use crate::event::{self, Event, EventChannel, OnMqtt};
|
||||
use crate::messages::PowerMessage;
|
||||
|
||||
#[derive(Debug, Clone, LuaDeviceConfig)]
|
||||
@@ -18,7 +17,7 @@ pub struct WasherConfig {
|
||||
mqtt: MqttDeviceConfig,
|
||||
// Power in Watt
|
||||
threshold: f32,
|
||||
#[device_config(rename = "event_channel", user_data, with = "TxHelper")]
|
||||
#[device_config(rename("event_channel"), from_lua, with(|ec: EventChannel| ec.get_tx()))]
|
||||
pub tx: event::Sender,
|
||||
}
|
||||
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
use std::net::{Ipv4Addr, SocketAddr};
|
||||
use std::time::Duration;
|
||||
|
||||
use mlua::FromLua;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::event::{self, EventChannel};
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct DurationSeconds(u64);
|
||||
|
||||
impl From<DurationSeconds> for Duration {
|
||||
fn from(value: DurationSeconds) -> Self {
|
||||
Self::from_secs(value.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Ipv4SocketAddr<const PORT: u16>(Ipv4Addr);
|
||||
|
||||
impl<const PORT: u16> From<Ipv4SocketAddr<PORT>> for SocketAddr {
|
||||
fn from(ip: Ipv4SocketAddr<PORT>) -> Self {
|
||||
Self::from((ip.0, PORT))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TxHelper(EventChannel);
|
||||
|
||||
impl<'lua> FromLua<'lua> for TxHelper {
|
||||
fn from_lua(value: mlua::Value<'lua>, lua: &'lua mlua::Lua) -> mlua::Result<Self> {
|
||||
Ok(TxHelper(mlua::FromLua::from_lua(value, lua)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TxHelper> for event::Sender {
|
||||
fn from(value: TxHelper) -> Self {
|
||||
value.0.get_tx()
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ pub mod device_manager;
|
||||
pub mod devices;
|
||||
pub mod error;
|
||||
pub mod event;
|
||||
pub mod helper;
|
||||
pub mod messages;
|
||||
pub mod mqtt;
|
||||
pub mod schedule;
|
||||
|
||||
Reference in New Issue
Block a user