diff --git a/src/asyncv/attribute/message.rs b/src/asyncv/attribute/message.rs index 1e52a89..05a3b77 100644 --- a/src/asyncv/attribute/message.rs +++ b/src/asyncv/attribute/message.rs @@ -1,27 +1,18 @@ -pub mod boolean; -mod core_members; +pub mod attribute; mod dispatcher; use async_trait::async_trait; use bytes::Bytes; -pub use core_members::MessageCoreMembers; pub use dispatcher::MessageDispatcher; pub use super::AttributeBuilder; pub type MessageClient = rumqttc::AsyncClient; -pub type AttributeId = u32; - /// Trait to manage an message attribute (MQTT) /// Sync version #[async_trait] pub trait OnMessageHandler: Send + Sync { async fn on_message(&mut self, data: &Bytes); } - -#[async_trait] -pub trait OnBooleanMessage: Send + Sync { - async fn on_message_boolean(&mut self, id: AttributeId, data: bool); -} diff --git a/src/asyncv/attribute/message/boolean/attribute.rs b/src/asyncv/attribute/message/attribute.rs similarity index 76% rename from src/asyncv/attribute/message/boolean/attribute.rs rename to src/asyncv/attribute/message/attribute.rs index 1a5b9ad..6d691b9 100644 --- a/src/asyncv/attribute/message/boolean/attribute.rs +++ b/src/asyncv/attribute/message/attribute.rs @@ -1,15 +1,16 @@ mod inner; use inner::AttributeInner; +use tokio::time::sleep; use std::future::Future; use std::sync::Arc; +use std::time::Duration; use tokio::sync::Mutex; -use crate::asyncv::attribute::message::OnBooleanMessage; +pub use super::MessageClient; use crate::AttributeError; -use async_trait::async_trait; -use super::{AttributeBuilder, MessageCoreMembers}; +use super::AttributeBuilder; pub use super::OnMessageHandler; @@ -46,6 +47,20 @@ impl Attribute { Ok(self) } + // wait_change() + // wait_change_then() + + pub fn when_change(&self, function: F) + where + F: Future + Send + 'static, + // F: Future> + Send + 'static, + { + tokio::spawn(async move { + sleep(Duration::from_secs(1)).await; + function.await + }); + } + // pub async fn on_change_handler(&self, handler: Box) { // self.inner.lock().await.on_change_handler(handler); // } @@ -55,7 +70,7 @@ impl Attribute { /// Set the value of the attribute /// - pub async fn set>(&self, value: INTO_TYPE) -> Result<(), AttributeError> { + pub async fn set>(&self, value: I) -> Result<(), AttributeError> { self.inner.lock().await.set(value.into()).await?; // let cv = self.inner.lock().await.set_ensure_lock_clone(); // cv.with_lock(|mut done| { diff --git a/src/asyncv/attribute/message/boolean/attribute/inner.rs b/src/asyncv/attribute/message/attribute/inner.rs similarity index 96% rename from src/asyncv/attribute/message/boolean/attribute/inner.rs rename to src/asyncv/attribute/message/attribute/inner.rs index b3e433a..70f89cb 100644 --- a/src/asyncv/attribute/message/boolean/attribute/inner.rs +++ b/src/asyncv/attribute/message/attribute/inner.rs @@ -3,27 +3,18 @@ use std::sync::Arc; use std::sync::Weak; use tokio::sync::Mutex; +use crate::asyncv::MessageDispatcher; use crate::AttributeError; use super::MessageClient; use super::OnMessageHandler; -use std::future::Future; -use std::pin::Pin; - -use futures::future::BoxFuture; -use futures::FutureExt; - use bytes::Bytes; use async_trait::async_trait; use crate::asyncv::AttributeBuilder; -use super::MessageCoreMembers; -// use super::OnChangeHandler; -use super::OnMessageHandler; - use tokio::sync::Notify; use super::AttributePayloadManager; diff --git a/src/asyncv/attribute/message/boolean.rs b/src/asyncv/attribute/message/boolean.rs deleted file mode 100644 index 90905f8..0000000 --- a/src/asyncv/attribute/message/boolean.rs +++ /dev/null @@ -1,8 +0,0 @@ -pub mod attribute; - -pub use super::AttributeBuilder; -pub use super::MessageClient; -pub use super::MessageDispatcher; - -pub use super::MessageCoreMembers; -pub use super::OnMessageHandler; diff --git a/src/asyncv/builder.rs b/src/asyncv/builder.rs index 4c6652b..c5114e4 100644 --- a/src/asyncv/builder.rs +++ b/src/asyncv/builder.rs @@ -2,18 +2,14 @@ use std::sync::Weak; use tokio::sync::Mutex; -use super::attribute::message::boolean::attribute::Attribute; -use super::attribute::message::boolean::attribute::AttributePayloadManager; -use super::attribute::message::AttributeId; +use super::attribute::message::attribute::Attribute; +use super::attribute::message::attribute::AttributePayloadManager; 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, @@ -31,15 +27,10 @@ impl AttributeBuilder { message_client: MessageClient, message_dispatcher: Weak>, ) -> AttributeBuilder { - unsafe { - let id = ID_POOL; - ID_POOL += 1; - AttributeBuilder { - id: id, - message_client, - message_dispatcher, - topic: None, - } + AttributeBuilder { + message_client, + message_dispatcher, + topic: None, } } diff --git a/src/examples/test_async.rs b/src/examples/test_async.rs index c1bd0d1..ee8874c 100644 --- a/src/examples/test_async.rs +++ b/src/examples/test_async.rs @@ -2,9 +2,8 @@ use std::sync::Arc; use async_trait::async_trait; -use panduza::asyncv::attribute::message::boolean::attribute::AttributePayloadManager; -use panduza::asyncv::attribute::message::AttributeId; -use panduza::asyncv::attribute::message::OnBooleanMessage; +use panduza::asyncv::attribute::message::attribute::AttributePayloadManager; + use panduza::asyncv::Reactor; use panduza::ReactorSettings; use tokio::time::sleep; @@ -19,18 +18,18 @@ struct BooleanPayload { impl Into for bool { fn into(self) -> BooleanPayload { - todo!() + return BooleanPayload { value: true }; } } impl From> for BooleanPayload { fn from(value: Vec) -> Self { - todo!() + return BooleanPayload { value: true }; } } impl Into> for BooleanPayload { fn into(self) -> Vec { - todo!() + return vec![1]; } } impl AttributePayloadManager for BooleanPayload {} @@ -59,5 +58,9 @@ async fn main() { println!("send data"); pp.set(true).await.unwrap(); + pp.when_change(async move { + println!("cooucou"); + }); + sleep(Duration::from_secs(60)).await; }