Skip to content

Commit

Permalink
src: device: Change request pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
RaulTrombin committed Aug 7, 2024
1 parent fa1f76a commit 5c78e7f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use serde::{Deserialize, Serialize};
use tracing::info;

#[macro_use]
Expand All @@ -9,6 +10,12 @@ mod device;
mod logger;
mod server;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "module")]
pub enum ModuleType {
DeviceManager(device::manager::Request),
}

#[tokio::main]
async fn main() {
// CLI should be started before logger to allow control over verbosity
Expand Down
54 changes: 30 additions & 24 deletions src/server/protocols/v1/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::sync::{Arc, Mutex};
use tracing::info;
use uuid::Uuid;

use crate::device::manager::{ManagerActorHandler, Request};
use crate::device::manager::ManagerActorHandler;

pub struct StringMessage(String);

Expand Down Expand Up @@ -134,7 +134,7 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WebsocketActor {
match msg {
Ok(ws::Message::Ping(msg)) => ctx.pong(&msg),
Ok(ws::Message::Text(text)) => {
let manager_requests: Vec<Request> = match serde_json::from_str(&text) {
let manager_requests: Vec<crate::ModuleType> = match serde_json::from_str(&text) {
Ok(requests) => requests,
Err(err) => match serde_json::from_str(&text) {
Ok(request) => vec![request],
Expand All @@ -147,27 +147,31 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WebsocketActor {
};

for request in manager_requests {
let manager_handler = self.manager_handler.clone();

let future =
async move { manager_handler.send(request).await }.into_actor(self);

future
.then(|res, _, ctx| {
match &res {
Ok(result) => {
crate::server::protocols::v1::websocket::send_to_websockets(
json!(result),
None,
);
}
Err(err) => {
ctx.text(serde_json::to_string_pretty(err).unwrap());
}
}
fut::ready(())
})
.wait(ctx);
match request {
crate::ModuleType::DeviceManager(request) => {
let manager_handler = self.manager_handler.clone();

let future =
async move { manager_handler.send(request).await }.into_actor(self);

future
.then(|res, _, ctx| {
match &res {
Ok(result) => {
crate::server::protocols::v1::websocket::send_to_websockets(
json!(result),
None,
);
}
Err(err) => {
ctx.text(serde_json::to_string_pretty(err).unwrap());
}
}
fut::ready(())
})
.wait(ctx);
}
}
}
}
Ok(ws::Message::Close(msg)) => ctx.close(msg),
Expand All @@ -191,7 +195,9 @@ pub async fn websocket(
let device_number = query.into_inner().device_number;

if let Some(device_number) = device_number {
let request = crate::device::manager::Request::Info(device_number);
let request = crate::device::manager::Request::Info(crate::device::manager::UuidWrapper {
uuid: device_number,
});
match manager_handler.send(request).await {
Ok(response) => {
info!(
Expand Down

0 comments on commit 5c78e7f

Please sign in to comment.