pub(crate) mod mqtt; pub(crate) use mqtt::*; use tokio::{runtime, sync::mpsc::Sender}; use std::env; use homie5::HomieDomain; pub struct Settings { pub hostname: String, pub port: u16, pub username: String, pub password: String, pub client_id: String, pub homie_domain: HomieDomain, } pub fn get_settings() -> Settings { let hostname = env::var("HOMIE_MQTT_HOST").unwrap_or_default(); let port = if let Ok(port) = env::var("HOMIE_MQTT_PORT") { port.parse::().expect("Not a valid number for port!") } else { 1883 }; let username = env::var("HOMIE_MQTT_USERNAME").unwrap_or_default(); let password = env::var("HOMIE_MQTT_PASSWORD").unwrap_or_default(); let client_id = if let Ok(client_id) = env::var("HOMIE_MQTT_CLIENT_ID") { client_id } else { String::from("aslkdnlauidhwwkednwek") }; let homie_domain = if let Ok(homie_domain) = env::var("HOMIE_MQTT_HOMIE_DOMAIN") { homie_domain.try_into().unwrap() } else { HomieDomain::Default }; Settings { hostname, port, username, password, client_id, homie_domain, } } pub fn setup_ctrlc(ctrl_sender: Sender, exit_variant: T) where T: Send + Sync + Clone + 'static, { if let Err(err) = ctrlc::set_handler(move || { let rt = runtime::Runtime::new().unwrap(); let ctrl_sender = ctrl_sender.clone(); let exit_variant_clone = exit_variant.clone(); // Clone exit_variant here rt.block_on(async move { ctrl_sender .send(exit_variant_clone) .await .expect("Error during application shutdown!"); }); }) { log::error!("Fatal Error: Cannot set ctrl-c app exit handler:\n{:#?}", err); panic!("Will exit now"); } }