diff --git a/Cargo.lock b/Cargo.lock index 612faa2..f0174e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -204,6 +204,7 @@ dependencies = [ "dotenv", "handlebars", "hex", + "humantime-serde", "log", "lrwn_filters", "once_cell", @@ -703,6 +704,22 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] +name = "humantime-serde" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57a3db5ea5923d99402c94e9feb261dc5ee9b4efa158b0315f788cf549cc200c" +dependencies = [ + "humantime", + "serde", +] + [[package]] name = "iana-time-zone" version = "0.1.60" diff --git a/Cargo.toml b/Cargo.toml index e169885..28f8530 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ lrwn_filters = { version = "4.7", features = ["serde"] } serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } + humantime-serde = "1.1" log = "0.4" simple_logger = "4.3" syslog = "6.1" diff --git a/src/cmd/configfile.rs b/src/cmd/configfile.rs index da8dfea..d174981 100644 --- a/src/cmd/configfile.rs +++ b/src/cmd/configfile.rs @@ -81,6 +81,12 @@ pub fn run(config: &Configuration) { # a random id will be generated by ChirpStack. client_id="{{ mqtt.client_id }}" + # Keep alive interval. + # + # This defines the maximum time that that should pass without communication + # between the client and server. + keep_alive_interval="{{ integration.mqtt.keep_alive_interval }}" + # CA certificate file (optional) # # Use this when setting up a secure connection (when server uses ssl://...) diff --git a/src/config.rs b/src/config.rs index f5305c9..cf605bc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; use std::fs; +use std::time::Duration; use anyhow::Result; use serde::{Deserialize, Serialize}; @@ -52,6 +53,8 @@ pub struct Mqtt { pub qos: u8, pub clean_session: bool, pub client_id: String, + #[serde(with = "humantime_serde")] + pub keep_alive_interval: Duration, pub ca_cert: String, pub tls_cert: String, pub tls_key: String, @@ -68,6 +71,7 @@ impl Default for Mqtt { qos: 0, clean_session: false, client_id: "".into(), + keep_alive_interval: Duration::from_secs(30), ca_cert: "".into(), tls_cert: "".into(), tls_key: "".into(), diff --git a/src/mqtt.rs b/src/mqtt.rs index e0559d9..2ebedc2 100644 --- a/src/mqtt.rs +++ b/src/mqtt.rs @@ -141,6 +141,7 @@ pub async fn setup(conf: &Configuration) -> Result<()> { mqtt_opts.set_last_will(lwt_msg); mqtt_opts.set_clean_start(conf.mqtt.clean_session); + mqtt_opts.set_keep_alive(conf.mqtt.keep_alive_interval); if !conf.mqtt.username.is_empty() || !conf.mqtt.password.is_empty() { mqtt_opts.set_credentials(&conf.mqtt.username, &conf.mqtt.password); }