Compare commits
2 Commits
5947098bfb
...
feature/im
| Author | SHA1 | Date | |
|---|---|---|---|
|
ad158f2c22
|
|||
|
f36adf2f19
|
@@ -1,12 +1,8 @@
|
||||
use std::fs::{self, File};
|
||||
use std::io::Write;
|
||||
|
||||
use automation::config::{
|
||||
Config, FulfillmentConfig, Module as ConfigModule, Schedule, SetupFunction,
|
||||
};
|
||||
use automation::config::generate_definitions;
|
||||
use automation_lib::Module;
|
||||
use automation_lib::mqtt::{MqttConfig, WrappedAsyncClient};
|
||||
use lua_typed::Typed;
|
||||
use tracing::{info, warn};
|
||||
|
||||
extern crate automation_devices;
|
||||
@@ -29,28 +25,6 @@ fn write_definitions(filename: &str, definitions: &str) -> std::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn config_definitions() -> String {
|
||||
let mut output = "---@meta\n\n".to_string();
|
||||
|
||||
output +=
|
||||
&FulfillmentConfig::generate_full().expect("FulfillmentConfig should have a definition");
|
||||
output += "\n";
|
||||
output += &Config::generate_full().expect("Config should have a definition");
|
||||
output += "\n";
|
||||
output += &SetupFunction::generate_full().expect("SetupFunction should have a definition");
|
||||
output += "\n";
|
||||
output += &Schedule::generate_full().expect("Schedule should have a definition");
|
||||
output += "\n";
|
||||
output += &ConfigModule::generate_full().expect("Module should have a definition");
|
||||
output += "\n";
|
||||
output += &MqttConfig::generate_full().expect("MqttConfig should have a definition");
|
||||
output += "\n";
|
||||
output +=
|
||||
&WrappedAsyncClient::generate_full().expect("WrappedAsyncClient should have a definition");
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
@@ -65,7 +39,7 @@ fn main() -> std::io::Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
write_definitions("config.lua", &config_definitions())?;
|
||||
write_definitions("config.lua", &generate_definitions())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::net::{Ipv4Addr, SocketAddr};
|
||||
use std::ops::Deref;
|
||||
|
||||
use automation_lib::action_callback::ActionCallback;
|
||||
use automation_lib::device::Device;
|
||||
@@ -37,7 +38,7 @@ pub struct FulfillmentConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SetupFunction(mlua::Function);
|
||||
struct SetupFunction(mlua::Function);
|
||||
|
||||
impl Typed for SetupFunction {
|
||||
fn type_name() -> String {
|
||||
@@ -60,8 +61,16 @@ impl FromLua for SetupFunction {
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for SetupFunction {
|
||||
type Target = mlua::Function;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Schedule(HashMap<String, ActionCallback<()>>);
|
||||
struct Schedule(HashMap<String, ActionCallback<()>>);
|
||||
|
||||
impl Typed for Schedule {
|
||||
fn type_name() -> String {
|
||||
@@ -83,12 +92,22 @@ impl FromLua for Schedule {
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoIterator for Schedule {
|
||||
type Item = <HashMap<String, ActionCallback<()>> as IntoIterator>::Item;
|
||||
|
||||
type IntoIter = <HashMap<String, ActionCallback<()>> as IntoIterator>::IntoIter;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.0.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Module {
|
||||
pub setup: Option<SetupFunction>,
|
||||
pub devices: Vec<Box<dyn Device>>,
|
||||
pub schedule: Schedule,
|
||||
pub modules: Vec<Module>,
|
||||
struct Module {
|
||||
setup: Option<SetupFunction>,
|
||||
devices: Vec<Box<dyn Device>>,
|
||||
schedule: Schedule,
|
||||
modules: Vec<Module>,
|
||||
}
|
||||
|
||||
// TODO: Add option to typed to rename field
|
||||
@@ -189,7 +208,7 @@ impl Modules {
|
||||
modules.extend(module.modules);
|
||||
|
||||
if let Some(setup) = module.setup {
|
||||
let result: mlua::Value = setup.0.call_async(client.clone()).await?;
|
||||
let result: mlua::Value = setup.call_async(client.clone()).await?;
|
||||
|
||||
if result.is_nil() {
|
||||
// We ignore nil results
|
||||
@@ -209,7 +228,7 @@ impl Modules {
|
||||
}
|
||||
|
||||
devices.extend(module.devices);
|
||||
for (cron, f) in module.schedule.0 {
|
||||
for (cron, f) in module.schedule {
|
||||
scheduler.add_job(cron, f);
|
||||
}
|
||||
}
|
||||
@@ -245,3 +264,25 @@ fn default_fulfillment_ip() -> Ipv4Addr {
|
||||
fn default_fulfillment_port() -> u16 {
|
||||
7878
|
||||
}
|
||||
|
||||
pub fn generate_definitions() -> String {
|
||||
let mut output = "---@meta\n\n".to_string();
|
||||
|
||||
output +=
|
||||
&FulfillmentConfig::generate_full().expect("FulfillmentConfig should have a definition");
|
||||
output += "\n";
|
||||
output += &Config::generate_full().expect("Config should have a definition");
|
||||
output += "\n";
|
||||
output += &SetupFunction::generate_full().expect("SetupFunction should have a definition");
|
||||
output += "\n";
|
||||
output += &Schedule::generate_full().expect("Schedule should have a definition");
|
||||
output += "\n";
|
||||
output += &Module::generate_full().expect("Module should have a definition");
|
||||
output += "\n";
|
||||
output += &MqttConfig::generate_full().expect("MqttConfig should have a definition");
|
||||
output += "\n";
|
||||
output +=
|
||||
&WrappedAsyncClient::generate_full().expect("WrappedAsyncClient should have a definition");
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user