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 9a3aa38 commit 3b33944
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 90 deletions.
4 changes: 0 additions & 4 deletions src/asyncv/attribute/message/boolean.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
28 changes: 16 additions & 12 deletions src/asyncv/attribute/message/boolean/attribute.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod inner;
use inner::InnerBoolean;
use inner::AttributeInner;

use std::future::Future;
use std::sync::Arc;
Expand All @@ -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;

Expand All @@ -21,17 +20,22 @@ pub use super::OnMessageHandler;

// pub use inner_msg_att_bool::OnChangeHandlerFunction;

pub trait AttributePayloadManager:
Into<Vec<u8>> + From<Vec<u8>> + PartialEq + Copy + Sync + Send + 'static
{
}

/// Attribute to manage a boolean
pub struct AttributeBoolean {
pub struct Attribute<TYPE: AttributePayloadManager> {
///
inner: Arc<Mutex<InnerBoolean>>,
inner: Arc<Mutex<AttributeInner<TYPE>>>,
}

impl AttributeBoolean {
impl<TYPE: AttributePayloadManager> Attribute<TYPE> {
///
pub fn new(builder: BuilderBoolean) -> AttributeBoolean {
AttributeBoolean {
inner: InnerBoolean::new(builder).to_arc_mutex(),
pub fn new(builder: AttributeBuilder) -> Attribute<TYPE> {
Attribute {
inner: AttributeInner::new(builder).to_arc_mutex(),
}
}

Expand All @@ -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<INTO_TYPE: Into<TYPE>>(&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 {
Expand All @@ -64,7 +68,7 @@ impl AttributeBoolean {

/// Get the value of the attribute
///
pub async fn get(&self) -> Option<bool> {
pub async fn get(&self) -> Option<TYPE> {
self.inner.lock().await.get()
}
}
52 changes: 27 additions & 25 deletions src/asyncv/attribute/message/boolean/attribute/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TYPE: Into<Vec<u8>> + From<Vec<u8>> + PartialEq + Copy> {
pub struct AttributeInner<TYPE: AttributePayloadManager> {
/// Members at the core of each attribute
core: MessageCoreMembers,
/// Current value of the attribute
Expand All @@ -31,9 +33,9 @@ pub struct AttributeInner<TYPE: Into<Vec<u8>> + From<Vec<u8>> + PartialEq + Copy
// / Handler to call when the value change
}

impl<TYPE: Into<Vec<u8>> + From<Vec<u8>> + PartialEq + Copy> AttributeInner<TYPE> {
impl<TYPE: AttributePayloadManager> AttributeInner<TYPE> {
///
pub fn new(builder: BuilderBoolean) -> AttributeInner<TYPE> {
pub fn new(builder: AttributeBuilder) -> AttributeInner<TYPE> {
AttributeInner {
core: MessageCoreMembers::new(
builder.message_client,
Expand Down Expand Up @@ -118,30 +120,30 @@ impl<TYPE: Into<Vec<u8>> + From<Vec<u8>> + PartialEq + Copy> AttributeInner<TYPE
}

#[async_trait]
impl<TYPE: Into<Vec<u8>> + From<Vec<u8>> + PartialEq + Copy> OnMessageHandler for AttributeInner {
impl<TYPE: AttributePayloadManager> OnMessageHandler for AttributeInner<TYPE> {
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);
// }
}
}
41 changes: 0 additions & 41 deletions src/asyncv/attribute/message/boolean/builder.rs

This file was deleted.

7 changes: 4 additions & 3 deletions src/asyncv/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,7 +49,7 @@ impl AttributeBuilder {
self
}

pub fn with_type_boolean(self) -> BuilderBoolean {
BuilderBoolean::new(self)
pub fn build_with_payload_type<TYPE: AttributePayloadManager>(self) -> Attribute<TYPE> {
Attribute::new(self)
}
}
31 changes: 26 additions & 5 deletions src/examples/test_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,6 +12,29 @@ use tokio::time::Duration;

use tokio::sync::Mutex;

#[derive(Copy, Clone, PartialEq)]
struct BooleanPayload {
value: bool,
}

impl Into<BooleanPayload> for bool {
fn into(self) -> BooleanPayload {
todo!()
}
}

impl From<Vec<u8>> for BooleanPayload {
fn from(value: Vec<u8>) -> Self {
todo!()
}
}
impl Into<Vec<u8>> for BooleanPayload {
fn into(self) -> Vec<u8> {
todo!()
}
}
impl AttributePayloadManager for BooleanPayload {}

#[tokio::main]
async fn main() {
let settings = ReactorSettings::new("localhost", 1883);
Expand All @@ -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::<BooleanPayload>();

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

0 comments on commit 3b33944

Please sign in to comment.