Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
XdoctorwhoZ committed Aug 4, 2024
1 parent dadab8a commit 721c322
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 53 deletions.
11 changes: 1 addition & 10 deletions src/asyncv/attribute/message.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
pub mod boolean;
mod core_members;
pub mod attribute;
mod dispatcher;

use async_trait::async_trait;
use bytes::Bytes;

pub use core_members::MessageCoreMembers;
pub use dispatcher::MessageDispatcher;

pub use super::AttributeBuilder;

pub type MessageClient = rumqttc::AsyncClient;

pub type AttributeId = u32;

/// Trait to manage an message attribute (MQTT)
/// Sync version
#[async_trait]
pub trait OnMessageHandler: Send + Sync {
async fn on_message(&mut self, data: &Bytes);
}

#[async_trait]
pub trait OnBooleanMessage: Send + Sync {
async fn on_message_boolean(&mut self, id: AttributeId, data: bool);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
mod inner;
use inner::AttributeInner;
use tokio::time::sleep;

use std::future::Future;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;

use crate::asyncv::attribute::message::OnBooleanMessage;
pub use super::MessageClient;
use crate::AttributeError;
use async_trait::async_trait;

use super::{AttributeBuilder, MessageCoreMembers};
use super::AttributeBuilder;

pub use super::OnMessageHandler;

Expand Down Expand Up @@ -46,6 +47,20 @@ impl<TYPE: AttributePayloadManager> Attribute<TYPE> {
Ok(self)
}

// wait_change()
// wait_change_then()

pub fn when_change<F>(&self, function: F)
where
F: Future<Output = ()> + Send + 'static,
// F: Future<Output = Result<bool, ()>> + Send + 'static,
{
tokio::spawn(async move {
sleep(Duration::from_secs(1)).await;
function.await
});
}

// pub async fn on_change_handler(&self, handler: Box<dyn OnChangeHandler>) {
// self.inner.lock().await.on_change_handler(handler);
// }
Expand All @@ -55,7 +70,7 @@ impl<TYPE: AttributePayloadManager> Attribute<TYPE> {

/// Set the value of the attribute
///
pub async fn set<INTO_TYPE: Into<TYPE>>(&self, value: INTO_TYPE) -> Result<(), AttributeError> {
pub async fn set<I: Into<TYPE>>(&self, value: I) -> 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| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,18 @@ use std::sync::Arc;
use std::sync::Weak;
use tokio::sync::Mutex;

use crate::asyncv::MessageDispatcher;
use crate::AttributeError;

use super::MessageClient;
use super::OnMessageHandler;

use std::future::Future;
use std::pin::Pin;

use futures::future::BoxFuture;
use futures::FutureExt;

use bytes::Bytes;

use async_trait::async_trait;

use crate::asyncv::AttributeBuilder;

use super::MessageCoreMembers;
// use super::OnChangeHandler;
use super::OnMessageHandler;

use tokio::sync::Notify;

use super::AttributePayloadManager;
Expand Down
8 changes: 0 additions & 8 deletions src/asyncv/attribute/message/boolean.rs

This file was deleted.

21 changes: 6 additions & 15 deletions src/asyncv/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ use std::sync::Weak;

use tokio::sync::Mutex;

use super::attribute::message::boolean::attribute::Attribute;
use super::attribute::message::boolean::attribute::AttributePayloadManager;
use super::attribute::message::AttributeId;
use super::attribute::message::attribute::Attribute;
use super::attribute::message::attribute::AttributePayloadManager;
pub use super::MessageClient;
pub use super::MessageDispatcher;

static mut ID_POOL: AttributeId = 0;

/// Object that allow to build an generic attribute
///
pub struct AttributeBuilder {
pub id: AttributeId,
/// The mqtt client
pub message_client: MessageClient,

Expand All @@ -31,15 +27,10 @@ impl AttributeBuilder {
message_client: MessageClient,
message_dispatcher: Weak<Mutex<MessageDispatcher>>,
) -> AttributeBuilder {
unsafe {
let id = ID_POOL;
ID_POOL += 1;
AttributeBuilder {
id: id,
message_client,
message_dispatcher,
topic: None,
}
AttributeBuilder {
message_client,
message_dispatcher,
topic: None,
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/examples/test_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ 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::attribute::message::attribute::AttributePayloadManager;

use panduza::asyncv::Reactor;
use panduza::ReactorSettings;
use tokio::time::sleep;
Expand All @@ -19,18 +18,18 @@ struct BooleanPayload {

impl Into<BooleanPayload> for bool {
fn into(self) -> BooleanPayload {
todo!()
return BooleanPayload { value: true };
}
}

impl From<Vec<u8>> for BooleanPayload {
fn from(value: Vec<u8>) -> Self {
todo!()
return BooleanPayload { value: true };
}
}
impl Into<Vec<u8>> for BooleanPayload {
fn into(self) -> Vec<u8> {
todo!()
return vec![1];
}
}
impl AttributePayloadManager for BooleanPayload {}
Expand Down Expand Up @@ -59,5 +58,9 @@ async fn main() {
println!("send data");
pp.set(true).await.unwrap();

pp.when_change(async move {
println!("cooucou");
});

sleep(Duration::from_secs(60)).await;
}

0 comments on commit 721c322

Please sign in to comment.