add configuation passing

This commit is contained in:
Laila van Reenen 2025-04-11 17:30:00 +02:00
parent d2d31b2bc1
commit f186e43733
Signed by: LailaTheElf
GPG Key ID: 8A3EF0226518C12D
3 changed files with 8 additions and 8 deletions

2
Cargo.lock generated
View File

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

View File

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

View File

@ -9,8 +9,8 @@ pub mod mqtt_client {
pub use crossbeam::channel::{Receiver, Sender}; pub use crossbeam::channel::{Receiver, Sender};
pub use rumqttc::QoS; pub use rumqttc::QoS;
pub trait MqttTool { pub trait MqttTool<S: std::marker::Send> {
fn new(client: Client, tx: Sender<MqttMessage>) -> Self; fn new(client: Client, tx: Sender<MqttMessage>, config: S) -> Self;
fn run(&mut self, rx: Receiver<MqttMessage>); fn run(&mut self, rx: Receiver<MqttMessage>);
} }
@ -21,7 +21,7 @@ pub mod mqtt_client {
pub qos: QoS, pub qos: QoS,
} }
pub fn run<T: MqttTool>(host: String, port: u16, client: String, user: String, pass: String) { 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 (rx_sender, rx_recever) = unbounded::<MqttMessage>();
@ -49,7 +49,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::<T>(connection, rx_sender); handeler::<S, T>(connection, rx_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"),
@ -60,7 +60,7 @@ 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); let mut tool = T::new(client, tx_sender, config);
tool.run(rx_recever); tool.run(rx_recever);
println!("WARN : rool_runner: tool has ended"); println!("WARN : rool_runner: tool has ended");
}); });
@ -94,7 +94,7 @@ pub mod mqtt_client {
} }
} }
pub(self) fn handeler<T: MqttTool>(mut connection: Connection, rx: Sender<MqttMessage>) { pub(self) fn handeler<S: std::marker::Send,T: MqttTool<S>>(mut connection: Connection, rx: Sender<MqttMessage>) {
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 {