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 00445fa commit 9a3aa38
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 66 deletions.
8 changes: 0 additions & 8 deletions src/asyncv/attribute/message/boolean/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ impl AttributeBoolean {
}
}

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

/// Initialize the attribute
///
pub async fn init(self) -> Result<Self, AttributeError> {
Expand Down
52 changes: 16 additions & 36 deletions src/asyncv/attribute/message/boolean/attribute/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,33 +21,27 @@ use monitor::Monitor;

/// Inner implementation of the boolean message attribute
///
pub struct InnerBoolean {
id: AttributeId,
pub struct AttributeInner<TYPE: Into<Vec<u8>> + From<Vec<u8>> + PartialEq + Copy> {
/// Members at the core of each attribute
core: MessageCoreMembers,
/// Current value of the attribute
value: Option<bool>,
value: Option<TYPE>,
/// Requested value of the attribute (set by the user)
requested_value: Option<bool>,
requested_value: Option<TYPE>,
// / Handler to call when the value change
// on_change_handler: Option<Box<dyn OnChangeHandler>>,
on_change_handler: Option<Arc<Mutex<dyn OnBooleanMessage>>>,
// set_ensure_lock: Arc<Monitor<bool>>,
}

impl InnerBoolean {
impl<TYPE: Into<Vec<u8>> + From<Vec<u8>> + PartialEq + Copy> AttributeInner<TYPE> {
///
pub fn new(builder: BuilderBoolean) -> InnerBoolean {
InnerBoolean {
id: builder.id,
pub fn new(builder: BuilderBoolean) -> AttributeInner<TYPE> {
AttributeInner {
core: MessageCoreMembers::new(
builder.message_client,
builder.message_dispatcher,
builder.topic.unwrap(),
),
value: None,
requested_value: None,
on_change_handler: None, // set_ensure_lock: None,
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -96,25 +88,21 @@ 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),
}
}

/// Get the value of the attribute
/// If None, the first value is not yet received
///
pub fn get(&self) -> Option<bool> {
pub fn get(&self) -> Option<TYPE> {
return self.value;
}

pub fn on_change_handler(&mut self, handler: Arc<Mutex<dyn OnBooleanMessage>>) {
self.on_change_handler = Some(handler);
}

// pub fn on_change(&mut self, handler: OnChangeHandlerFunction) {
// self.on_change_handler = Some(handler);
// }
Expand All @@ -130,7 +118,7 @@ impl InnerBoolean {
}

#[async_trait]
impl OnMessageHandler for InnerBoolean {
impl<TYPE: Into<Vec<u8>> + From<Vec<u8>> + PartialEq + Copy> OnMessageHandler for AttributeInner {
async fn on_message(&mut self, data: &Bytes) {
println!("boolean");

Expand All @@ -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;
}
}
}
23 changes: 1 addition & 22 deletions src/examples/test_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mutex<TestBehaviour>> {
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);
Expand All @@ -43,18 +26,14 @@ 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")
.with_type_boolean()
// .control_config (exemple pour la suite)
.finish()
.await
.unwrap()
.with_boolean_message_handler(lll)
.await;
.unwrap();

println!("send data");
pp.set(true).await.unwrap();
Expand Down

0 comments on commit 9a3aa38

Please sign in to comment.