Compare commits
6 Commits
feature/im
...
2f181e35f3
| Author | SHA1 | Date | |
|---|---|---|---|
|
2f181e35f3
|
|||
|
613fd63895
|
|||
|
cfa482aa03
|
|||
|
379d840158
|
|||
|
0c428d1d9b
|
|||
|
56275811f7
|
21
Cargo.lock
generated
21
Cargo.lock
generated
@@ -97,6 +97,7 @@ dependencies = [
|
|||||||
"axum",
|
"axum",
|
||||||
"config",
|
"config",
|
||||||
"dotenvy",
|
"dotenvy",
|
||||||
|
"git-version",
|
||||||
"google_home",
|
"google_home",
|
||||||
"hostname",
|
"hostname",
|
||||||
"mlua",
|
"mlua",
|
||||||
@@ -647,6 +648,26 @@ version = "0.31.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
|
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "git-version"
|
||||||
|
version = "0.3.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1ad568aa3db0fcbc81f2f116137f263d7304f512a1209b35b85150d3ef88ad19"
|
||||||
|
dependencies = [
|
||||||
|
"git-version-macro",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "git-version-macro"
|
||||||
|
version = "0.3.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "53010ccb100b96a67bc32c0175f0ed1426b31b655d562898e57325f81c023ac0"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.106",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "google_home"
|
name = "google_home"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|||||||
@@ -74,6 +74,7 @@ config = { version = "0.15.15", default-features = false, features = [
|
|||||||
"toml",
|
"toml",
|
||||||
] }
|
] }
|
||||||
dotenvy = { workspace = true }
|
dotenvy = { workspace = true }
|
||||||
|
git-version = "0.3.9"
|
||||||
google_home = { workspace = true }
|
google_home = { workspace = true }
|
||||||
hostname = { workspace = true }
|
hostname = { workspace = true }
|
||||||
mlua = { workspace = true }
|
mlua = { workspace = true }
|
||||||
|
|||||||
@@ -35,9 +35,9 @@ pub struct Config {
|
|||||||
pub sensor_type: SensorType,
|
pub sensor_type: SensorType,
|
||||||
|
|
||||||
#[device_config(from_lua, default)]
|
#[device_config(from_lua, default)]
|
||||||
pub callback: ActionCallback<ContactSensor, bool>,
|
pub callback: ActionCallback<(ContactSensor, bool)>,
|
||||||
#[device_config(from_lua, default)]
|
#[device_config(from_lua, default)]
|
||||||
pub battery_callback: ActionCallback<ContactSensor, f32>,
|
pub battery_callback: ActionCallback<(ContactSensor, f32)>,
|
||||||
|
|
||||||
#[device_config(from_lua)]
|
#[device_config(from_lua)]
|
||||||
pub client: WrappedAsyncClient,
|
pub client: WrappedAsyncClient,
|
||||||
@@ -165,14 +165,17 @@ impl OnMqtt for ContactSensor {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.config.callback.call(self, &!is_closed).await;
|
self.config.callback.call((self.clone(), !is_closed)).await;
|
||||||
|
|
||||||
debug!(id = self.get_id(), "Updating state to {is_closed}");
|
debug!(id = self.get_id(), "Updating state to {is_closed}");
|
||||||
self.state_mut().await.is_closed = is_closed;
|
self.state_mut().await.is_closed = is_closed;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(battery) = message.battery {
|
if let Some(battery) = message.battery {
|
||||||
self.config.battery_callback.call(self, &battery).await;
|
self.config
|
||||||
|
.battery_callback
|
||||||
|
.call((self.clone(), battery))
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,19 +21,19 @@ pub struct Config {
|
|||||||
pub client: WrappedAsyncClient,
|
pub client: WrappedAsyncClient,
|
||||||
|
|
||||||
#[device_config(from_lua, default)]
|
#[device_config(from_lua, default)]
|
||||||
pub left_callback: ActionCallback<HueSwitch, ()>,
|
pub left_callback: ActionCallback<HueSwitch>,
|
||||||
|
|
||||||
#[device_config(from_lua, default)]
|
#[device_config(from_lua, default)]
|
||||||
pub right_callback: ActionCallback<HueSwitch, ()>,
|
pub right_callback: ActionCallback<HueSwitch>,
|
||||||
|
|
||||||
#[device_config(from_lua, default)]
|
#[device_config(from_lua, default)]
|
||||||
pub left_hold_callback: ActionCallback<HueSwitch, ()>,
|
pub left_hold_callback: ActionCallback<HueSwitch>,
|
||||||
|
|
||||||
#[device_config(from_lua, default)]
|
#[device_config(from_lua, default)]
|
||||||
pub right_hold_callback: ActionCallback<HueSwitch, ()>,
|
pub right_hold_callback: ActionCallback<HueSwitch>,
|
||||||
|
|
||||||
#[device_config(from_lua, default)]
|
#[device_config(from_lua, default)]
|
||||||
pub battery_callback: ActionCallback<HueSwitch, f32>,
|
pub battery_callback: ActionCallback<(HueSwitch, f32)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Deserialize)]
|
#[derive(Debug, Copy, Clone, Deserialize)]
|
||||||
@@ -104,19 +104,21 @@ impl OnMqtt for HueSwitch {
|
|||||||
);
|
);
|
||||||
|
|
||||||
match action {
|
match action {
|
||||||
Action::LeftPressRelease => self.config.left_callback.call(self, &()).await,
|
Action::LeftPressRelease => self.config.left_callback.call(self.clone()).await,
|
||||||
Action::RightPressRelease => self.config.right_callback.call(self, &()).await,
|
Action::RightPressRelease => {
|
||||||
Action::LeftHold => self.config.left_hold_callback.call(self, &()).await,
|
self.config.right_callback.call(self.clone()).await
|
||||||
Action::RightHold => self.config.right_hold_callback.call(self, &()).await,
|
}
|
||||||
|
Action::LeftHold => self.config.left_hold_callback.call(self.clone()).await,
|
||||||
|
Action::RightHold => self.config.right_hold_callback.call(self.clone()).await,
|
||||||
// If there is no hold action, the switch will act like a normal release
|
// If there is no hold action, the switch will act like a normal release
|
||||||
Action::RightHoldRelease => {
|
Action::RightHoldRelease => {
|
||||||
if !self.config.right_hold_callback.is_set() {
|
if self.config.right_hold_callback.is_empty() {
|
||||||
self.config.right_callback.call(self, &()).await
|
self.config.right_callback.call(self.clone()).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Action::LeftHoldRelease => {
|
Action::LeftHoldRelease => {
|
||||||
if !self.config.left_hold_callback.is_set() {
|
if self.config.left_hold_callback.is_empty() {
|
||||||
self.config.left_callback.call(self, &()).await
|
self.config.left_callback.call(self.clone()).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
@@ -124,7 +126,10 @@ impl OnMqtt for HueSwitch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Some(battery) = message.battery {
|
if let Some(battery) = message.battery {
|
||||||
self.config.battery_callback.call(self, &battery).await;
|
self.config
|
||||||
|
.battery_callback
|
||||||
|
.call((self.clone(), battery))
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,10 +23,10 @@ pub struct Config {
|
|||||||
#[device_config(from_lua)]
|
#[device_config(from_lua)]
|
||||||
pub client: WrappedAsyncClient,
|
pub client: WrappedAsyncClient,
|
||||||
|
|
||||||
#[device_config(from_lua)]
|
|
||||||
pub callback: ActionCallback<IkeaRemote, bool>,
|
|
||||||
#[device_config(from_lua, default)]
|
#[device_config(from_lua, default)]
|
||||||
pub battery_callback: ActionCallback<IkeaRemote, f32>,
|
pub callback: ActionCallback<(IkeaRemote, bool)>,
|
||||||
|
#[device_config(from_lua, default)]
|
||||||
|
pub battery_callback: ActionCallback<(IkeaRemote, f32)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, LuaDevice)]
|
#[derive(Debug, Clone, LuaDevice)]
|
||||||
@@ -88,12 +88,15 @@ impl OnMqtt for IkeaRemote {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if let Some(on) = on {
|
if let Some(on) = on {
|
||||||
self.config.callback.call(self, &on).await;
|
self.config.callback.call((self.clone(), on)).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(battery) = message.battery {
|
if let Some(battery) = message.battery {
|
||||||
self.config.battery_callback.call(self, &battery).await;
|
self.config
|
||||||
|
.battery_callback
|
||||||
|
.call((self.clone(), battery))
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ pub struct Config {
|
|||||||
pub max: isize,
|
pub max: isize,
|
||||||
|
|
||||||
#[device_config(from_lua, default)]
|
#[device_config(from_lua, default)]
|
||||||
pub callback: ActionCallback<LightSensor, bool>,
|
pub callback: ActionCallback<(LightSensor, bool)>,
|
||||||
|
|
||||||
#[device_config(from_lua)]
|
#[device_config(from_lua)]
|
||||||
pub client: WrappedAsyncClient,
|
pub client: WrappedAsyncClient,
|
||||||
@@ -114,7 +114,7 @@ impl OnMqtt for LightSensor {
|
|||||||
|
|
||||||
self.config
|
self.config
|
||||||
.callback
|
.callback
|
||||||
.call(self, &!self.state().await.is_dark)
|
.call((self.clone(), !self.state().await.is_dark))
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ pub struct Config {
|
|||||||
pub mqtt: MqttDeviceConfig,
|
pub mqtt: MqttDeviceConfig,
|
||||||
|
|
||||||
#[device_config(from_lua, default)]
|
#[device_config(from_lua, default)]
|
||||||
pub callback: ActionCallback<Presence, bool>,
|
pub callback: ActionCallback<(Presence, bool)>,
|
||||||
|
|
||||||
#[device_config(from_lua)]
|
#[device_config(from_lua)]
|
||||||
pub client: WrappedAsyncClient,
|
pub client: WrappedAsyncClient,
|
||||||
@@ -118,7 +118,10 @@ impl OnMqtt for Presence {
|
|||||||
debug!("Overall presence updated: {overall_presence}");
|
debug!("Overall presence updated: {overall_presence}");
|
||||||
self.state_mut().await.current_overall_presence = overall_presence;
|
self.state_mut().await.current_overall_presence = overall_presence;
|
||||||
|
|
||||||
self.config.callback.call(self, &overall_presence).await;
|
self.config
|
||||||
|
.callback
|
||||||
|
.call((self.clone(), overall_presence))
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ pub struct Config {
|
|||||||
pub threshold: f32,
|
pub threshold: f32,
|
||||||
|
|
||||||
#[device_config(from_lua, default)]
|
#[device_config(from_lua, default)]
|
||||||
pub done_callback: ActionCallback<Washer, ()>,
|
pub done_callback: ActionCallback<Washer>,
|
||||||
|
|
||||||
#[device_config(from_lua)]
|
#[device_config(from_lua)]
|
||||||
pub client: WrappedAsyncClient,
|
pub client: WrappedAsyncClient,
|
||||||
@@ -109,7 +109,7 @@ impl OnMqtt for Washer {
|
|||||||
|
|
||||||
self.state_mut().await.running = 0;
|
self.state_mut().await.running = 0;
|
||||||
|
|
||||||
self.config.done_callback.call(self, &()).await;
|
self.config.done_callback.call(self.clone()).await;
|
||||||
} else if power < self.config.threshold {
|
} else if power < self.config.threshold {
|
||||||
// Prevent false positives
|
// Prevent false positives
|
||||||
self.state_mut().await.running = 0;
|
self.state_mut().await.running = 0;
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use automation_lib::device::{Device, LuaDeviceCreate};
|
|||||||
use automation_lib::event::OnMqtt;
|
use automation_lib::event::OnMqtt;
|
||||||
use automation_lib::helpers::serialization::state_deserializer;
|
use automation_lib::helpers::serialization::state_deserializer;
|
||||||
use automation_lib::mqtt::WrappedAsyncClient;
|
use automation_lib::mqtt::WrappedAsyncClient;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::{LuaDevice, LuaDeviceConfig, LuaSerialize};
|
||||||
use google_home::device;
|
use google_home::device;
|
||||||
use google_home::errors::ErrorCode;
|
use google_home::errors::ErrorCode;
|
||||||
use google_home::traits::{Brightness, Color, ColorSetting, ColorTemperatureRange, OnOff};
|
use google_home::traits::{Brightness, Color, ColorSetting, ColorTemperatureRange, OnOff};
|
||||||
@@ -34,13 +34,13 @@ pub struct Config<T: LightState> {
|
|||||||
pub mqtt: MqttDeviceConfig,
|
pub mqtt: MqttDeviceConfig,
|
||||||
|
|
||||||
#[device_config(from_lua, default)]
|
#[device_config(from_lua, default)]
|
||||||
pub callback: ActionCallback<Light<T>, T>,
|
pub callback: ActionCallback<(Light<T>, T)>,
|
||||||
|
|
||||||
#[device_config(from_lua)]
|
#[device_config(from_lua)]
|
||||||
pub client: WrappedAsyncClient,
|
pub client: WrappedAsyncClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Default, Serialize, Deserialize, LuaSerialize)]
|
||||||
pub struct StateOnOff {
|
pub struct StateOnOff {
|
||||||
#[serde(deserialize_with = "state_deserializer")]
|
#[serde(deserialize_with = "state_deserializer")]
|
||||||
state: bool,
|
state: bool,
|
||||||
@@ -48,7 +48,7 @@ pub struct StateOnOff {
|
|||||||
|
|
||||||
impl LightState for StateOnOff {}
|
impl LightState for StateOnOff {}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Default, Serialize, Deserialize, LuaSerialize)]
|
||||||
pub struct StateBrightness {
|
pub struct StateBrightness {
|
||||||
#[serde(deserialize_with = "state_deserializer")]
|
#[serde(deserialize_with = "state_deserializer")]
|
||||||
state: bool,
|
state: bool,
|
||||||
@@ -63,7 +63,7 @@ impl From<StateBrightness> for StateOnOff {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Default, Serialize, Deserialize, LuaSerialize)]
|
||||||
pub struct StateColorTemperature {
|
pub struct StateColorTemperature {
|
||||||
#[serde(deserialize_with = "state_deserializer")]
|
#[serde(deserialize_with = "state_deserializer")]
|
||||||
state: bool,
|
state: bool,
|
||||||
@@ -165,7 +165,7 @@ impl OnMqtt for Light<StateOnOff> {
|
|||||||
|
|
||||||
self.config
|
self.config
|
||||||
.callback
|
.callback
|
||||||
.call(self, self.state().await.deref())
|
.call((self.clone(), self.state().await.clone()))
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -204,7 +204,7 @@ impl OnMqtt for Light<StateBrightness> {
|
|||||||
|
|
||||||
self.config
|
self.config
|
||||||
.callback
|
.callback
|
||||||
.call(self, self.state().await.deref())
|
.call((self.clone(), self.state().await.clone()))
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -245,7 +245,7 @@ impl OnMqtt for Light<StateColorTemperature> {
|
|||||||
|
|
||||||
self.config
|
self.config
|
||||||
.callback
|
.callback
|
||||||
.call(self, self.state().await.deref())
|
.call((self.clone(), self.state().await.clone()))
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use automation_lib::device::{Device, LuaDeviceCreate};
|
|||||||
use automation_lib::event::OnMqtt;
|
use automation_lib::event::OnMqtt;
|
||||||
use automation_lib::helpers::serialization::state_deserializer;
|
use automation_lib::helpers::serialization::state_deserializer;
|
||||||
use automation_lib::mqtt::WrappedAsyncClient;
|
use automation_lib::mqtt::WrappedAsyncClient;
|
||||||
use automation_macro::{LuaDevice, LuaDeviceConfig};
|
use automation_macro::{LuaDevice, LuaDeviceConfig, LuaSerialize};
|
||||||
use google_home::device;
|
use google_home::device;
|
||||||
use google_home::errors::ErrorCode;
|
use google_home::errors::ErrorCode;
|
||||||
use google_home::traits::OnOff;
|
use google_home::traits::OnOff;
|
||||||
@@ -51,13 +51,13 @@ pub struct Config<T: OutletState> {
|
|||||||
pub outlet_type: OutletType,
|
pub outlet_type: OutletType,
|
||||||
|
|
||||||
#[device_config(from_lua, default)]
|
#[device_config(from_lua, default)]
|
||||||
pub callback: ActionCallback<Outlet<T>, T>,
|
pub callback: ActionCallback<(Outlet<T>, T)>,
|
||||||
|
|
||||||
#[device_config(from_lua)]
|
#[device_config(from_lua)]
|
||||||
pub client: WrappedAsyncClient,
|
pub client: WrappedAsyncClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Default, Serialize, Deserialize, LuaSerialize)]
|
||||||
pub struct StateOnOff {
|
pub struct StateOnOff {
|
||||||
#[serde(deserialize_with = "state_deserializer")]
|
#[serde(deserialize_with = "state_deserializer")]
|
||||||
state: bool,
|
state: bool,
|
||||||
@@ -65,7 +65,7 @@ pub struct StateOnOff {
|
|||||||
|
|
||||||
impl OutletState for StateOnOff {}
|
impl OutletState for StateOnOff {}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Default, Serialize, Deserialize, LuaSerialize)]
|
||||||
pub struct StatePower {
|
pub struct StatePower {
|
||||||
#[serde(deserialize_with = "state_deserializer")]
|
#[serde(deserialize_with = "state_deserializer")]
|
||||||
state: bool,
|
state: bool,
|
||||||
@@ -155,7 +155,7 @@ impl OnMqtt for Outlet<StateOnOff> {
|
|||||||
|
|
||||||
self.config
|
self.config
|
||||||
.callback
|
.callback
|
||||||
.call(self, self.state().await.deref())
|
.call((self.clone(), self.state().await.clone()))
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -192,7 +192,7 @@ impl OnMqtt for Outlet<StatePower> {
|
|||||||
|
|
||||||
self.config
|
self.config
|
||||||
.callback
|
.callback
|
||||||
.call(self, self.state().await.deref())
|
.call((self.clone(), self.state().await.clone()))
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,71 +1,70 @@
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use mlua::{FromLua, IntoLua, LuaSerdeExt};
|
use futures::future::try_join_all;
|
||||||
use serde::Serialize;
|
use mlua::{FromLua, IntoLuaMulti};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct Internal {
|
pub struct ActionCallback<P> {
|
||||||
uuid: uuid::Uuid,
|
callbacks: Vec<mlua::Function>,
|
||||||
lua: mlua::Lua,
|
_parameters: PhantomData<P>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
// NOTE: For some reason the derive macro combined with PhantomData leads to issues where it
|
||||||
pub struct ActionCallback<T, S> {
|
// requires all types part of P to implement default, even if they never actually get constructed.
|
||||||
internal: Option<Internal>,
|
// By manually implemented Default it works fine.
|
||||||
_this: PhantomData<T>,
|
impl<P> Default for ActionCallback<P> {
|
||||||
_state: PhantomData<S>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, S> Default for ActionCallback<T, S> {
|
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
internal: None,
|
callbacks: Default::default(),
|
||||||
_this: PhantomData::<T>,
|
_parameters: Default::default(),
|
||||||
_state: PhantomData::<S>,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, S> FromLua for ActionCallback<T, S> {
|
impl<P> FromLua for ActionCallback<P> {
|
||||||
fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> {
|
fn from_lua(value: mlua::Value, _lua: &mlua::Lua) -> mlua::Result<Self> {
|
||||||
let uuid = uuid::Uuid::new_v4();
|
let callbacks = match value {
|
||||||
lua.set_named_registry_value(&uuid.to_string(), value)?;
|
mlua::Value::Function(f) => vec![f],
|
||||||
|
mlua::Value::Table(table) => table
|
||||||
|
.pairs::<mlua::Value, mlua::Function>()
|
||||||
|
.map(|pair| {
|
||||||
|
let (_, f) = pair?;
|
||||||
|
|
||||||
|
Ok::<_, mlua::Error>(f)
|
||||||
|
})
|
||||||
|
.try_collect()?,
|
||||||
|
_ => {
|
||||||
|
return Err(mlua::Error::FromLuaConversionError {
|
||||||
|
from: value.type_name(),
|
||||||
|
to: "ActionCallback".into(),
|
||||||
|
message: Some("expected function or table of functions".into()),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
Ok(ActionCallback {
|
Ok(ActionCallback {
|
||||||
internal: Some(Internal {
|
callbacks,
|
||||||
uuid,
|
_parameters: PhantomData::<P>,
|
||||||
lua: lua.clone(),
|
|
||||||
}),
|
|
||||||
_this: PhantomData::<T>,
|
|
||||||
_state: PhantomData::<S>,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Return proper error here
|
// TODO: Return proper error here
|
||||||
impl<T, S> ActionCallback<T, S>
|
impl<P> ActionCallback<P>
|
||||||
where
|
where
|
||||||
T: IntoLua + Sync + Send + Clone + 'static,
|
P: IntoLuaMulti + Sync + Clone,
|
||||||
S: Serialize,
|
|
||||||
{
|
{
|
||||||
pub async fn call(&self, this: &T, state: &S) {
|
pub async fn call(&self, parameters: P) {
|
||||||
let Some(internal) = self.internal.as_ref() else {
|
try_join_all(
|
||||||
return;
|
self.callbacks
|
||||||
};
|
.iter()
|
||||||
|
.map(async |f| f.call_async::<()>(parameters.clone()).await),
|
||||||
let state = internal.lua.to_value(state).unwrap();
|
)
|
||||||
|
.await
|
||||||
let callback: mlua::Value = internal
|
|
||||||
.lua
|
|
||||||
.named_registry_value(&internal.uuid.to_string())
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
match callback {
|
|
||||||
mlua::Value::Function(f) => f.call_async::<()>((this.clone(), state)).await.unwrap(),
|
|
||||||
_ => todo!("Only functions are currently supported"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_set(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.internal.is_some()
|
self.callbacks.is_empty()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ impl mlua::UserData for Timeout {
|
|||||||
|
|
||||||
methods.add_async_method(
|
methods.add_async_method(
|
||||||
"start",
|
"start",
|
||||||
async |_lua, this, (timeout, callback): (f32, ActionCallback<mlua::Value, bool>)| {
|
async |_lua, this, (timeout, callback): (f32, ActionCallback<()>)| {
|
||||||
if let Some(handle) = this.state.write().await.handle.take() {
|
if let Some(handle) = this.state.write().await.handle.take() {
|
||||||
handle.abort();
|
handle.abort();
|
||||||
}
|
}
|
||||||
@@ -42,7 +42,7 @@ impl mlua::UserData for Timeout {
|
|||||||
async move {
|
async move {
|
||||||
tokio::time::sleep(timeout).await;
|
tokio::time::sleep(timeout).await;
|
||||||
|
|
||||||
callback.call(&mlua::Nil, &false).await;
|
callback.call(()).await;
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#![allow(incomplete_features)]
|
#![allow(incomplete_features)]
|
||||||
|
#![feature(iterator_try_collect)]
|
||||||
|
|
||||||
pub mod action_callback;
|
pub mod action_callback;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ mod impl_device;
|
|||||||
mod lua_device_config;
|
mod lua_device_config;
|
||||||
|
|
||||||
use lua_device_config::impl_lua_device_config_macro;
|
use lua_device_config::impl_lua_device_config_macro;
|
||||||
|
use quote::quote;
|
||||||
use syn::{DeriveInput, parse_macro_input};
|
use syn::{DeriveInput, parse_macro_input};
|
||||||
|
|
||||||
use crate::impl_device::impl_device_macro;
|
use crate::impl_device::impl_device_macro;
|
||||||
@@ -20,3 +21,19 @@ pub fn impl_device(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|||||||
|
|
||||||
impl_device_macro(&ast).into()
|
impl_device_macro(&ast).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[proc_macro_derive(LuaSerialize, attributes(traits))]
|
||||||
|
pub fn lua_serialize(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||||
|
let ast = parse_macro_input!(input as DeriveInput);
|
||||||
|
|
||||||
|
let name = &ast.ident;
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
impl ::mlua::IntoLua for #name {
|
||||||
|
fn into_lua(self, lua: &::mlua::Lua) -> ::mlua::Result<::mlua::Value> {
|
||||||
|
::mlua::LuaSerdeExt::to_value(lua, &self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#![feature(iter_intersperse)]
|
#![feature(iter_intersperse)]
|
||||||
mod config;
|
mod config;
|
||||||
mod secret;
|
mod secret;
|
||||||
|
mod version;
|
||||||
mod web;
|
mod web;
|
||||||
|
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
@@ -27,6 +28,7 @@ use tracing::{debug, error, info, warn};
|
|||||||
use web::{ApiError, User};
|
use web::{ApiError, User};
|
||||||
|
|
||||||
use crate::secret::EnvironmentSecretFile;
|
use crate::secret::EnvironmentSecretFile;
|
||||||
|
use crate::version::VERSION;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct AppState {
|
struct AppState {
|
||||||
@@ -76,6 +78,8 @@ async fn app() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
|
info!(version = VERSION, "automation_rs");
|
||||||
|
|
||||||
let config: Config = ::config::Config::builder()
|
let config: Config = ::config::Config::builder()
|
||||||
.add_source(
|
.add_source(
|
||||||
File::with_name(&format!("{}.toml", std::env!("CARGO_PKG_NAME"))).required(false),
|
File::with_name(&format!("{}.toml", std::env!("CARGO_PKG_NAME"))).required(false),
|
||||||
@@ -91,8 +95,6 @@ async fn app() -> anyhow::Result<()> {
|
|||||||
.try_deserialize()
|
.try_deserialize()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
info!("Starting automation_rs...");
|
|
||||||
|
|
||||||
// Setup the device handler
|
// Setup the device handler
|
||||||
let device_manager = DeviceManager::new().await;
|
let device_manager = DeviceManager::new().await;
|
||||||
|
|
||||||
|
|||||||
11
src/version.rs
Normal file
11
src/version.rs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
pub const VERSION: &str = get_version();
|
||||||
|
|
||||||
|
const fn get_version() -> &'static str {
|
||||||
|
if let Some(version) = std::option_env!("RELEASE_VERSION")
|
||||||
|
&& !version.is_empty()
|
||||||
|
{
|
||||||
|
version
|
||||||
|
} else {
|
||||||
|
git_version::git_version!(fallback = "unknown")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user