diff --git a/src/asyncv/attribute/message/boolean/attribute.rs b/src/asyncv/attribute/message/boolean/attribute.rs index cd2d409..c5683b3 100644 --- a/src/asyncv/attribute/message/boolean/attribute.rs +++ b/src/asyncv/attribute/message/boolean/attribute.rs @@ -35,14 +35,21 @@ impl AttributeBoolean { } } - pub fn with_boolean_message_handler( - mut self, + pub async fn with_boolean_message_handler( + self, handler: Arc>, ) -> Self { - // let Box Result { + self.inner.lock().await.init(self.inner.clone()).await?; + Ok(self) + } + // pub async fn on_change_handler(&self, handler: Box) { // self.inner.lock().await.on_change_handler(handler); // } diff --git a/src/asyncv/attribute/message/boolean/attribute/inner.rs b/src/asyncv/attribute/message/boolean/attribute/inner.rs index bf7f773..034540d 100644 --- a/src/asyncv/attribute/message/boolean/attribute/inner.rs +++ b/src/asyncv/attribute/message/boolean/attribute/inner.rs @@ -70,6 +70,15 @@ impl InnerBoolean { // } // } + /// Initialize the attribute + /// + pub async fn init( + &self, + attribute: Arc>, + ) -> Result<(), AttributeError> { + self.core.init(attribute).await + } + fn set_ensure_ok(&self) -> bool { return self.requested_value == self.value; } @@ -127,18 +136,15 @@ impl OnMessageHandler for InnerBoolean { // OnChangeHandlerFunction - // if let Some(handler) = self.on_change_handler.as_ref() { - // tokio::spawn(*(handler.clone())); - // } if data.len() == 1 { match data[0] { b'1' => { self.value = Some(true); - self.set_ensure_update(); + // self.set_ensure_update(); } b'0' => { self.value = Some(false); - self.set_ensure_update(); + // self.set_ensure_update(); } _ => { println!("unexcpedted payload {:?}", data); @@ -149,5 +155,13 @@ impl OnMessageHandler for InnerBoolean { } else { println!("wierd payload {:?}", data); } + + if let Some(handler) = self.on_change_handler.as_ref() { + let h = handler.clone(); + h.lock() + .await + .on_message_boolean(self.id, self.value.unwrap()) + .await; + } } } diff --git a/src/asyncv/attribute/message/boolean/builder.rs b/src/asyncv/attribute/message/boolean/builder.rs index 5f069ce..4638549 100644 --- a/src/asyncv/attribute/message/boolean/builder.rs +++ b/src/asyncv/attribute/message/boolean/builder.rs @@ -3,6 +3,7 @@ use std::sync::Weak; use tokio::sync::Mutex; use crate::asyncv::attribute::message::AttributeId; +use crate::AttributeError; use super::AttributeBoolean; use super::AttributeBuilder; @@ -34,7 +35,7 @@ impl BuilderBoolean { } } - pub fn finish(self) -> AttributeBoolean { - AttributeBoolean::new(self) + pub async fn finish(self) -> Result { + AttributeBoolean::new(self).init().await } } diff --git a/src/asyncv/attribute/message/dispatcher.rs b/src/asyncv/attribute/message/dispatcher.rs index 90c948c..839d155 100644 --- a/src/asyncv/attribute/message/dispatcher.rs +++ b/src/asyncv/attribute/message/dispatcher.rs @@ -34,11 +34,11 @@ impl MessageDispatcher { /// Trigger the on_message of the attribute /// pub async fn trigger_on_change(&self, topic: &str, new_value: &Bytes) { - // println!("{:?}", self.message_attributes.keys()); + println!("{:?}", self.message_attributes.keys()); if let Some(attribute) = self.message_attributes.get(topic) { match attribute.upgrade() { Some(attribute) => { - attribute.lock().await.on_message(new_value); + attribute.lock().await.on_message(new_value).await; } None => { println!("Attribute not found"); diff --git a/src/examples/test_async.rs b/src/examples/test_async.rs index effdd7d..ffbdbbf 100644 --- a/src/examples/test_async.rs +++ b/src/examples/test_async.rs @@ -11,7 +11,9 @@ use tokio::time::Duration; use tokio::sync::Mutex; -struct TestBehaviour {} +struct TestBehaviour { + a1: AttributeBoolean, +} impl TestBehaviour { pub fn to_arc_mutex(self) -> Arc> { @@ -43,12 +45,16 @@ async fn main() { let lll = TestBehaviour {}.to_arc_mutex(); - let pp = reactor + let pp: panduza::asyncv::attribute::message::boolean::AttributeBoolean = reactor .create_new_attribute() .with_topic("test") .with_type_boolean() + // .control_config (exemple pour la suite) .finish() - .with_boolean_message_handler(lll); + .await + .unwrap() + .with_boolean_message_handler(lll) + .await; println!("send data"); pp.set(true).await.unwrap();