Instead of using pollster we use tokio::spawn to spawn a seperate task for subscribing and publishing to mqtt

This commit is contained in:
Dreaded_X 2022-12-25 01:29:30 +01:00
parent 1299443a7c
commit 6c4ae9ec35
3 changed files with 11 additions and 6 deletions

1
Cargo.lock generated
View File

@ -33,7 +33,6 @@ dependencies = [
"impl_cast",
"log",
"paste",
"pollster",
"rumqttc",
"serde",
"serde_json",

View File

@ -16,7 +16,6 @@ tokio = { version = "1", features = ["full"] }
warp = "0.3"
log = "0.4"
env_logger = "0.10"
pollster = "0.2.5"
toml = "0.5.10"
dotenv = "0.15.0"

View File

@ -1,5 +1,3 @@
use pollster::FutureExt as _;
use google_home::errors::ErrorCode;
use google_home::{GoogleHomeDevice, device, types::Type, traits};
use rumqttc::{AsyncClient, Publish};
@ -19,7 +17,13 @@ pub struct IkeaOutlet {
impl IkeaOutlet {
pub fn new(name: String, zigbee: Zigbee, client: AsyncClient) -> Self {
client.subscribe(zigbee.get_topic(), rumqttc::QoS::AtLeastOnce).block_on().unwrap();
let c = client.clone();
let topic = zigbee.get_topic().to_owned();
// @TODO Handle potential errors here
tokio::spawn(async move {
c.subscribe(topic, rumqttc::QoS::AtLeastOnce).await.unwrap();
});
Self{ name, zigbee, client, last_known_state: false }
}
}
@ -97,7 +101,10 @@ impl traits::OnOff for IkeaOutlet {
// @TODO Handle potential errors here
// @NOTE We are blocking here, ideally this function would just be async, however that is
// currently not really possible
self.client.publish(topic + "/set", rumqttc::QoS::AtLeastOnce, false, serde_json::to_string(&message).unwrap()).block_on().unwrap();
let client = self.client.clone();
tokio::spawn(async move {
client.publish(topic + "/set", rumqttc::QoS::AtLeastOnce, false, serde_json::to_string(&message).unwrap()).await.unwrap();
});
Ok(())
}