From 3b339444281ce385f1b9efd43f1f618cfe11d824 Mon Sep 17 00:00:00 2001 From: XdoctorwhoZ Date: Sat, 3 Aug 2024 21:38:15 +0100 Subject: [PATCH] up --- src/asyncv/attribute/message/boolean.rs | 4 -- .../attribute/message/boolean/attribute.rs | 28 +++++----- .../message/boolean/attribute/inner.rs | 52 ++++++++++--------- .../attribute/message/boolean/builder.rs | 41 --------------- src/asyncv/builder.rs | 7 +-- src/examples/test_async.rs | 31 +++++++++-- 6 files changed, 73 insertions(+), 90 deletions(-) delete mode 100644 src/asyncv/attribute/message/boolean/builder.rs diff --git a/src/asyncv/attribute/message/boolean.rs b/src/asyncv/attribute/message/boolean.rs index b60d940..90905f8 100644 --- a/src/asyncv/attribute/message/boolean.rs +++ b/src/asyncv/attribute/message/boolean.rs @@ -1,8 +1,4 @@ pub mod attribute; -pub mod builder; - -pub use attribute::AttributeBoolean; -pub use builder::BuilderBoolean; pub use super::AttributeBuilder; pub use super::MessageClient; diff --git a/src/asyncv/attribute/message/boolean/attribute.rs b/src/asyncv/attribute/message/boolean/attribute.rs index c92aa9c..1a5b9ad 100644 --- a/src/asyncv/attribute/message/boolean/attribute.rs +++ b/src/asyncv/attribute/message/boolean/attribute.rs @@ -1,5 +1,5 @@ mod inner; -use inner::InnerBoolean; +use inner::AttributeInner; use std::future::Future; use std::sync::Arc; @@ -9,8 +9,7 @@ use crate::asyncv::attribute::message::OnBooleanMessage; use crate::AttributeError; use async_trait::async_trait; -pub use super::BuilderBoolean; -use super::MessageCoreMembers; +use super::{AttributeBuilder, MessageCoreMembers}; pub use super::OnMessageHandler; @@ -21,17 +20,22 @@ pub use super::OnMessageHandler; // pub use inner_msg_att_bool::OnChangeHandlerFunction; +pub trait AttributePayloadManager: + Into> + From> + PartialEq + Copy + Sync + Send + 'static +{ +} + /// Attribute to manage a boolean -pub struct AttributeBoolean { +pub struct Attribute { /// - inner: Arc>, + inner: Arc>>, } -impl AttributeBoolean { +impl Attribute { /// - pub fn new(builder: BuilderBoolean) -> AttributeBoolean { - AttributeBoolean { - inner: InnerBoolean::new(builder).to_arc_mutex(), + pub fn new(builder: AttributeBuilder) -> Attribute { + Attribute { + inner: AttributeInner::new(builder).to_arc_mutex(), } } @@ -51,8 +55,8 @@ impl AttributeBoolean { /// Set the value of the attribute /// - pub async fn set(&self, value: bool) -> Result<(), AttributeError> { - self.inner.lock().await.set(value).await?; + pub async fn set>(&self, value: INTO_TYPE) -> 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| { // while !*done { @@ -64,7 +68,7 @@ impl AttributeBoolean { /// Get the value of the attribute /// - pub async fn get(&self) -> Option { + pub async fn get(&self) -> Option { self.inner.lock().await.get() } } diff --git a/src/asyncv/attribute/message/boolean/attribute/inner.rs b/src/asyncv/attribute/message/boolean/attribute/inner.rs index 3f6c476..0bb92e5 100644 --- a/src/asyncv/attribute/message/boolean/attribute/inner.rs +++ b/src/asyncv/attribute/message/boolean/attribute/inner.rs @@ -10,18 +10,20 @@ use bytes::Bytes; use async_trait::async_trait; +use crate::asyncv::AttributeBuilder; use crate::AttributeError; use super::MessageCoreMembers; // use super::OnChangeHandler; use super::OnMessageHandler; -pub use super::BuilderBoolean; -use monitor::Monitor; +use tokio::sync::Notify; + +use super::AttributePayloadManager; /// Inner implementation of the boolean message attribute /// -pub struct AttributeInner> + From> + PartialEq + Copy> { +pub struct AttributeInner { /// Members at the core of each attribute core: MessageCoreMembers, /// Current value of the attribute @@ -31,9 +33,9 @@ pub struct AttributeInner> + From> + PartialEq + Copy // / Handler to call when the value change } -impl> + From> + PartialEq + Copy> AttributeInner { +impl AttributeInner { /// - pub fn new(builder: BuilderBoolean) -> AttributeInner { + pub fn new(builder: AttributeBuilder) -> AttributeInner { AttributeInner { core: MessageCoreMembers::new( builder.message_client, @@ -118,30 +120,30 @@ impl> + From> + PartialEq + Copy> AttributeInner> + From> + PartialEq + Copy> OnMessageHandler for AttributeInner { +impl OnMessageHandler for AttributeInner { async fn on_message(&mut self, data: &Bytes) { println!("boolean"); // OnChangeHandlerFunction - if data.len() == 1 { - match data[0] { - b'1' => { - self.value = Some(true); - // self.set_ensure_update(); - } - b'0' => { - self.value = Some(false); - // self.set_ensure_update(); - } - _ => { - println!("unexcpedted payload {:?}", data); - return; - } - }; - // Do something with the value - } else { - println!("wierd payload {:?}", data); - } + // if data.len() == 1 { + // match data[0] { + // b'1' => { + // self.value = Some(true); + // // self.set_ensure_update(); + // } + // b'0' => { + // self.value = Some(false); + // // self.set_ensure_update(); + // } + // _ => { + // println!("unexcpedted payload {:?}", data); + // return; + // } + // }; + // // Do something with the value + // } else { + // println!("wierd payload {:?}", data); + // } } } diff --git a/src/asyncv/attribute/message/boolean/builder.rs b/src/asyncv/attribute/message/boolean/builder.rs deleted file mode 100644 index 4638549..0000000 --- a/src/asyncv/attribute/message/boolean/builder.rs +++ /dev/null @@ -1,41 +0,0 @@ -use std::sync::Weak; - -use tokio::sync::Mutex; - -use crate::asyncv::attribute::message::AttributeId; -use crate::AttributeError; - -use super::AttributeBoolean; -use super::AttributeBuilder; - -pub use super::MessageClient; -pub use super::MessageDispatcher; - -pub struct BuilderBoolean { - pub id: AttributeId, - /// The mqtt client - pub message_client: MessageClient, - - /// The Object that allow the reactor to dispatch - /// incoming messages on attributes - pub message_dispatcher: Weak>, - - /// Topic of the attribute - pub topic: Option, -} - -impl BuilderBoolean { - /// New boolean builder - pub fn new(parent_builder: AttributeBuilder) -> BuilderBoolean { - BuilderBoolean { - id: parent_builder.id, - message_client: parent_builder.message_client, - message_dispatcher: parent_builder.message_dispatcher, - topic: parent_builder.topic, - } - } - - pub async fn finish(self) -> Result { - AttributeBoolean::new(self).init().await - } -} diff --git a/src/asyncv/builder.rs b/src/asyncv/builder.rs index 9192a40..4c6652b 100644 --- a/src/asyncv/builder.rs +++ b/src/asyncv/builder.rs @@ -2,7 +2,8 @@ use std::sync::Weak; use tokio::sync::Mutex; -use super::attribute::message::boolean::BuilderBoolean; +use super::attribute::message::boolean::attribute::Attribute; +use super::attribute::message::boolean::attribute::AttributePayloadManager; use super::attribute::message::AttributeId; pub use super::MessageClient; pub use super::MessageDispatcher; @@ -48,7 +49,7 @@ impl AttributeBuilder { self } - pub fn with_type_boolean(self) -> BuilderBoolean { - BuilderBoolean::new(self) + pub fn build_with_payload_type(self) -> Attribute { + Attribute::new(self) } } diff --git a/src/examples/test_async.rs b/src/examples/test_async.rs index 7241964..c1bd0d1 100644 --- a/src/examples/test_async.rs +++ b/src/examples/test_async.rs @@ -2,6 +2,7 @@ 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::Reactor; @@ -11,6 +12,29 @@ use tokio::time::Duration; use tokio::sync::Mutex; +#[derive(Copy, Clone, PartialEq)] +struct BooleanPayload { + value: bool, +} + +impl Into for bool { + fn into(self) -> BooleanPayload { + todo!() + } +} + +impl From> for BooleanPayload { + fn from(value: Vec) -> Self { + todo!() + } +} +impl Into> for BooleanPayload { + fn into(self) -> Vec { + todo!() + } +} +impl AttributePayloadManager for BooleanPayload {} + #[tokio::main] async fn main() { let settings = ReactorSettings::new("localhost", 1883); @@ -26,14 +50,11 @@ async fn main() { // reactor.scan_platforms(); - let pp: panduza::asyncv::attribute::message::boolean::AttributeBoolean = reactor + let pp = reactor .create_new_attribute() .with_topic("test") - .with_type_boolean() // .control_config (exemple pour la suite) - .finish() - .await - .unwrap(); + .build_with_payload_type::(); println!("send data"); pp.set(true).await.unwrap();