From 80c3a7ca5e78e7815f90aa481f921250f8aa5659 Mon Sep 17 00:00:00 2001 From: Claas Date: Sun, 20 Oct 2024 20:36:13 +0200 Subject: [PATCH] Refactor to reduce duplicate documentation --- fan-controller/src/configuration.rs | 22 +++++++++++++++------- fan-controller/src/main.rs | 4 ++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/fan-controller/src/configuration.rs b/fan-controller/src/configuration.rs index f86cc6a..7da86c9 100644 --- a/fan-controller/src/configuration.rs +++ b/fan-controller/src/configuration.rs @@ -3,14 +3,14 @@ use embassy_time::Duration; //TODO make configurable /// Don't put credentials in the source code -pub(crate) const WIFI_NETWORK: &str = ""; // env!("FAN_CONTROL_WIFI_NETWORK"); +pub(crate) const WIFI_NETWORK: &str = env!("FAN_CONTROL_WIFI_NETWORK"); //TODO make configurable /// Don't put credentials in the source code -pub(crate) const WIFI_PASSWORD: &str = ""; //env!("FAN_CONTROL_WIFI_PASSWORD"); +pub(crate) const WIFI_PASSWORD: &str = env!("FAN_CONTROL_WIFI_PASSWORD"); //TODO make configurable -pub(crate) const MQTT_BROKER_ADDRESS: &str = "homeassistant.local"; +pub(crate) const MQTT_BROKER_ADDRESS: &str = "homeassistant"; //TODO make configurable pub(crate) const MQTT_BROKER_PORT: u16 = 1883; @@ -21,10 +21,18 @@ pub(crate) const MQTT_BROKER_PORT: u16 = 1883; pub(crate) const MQTT_BROKER_IP_ADDRESS: Option = None; //TODO make configurable -pub(crate) const MQTT_BROKER_USERNAME: &str = ""; +/// Set in Homeassitant under Settings > People > Users Tab. Not to be confused with the People tab. +/// The Users Tab might only be visible in advanced mode as administrator. +/// A separate account is recommended for each device. +pub(crate) struct MqttBrokerCredentials<'a> { + pub(crate) username: &'a str, + pub(crate) password: &'a [u8], +} -//TODO make configurable -pub(crate) const MQTT_BROKER_PASSWORD: &[u8] = b""; +pub(crate) const MQTT_BROKER_CREDENTIALS: MqttBrokerCredentials = MqttBrokerCredentials { + username: "fancontroller", + password: b"test", +}; //TODO make configurable /// Prefix is "homeassistant", but it can be changed in home assistant configuration @@ -43,4 +51,4 @@ const _: () = { /// The timeout not to be confused with the keep alive interval is used for packets that require a /// response packet from the broker. If the client does not receive a response within the timeout /// the client will stop waiting for a response which can lead to a disconnect or retry in some cases. -pub(crate) const TIMEOUT: Duration = Duration::from_secs(5); \ No newline at end of file +pub(crate) const TIMEOUT: Duration = Duration::from_secs(5); diff --git a/fan-controller/src/main.rs b/fan-controller/src/main.rs index 90a8d23..bdd7c3c 100644 --- a/fan-controller/src/main.rs +++ b/fan-controller/src/main.rs @@ -281,8 +281,8 @@ async fn mqtt_task( use mqtt::task; let packet = Connect { client_identifier: "testfan", - username: configuration::MQTT_BROKER_USERNAME, - password: configuration::MQTT_BROKER_PASSWORD, + username: configuration::MQTT_BROKER_CREDENTIALS.username, + password: configuration::MQTT_BROKER_CREDENTIALS.password, keep_alive_seconds: configuration::KEEP_ALIVE.as_secs() as u16, }; -- 2.51.2