diff --git a/src/asyncv/attribute/message/boolean/attribute.rs b/src/asyncv/attribute/message/boolean/attribute.rs index c5683b3..c92aa9c 100644 --- a/src/asyncv/attribute/message/boolean/attribute.rs +++ b/src/asyncv/attribute/message/boolean/attribute.rs @@ -35,14 +35,6 @@ impl AttributeBoolean { } } - pub async fn with_boolean_message_handler( - self, - handler: Arc>, - ) -> Self { - self.inner.lock().await.on_change_handler(handler); - self - } - /// Initialize the attribute /// pub async fn init(self) -> Result { diff --git a/src/asyncv/attribute/message/boolean/attribute/inner.rs b/src/asyncv/attribute/message/boolean/attribute/inner.rs index 034540d..3f6c476 100644 --- a/src/asyncv/attribute/message/boolean/attribute/inner.rs +++ b/src/asyncv/attribute/message/boolean/attribute/inner.rs @@ -10,8 +10,6 @@ use bytes::Bytes; use async_trait::async_trait; -use crate::asyncv::attribute::message::AttributeId; -use crate::asyncv::attribute::message::OnBooleanMessage; use crate::AttributeError; use super::MessageCoreMembers; @@ -23,25 +21,20 @@ use monitor::Monitor; /// Inner implementation of the boolean message attribute /// -pub struct InnerBoolean { - id: AttributeId, +pub struct AttributeInner> + From> + PartialEq + Copy> { /// Members at the core of each attribute core: MessageCoreMembers, /// Current value of the attribute - value: Option, + value: Option, /// Requested value of the attribute (set by the user) - requested_value: Option, + requested_value: Option, // / Handler to call when the value change - // on_change_handler: Option>, - on_change_handler: Option>>, - // set_ensure_lock: Arc>, } -impl InnerBoolean { +impl> + From> + PartialEq + Copy> AttributeInner { /// - pub fn new(builder: BuilderBoolean) -> InnerBoolean { - InnerBoolean { - id: builder.id, + pub fn new(builder: BuilderBoolean) -> AttributeInner { + AttributeInner { core: MessageCoreMembers::new( builder.message_client, builder.message_dispatcher, @@ -49,7 +42,6 @@ impl InnerBoolean { ), value: None, requested_value: None, - on_change_handler: None, // set_ensure_lock: None, } } @@ -79,13 +71,13 @@ impl InnerBoolean { self.core.init(attribute).await } - fn set_ensure_ok(&self) -> bool { - return self.requested_value == self.value; - } + // fn set_ensure_ok(&self) -> bool { + // return self.requested_value == self.value; + // } /// Set the value of the attribute /// - pub async fn set(&mut self, new_value: bool) -> Result<(), AttributeError> { + pub async fn set(&mut self, new_value: TYPE) -> Result<(), AttributeError> { // Do not go further if the value is already set if let Some(current_value) = self.value { if current_value == new_value { @@ -96,10 +88,10 @@ impl InnerBoolean { // Set the requested value and publish the request self.requested_value = Some(new_value); match self.requested_value { - Some(requested_value) => match requested_value { - true => self.core.publish("1").await, - false => self.core.publish("0").await, - }, + Some(requested_value) => { + self.core.publish(requested_value.into()).await; + Ok(()) + } None => Err(AttributeError::Unkonwn), } } @@ -107,14 +99,10 @@ impl InnerBoolean { /// Get the value of the attribute /// If None, the first value is not yet received /// - pub fn get(&self) -> Option { + pub fn get(&self) -> Option { return self.value; } - pub fn on_change_handler(&mut self, handler: Arc>) { - self.on_change_handler = Some(handler); - } - // pub fn on_change(&mut self, handler: OnChangeHandlerFunction) { // self.on_change_handler = Some(handler); // } @@ -130,7 +118,7 @@ impl InnerBoolean { } #[async_trait] -impl OnMessageHandler for InnerBoolean { +impl> + From> + PartialEq + Copy> OnMessageHandler for AttributeInner { async fn on_message(&mut self, data: &Bytes) { println!("boolean"); @@ -155,13 +143,5 @@ 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/examples/test_async.rs b/src/examples/test_async.rs index ffbdbbf..7241964 100644 --- a/src/examples/test_async.rs +++ b/src/examples/test_async.rs @@ -11,23 +11,6 @@ use tokio::time::Duration; use tokio::sync::Mutex; -struct TestBehaviour { - a1: AttributeBoolean, -} - -impl TestBehaviour { - pub fn to_arc_mutex(self) -> Arc> { - Arc::new(Mutex::new(self)) - } -} - -#[async_trait] -impl OnBooleanMessage for TestBehaviour { - async fn on_message_boolean(&mut self, id: AttributeId, data: bool) { - println!("on bool {}{}", id, data) - } -} - #[tokio::main] async fn main() { let settings = ReactorSettings::new("localhost", 1883); @@ -43,8 +26,6 @@ async fn main() { // reactor.scan_platforms(); - let lll = TestBehaviour {}.to_arc_mutex(); - let pp: panduza::asyncv::attribute::message::boolean::AttributeBoolean = reactor .create_new_attribute() .with_topic("test") @@ -52,9 +33,7 @@ async fn main() { // .control_config (exemple pour la suite) .finish() .await - .unwrap() - .with_boolean_message_handler(lll) - .await; + .unwrap(); println!("send data"); pp.set(true).await.unwrap();