refactor(config)!: Moved Timeout into utils module and moved module
The module is now setup in automation_lib::lua::utils.
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -99,7 +99,6 @@ dependencies = [
|
|||||||
"dotenvy",
|
"dotenvy",
|
||||||
"git-version",
|
"git-version",
|
||||||
"google_home",
|
"google_home",
|
||||||
"hostname",
|
|
||||||
"mlua",
|
"mlua",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"rumqttc",
|
"rumqttc",
|
||||||
@@ -151,6 +150,7 @@ dependencies = [
|
|||||||
"dyn-clone",
|
"dyn-clone",
|
||||||
"futures",
|
"futures",
|
||||||
"google_home",
|
"google_home",
|
||||||
|
"hostname",
|
||||||
"indexmap",
|
"indexmap",
|
||||||
"inventory",
|
"inventory",
|
||||||
"mlua",
|
"mlua",
|
||||||
|
|||||||
@@ -77,7 +77,6 @@ config = { version = "0.15.15", default-features = false, features = [
|
|||||||
dotenvy = { workspace = true }
|
dotenvy = { workspace = true }
|
||||||
git-version = "0.3.9"
|
git-version = "0.3.9"
|
||||||
google_home = { workspace = true }
|
google_home = { workspace = true }
|
||||||
hostname = { workspace = true }
|
|
||||||
mlua = { workspace = true }
|
mlua = { workspace = true }
|
||||||
reqwest = { workspace = true }
|
reqwest = { workspace = true }
|
||||||
rumqttc = { workspace = true }
|
rumqttc = { workspace = true }
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ bytes = { workspace = true }
|
|||||||
dyn-clone = { workspace = true }
|
dyn-clone = { workspace = true }
|
||||||
futures = { workspace = true }
|
futures = { workspace = true }
|
||||||
google_home = { workspace = true }
|
google_home = { workspace = true }
|
||||||
|
hostname = { workspace = true }
|
||||||
indexmap = { workspace = true }
|
indexmap = { workspace = true }
|
||||||
inventory = { workspace = true }
|
inventory = { workspace = true }
|
||||||
mlua = { workspace = true }
|
mlua = { workspace = true }
|
||||||
|
|||||||
@@ -1,11 +1 @@
|
|||||||
pub mod serialization;
|
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 +1,3 @@
|
|||||||
pub mod traits;
|
pub mod traits;
|
||||||
|
|
||||||
|
mod utils;
|
||||||
|
|||||||
31
automation_lib/src/lua/utils/mod.rs
Normal file
31
automation_lib/src/lua/utils/mod.rs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
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 function kettle_timeout()
|
||||||
local timeout = Timeout.new()
|
local timeout = utils.Timeout.new()
|
||||||
|
|
||||||
return function(self, state)
|
return function(self, state)
|
||||||
if state.state and state.power < 100 then
|
if state.state and state.power < 100 then
|
||||||
@@ -308,7 +308,7 @@ device_manager:add(devices.IkeaRemote.new({
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
local function off_timeout(duration)
|
local function off_timeout(duration)
|
||||||
local timeout = Timeout.new()
|
local timeout = utils.Timeout.new()
|
||||||
|
|
||||||
return function(self, state)
|
return function(self, state)
|
||||||
if state.state then
|
if state.state then
|
||||||
@@ -371,7 +371,7 @@ local workbench_light = devices.LightColorTemperature.new({
|
|||||||
turn_off_when_away(workbench_light)
|
turn_off_when_away(workbench_light)
|
||||||
device_manager:add(workbench_light)
|
device_manager:add(workbench_light)
|
||||||
|
|
||||||
local delay_color_temp = Timeout.new()
|
local delay_color_temp = utils.Timeout.new()
|
||||||
device_manager:add(devices.IkeaRemote.new({
|
device_manager:add(devices.IkeaRemote.new({
|
||||||
name = "Remote",
|
name = "Remote",
|
||||||
room = "Workbench",
|
room = "Workbench",
|
||||||
@@ -424,7 +424,7 @@ device_manager:add(devices.HueSwitch.new({
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
local hallway_light_automation = {
|
local hallway_light_automation = {
|
||||||
timeout = Timeout.new(),
|
timeout = utils.Timeout.new(),
|
||||||
forced = false,
|
forced = false,
|
||||||
switch_callback = function(self)
|
switch_callback = function(self)
|
||||||
return function(_, on)
|
return function(_, on)
|
||||||
@@ -512,7 +512,7 @@ hallway_light_automation.group = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
local function presence(duration)
|
local function presence(duration)
|
||||||
local timeout = Timeout.new()
|
local timeout = utils.Timeout.new()
|
||||||
|
|
||||||
return function(_, open)
|
return function(_, open)
|
||||||
if open then
|
if open then
|
||||||
|
|||||||
20
src/main.rs
20
src/main.rs
@@ -7,12 +7,10 @@ mod web;
|
|||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::process;
|
use std::process;
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
|
||||||
|
|
||||||
use ::config::{Environment, File};
|
use ::config::{Environment, File};
|
||||||
use automation_lib::config::{FulfillmentConfig, MqttConfig};
|
use automation_lib::config::{FulfillmentConfig, MqttConfig};
|
||||||
use automation_lib::device_manager::DeviceManager;
|
use automation_lib::device_manager::DeviceManager;
|
||||||
use automation_lib::helpers;
|
|
||||||
use automation_lib::mqtt::{self, WrappedAsyncClient};
|
use automation_lib::mqtt::{self, WrappedAsyncClient};
|
||||||
use axum::extract::{FromRef, State};
|
use axum::extract::{FromRef, State};
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
@@ -163,24 +161,6 @@ async fn app() -> anyhow::Result<()> {
|
|||||||
lua.register_module("variables", lua.to_value(&config.variables)?)?;
|
lua.register_module("variables", lua.to_value(&config.variables)?)?;
|
||||||
lua.register_module("secrets", lua.to_value(&config.secrets)?)?;
|
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)?;
|
|
||||||
lua.register_module("utils", utils)?;
|
|
||||||
|
|
||||||
helpers::register_with_lua(&lua)?;
|
|
||||||
|
|
||||||
let entrypoint = Path::new(&config.entrypoint);
|
let entrypoint = Path::new(&config.entrypoint);
|
||||||
let fulfillment_config: mlua::Value = lua.load(entrypoint).eval_async().await?;
|
let fulfillment_config: mlua::Value = lua.load(entrypoint).eval_async().await?;
|
||||||
let fulfillment_config: FulfillmentConfig = lua.from_value(fulfillment_config)?;
|
let fulfillment_config: FulfillmentConfig = lua.from_value(fulfillment_config)?;
|
||||||
|
|||||||
Reference in New Issue
Block a user