Compare commits
5 Commits
5271e5ad81
...
60bf0f236c
| Author | SHA1 | Date | |
|---|---|---|---|
|
60bf0f236c
|
|||
|
07e2d18a28
|
|||
|
d008bb0786
|
|||
|
43cb1b31fd
|
|||
|
ff78ac0c76
|
3
Cargo.lock
generated
3
Cargo.lock
generated
@@ -99,6 +99,8 @@ dependencies = [
|
||||
"dotenvy",
|
||||
"git-version",
|
||||
"google_home",
|
||||
"hostname",
|
||||
"inventory",
|
||||
"mlua",
|
||||
"reqwest",
|
||||
"rumqttc",
|
||||
@@ -150,7 +152,6 @@ dependencies = [
|
||||
"dyn-clone",
|
||||
"futures",
|
||||
"google_home",
|
||||
"hostname",
|
||||
"indexmap",
|
||||
"inventory",
|
||||
"mlua",
|
||||
|
||||
@@ -77,6 +77,8 @@ config = { version = "0.15.15", default-features = false, features = [
|
||||
dotenvy = { workspace = true }
|
||||
git-version = "0.3.9"
|
||||
google_home = { workspace = true }
|
||||
hostname = { workspace = true }
|
||||
inventory = { workspace = true }
|
||||
mlua = { workspace = true }
|
||||
reqwest = { workspace = true }
|
||||
rumqttc = { workspace = true }
|
||||
|
||||
@@ -36,7 +36,7 @@ macro_rules! register_device {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
||||
pub fn register_with_lua(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
||||
let devices = lua.create_table()?;
|
||||
|
||||
register_device!(lua, devices, AirFilter);
|
||||
@@ -60,4 +60,4 @@ pub fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
||||
Ok(devices)
|
||||
}
|
||||
|
||||
inventory::submit! {Module::new("devices", create_module)}
|
||||
inventory::submit! {Module::new("devices", register_with_lua)}
|
||||
|
||||
@@ -10,7 +10,6 @@ bytes = { workspace = true }
|
||||
dyn-clone = { workspace = true }
|
||||
futures = { workspace = true }
|
||||
google_home = { workspace = true }
|
||||
hostname = { workspace = true }
|
||||
indexmap = { workspace = true }
|
||||
inventory = { workspace = true }
|
||||
mlua = { workspace = true }
|
||||
|
||||
@@ -1 +1,11 @@
|
||||
pub mod serialization;
|
||||
mod timeout;
|
||||
|
||||
pub use timeout::Timeout;
|
||||
|
||||
pub fn register_with_lua(lua: &mlua::Lua) -> mlua::Result<()> {
|
||||
lua.globals()
|
||||
.set("Timeout", lua.create_proxy::<Timeout>()?)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#![allow(incomplete_features)]
|
||||
#![feature(iterator_try_collect)]
|
||||
|
||||
use tracing::debug;
|
||||
|
||||
pub mod action_callback;
|
||||
pub mod config;
|
||||
pub mod device;
|
||||
@@ -36,15 +34,4 @@ impl Module {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_modules(lua: &mlua::Lua) -> mlua::Result<()> {
|
||||
debug!("Loading modules...");
|
||||
for module in inventory::iter::<Module> {
|
||||
debug!(name = module.get_name(), "Registering");
|
||||
let table = module.register(lua)?;
|
||||
lua.register_module(module.get_name(), table)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
inventory::collect!(Module);
|
||||
|
||||
@@ -1,3 +1 @@
|
||||
pub mod traits;
|
||||
|
||||
mod utils;
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
mod timeout;
|
||||
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
pub use timeout::Timeout;
|
||||
|
||||
use crate::Module;
|
||||
|
||||
fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
||||
let utils = lua.create_table()?;
|
||||
|
||||
utils.set("Timeout", lua.create_proxy::<Timeout>()?)?;
|
||||
|
||||
let get_hostname = lua.create_function(|_lua, ()| {
|
||||
hostname::get()
|
||||
.map(|name| name.to_str().unwrap_or("unknown").to_owned())
|
||||
.map_err(mlua::ExternalError::into_lua_err)
|
||||
})?;
|
||||
utils.set("get_hostname", get_hostname)?;
|
||||
let get_epoch = lua.create_function(|_lua, ()| {
|
||||
Ok(SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("Time is after UNIX EPOCH")
|
||||
.as_millis())
|
||||
})?;
|
||||
utils.set("get_epoch", get_epoch)?;
|
||||
|
||||
Ok(utils)
|
||||
}
|
||||
|
||||
inventory::submit! {Module::new("utils", create_module)}
|
||||
10
config.lua
10
config.lua
@@ -259,7 +259,7 @@ device_manager:add(devices.IkeaRemote.new({
|
||||
}))
|
||||
|
||||
local function kettle_timeout()
|
||||
local timeout = utils.Timeout.new()
|
||||
local timeout = Timeout.new()
|
||||
|
||||
return function(self, state)
|
||||
if state.state and state.power < 100 then
|
||||
@@ -308,7 +308,7 @@ device_manager:add(devices.IkeaRemote.new({
|
||||
}))
|
||||
|
||||
local function off_timeout(duration)
|
||||
local timeout = utils.Timeout.new()
|
||||
local timeout = Timeout.new()
|
||||
|
||||
return function(self, state)
|
||||
if state.state then
|
||||
@@ -371,7 +371,7 @@ local workbench_light = devices.LightColorTemperature.new({
|
||||
turn_off_when_away(workbench_light)
|
||||
device_manager:add(workbench_light)
|
||||
|
||||
local delay_color_temp = utils.Timeout.new()
|
||||
local delay_color_temp = Timeout.new()
|
||||
device_manager:add(devices.IkeaRemote.new({
|
||||
name = "Remote",
|
||||
room = "Workbench",
|
||||
@@ -424,7 +424,7 @@ device_manager:add(devices.HueSwitch.new({
|
||||
}))
|
||||
|
||||
local hallway_light_automation = {
|
||||
timeout = utils.Timeout.new(),
|
||||
timeout = Timeout.new(),
|
||||
forced = false,
|
||||
switch_callback = function(self)
|
||||
return function(_, on)
|
||||
@@ -512,7 +512,7 @@ hallway_light_automation.group = {
|
||||
}
|
||||
|
||||
local function presence(duration)
|
||||
local timeout = utils.Timeout.new()
|
||||
local timeout = Timeout.new()
|
||||
|
||||
return function(_, open)
|
||||
if open then
|
||||
|
||||
32
src/main.rs
32
src/main.rs
@@ -7,11 +7,13 @@ mod web;
|
||||
use std::net::SocketAddr;
|
||||
use std::path::Path;
|
||||
use std::process;
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use ::config::{Environment, File};
|
||||
use automation_lib::config::{FulfillmentConfig, MqttConfig};
|
||||
use automation_lib::device_manager::DeviceManager;
|
||||
use automation_lib::mqtt::{self, WrappedAsyncClient};
|
||||
use automation_lib::{Module, helpers};
|
||||
use axum::extract::{FromRef, State};
|
||||
use axum::http::StatusCode;
|
||||
use axum::routing::post;
|
||||
@@ -139,7 +141,12 @@ async fn app() -> anyhow::Result<()> {
|
||||
})?;
|
||||
lua.globals().set("print", print)?;
|
||||
|
||||
automation_lib::load_modules(&lua)?;
|
||||
debug!("Loading modules...");
|
||||
for module in inventory::iter::<Module> {
|
||||
debug!(name = module.get_name(), "Registering");
|
||||
let table = module.register(&lua)?;
|
||||
lua.register_module(module.get_name(), table)?;
|
||||
}
|
||||
|
||||
let mqtt = lua.create_table()?;
|
||||
let event_channel = device_manager.event_channel();
|
||||
@@ -161,6 +168,29 @@ async fn app() -> anyhow::Result<()> {
|
||||
lua.register_module("variables", lua.to_value(&config.variables)?)?;
|
||||
lua.register_module("secrets", lua.to_value(&config.secrets)?)?;
|
||||
|
||||
let utils = lua.create_table()?;
|
||||
let get_hostname = lua.create_function(|_lua, ()| {
|
||||
hostname::get()
|
||||
.map(|name| name.to_str().unwrap_or("unknown").to_owned())
|
||||
.map_err(mlua::ExternalError::into_lua_err)
|
||||
})?;
|
||||
utils.set("get_hostname", get_hostname)?;
|
||||
let get_epoch = lua.create_function(|_lua, ()| {
|
||||
Ok(SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("Time is after UNIX EPOCH")
|
||||
.as_millis())
|
||||
})?;
|
||||
utils.set("get_epoch", get_epoch)?;
|
||||
let sleep = lua.create_async_function(async |_lua, duration: u64| {
|
||||
tokio::time::sleep(Duration::from_millis(duration)).await;
|
||||
Ok(())
|
||||
})?;
|
||||
utils.set("sleep", sleep)?;
|
||||
lua.register_module("utils", utils)?;
|
||||
|
||||
helpers::register_with_lua(&lua)?;
|
||||
|
||||
let entrypoint = Path::new(&config.entrypoint);
|
||||
let fulfillment_config: mlua::Value = lua.load(entrypoint).eval_async().await?;
|
||||
let fulfillment_config: FulfillmentConfig = lua.from_value(fulfillment_config)?;
|
||||
|
||||
Reference in New Issue
Block a user