From 808549bcba77ae09ce47d1e286b8f53c7ccaa5b9 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Fri, 3 May 2024 19:37:16 +0200 Subject: [PATCH] Added lua function to get the current hostname This makes it possible to set options depending on what machine we are running --- Cargo.lock | 33 ++++++++++++++++++++++++++++++++- Cargo.toml | 10 +++++++++- config.lua | 17 +++++++++++------ src/main.rs | 6 ++++++ 4 files changed, 58 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0929a76..7861fb7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -86,6 +86,7 @@ dependencies = [ "eui48", "futures", "google-home", + "hostname", "indexmap 2.0.0", "mlua", "once_cell", @@ -693,6 +694,17 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "hostname" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9c7c7c8ac16c798734b8a24560c1362120597c40d5e1459f09498f8f6c8f2ba" +dependencies = [ + "cfg-if", + "libc", + "windows 0.52.0", +] + [[package]] name = "http" version = "0.2.9" @@ -794,7 +806,7 @@ dependencies = [ "iana-time-zone-haiku", "js-sys", "wasm-bindgen", - "windows", + "windows 0.48.0", ] [[package]] @@ -2300,6 +2312,25 @@ dependencies = [ "windows-targets 0.48.1", ] +[[package]] +name = "windows" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e48a53791691ab099e5e2ad123536d0fff50652600abaf43bbf952894110d0be" +dependencies = [ + "windows-core", + "windows-targets 0.52.5", +] + +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.5", +] + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index e1f3832..98cf13f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,8 +43,16 @@ enum_dispatch = "0.3.12" indexmap = { version = "2.0.0", features = ["serde"] } serde_yaml = "0.9.27" tokio-cron-scheduler = "0.9.4" -mlua = { version = "0.9.7", features = ["lua54", "vendored", "macros", "serialize", "async", "send"] } +mlua = { version = "0.9.7", features = [ + "lua54", + "vendored", + "macros", + "serialize", + "async", + "send", +] } once_cell = "1.19.0" +hostname = "0.4.0" [patch.crates-io] wakey = { git = "https://git.huizinga.dev/Dreaded_X/wakey" } diff --git a/config.lua b/config.lua index 58de353..bdacb27 100644 --- a/config.lua +++ b/config.lua @@ -1,8 +1,7 @@ print("Hello from lua") -automation.fulfillment = { - openid_url = "https://login.huizinga.dev/api/oidc", -} +local host = automation.util.get_hostname() +print("Running @" .. host) local debug, value = pcall(automation.util.get_env, "DEBUG") if debug and value ~= "true" then @@ -17,13 +16,19 @@ local function mqtt_automation(topic) return "automation/" .. topic end +automation.fulfillment = { + openid_url = "https://login.huizinga.dev/api/oidc", +} + local mqtt_client = automation.new_mqtt_client({ - host = debug and "olympus.lan.huizinga.dev" or "mosquitto", + host = (host == "zeus" and "olympus.lan.huizinga.dev") + or (host == "hephaestus" and "olympus.vpn.huizinga.dev") + or "mosquitto", port = 8883, - client_name = debug and "automation-debug" or "automation_rs", + client_name = "automation-" .. host, username = "mqtt", password = automation.util.get_env("MQTT_PASSWORD"), - tls = debug and true or false, + tls = host == "zeus" or host == "hephaestus", }) automation.device_manager:add(Ntfy.new({ diff --git a/src/main.rs b/src/main.rs index 9a94147..b88c35c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -84,6 +84,12 @@ async fn app() -> anyhow::Result<()> { std::env::var(name).map_err(mlua::ExternalError::into_lua_err) })?; util.set("get_env", get_env)?; + 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) + })?; + util.set("get_hostname", get_hostname)?; automation.set("util", util)?; lua.globals().set("automation", automation)?;