Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
XdoctorwhoZ committed Aug 3, 2024
1 parent 349b2a6 commit 00445fa
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
13 changes: 10 additions & 3 deletions src/asyncv/attribute/message/boolean/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,21 @@ impl AttributeBoolean {
}
}

pub fn with_boolean_message_handler(
mut self,
pub async fn with_boolean_message_handler(
self,
handler: Arc<Mutex<dyn OnBooleanMessage>>,
) -> Self {
// let Box<dyn
self.inner.lock().await.on_change_handler(handler);
self
}

/// Initialize the attribute
///
pub async fn init(self) -> Result<Self, AttributeError> {
self.inner.lock().await.init(self.inner.clone()).await?;
Ok(self)
}

// pub async fn on_change_handler(&self, handler: Box<dyn OnChangeHandler>) {
// self.inner.lock().await.on_change_handler(handler);
// }
Expand Down
24 changes: 19 additions & 5 deletions src/asyncv/attribute/message/boolean/attribute/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ impl InnerBoolean {
// }
// }

/// Initialize the attribute
///
pub async fn init(
&self,
attribute: Arc<Mutex<dyn OnMessageHandler>>,
) -> Result<(), AttributeError> {
self.core.init(attribute).await
}

fn set_ensure_ok(&self) -> bool {
return self.requested_value == self.value;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}
}
5 changes: 3 additions & 2 deletions src/asyncv/attribute/message/boolean/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,7 +35,7 @@ impl BuilderBoolean {
}
}

pub fn finish(self) -> AttributeBoolean {
AttributeBoolean::new(self)
pub async fn finish(self) -> Result<AttributeBoolean, AttributeError> {
AttributeBoolean::new(self).init().await
}
}
4 changes: 2 additions & 2 deletions src/asyncv/attribute/message/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
12 changes: 9 additions & 3 deletions src/examples/test_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mutex<TestBehaviour>> {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 00445fa

Please sign in to comment.