WIP
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -91,6 +91,7 @@ dependencies = [
|
||||
"automation_macro",
|
||||
"axum",
|
||||
"config",
|
||||
"futures",
|
||||
"git-version",
|
||||
"google_home",
|
||||
"inventory",
|
||||
|
||||
@@ -74,6 +74,7 @@ config = { version = "0.15.15", default-features = false, features = [
|
||||
"async",
|
||||
"toml",
|
||||
] }
|
||||
futures = { workspace = true }
|
||||
git-version = "0.3.9"
|
||||
google_home = { workspace = true }
|
||||
lua_typed = { workspace = true }
|
||||
|
||||
@@ -27,7 +27,7 @@ impl mlua::FromLua for Box<dyn Device> {
|
||||
fn from_lua(value: mlua::Value, _lua: &mlua::Lua) -> mlua::Result<Self> {
|
||||
match value {
|
||||
mlua::Value::UserData(ud) => {
|
||||
let ud = if ud.is::<Box<dyn Device>>() {
|
||||
let ud = if ud.is::<Self>() {
|
||||
ud
|
||||
} else {
|
||||
ud.call_method::<_>("__box", ())?
|
||||
|
||||
1212
config.lua
1212
config.lua
File diff suppressed because it is too large
Load Diff
@@ -9,6 +9,7 @@ local FulfillmentConfig
|
||||
|
||||
---@class Config
|
||||
---@field fulfillment FulfillmentConfig
|
||||
---@field devices DeviceInterface[]?
|
||||
---@field schedule table<string, function>?
|
||||
---@field devices Devices?
|
||||
---@field mqtt AsyncClient
|
||||
---@field schedule table<string, fun() | fun()[]>?
|
||||
local Config
|
||||
|
||||
@@ -11,6 +11,7 @@ use automation::secret::EnvironmentSecretFile;
|
||||
use automation::version::VERSION;
|
||||
use automation::web::{ApiError, User};
|
||||
use automation_lib::device_manager::DeviceManager;
|
||||
use automation_lib::mqtt::WrappedAsyncClient;
|
||||
use axum::extract::{FromRef, State};
|
||||
use axum::http::StatusCode;
|
||||
use axum::routing::post;
|
||||
@@ -139,7 +140,7 @@ async fn app() -> anyhow::Result<()> {
|
||||
let entrypoint = Path::new(&setup.entrypoint);
|
||||
let config: Config = lua.load(entrypoint).eval_async().await?;
|
||||
|
||||
for device in config.devices {
|
||||
for device in config.devices.get(config.mqtt).await? {
|
||||
device_manager.add(device).await;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
use std::collections::HashMap;
|
||||
use std::net::{Ipv4Addr, SocketAddr};
|
||||
|
||||
use automation_lib::action_callback::ActionCallback;
|
||||
use automation_lib::device::Device;
|
||||
use automation_lib::mqtt::WrappedAsyncClient;
|
||||
use automation_macro::LuaDeviceConfig;
|
||||
use futures::future::try_join_all;
|
||||
use lua_typed::Typed;
|
||||
use mlua::{FromLua, LuaSerdeExt};
|
||||
use serde::Deserialize;
|
||||
use tracing::warn;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Setup {
|
||||
@@ -31,15 +36,77 @@ pub struct FulfillmentConfig {
|
||||
pub port: u16,
|
||||
}
|
||||
|
||||
#[derive(Debug, Typed, Default)]
|
||||
pub struct Devices {
|
||||
devices: Vec<Box<dyn Device>>,
|
||||
fs: Vec<mlua::Function>,
|
||||
}
|
||||
|
||||
impl Devices {
|
||||
pub async fn get(mut self, client: WrappedAsyncClient) -> mlua::Result<Vec<Box<dyn Device>>> {
|
||||
let devices = try_join_all(
|
||||
self.fs
|
||||
.iter()
|
||||
.map(async |f| f.call_async::<Vec<Box<dyn Device>>>(client.clone()).await),
|
||||
)
|
||||
.await?
|
||||
.into_iter()
|
||||
.flatten();
|
||||
|
||||
self.devices.extend(devices);
|
||||
|
||||
Ok(self.devices)
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_devices(
|
||||
value: mlua::Value,
|
||||
lua: &mlua::Lua,
|
||||
) -> mlua::Result<(Vec<Box<dyn Device>>, Vec<mlua::Function>)> {
|
||||
let mut devices = Vec::new();
|
||||
let mut fs = Vec::new();
|
||||
|
||||
if let Ok(device) = Box::<dyn Device>::from_lua(value.clone(), lua) {
|
||||
devices.push(device);
|
||||
warn!("Converted to device");
|
||||
} else {
|
||||
match value {
|
||||
mlua::Value::Function(f) => fs.push(f),
|
||||
mlua::Value::Table(table) => {
|
||||
for pair in table.pairs::<mlua::Value, mlua::Value>() {
|
||||
let (_, value) = pair?;
|
||||
let (a, b) = extract_devices(value, lua)?;
|
||||
|
||||
devices.extend(a);
|
||||
fs.extend(b);
|
||||
}
|
||||
}
|
||||
_ => Err(mlua::Error::RuntimeError("Unexpected type".into()))?,
|
||||
}
|
||||
}
|
||||
|
||||
Ok((devices, fs))
|
||||
}
|
||||
|
||||
impl mlua::FromLua for Devices {
|
||||
fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> {
|
||||
let (devices, fs) = extract_devices(value, lua)?;
|
||||
|
||||
Ok(Devices { devices, fs })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, LuaDeviceConfig, Typed)]
|
||||
pub struct Config {
|
||||
pub fulfillment: FulfillmentConfig,
|
||||
#[device_config(from_lua, default)]
|
||||
#[typed(default)]
|
||||
pub devices: Vec<Box<dyn Device>>,
|
||||
pub devices: Devices,
|
||||
#[device_config(from_lua)]
|
||||
pub mqtt: WrappedAsyncClient,
|
||||
#[device_config(from_lua, default)]
|
||||
#[typed(default)]
|
||||
pub schedule: HashMap<String, mlua::Function>,
|
||||
pub schedule: HashMap<String, ActionCallback<()>>,
|
||||
}
|
||||
|
||||
impl From<FulfillmentConfig> for SocketAddr {
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
use std::collections::HashMap;
|
||||
use std::pin::Pin;
|
||||
|
||||
use automation_lib::action_callback::ActionCallback;
|
||||
use tokio_cron_scheduler::{Job, JobScheduler, JobSchedulerError};
|
||||
|
||||
pub async fn start_scheduler(
|
||||
schedule: HashMap<String, mlua::Function>,
|
||||
schedule: HashMap<String, ActionCallback<()>>,
|
||||
) -> Result<(), JobSchedulerError> {
|
||||
let scheduler = JobScheduler::new().await?;
|
||||
|
||||
@@ -14,7 +15,7 @@ pub async fn start_scheduler(
|
||||
let f = f.clone();
|
||||
|
||||
Box::pin(async move {
|
||||
f.call_async::<()>(()).await.unwrap();
|
||||
f.call(()).await;
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user