Moved some config over to an actual config file, improved error handling
This commit is contained in:
30
src/main.rs
30
src/main.rs
@@ -1,34 +1,32 @@
|
||||
use std::{time::Duration, sync::{Arc, RwLock}, process::exit, net::SocketAddr};
|
||||
mod config;
|
||||
|
||||
use std::{time::Duration, sync::{Arc, RwLock}, process, net::SocketAddr};
|
||||
|
||||
use config::Config;
|
||||
use dotenv::dotenv;
|
||||
use warp::Filter;
|
||||
use rumqttc::{MqttOptions, Transport, AsyncClient};
|
||||
use dotenv::dotenv;
|
||||
use env_logger::Builder;
|
||||
use log::{error, info, LevelFilter};
|
||||
|
||||
use automation::{devices::{Devices, IkeaOutlet, TestOutlet}, zigbee::Zigbee, mqtt::Notifier};
|
||||
use google_home::{GoogleHome, Request};
|
||||
|
||||
fn get_required_env(name: &str) -> String {
|
||||
match std::env::var(name) {
|
||||
Ok(value) => value,
|
||||
_ => {
|
||||
error!("Environment variable ${name} is not set!");
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
dotenv().ok();
|
||||
|
||||
// Setup logger
|
||||
Builder::new()
|
||||
.filter_module("automation", LevelFilter::Info)
|
||||
.parse_default_env()
|
||||
.init();
|
||||
|
||||
// Load dotfiles
|
||||
dotenv().ok();
|
||||
let config = std::env::var("AUTOMATION_CONFIG").unwrap_or("./config/config.toml".to_owned());
|
||||
let config = Config::build(&config).unwrap_or_else(|err| {
|
||||
error!("Failed to load config: {err}");
|
||||
process::exit(1);
|
||||
});
|
||||
|
||||
info!("Starting automation_rs...");
|
||||
|
||||
@@ -37,8 +35,8 @@ async fn main() {
|
||||
let devices = Arc::new(RwLock::new(Devices::new()));
|
||||
|
||||
// Setup MQTT
|
||||
let mut mqttoptions = MqttOptions::new("rust-test", get_required_env("MQTT_HOST"), 8883);
|
||||
mqttoptions.set_credentials(get_required_env("MQTT_USERNAME"), get_required_env("MQTT_PASSWORD"));
|
||||
let mut mqttoptions = MqttOptions::new("rust-test", config.mqtt.host, config.mqtt.port);
|
||||
mqttoptions.set_credentials(config.mqtt.username, config.mqtt.password.unwrap());
|
||||
mqttoptions.set_keep_alive(Duration::from_secs(5));
|
||||
mqttoptions.set_transport(Transport::tls_with_default_config());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user