Compare commits
9 Commits
b327d32177
...
295491c5fc
| Author | SHA1 | Date | |
|---|---|---|---|
|
295491c5fc
|
|||
|
9f244b3475
|
|||
|
15a6e83ad8
|
|||
|
c727579290
|
|||
|
19e8663f26
|
|||
|
f5c4495cad
|
|||
|
9bbf0a5422
|
|||
|
85e3c7b877
|
|||
|
a2130005de
|
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -98,7 +98,6 @@ dependencies = [
|
|||||||
"config",
|
"config",
|
||||||
"git-version",
|
"git-version",
|
||||||
"google_home",
|
"google_home",
|
||||||
"inventory",
|
|
||||||
"mlua",
|
"mlua",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"rumqttc",
|
"rumqttc",
|
||||||
|
|||||||
@@ -76,7 +76,6 @@ config = { version = "0.15.15", default-features = false, features = [
|
|||||||
] }
|
] }
|
||||||
git-version = "0.3.9"
|
git-version = "0.3.9"
|
||||||
google_home = { workspace = true }
|
google_home = { workspace = true }
|
||||||
inventory = { workspace = true }
|
|
||||||
mlua = { workspace = true }
|
mlua = { workspace = true }
|
||||||
reqwest = { workspace = true }
|
reqwest = { workspace = true }
|
||||||
rumqttc = { workspace = true }
|
rumqttc = { workspace = true }
|
||||||
|
|||||||
@@ -15,7 +15,20 @@ mod zigbee;
|
|||||||
|
|
||||||
use automation_lib::Module;
|
use automation_lib::Module;
|
||||||
use automation_lib::device::{Device, LuaDeviceCreate};
|
use automation_lib::device::{Device, LuaDeviceCreate};
|
||||||
use tracing::{debug, warn};
|
use tracing::debug;
|
||||||
|
|
||||||
|
macro_rules! register_device {
|
||||||
|
($device:ty) => {
|
||||||
|
::inventory::submit!(crate::RegisteredDevice::new(
|
||||||
|
<$device as ::lua_typed::Typed>::type_name,
|
||||||
|
::mlua::Lua::create_proxy::<$device>
|
||||||
|
));
|
||||||
|
|
||||||
|
crate::register_type!($device);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) use register_device;
|
||||||
|
|
||||||
type DeviceNameFn = fn() -> String;
|
type DeviceNameFn = fn() -> String;
|
||||||
type RegisterDeviceFn = fn(lua: &mlua::Lua) -> mlua::Result<mlua::AnyUserData>;
|
type RegisterDeviceFn = fn(lua: &mlua::Lua) -> mlua::Result<mlua::AnyUserData>;
|
||||||
@@ -42,18 +55,6 @@ impl RegisteredDevice {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! register_device {
|
|
||||||
($device:ty) => {
|
|
||||||
::inventory::submit!(crate::RegisteredDevice::new(
|
|
||||||
<$device as ::lua_typed::Typed>::type_name,
|
|
||||||
::mlua::Lua::create_proxy::<$device>
|
|
||||||
));
|
|
||||||
|
|
||||||
crate::register_type!($device);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
pub(crate) use register_device;
|
|
||||||
|
|
||||||
inventory::collect!(RegisteredDevice);
|
inventory::collect!(RegisteredDevice);
|
||||||
|
|
||||||
pub fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
pub fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
||||||
@@ -70,9 +71,7 @@ pub fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
|||||||
Ok(devices)
|
Ok(devices)
|
||||||
}
|
}
|
||||||
|
|
||||||
type RegisterTypeFn = fn() -> Option<String>;
|
inventory::submit! {Module::new("automation:devices", create_module)}
|
||||||
|
|
||||||
pub struct RegisteredType(RegisterTypeFn);
|
|
||||||
|
|
||||||
macro_rules! register_type {
|
macro_rules! register_type {
|
||||||
($ty:ty) => {
|
($ty:ty) => {
|
||||||
@@ -81,25 +80,20 @@ macro_rules! register_type {
|
|||||||
));
|
));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) use register_type;
|
pub(crate) use register_type;
|
||||||
|
|
||||||
|
type RegisterTypeFn = fn() -> Option<String>;
|
||||||
|
|
||||||
|
pub struct RegisteredType(RegisterTypeFn);
|
||||||
|
|
||||||
inventory::collect!(RegisteredType);
|
inventory::collect!(RegisteredType);
|
||||||
|
|
||||||
fn generate_definitions() -> String {
|
pub fn generate_definitions() {
|
||||||
let mut output = String::new();
|
println!("---@meta\n\nlocal devices\n");
|
||||||
|
|
||||||
output += "---@meta\n\nlocal devices\n\n";
|
|
||||||
for ty in inventory::iter::<RegisteredType> {
|
for ty in inventory::iter::<RegisteredType> {
|
||||||
if let Some(def) = ty.0() {
|
let def = ty.0().unwrap();
|
||||||
output += &(def + "\n");
|
println!("{def}");
|
||||||
} else {
|
|
||||||
// NOTE: Due to how this works the typed is erased, so we don't know the cause
|
|
||||||
warn!("Registered type is missing generate_full function");
|
|
||||||
}
|
}
|
||||||
|
println!("return devices")
|
||||||
}
|
}
|
||||||
output += "return devices";
|
|
||||||
|
|
||||||
output
|
|
||||||
}
|
|
||||||
|
|
||||||
inventory::submit! {Module::new("automation:devices", create_module, Some(generate_definitions))}
|
|
||||||
|
|||||||
@@ -17,25 +17,15 @@ pub mod mqtt;
|
|||||||
pub mod schedule;
|
pub mod schedule;
|
||||||
|
|
||||||
type RegisterFn = fn(lua: &mlua::Lua) -> mlua::Result<mlua::Table>;
|
type RegisterFn = fn(lua: &mlua::Lua) -> mlua::Result<mlua::Table>;
|
||||||
type DefinitionsFn = fn() -> String;
|
|
||||||
|
|
||||||
pub struct Module {
|
pub struct Module {
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
register_fn: RegisterFn,
|
register_fn: RegisterFn,
|
||||||
definitions_fn: Option<DefinitionsFn>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Module {
|
impl Module {
|
||||||
pub const fn new(
|
pub const fn new(name: &'static str, register_fn: RegisterFn) -> Self {
|
||||||
name: &'static str,
|
Self { name, register_fn }
|
||||||
register_fn: RegisterFn,
|
|
||||||
definitions_fn: Option<DefinitionsFn>,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
|
||||||
name,
|
|
||||||
register_fn,
|
|
||||||
definitions_fn,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn get_name(&self) -> &'static str {
|
pub const fn get_name(&self) -> &'static str {
|
||||||
@@ -45,10 +35,6 @@ impl Module {
|
|||||||
pub fn register(&self, lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
pub fn register(&self, lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
||||||
(self.register_fn)(lua)
|
(self.register_fn)(lua)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn definitions(&self) -> Option<String> {
|
|
||||||
self.definitions_fn.map(|f| f())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_modules(lua: &mlua::Lua) -> mlua::Result<()> {
|
pub fn load_modules(lua: &mlua::Lua) -> mlua::Result<()> {
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ mod timeout;
|
|||||||
|
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
use lua_typed::Typed;
|
|
||||||
pub use timeout::Timeout;
|
pub use timeout::Timeout;
|
||||||
|
|
||||||
use crate::Module;
|
use crate::Module;
|
||||||
@@ -29,20 +28,4 @@ fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
|||||||
Ok(utils)
|
Ok(utils)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_definitions() -> String {
|
inventory::submit! {Module::new("automation:utils", create_module)}
|
||||||
let mut output = String::new();
|
|
||||||
|
|
||||||
output += "---@meta\n\nlocal utils\n\n";
|
|
||||||
|
|
||||||
output += &Timeout::generate_full().expect("Timeout should have generate_full");
|
|
||||||
output += "\n";
|
|
||||||
|
|
||||||
output += "---@return string\nfunction utils.get_hostname() end\n\n";
|
|
||||||
output += "---@return integer\nfunction utils.get_epoch() end\n\n";
|
|
||||||
|
|
||||||
output += "return utils";
|
|
||||||
|
|
||||||
output
|
|
||||||
}
|
|
||||||
|
|
||||||
inventory::submit! {Module::new("automation:utils", create_module, Some(generate_definitions))}
|
|
||||||
|
|||||||
@@ -132,20 +132,4 @@ fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
|||||||
Ok(mqtt)
|
Ok(mqtt)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_definitions() -> String {
|
inventory::submit! {Module::new("automation:mqtt", create_module)}
|
||||||
let mut output = String::new();
|
|
||||||
|
|
||||||
output += "---@meta\n\nlocal mqtt\n\n";
|
|
||||||
|
|
||||||
output += &MqttConfig::generate_full().expect("WrappedAsyncClient should have generate_full");
|
|
||||||
output += "\n";
|
|
||||||
output +=
|
|
||||||
&WrappedAsyncClient::generate_full().expect("WrappedAsyncClient should have generate_full");
|
|
||||||
output += "\n";
|
|
||||||
|
|
||||||
output += "return mqtt";
|
|
||||||
|
|
||||||
output
|
|
||||||
}
|
|
||||||
|
|
||||||
inventory::submit! {Module::new("automation:mqtt", create_module, Some(generate_definitions))}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user