Compare commits
2 Commits
master
...
b327d32177
| Author | SHA1 | Date | |
|---|---|---|---|
|
b327d32177
|
|||
|
d3d9a19f52
|
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -98,6 +98,7 @@ dependencies = [
|
|||||||
"config",
|
"config",
|
||||||
"git-version",
|
"git-version",
|
||||||
"google_home",
|
"google_home",
|
||||||
|
"inventory",
|
||||||
"mlua",
|
"mlua",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"rumqttc",
|
"rumqttc",
|
||||||
|
|||||||
@@ -76,6 +76,7 @@ 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,20 +15,7 @@ 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;
|
use tracing::{debug, warn};
|
||||||
|
|
||||||
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>;
|
||||||
@@ -55,6 +42,18 @@ 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> {
|
||||||
@@ -71,7 +70,9 @@ pub fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
|||||||
Ok(devices)
|
Ok(devices)
|
||||||
}
|
}
|
||||||
|
|
||||||
inventory::submit! {Module::new("automation:devices", create_module)}
|
type RegisterTypeFn = fn() -> Option<String>;
|
||||||
|
|
||||||
|
pub struct RegisteredType(RegisterTypeFn);
|
||||||
|
|
||||||
macro_rules! register_type {
|
macro_rules! register_type {
|
||||||
($ty:ty) => {
|
($ty:ty) => {
|
||||||
@@ -80,20 +81,25 @@ 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);
|
||||||
|
|
||||||
pub fn generate_definitions() {
|
fn generate_definitions() -> String {
|
||||||
println!("---@meta\n\nlocal devices\n");
|
let mut output = String::new();
|
||||||
|
|
||||||
|
output += "---@meta\n\nlocal devices\n\n";
|
||||||
for ty in inventory::iter::<RegisteredType> {
|
for ty in inventory::iter::<RegisteredType> {
|
||||||
let def = ty.0().unwrap();
|
if let Some(def) = ty.0() {
|
||||||
println!("{def}");
|
output += &(def + "\n");
|
||||||
|
} 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))}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ use std::sync::Arc;
|
|||||||
use futures::Future;
|
use futures::Future;
|
||||||
use futures::future::join_all;
|
use futures::future::join_all;
|
||||||
use lua_typed::Typed;
|
use lua_typed::Typed;
|
||||||
|
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 tracing::{debug, instrument, trace};
|
use tracing::{debug, instrument, trace};
|
||||||
@@ -14,7 +15,7 @@ use crate::event::{Event, EventChannel, OnMqtt};
|
|||||||
|
|
||||||
pub type DeviceMap = HashMap<String, Box<dyn Device>>;
|
pub type DeviceMap = HashMap<String, Box<dyn Device>>;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, FromLua)]
|
||||||
pub struct DeviceManager {
|
pub struct DeviceManager {
|
||||||
devices: Arc<RwLock<DeviceMap>>,
|
devices: Arc<RwLock<DeviceMap>>,
|
||||||
event_channel: EventChannel,
|
event_channel: EventChannel,
|
||||||
|
|||||||
@@ -17,15 +17,25 @@ 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(name: &'static str, register_fn: RegisterFn) -> Self {
|
pub const fn new(
|
||||||
Self { name, register_fn }
|
name: &'static str,
|
||||||
|
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 {
|
||||||
@@ -35,6 +45,10 @@ 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,6 +2,7 @@ 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;
|
||||||
@@ -28,4 +29,20 @@ fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
|||||||
Ok(utils)
|
Ok(utils)
|
||||||
}
|
}
|
||||||
|
|
||||||
inventory::submit! {Module::new("automation:utils", create_module)}
|
fn generate_definitions() -> String {
|
||||||
|
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))}
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
use lua_typed::Typed;
|
use lua_typed::Typed;
|
||||||
use mlua::FromLua;
|
use mlua::{FromLua, LuaSerdeExt};
|
||||||
use rumqttc::{AsyncClient, Event, EventLoop, Incoming};
|
use rumqttc::{AsyncClient, Event, EventLoop, Incoming};
|
||||||
use tracing::{debug, warn};
|
use tracing::{debug, warn};
|
||||||
|
|
||||||
|
use crate::Module;
|
||||||
use crate::config::MqttConfig;
|
use crate::config::MqttConfig;
|
||||||
|
use crate::device_manager::DeviceManager;
|
||||||
use crate::event::{self, EventChannel};
|
use crate::event::{self, EventChannel};
|
||||||
|
|
||||||
#[derive(Debug, Clone, FromLua)]
|
#[derive(Debug, Clone, FromLua)]
|
||||||
@@ -39,9 +41,10 @@ impl Typed for WrappedAsyncClient {
|
|||||||
let type_name = Self::type_name();
|
let type_name = Self::type_name();
|
||||||
|
|
||||||
output += &format!("mqtt.{type_name} = {{}}\n");
|
output += &format!("mqtt.{type_name} = {{}}\n");
|
||||||
|
output += &format!("---@param device_manager {}\n", DeviceManager::type_name());
|
||||||
output += &format!("---@param config {}\n", MqttConfig::type_name());
|
output += &format!("---@param config {}\n", MqttConfig::type_name());
|
||||||
output += &format!("---@return {type_name}\n");
|
output += &format!("---@return {type_name}\n");
|
||||||
output += "function mqtt.new(config) end\n";
|
output += "function mqtt.new(device_manager, config) end\n";
|
||||||
|
|
||||||
Some(output)
|
Some(output)
|
||||||
}
|
}
|
||||||
@@ -108,3 +111,41 @@ pub fn start(mut eventloop: EventLoop, event_channel: &EventChannel) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
||||||
|
let mqtt = lua.create_table()?;
|
||||||
|
let mqtt_new = lua.create_function(
|
||||||
|
move |lua, (device_manager, config): (DeviceManager, mlua::Value)| {
|
||||||
|
let event_channel = device_manager.event_channel();
|
||||||
|
let config: MqttConfig = lua.from_value(config)?;
|
||||||
|
|
||||||
|
// Create a mqtt client
|
||||||
|
// TODO: When starting up, the devices are not yet created, this could lead to a device being out of sync
|
||||||
|
let (client, eventloop) = AsyncClient::new(config.into(), 100);
|
||||||
|
start(eventloop, &event_channel);
|
||||||
|
|
||||||
|
Ok(WrappedAsyncClient(client))
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
mqtt.set("new", mqtt_new)?;
|
||||||
|
|
||||||
|
Ok(mqtt)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_definitions() -> String {
|
||||||
|
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))}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ local fulfillment = {
|
|||||||
openid_url = "https://login.huizinga.dev/api/oidc",
|
openid_url = "https://login.huizinga.dev/api/oidc",
|
||||||
}
|
}
|
||||||
|
|
||||||
local mqtt_client = require("automation:mqtt").new({
|
local mqtt_client = require("automation:mqtt").new(device_manager, {
|
||||||
host = ((host == "zeus" or host == "hephaestus") and "olympus.lan.huizinga.dev") or "mosquitto",
|
host = ((host == "zeus" or host == "hephaestus") and "olympus.lan.huizinga.dev") or "mosquitto",
|
||||||
port = 8883,
|
port = 8883,
|
||||||
client_name = "automation-" .. host,
|
client_name = "automation-" .. host,
|
||||||
|
|||||||
19
src/main.rs
19
src/main.rs
@@ -9,9 +9,8 @@ use std::path::Path;
|
|||||||
use std::process;
|
use std::process;
|
||||||
|
|
||||||
use ::config::{Environment, File};
|
use ::config::{Environment, File};
|
||||||
use automation_lib::config::{FulfillmentConfig, MqttConfig};
|
use automation_lib::config::FulfillmentConfig;
|
||||||
use automation_lib::device_manager::DeviceManager;
|
use automation_lib::device_manager::DeviceManager;
|
||||||
use automation_lib::mqtt::{self, WrappedAsyncClient};
|
|
||||||
use axum::extract::{FromRef, State};
|
use axum::extract::{FromRef, State};
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use axum::routing::post;
|
use axum::routing::post;
|
||||||
@@ -19,7 +18,6 @@ use axum::{Json, Router};
|
|||||||
use config::Config;
|
use config::Config;
|
||||||
use google_home::{GoogleHome, Request, Response};
|
use google_home::{GoogleHome, Request, Response};
|
||||||
use mlua::LuaSerdeExt;
|
use mlua::LuaSerdeExt;
|
||||||
use rumqttc::AsyncClient;
|
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
use tracing::{debug, error, info, warn};
|
use tracing::{debug, error, info, warn};
|
||||||
use web::{ApiError, User};
|
use web::{ApiError, User};
|
||||||
@@ -138,21 +136,6 @@ async fn app() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
automation_lib::load_modules(&lua)?;
|
automation_lib::load_modules(&lua)?;
|
||||||
|
|
||||||
let mqtt = lua.create_table()?;
|
|
||||||
let event_channel = device_manager.event_channel();
|
|
||||||
let mqtt_new = lua.create_function(move |lua, config: mlua::Value| {
|
|
||||||
let config: MqttConfig = lua.from_value(config)?;
|
|
||||||
|
|
||||||
// Create a mqtt client
|
|
||||||
// TODO: When starting up, the devices are not yet created, this could lead to a device being out of sync
|
|
||||||
let (client, eventloop) = AsyncClient::new(config.into(), 100);
|
|
||||||
mqtt::start(eventloop, &event_channel);
|
|
||||||
|
|
||||||
Ok(WrappedAsyncClient(client))
|
|
||||||
})?;
|
|
||||||
mqtt.set("new", mqtt_new)?;
|
|
||||||
lua.register_module("automation:mqtt", mqtt)?;
|
|
||||||
|
|
||||||
lua.register_module("automation:device_manager", device_manager.clone())?;
|
lua.register_module("automation:device_manager", device_manager.clone())?;
|
||||||
|
|
||||||
lua.register_module("automation:variables", lua.to_value(&config.variables)?)?;
|
lua.register_module("automation:variables", lua.to_value(&config.variables)?)?;
|
||||||
|
|||||||
Reference in New Issue
Block a user