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]]
name = "mqtt-client"
version = "2.0.2"
version = "3.0.0"
dependencies = [
"crossbeam",
"rumqttc",

View File

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

View File

@ -9,8 +9,8 @@ pub mod mqtt_client {
pub use crossbeam::channel::{Receiver, Sender};
pub use rumqttc::QoS;
pub trait MqttTool {
fn new(client: Client, tx: Sender<MqttMessage>) -> Self;
pub trait MqttTool<S: std::marker::Send> {
fn new(client: Client, tx: Sender<MqttMessage>, config: S) -> Self;
fn run(&mut self, rx: Receiver<MqttMessage>);
}
@ -21,7 +21,7 @@ pub mod mqtt_client {
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 (rx_sender, rx_recever) = unbounded::<MqttMessage>();
@ -49,7 +49,7 @@ pub mod mqtt_client {
let mqtt = thread::Builder::new()
.name("mqtt".to_string())
.spawn(move || {
handeler::<T>(connection, rx_sender);
handeler::<S, T>(connection, rx_sender);
});
match mqtt {
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()
.name("tool runner".to_string())
.spawn(move || {
let mut tool = T::new(client, tx_sender);
let mut tool = T::new(client, tx_sender, config);
tool.run(rx_recever);
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() {
let mut delay: bool = false;
match notification {