From c39661d00e6bc5536b460da7f119473184ccf5b1 Mon Sep 17 00:00:00 2001 From: XdoctorwhoZ Date: Sat, 3 Aug 2024 15:33:55 +0100 Subject: [PATCH] up --- .../message/boolean/attribute/inner.rs | 2 ++ src/asyncv/attribute/message/boolean/builder.rs | 4 ++++ src/asyncv/builder.rs | 17 +++++++++++++---- src/asyncv/reactor/message_engine.rs | 2 +- src/examples/test_async.rs | 13 ++++++++++++- 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/asyncv/attribute/message/boolean/attribute/inner.rs b/src/asyncv/attribute/message/boolean/attribute/inner.rs index 043a8da..aab5e7a 100644 --- a/src/asyncv/attribute/message/boolean/attribute/inner.rs +++ b/src/asyncv/attribute/message/boolean/attribute/inner.rs @@ -8,6 +8,7 @@ use tokio::sync::Mutex; use bytes::Bytes; +use crate::asyncv::attribute::message::AttributeId; use crate::AttributeError; use super::MessageCoreMembers; @@ -42,6 +43,7 @@ impl InnerBoolean { /// pub fn new(builder: BuilderBoolean) -> InnerBoolean { InnerBoolean { + id: builder.id, core: MessageCoreMembers::new( builder.message_client, builder.message_dispatcher, diff --git a/src/asyncv/attribute/message/boolean/builder.rs b/src/asyncv/attribute/message/boolean/builder.rs index bcc7876..5f069ce 100644 --- a/src/asyncv/attribute/message/boolean/builder.rs +++ b/src/asyncv/attribute/message/boolean/builder.rs @@ -2,6 +2,8 @@ use std::sync::Weak; use tokio::sync::Mutex; +use crate::asyncv::attribute::message::AttributeId; + use super::AttributeBoolean; use super::AttributeBuilder; @@ -9,6 +11,7 @@ pub use super::MessageClient; pub use super::MessageDispatcher; pub struct BuilderBoolean { + pub id: AttributeId, /// The mqtt client pub message_client: MessageClient, @@ -24,6 +27,7 @@ impl BuilderBoolean { /// New boolean builder pub fn new(parent_builder: AttributeBuilder) -> BuilderBoolean { BuilderBoolean { + id: parent_builder.id, message_client: parent_builder.message_client, message_dispatcher: parent_builder.message_dispatcher, topic: parent_builder.topic, diff --git a/src/asyncv/builder.rs b/src/asyncv/builder.rs index 8beeb2e..9192a40 100644 --- a/src/asyncv/builder.rs +++ b/src/asyncv/builder.rs @@ -3,12 +3,16 @@ use std::sync::Weak; use tokio::sync::Mutex; use super::attribute::message::boolean::BuilderBoolean; +use super::attribute::message::AttributeId; pub use super::MessageClient; pub use super::MessageDispatcher; +static mut ID_POOL: AttributeId = 0; + /// Object that allow to build an generic attribute /// pub struct AttributeBuilder { + pub id: AttributeId, /// The mqtt client pub message_client: MessageClient, @@ -26,10 +30,15 @@ impl AttributeBuilder { message_client: MessageClient, message_dispatcher: Weak>, ) -> AttributeBuilder { - AttributeBuilder { - message_client, - message_dispatcher, - topic: None, + unsafe { + let id = ID_POOL; + ID_POOL += 1; + AttributeBuilder { + id: id, + message_client, + message_dispatcher, + topic: None, + } } } diff --git a/src/asyncv/reactor/message_engine.rs b/src/asyncv/reactor/message_engine.rs index 97e5233..19c3101 100644 --- a/src/asyncv/reactor/message_engine.rs +++ b/src/asyncv/reactor/message_engine.rs @@ -24,7 +24,7 @@ impl MessageEngine { pub async fn run(&mut self) { while let Ok(event) = self.message_event_loop.poll().await { - // println!("Notification = {:?}", notification); + println!("Notification = {:?}", event); // match notification { // Ok(event) => { match event { diff --git a/src/examples/test_async.rs b/src/examples/test_async.rs index e20b978..01d0f82 100644 --- a/src/examples/test_async.rs +++ b/src/examples/test_async.rs @@ -1,8 +1,18 @@ +use async_trait::async_trait; +use panduza::asyncv::attribute::message::AttributeId; +use panduza::asyncv::attribute::message::OnMessageBoolean; use panduza::asyncv::Reactor; use panduza::ReactorSettings; use tokio::time::sleep; use tokio::time::Duration; +struct TestBehaviour {} + +#[async_trait] +impl OnMessageBoolean for TestBehaviour { + async fn on_message_boolean(&mut self, id: AttributeId, data: bool) {} +} + #[tokio::main] async fn main() { let settings = ReactorSettings::new("localhost", 1883); @@ -24,7 +34,8 @@ async fn main() { .with_type_boolean() .finish(); - // pp.set(true).await.unwrap(); + println!("send data"); + pp.set(true).await.unwrap(); sleep(Duration::from_secs(60)).await; }