allow initialisation when reconnected

This commit is contained in:
Laila van Reenen 2025-05-05 14:54:54 +02:00
parent f51a08fb50
commit 49e8adf2eb
Signed by: LailaTheElf
GPG Key ID: 8A3EF0226518C12D
3 changed files with 20 additions and 12 deletions

2
Cargo.lock generated
View File

@ -246,7 +246,7 @@ dependencies = [
[[package]] [[package]]
name = "mqtt-client" name = "mqtt-client"
version = "3.0.1" version = "4.0.0"
dependencies = [ dependencies = [
"crossbeam", "crossbeam",
"rumqttc", "rumqttc",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "mqtt-client" name = "mqtt-client"
version = "3.0.1" version = "4.0.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@ -11,8 +11,8 @@ pub mod mqtt_client {
pub use rumqttc::QoS; pub use rumqttc::QoS;
pub trait MqttTool<S: std::marker::Send> { pub trait MqttTool<S: std::marker::Send> {
fn new(client: Client, tx: Sender<MqttMessage>, config: S) -> Self; fn new(tx: Sender<MqttMessage>, config: S, client: Client) -> Self;
fn run(&mut self, rx: Receiver<MqttMessage>); fn run(&mut self, event_recever: Receiver<MqttEvent>);
} }
pub struct MqttMessage { pub struct MqttMessage {
@ -22,9 +22,15 @@ pub mod mqtt_client {
pub qos: QoS, pub qos: QoS,
} }
pub enum MqttEvent {
Connected,
Disconnected,
Message(MqttMessage)
}
pub fn run<S: std::marker::Send, T: MqttTool<S>>(host: String, port: u16, client: String, user: String, pass: String, config: S) where S: 'static { pub fn run<S: std::marker::Send, T: MqttTool<S>>(host: String, port: u16, client: String, user: String, pass: String, config: S) where S: 'static {
let (tx_sender, tx_recever) = unbounded::<MqttMessage>(); let (tx_sender, tx_recever) = unbounded::<MqttMessage>();
let (rx_sender, rx_recever) = unbounded::<MqttMessage>(); let (event_sender, event_recever) = unbounded::<MqttEvent>();
println!("INFO : mqtt client: run"); println!("INFO : mqtt client: run");
@ -50,7 +56,7 @@ pub mod mqtt_client {
let mqtt = thread::Builder::new() let mqtt = thread::Builder::new()
.name("mqtt".to_string()) .name("mqtt".to_string())
.spawn(move || { .spawn(move || {
handeler::<S, T>(connection, rx_sender); handeler::<S, T>(connection, event_sender);
}); });
match mqtt { match mqtt {
Err(_n) => println!("ERROR: mqtt client: failed to create mqtt thread"), Err(_n) => println!("ERROR: mqtt client: failed to create mqtt thread"),
@ -61,9 +67,8 @@ pub mod mqtt_client {
let tool_runner = thread::Builder::new() let tool_runner = thread::Builder::new()
.name("tool runner".to_string()) .name("tool runner".to_string())
.spawn(move || { .spawn(move || {
let mut tool = T::new(client, tx_sender, config); let mut tool = T::new(tx_sender, config, client);
tool.run(rx_recever); tool.run(event_recever);
println!("WARN : rool_runner: tool has ended");
}); });
match tool_runner { match tool_runner {
Err(_n) => println!("ERROR: mqtt client: failed to create publisher thread"), Err(_n) => println!("ERROR: mqtt client: failed to create publisher thread"),
@ -95,7 +100,7 @@ pub mod mqtt_client {
} }
} }
pub(self) fn handeler<S: std::marker::Send,T: MqttTool<S>>(mut connection: Connection, rx: Sender<MqttMessage>) { pub(self) fn handeler<S: std::marker::Send,T: MqttTool<S>>(mut connection: Connection, rx: Sender<MqttEvent>) {
for (_i, notification) in connection.iter().enumerate() { for (_i, notification) in connection.iter().enumerate() {
let mut delay: bool = false; let mut delay: bool = false;
match notification { match notification {
@ -146,7 +151,10 @@ pub mod mqtt_client {
}, },
Event::Incoming(n) => match n { Event::Incoming(n) => match n {
Packet::Connect(_) => println!("INFO : mqtt: connected"), Packet::Connect(_) => println!("INFO : mqtt: connected"),
Packet::ConnAck(_) => println!("INFO : mqtt: connaction acknolaged"), Packet::ConnAck(_) => {
println!("INFO : mqtt: connaction acknolaged");
let _ = rx.send(MqttEvent::Connected);
},
Packet::Publish(msg) => { Packet::Publish(msg) => {
let mut payload: String = String::from(""); let mut payload: String = String::from("");
match String::from_utf8(msg.payload.to_vec()) { match String::from_utf8(msg.payload.to_vec()) {
@ -161,7 +169,7 @@ pub mod mqtt_client {
qos: msg.qos, qos: msg.qos,
}; };
match rx.send(message) { match rx.send(MqttEvent::Message(message)) {
Err(n) => println!("ERROR: faild to send incomming message to tool ({:?})", n), Err(n) => println!("ERROR: faild to send incomming message to tool ({:?})", n),
Ok(_n) => {} Ok(_n) => {}
} }