From ab947b895c3767ab404bfae71a5227a760117071 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Sun, 5 May 2024 11:08:58 +0700 Subject: [PATCH] Use a Uuid for the mqtt client ID --- Cargo.lock | 1 + Cargo.toml | 1 + src/mqtt/mqttc.rs | 5 +++-- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8c3f553..acff494 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1582,6 +1582,7 @@ dependencies = [ "tokio-stream", "tokio-util", "toml", + "uuid", "validator 0.17.0", "validator_derive 0.17.0", ] diff --git a/Cargo.toml b/Cargo.toml index 32eeaef..9741eb7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,6 +41,7 @@ tokio = { version = "1.27.0", features = ["rt-multi-thread", "macros", "io-util" tokio-stream = "0.1.12" tokio-util = { version = "0.7.7", features = ["full", "tracing"] } toml = "0.8.2" +uuid = { version = "1.8.0", features = ["v4"] } validator = "0.17.0" validator_derive = "0.17.0" diff --git a/src/mqtt/mqttc.rs b/src/mqtt/mqttc.rs index 338fc73..c7f8d06 100644 --- a/src/mqtt/mqttc.rs +++ b/src/mqtt/mqttc.rs @@ -22,6 +22,7 @@ use tokio::{ }; use tokio_stream::{wrappers::BroadcastStream, StreamExt}; use tokio_util::sync::CancellationToken; +use uuid::Uuid; pub(crate) struct Mqtt { cancel: CancellationToken, @@ -125,7 +126,7 @@ impl<'a> MqttBackend<'a> { async fn run(&mut self) -> AnyResult<()> { log::trace!("Run MQTT Server"); let mut mqttoptions = MqttOptions::new( - "Neolink".to_string(), + format!("Neolink{}", Uuid::new_v4()), &self.config.broker_addr, self.config.port, ); @@ -519,7 +520,7 @@ impl LastWillMqtt { ) -> AnyResult { log::trace!("Run MQTT Last Will"); let mut mqttoptions = MqttOptions::new( - format!("NeolinkLastWill_{}", topic), + format!("NeolinkLastWill_{}_{}", topic, Uuid::new_v4()), &config.broker_addr, config.port, );