use crate::{MqttBrokerConfiguration, task::MqttBrokerCredentials}; use embassy_time::Duration; /// Thanks! /// - https://users.rust-lang.org/t/pass-numeric-compile-time-arguments/59093/2 /// - https://www.reddit.com/r/rust/comments/10ol38k/comment/j6jrpvd const fn parse_u16(string: &str) -> u16 { let mut out: u16 = 0; let mut index: usize = 0; while index < string.len() { out *= 10; let byte = string.as_bytes()[index]; assert!( b'0' <= byte && byte <= b'9', "invalid digit. Needs to be a number", ); out += (byte - b'0') as u16; index += 1; } out } //TODO make configurable /// Don't put credentials in the source code 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"); //TODO make configurable pub(crate) const MQTT_BROKER_ADDRESS: &str = env!("FAN_CONTROL_MQTT_BROKER_ADDRESS"); //TODO make configurable pub(crate) const MQTT_BROKER_PORT: u16 = parse_u16(env!("FAN_CONTROL_MQTT_BROKER_PORT")); pub(crate) const MQTT_BROKER_CREDENTIALS: MqttBrokerCredentials = MqttBrokerCredentials { username: env!("FAN_CONTROL_MQTT_BROKER_USERNAME"), password: env!("FAN_CONTROL_MQTT_BROKER_PASSWORD").as_bytes(), }; /// The keep alive interval defines the maximum time between messages sent to the broker. /// The broker will disconnect the client if no message is received within 1.5 times of the keep alive interval. pub(crate) const KEEP_ALIVE: Duration = Duration::from_secs(60); const _: () = { // Check if the representation as u16 is correct let seconds = KEEP_ALIVE.as_secs() as u16; core::assert!(seconds == 60); }; /// How often the Wi-Fi link is checked once it is up, to notice the access point going away. pub(crate) const WIFI_LINK_CHECK_INTERVAL: Duration = Duration::from_secs(5); /// How long to wait between attempts to join the Wi-Fi network. Without it a network that is not /// there is retried as fast as the chip can answer, which is a busy loop for as long as the /// access point stays off. pub(crate) const WIFI_JOIN_RETRY_DELAY: Duration = Duration::from_secs(5); /// 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 MQTT_TIMEOUT: Duration = Duration::from_secs(60); /// The MQTT connection is established again after it was lost. The wait between attempts starts /// at this value and doubles up to [`MQTT_RECONNECT_BACKOFF_MAX`], so a broker that is down for a /// while is not asked every second while a short hiccup is still recovered from quickly. pub(crate) const MQTT_RECONNECT_BACKOFF_INITIAL: Duration = Duration::from_secs(1); /// The longest the client waits between two attempts to reconnect to the MQTT broker. pub(crate) const MQTT_RECONNECT_BACKOFF_MAX: Duration = Duration::from_secs(60); /// The timeout to wait for a response from the fans before cancelling waiting for a response. pub(crate) const FAN_TIMEOUT: Duration = Duration::from_secs(5); pub(crate) const MQTT_BROKER: MqttBrokerConfiguration<'_> = MqttBrokerConfiguration { address: MQTT_BROKER_ADDRESS, credentials: MQTT_BROKER_CREDENTIALS, client_identifier: env!("CARGO_PKG_NAME"), keep_alive_seconds: KEEP_ALIVE, };