use std::{collections::BTreeMap, time::Duration};
use serde::Deserialize;
use crate::{
core::{CoreError, CoreResult, LogConfig, LogSection, ServiceConfig, load_config},
mqtt::{MqttConfig, MqttReconnectConfig},
};
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(default, deny_unknown_fields)]
pub struct MqttServiceConfig {
pub name: String,
pub mode: String,
pub log: LogSection,
pub mqtt: MqttSection,
pub auth: Option<MqttAuthSection>,
}
impl Default for MqttServiceConfig {
fn default() -> Self {
let service = ServiceConfig::default();
Self {
name: service.name,
mode: service.mode,
log: service.log,
mqtt: MqttSection::default(),
auth: None,
}
}
}
impl MqttServiceConfig {
pub fn load(basename: &str, env_prefix: &str) -> Result<Self, config::ConfigError> {
load_config(basename, env_prefix)
}
pub fn log_config(&self) -> LogConfig {
self.log.to_log_config(&self.name)
}
pub fn mqtt_config(&self) -> CoreResult<MqttConfig> {
let broker = self.mqtt.brokers.get(&self.mqtt.broker).ok_or_else(|| {
CoreError::Config(config::ConfigError::Message(format!(
"missing mqtt broker profile: {}",
self.mqtt.broker
)))
})?;
Ok(MqttConfig {
client_id: self.mqtt.client_id.clone(),
host: broker.host.clone(),
port: broker.port,
username: (!broker.username.is_empty()).then(|| broker.username.clone()),
password: (!broker.password.is_empty()).then(|| broker.password.clone()),
keep_alive: Duration::from_secs(broker.keep_alive_secs),
clean_session: self.mqtt.clean_session,
inflight: self.mqtt.inflight,
subscriptions: Vec::new(),
reconnect: MqttReconnectConfig {
initial_delay: Duration::from_millis(self.mqtt.reconnect.initial_delay_ms),
max_delay: Duration::from_millis(self.mqtt.reconnect.max_delay_ms),
},
handler_timeout: Some(Duration::from_millis(self.mqtt.handler_timeout_ms)),
#[cfg(feature = "observability")]
metrics_registry: None,
})
}
pub fn jwt_secret(&self) -> Option<String> {
self.auth.as_ref().and_then(MqttAuthSection::jwt_secret)
}
}
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(default, deny_unknown_fields)]
pub struct MqttSection {
pub client_id: String,
pub broker: String,
pub handler_timeout_ms: u64,
pub inflight: usize,
pub clean_session: bool,
pub brokers: BTreeMap<String, MqttBrokerSection>,
pub reconnect: MqttReconnectSection,
}
impl Default for MqttSection {
fn default() -> Self {
Self {
client_id: "rs-zero-mqtt".to_string(),
broker: "default".to_string(),
handler_timeout_ms: 5000,
inflight: 10,
clean_session: true,
brokers: BTreeMap::new(),
reconnect: MqttReconnectSection::default(),
}
}
}
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(default, deny_unknown_fields)]
pub struct MqttBrokerSection {
pub host: String,
pub port: u16,
pub username: String,
pub password: String,
pub keep_alive_secs: u64,
}
impl Default for MqttBrokerSection {
fn default() -> Self {
Self {
host: "127.0.0.1".to_string(),
port: 1883,
username: String::new(),
password: String::new(),
keep_alive_secs: 30,
}
}
}
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(default, deny_unknown_fields)]
pub struct MqttReconnectSection {
pub initial_delay_ms: u64,
pub max_delay_ms: u64,
}
impl Default for MqttReconnectSection {
fn default() -> Self {
Self {
initial_delay_ms: 200,
max_delay_ms: 5000,
}
}
}
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(default, deny_unknown_fields)]
pub struct MqttAuthSection {
pub jwt_secret: String,
pub jwt_expires: u64,
}
impl Default for MqttAuthSection {
fn default() -> Self {
Self {
jwt_secret: String::new(),
jwt_expires: 7200,
}
}
}
impl MqttAuthSection {
fn jwt_secret(&self) -> Option<String> {
(!self.jwt_secret.is_empty()).then(|| self.jwt_secret.clone())
}
}