Skip to content

Commit

Permalink
new skeleton ok
Browse files Browse the repository at this point in the history
  • Loading branch information
XdoctorwhoZ committed Aug 4, 2024
1 parent 721c322 commit bd183ee
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 187 deletions.
2 changes: 2 additions & 0 deletions src/asyncv/attribute.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod message;

pub use message::MessageAttribute;

pub use super::AttributeBuilder;
pub use super::MessageClient;
pub use super::MessageDispatcher;
6 changes: 4 additions & 2 deletions src/asyncv/attribute/message.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
pub mod attribute;
mod attribute;
mod dispatcher;

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

pub type MessageAttribute<TYPE> = attribute::Attribute<TYPE>;

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
Expand Down
9 changes: 3 additions & 6 deletions src/asyncv/attribute/message/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,12 +22,8 @@ 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
#[derive(Clone)]
pub struct Attribute<TYPE: AttributePayloadManager> {
///
inner: Arc<Mutex<AttributeInner<TYPE>>>,
Expand Down
12 changes: 5 additions & 7 deletions src/asyncv/attribute/message/attribute/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,25 @@ 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<TYPE: AttributePayloadManager> {
/// 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<Mutex<MessageDispatcher>>,

/// The mqtt client
/// The message client (MQTT)
message_client: MessageClient,

/// The topic of the attribute
topic: String,

/// The topic for commands
topic_cmd: String,

/// Current value of the attribute
value: Option<TYPE>,
/// Requested value of the attribute (set by the user)
requested_value: Option<TYPE>,
// / Handler to call when the value change
}

impl<TYPE: AttributePayloadManager> AttributeInner<TYPE> {
Expand Down
18 changes: 14 additions & 4 deletions src/asyncv/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -19,6 +20,9 @@ pub struct AttributeBuilder {

/// Topic of the attribute
pub topic: Option<String>,

/// True if the attribute is readonly
pub is_read_only: bool,
}

impl AttributeBuilder {
Expand All @@ -31,6 +35,7 @@ impl AttributeBuilder {
message_client,
message_dispatcher,
topic: None,
is_read_only: true,
}
}

Expand All @@ -40,7 +45,12 @@ impl AttributeBuilder {
self
}

pub fn build_with_payload_type<TYPE: AttributePayloadManager>(self) -> Attribute<TYPE> {
Attribute::new(self)
pub fn as_read_write(mut self) -> Self {
self.is_read_only = false;
self
}

pub fn build_with_message_type<TYPE: AttributePayloadManager>(self) -> MessageAttribute<TYPE> {
MessageAttribute::new(self)
}
}
12 changes: 0 additions & 12 deletions src/asyncv/msg.rs

This file was deleted.

60 changes: 0 additions & 60 deletions src/asyncv/msg/att.rs

This file was deleted.

56 changes: 0 additions & 56 deletions src/asyncv/msg/att/inner_msg_att.rs

This file was deleted.

3 changes: 3 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions src/common/message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod boolean;

pub use boolean::BooleanMessage;
24 changes: 24 additions & 0 deletions src/common/message/boolean.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::AttributePayloadManager;

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

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

impl From<Vec<u8>> for BooleanMessage {
fn from(value: Vec<u8>) -> Self {
return BooleanMessage { value: true };
}
}
impl Into<Vec<u8>> for BooleanMessage {
fn into(self) -> Vec<u8> {
return vec![1];
}
}
impl AttributePayloadManager for BooleanMessage {}
45 changes: 5 additions & 40 deletions src/examples/test_async.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,30 @@
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<BooleanPayload> for bool {
fn into(self) -> BooleanPayload {
return BooleanPayload { value: true };
}
}

impl From<Vec<u8>> for BooleanPayload {
fn from(value: Vec<u8>) -> Self {
return BooleanPayload { value: true };
}
}
impl Into<Vec<u8>> for BooleanPayload {
fn into(self) -> Vec<u8> {
return vec![1];
}
}
impl AttributePayloadManager for BooleanPayload {}

#[tokio::main]
async fn main() {
let settings = ReactorSettings::new("localhost", 1883);
let mut reactor = Reactor::new(settings);

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::<BooleanPayload>();
.build_with_message_type::<BooleanMessage>();

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;
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>> + From<Vec<u8>> + PartialEq + Copy + Sync + Send + 'static
{
}

0 comments on commit bd183ee

Please sign in to comment.