From bd183ee96d98dd1c4ab7175e17752e0abacd1537 Mon Sep 17 00:00:00 2001 From: XdoctorwhoZ Date: Sun, 4 Aug 2024 07:11:18 +0100 Subject: [PATCH] new skeleton ok --- src/asyncv/attribute.rs | 2 + src/asyncv/attribute/message.rs | 6 +- src/asyncv/attribute/message/attribute.rs | 9 +-- .../attribute/message/attribute/inner.rs | 12 ++-- src/asyncv/builder.rs | 18 ++++-- src/asyncv/msg.rs | 12 ---- src/asyncv/msg/att.rs | 60 ------------------- src/asyncv/msg/att/inner_msg_att.rs | 56 ----------------- src/common.rs | 3 + src/common/message.rs | 3 + src/common/message/boolean.rs | 24 ++++++++ src/examples/test_async.rs | 45 ++------------ src/lib.rs | 8 +++ 13 files changed, 71 insertions(+), 187 deletions(-) delete mode 100644 src/asyncv/msg.rs delete mode 100644 src/asyncv/msg/att.rs delete mode 100644 src/asyncv/msg/att/inner_msg_att.rs create mode 100644 src/common/message.rs create mode 100644 src/common/message/boolean.rs diff --git a/src/asyncv/attribute.rs b/src/asyncv/attribute.rs index 4be1cbe..49f43c2 100644 --- a/src/asyncv/attribute.rs +++ b/src/asyncv/attribute.rs @@ -1,5 +1,7 @@ pub mod message; +pub use message::MessageAttribute; + pub use super::AttributeBuilder; pub use super::MessageClient; pub use super::MessageDispatcher; diff --git a/src/asyncv/attribute/message.rs b/src/asyncv/attribute/message.rs index 05a3b77..cc1e45b 100644 --- a/src/asyncv/attribute/message.rs +++ b/src/asyncv/attribute/message.rs @@ -1,14 +1,16 @@ -pub mod attribute; +mod attribute; mod dispatcher; use async_trait::async_trait; use bytes::Bytes; +pub type MessageAttribute = attribute::Attribute; + pub use dispatcher::MessageDispatcher; pub use super::AttributeBuilder; -pub type MessageClient = rumqttc::AsyncClient; +pub use super::MessageClient; /// Trait to manage an message attribute (MQTT) /// Sync version diff --git a/src/asyncv/attribute/message/attribute.rs b/src/asyncv/attribute/message/attribute.rs index 6d691b9..5854a88 100644 --- a/src/asyncv/attribute/message/attribute.rs +++ b/src/asyncv/attribute/message/attribute.rs @@ -9,10 +9,11 @@ use tokio::sync::Mutex; pub use super::MessageClient; use crate::AttributeError; +use crate::AttributePayloadManager; use super::AttributeBuilder; -pub use super::OnMessageHandler; +use super::OnMessageHandler; // use super::att::Att; // pub use super::CoreMembers; @@ -21,12 +22,8 @@ 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 +#[derive(Clone)] pub struct Attribute { /// inner: Arc>>, diff --git a/src/asyncv/attribute/message/attribute/inner.rs b/src/asyncv/attribute/message/attribute/inner.rs index 70f89cb..91eea2c 100644 --- a/src/asyncv/attribute/message/attribute/inner.rs +++ b/src/asyncv/attribute/message/attribute/inner.rs @@ -19,19 +19,18 @@ use tokio::sync::Notify; use super::AttributePayloadManager; -/// Inner implementation of the boolean message attribute +/// Inner implementation of the message attribute +/// This inner implementation allow the public part to be cloneable easly /// pub struct AttributeInner { - /// The data of the reactor, to be able to subscribe to the - /// reactor and route messages to the attribute + /// Reactor message dispatcher + /// (to attach this attribute to the incoming messages) message_dispatcher: Weak>, - - /// The mqtt client + /// The message client (MQTT) message_client: MessageClient, /// The topic of the attribute topic: String, - /// The topic for commands topic_cmd: String, @@ -39,7 +38,6 @@ pub struct AttributeInner { value: Option, /// Requested value of the attribute (set by the user) requested_value: Option, - // / Handler to call when the value change } impl AttributeInner { diff --git a/src/asyncv/builder.rs b/src/asyncv/builder.rs index c5114e4..50475f0 100644 --- a/src/asyncv/builder.rs +++ b/src/asyncv/builder.rs @@ -2,10 +2,11 @@ use std::sync::Weak; use tokio::sync::Mutex; -use super::attribute::message::attribute::Attribute; -use super::attribute::message::attribute::AttributePayloadManager; +use super::attribute::MessageAttribute; + pub use super::MessageClient; pub use super::MessageDispatcher; +use crate::AttributePayloadManager; /// Object that allow to build an generic attribute /// @@ -19,6 +20,9 @@ pub struct AttributeBuilder { /// Topic of the attribute pub topic: Option, + + /// True if the attribute is readonly + pub is_read_only: bool, } impl AttributeBuilder { @@ -31,6 +35,7 @@ impl AttributeBuilder { message_client, message_dispatcher, topic: None, + is_read_only: true, } } @@ -40,7 +45,12 @@ impl AttributeBuilder { self } - pub fn build_with_payload_type(self) -> Attribute { - Attribute::new(self) + pub fn as_read_write(mut self) -> Self { + self.is_read_only = false; + self + } + + pub fn build_with_message_type(self) -> MessageAttribute { + MessageAttribute::new(self) } } diff --git a/src/asyncv/msg.rs b/src/asyncv/msg.rs deleted file mode 100644 index 012220d..0000000 --- a/src/asyncv/msg.rs +++ /dev/null @@ -1,12 +0,0 @@ -use bytes::Bytes; - -/// Members shared by all attributes -mod core_members; -pub type CoreMembers = core_members::CoreMembers; - -/// -pub mod att; -pub mod attribute_boolean; -pub use attribute_boolean::AttributeBoolean; - -pub use super::ReactorData; diff --git a/src/asyncv/msg/att.rs b/src/asyncv/msg/att.rs deleted file mode 100644 index 4f57507..0000000 --- a/src/asyncv/msg/att.rs +++ /dev/null @@ -1,60 +0,0 @@ -mod inner_msg_att; -pub type InnerAtt = inner_msg_att::InnerAtt; - -use rumqttc::AsyncClient; -use std::sync::Arc; -use tokio::sync::Mutex; - -use crate::AttributeError; - -use super::att_bool::AttBool; -pub use super::CoreMembers; -pub use super::OnMessageHandler; -pub use super::ReactorData; - -/// Generic Message Attribute -pub struct Att { - /// Attribute are mainly a wrapper for the inner manager - inner: Arc>, -} - -impl Att { - /// Create a new Message Attribute - pub fn new( - reactor_data: Arc>, - topic: String, - mqtt_client: AsyncClient, - ) -> Self { - // Create a new inner manager - let inner = InnerAtt::new( - Arc::downgrade(&reactor_data), - topic.clone(), - mqtt_client.clone(), - ) - .into_arc_mutex(); - - Self { inner: inner } - } - - /// Initialize the attribute - /// - pub async fn init(self) -> Result { - self.inner.lock().await.init(self.inner.clone()).await?; - Ok(self) - } - - /// Take the inner core data - /// - pub async fn take_core_members(self) -> Result { - Ok(self.inner.lock().await.clone_core()) - } - - /// Easy conversion to AttBool - /// - pub async fn into_att_bool(self) -> AttBool { - match self.take_core_members().await { - Ok(core_data) => AttBool::from_core_members(core_data).await, - Err(_) => panic!("Error"), - } - } -} diff --git a/src/asyncv/msg/att/inner_msg_att.rs b/src/asyncv/msg/att/inner_msg_att.rs deleted file mode 100644 index 7f96fd1..0000000 --- a/src/asyncv/msg/att/inner_msg_att.rs +++ /dev/null @@ -1,56 +0,0 @@ -pub use super::CoreMembers; -use super::OnMessageHandler; -use super::ReactorData; -use crate::AttributeError; -use bytes::Bytes; -use rumqttc::AsyncClient; -use std::sync::{Arc, Weak}; -use tokio::sync::Mutex; - -/// Inner implementation of the generic message attribute -/// -pub struct InnerAtt { - /// Members at the core of each attribute - core: CoreMembers, -} - -impl InnerAtt { - /// Create a new InnerAtt - /// - pub fn new( - reactor_data: Weak>, - topic: String, - mqtt_client: AsyncClient, - ) -> Self { - Self { - core: CoreMembers::new(reactor_data, topic, mqtt_client), - } - } - - /// Convert the InnerAtt to an Arc> - /// - pub fn into_arc_mutex(self) -> Arc> { - Arc::new(Mutex::new(self)) - } - - /// Initialize the attribute - /// - pub async fn init( - &self, - attribute: Arc>, - ) -> Result<(), AttributeError> { - self.core.init(attribute).await - } - - /// Clone the core members (to mutate into an other type) - /// - pub fn clone_core(&self) -> CoreMembers { - self.core.clone() - } -} - -impl OnMessageHandler for InnerAtt { - fn on_message(&mut self, data: &Bytes) { - println!("generic"); - } -} diff --git a/src/common.rs b/src/common.rs index 41efec5..cdd02ae 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,6 +1,9 @@ mod error; +mod message; mod pza_topic; mod reactor_settings; pub type AttributeError = error::AttributeError; pub type ReactorSettings = reactor_settings::ReactorSettings; + +pub use message::BooleanMessage; diff --git a/src/common/message.rs b/src/common/message.rs new file mode 100644 index 0000000..5e53e42 --- /dev/null +++ b/src/common/message.rs @@ -0,0 +1,3 @@ +mod boolean; + +pub use boolean::BooleanMessage; diff --git a/src/common/message/boolean.rs b/src/common/message/boolean.rs new file mode 100644 index 0000000..209bad9 --- /dev/null +++ b/src/common/message/boolean.rs @@ -0,0 +1,24 @@ +use crate::AttributePayloadManager; + +#[derive(Copy, Clone, PartialEq)] +pub struct BooleanMessage { + value: bool, +} + +impl Into for bool { + fn into(self) -> BooleanMessage { + return BooleanMessage { value: true }; + } +} + +impl From> for BooleanMessage { + fn from(value: Vec) -> Self { + return BooleanMessage { value: true }; + } +} +impl Into> for BooleanMessage { + fn into(self) -> Vec { + return vec![1]; + } +} +impl AttributePayloadManager for BooleanMessage {} diff --git a/src/examples/test_async.rs b/src/examples/test_async.rs index ee8874c..bfee1c9 100644 --- a/src/examples/test_async.rs +++ b/src/examples/test_async.rs @@ -1,39 +1,9 @@ -use std::sync::Arc; - -use async_trait::async_trait; - -use panduza::asyncv::attribute::message::attribute::AttributePayloadManager; - use panduza::asyncv::Reactor; +use panduza::BooleanMessage; use panduza::ReactorSettings; use tokio::time::sleep; use tokio::time::Duration; -use tokio::sync::Mutex; - -#[derive(Copy, Clone, PartialEq)] -struct BooleanPayload { - value: bool, -} - -impl Into for bool { - fn into(self) -> BooleanPayload { - return BooleanPayload { value: true }; - } -} - -impl From> for BooleanPayload { - fn from(value: Vec) -> Self { - return BooleanPayload { value: true }; - } -} -impl Into> for BooleanPayload { - fn into(self) -> Vec { - return vec![1]; - } -} -impl AttributePayloadManager for BooleanPayload {} - #[tokio::main] async fn main() { let settings = ReactorSettings::new("localhost", 1883); @@ -41,25 +11,20 @@ async fn main() { reactor.start(); - // // wait for connection - - // // sleep(time::Duration::from_secs(5)); - - // println!("-----------"); - - // reactor.scan_platforms(); - let pp = reactor .create_new_attribute() .with_topic("test") // .control_config (exemple pour la suite) - .build_with_payload_type::(); + .build_with_message_type::(); println!("send data"); pp.set(true).await.unwrap(); + let pp2 = pp.clone(); pp.when_change(async move { println!("cooucou"); + let _dat = pp2.get().await.unwrap(); + println!("cooucou"); }); sleep(Duration::from_secs(60)).await; diff --git a/src/lib.rs b/src/lib.rs index 9abdb71..2af030b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,3 +14,11 @@ pub type ReactorSettings = common::ReactorSettings; pub trait SyncMessageAttribute: Send + Sync { fn on_message(&self, data: &Bytes); } + +pub use common::BooleanMessage; +/// Trait for type that wan manage an attribute payload +/// +pub trait AttributePayloadManager: + Into> + From> + PartialEq + Copy + Sync + Send + 'static +{ +}