Compare commits
24 Commits
815ea5bb78
...
5cbd838e9a
Author | SHA1 | Date | |
---|---|---|---|
5cbd838e9a | |||
2aee44cba2 | |||
27324e2c70 | |||
2e553038ba | |||
808549bcba | |||
7e152599a1 | |||
8bc529cc1a | |||
aafdfcba1f | |||
3dbc14ccac | |||
55025a67fc | |||
742e3bd62a | |||
430355bb40 | |||
870da3eea9 | |||
c4978ab9ca | |||
e43bd4ea2a | |||
68b9676616 | |||
8377e1d696 | |||
c6b9005d1e | |||
4273a25acd | |||
92b7a2830a | |||
dec08fe0a2 | |||
f115e0e6e8 | |||
e79787561d | |||
668f13863a |
57
Cargo.lock
generated
57
Cargo.lock
generated
|
@ -17,18 +17,6 @@ version = "1.0.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "ahash"
|
|
||||||
version = "0.8.11"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
|
|
||||||
dependencies = [
|
|
||||||
"cfg-if",
|
|
||||||
"once_cell",
|
|
||||||
"version_check",
|
|
||||||
"zerocopy",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "aho-corasick"
|
name = "aho-corasick"
|
||||||
version = "1.0.2"
|
version = "1.0.2"
|
||||||
|
@ -38,12 +26,6 @@ dependencies = [
|
||||||
"memchr",
|
"memchr",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "allocator-api2"
|
|
||||||
version = "0.2.18"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "android-tzdata"
|
name = "android-tzdata"
|
||||||
version = "0.1.1"
|
version = "0.1.1"
|
||||||
|
@ -121,10 +103,8 @@ dependencies = [
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-cron-scheduler",
|
"tokio-cron-scheduler",
|
||||||
"tokio-util",
|
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
"uuid",
|
|
||||||
"wakey",
|
"wakey",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -679,10 +659,6 @@ name = "hashbrown"
|
||||||
version = "0.14.0"
|
version = "0.14.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a"
|
checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a"
|
||||||
dependencies = [
|
|
||||||
"ahash",
|
|
||||||
"allocator-api2",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hdrhistogram"
|
name = "hdrhistogram"
|
||||||
|
@ -1967,19 +1943,16 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio-util"
|
name = "tokio-util"
|
||||||
version = "0.7.11"
|
version = "0.7.8"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1"
|
checksum = "806fe8c2c87eccc8b3267cbae29ed3ab2d0bd37fca70ab622e46aaa9375ddb7d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bytes",
|
"bytes",
|
||||||
"futures-core",
|
"futures-core",
|
||||||
"futures-io",
|
|
||||||
"futures-sink",
|
"futures-sink",
|
||||||
"futures-util",
|
|
||||||
"hashbrown 0.14.0",
|
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"slab",
|
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"tracing",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -2157,9 +2130,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "uuid"
|
name = "uuid"
|
||||||
version = "1.8.0"
|
version = "1.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0"
|
checksum = "88ad59a7560b41a70d191093a945f0b87bc1deeda46fb237479708a1d6b6cdfc"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"getrandom",
|
"getrandom",
|
||||||
]
|
]
|
||||||
|
@ -2511,23 +2484,3 @@ name = "winsafe"
|
||||||
version = "0.0.19"
|
version = "0.0.19"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904"
|
checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "zerocopy"
|
|
||||||
version = "0.7.32"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be"
|
|
||||||
dependencies = [
|
|
||||||
"zerocopy-derive",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "zerocopy-derive"
|
|
||||||
version = "0.7.32"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn 2.0.60",
|
|
||||||
]
|
|
||||||
|
|
11
Cargo.toml
11
Cargo.toml
|
@ -43,11 +43,16 @@ enum_dispatch = "0.3.12"
|
||||||
indexmap = { version = "2.0.0", features = ["serde"] }
|
indexmap = { version = "2.0.0", features = ["serde"] }
|
||||||
serde_yaml = "0.9.27"
|
serde_yaml = "0.9.27"
|
||||||
tokio-cron-scheduler = "0.9.4"
|
tokio-cron-scheduler = "0.9.4"
|
||||||
mlua = { version = "0.9.7", features = ["lua54", "vendored", "macros", "serialize", "async", "send"] }
|
mlua = { version = "0.9.7", features = [
|
||||||
|
"lua54",
|
||||||
|
"vendored",
|
||||||
|
"macros",
|
||||||
|
"serialize",
|
||||||
|
"async",
|
||||||
|
"send",
|
||||||
|
] }
|
||||||
once_cell = "1.19.0"
|
once_cell = "1.19.0"
|
||||||
hostname = "0.4.0"
|
hostname = "0.4.0"
|
||||||
tokio-util = { version = "0.7.11", features = ["full"] }
|
|
||||||
uuid = "1.8.0"
|
|
||||||
|
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
wakey = { git = "https://git.huizinga.dev/Dreaded_X/wakey" }
|
wakey = { git = "https://git.huizinga.dev/Dreaded_X/wakey" }
|
||||||
|
|
13
config.lua
13
config.lua
|
@ -137,7 +137,8 @@ automation.device_manager:add(IkeaOutlet.new({
|
||||||
topic = mqtt_z2m("workbench/outlet"),
|
topic = mqtt_z2m("workbench/outlet"),
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
}))
|
}))
|
||||||
local hallway_lights = HueGroup.new({
|
|
||||||
|
local hallway_lights = automation.device_manager:add(HueGroup.new({
|
||||||
identifier = "hallway_lights",
|
identifier = "hallway_lights",
|
||||||
ip = hue_ip,
|
ip = hue_ip,
|
||||||
login = hue_token,
|
login = hue_token,
|
||||||
|
@ -148,8 +149,7 @@ local hallway_lights = HueGroup.new({
|
||||||
{ topic = mqtt_z2m("hallway/remote") },
|
{ topic = mqtt_z2m("hallway/remote") },
|
||||||
},
|
},
|
||||||
client = mqtt_client,
|
client = mqtt_client,
|
||||||
})
|
}))
|
||||||
automation.device_manager:add(hallway_lights)
|
|
||||||
|
|
||||||
automation.device_manager:add(ContactSensor.new({
|
automation.device_manager:add(ContactSensor.new({
|
||||||
identifier = "hallway_frontdoor",
|
identifier = "hallway_frontdoor",
|
||||||
|
@ -173,9 +173,6 @@ local bedroom_air_filter = AirFilter.new({
|
||||||
})
|
})
|
||||||
automation.device_manager:add(bedroom_air_filter)
|
automation.device_manager:add(bedroom_air_filter)
|
||||||
|
|
||||||
automation.device_manager:schedule("0 0 19 * * *", function()
|
automation.device_manager:schedule("0/1 * * * * *", function()
|
||||||
bedroom_air_filter:set_on(true)
|
print("Device: " .. bedroom_air_filter:get_id())
|
||||||
end)
|
|
||||||
automation.device_manager:schedule("0 0 20 * * *", function()
|
|
||||||
bedroom_air_filter:set_on(false)
|
|
||||||
end)
|
end)
|
||||||
|
|
|
@ -1,15 +1,11 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use std::pin::Pin;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use futures::future::join_all;
|
use futures::future::join_all;
|
||||||
use futures::Future;
|
|
||||||
use google_home::traits::OnOff;
|
|
||||||
use mlua::FromLua;
|
use mlua::FromLua;
|
||||||
use tokio::sync::{RwLock, RwLockReadGuard};
|
use tokio::sync::{RwLock, RwLockReadGuard};
|
||||||
use tokio_cron_scheduler::{Job, JobScheduler};
|
use tokio_cron_scheduler::{Job, JobScheduler};
|
||||||
use tokio_util::task::LocalPoolHandle;
|
|
||||||
use tracing::{debug, instrument, trace};
|
use tracing::{debug, instrument, trace};
|
||||||
|
|
||||||
use crate::devices::Device;
|
use crate::devices::Device;
|
||||||
|
@ -43,17 +39,6 @@ impl mlua::UserData for WrappedDevice {
|
||||||
methods.add_async_method("get_id", |_lua, this, _: ()| async {
|
methods.add_async_method("get_id", |_lua, this, _: ()| async {
|
||||||
Ok(crate::devices::Device::get_id(this.0.read().await.as_ref()))
|
Ok(crate::devices::Device::get_id(this.0.read().await.as_ref()))
|
||||||
});
|
});
|
||||||
|
|
||||||
methods.add_async_method("set_on", |_lua, this, on: bool| async move {
|
|
||||||
let mut device = this.0.write().await;
|
|
||||||
let device = device.as_mut();
|
|
||||||
|
|
||||||
if let Some(device) = device.cast_mut() as Option<&mut dyn OnOff> {
|
|
||||||
device.set_on(on).await.unwrap()
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,23 +180,6 @@ impl DeviceManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_schedule(
|
|
||||||
uuid: uuid::Uuid,
|
|
||||||
_: tokio_cron_scheduler::JobScheduler,
|
|
||||||
) -> Pin<Box<dyn Future<Output = ()> + Send>> {
|
|
||||||
Box::pin(async move {
|
|
||||||
// Lua is not Send, so we need to make sure that the task stays on the same thread
|
|
||||||
let pool = LocalPoolHandle::new(1);
|
|
||||||
pool.spawn_pinned(move || async move {
|
|
||||||
let lua = LUA.lock().await;
|
|
||||||
let f: mlua::Function = lua.named_registry_value(uuid.to_string().as_str()).unwrap();
|
|
||||||
f.call_async::<_, ()>(()).await.unwrap();
|
|
||||||
})
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
impl mlua::UserData for DeviceManager {
|
impl mlua::UserData for DeviceManager {
|
||||||
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
|
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||||
methods.add_async_method("add", |_lua, this, device: WrappedDevice| async move {
|
methods.add_async_method("add", |_lua, this, device: WrappedDevice| async move {
|
||||||
|
@ -224,9 +192,22 @@ impl mlua::UserData for DeviceManager {
|
||||||
"schedule",
|
"schedule",
|
||||||
|lua, this, (schedule, f): (String, mlua::Function)| async move {
|
|lua, this, (schedule, f): (String, mlua::Function)| async move {
|
||||||
debug!("schedule = {schedule}");
|
debug!("schedule = {schedule}");
|
||||||
let job = Job::new_async(schedule.as_str(), run_schedule).unwrap();
|
let uuid = this
|
||||||
|
.scheduler
|
||||||
|
.add(
|
||||||
|
Job::new_async(schedule.as_str(), |uuid, _lock| {
|
||||||
|
Box::pin(async move {
|
||||||
|
let lua = LUA.lock().await;
|
||||||
|
let f: mlua::Function =
|
||||||
|
lua.named_registry_value(uuid.to_string().as_str()).unwrap();
|
||||||
|
|
||||||
let uuid = this.scheduler.add(job).await.unwrap();
|
f.call::<_, ()>(()).unwrap();
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
// Store the function in the registry
|
// Store the function in the registry
|
||||||
lua.set_named_registry_value(uuid.to_string().as_str(), f)
|
lua.set_named_registry_value(uuid.to_string().as_str(), f)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user