Skip to content

Commit

Permalink
src: device: manager: Update request pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
RaulTrombin committed Aug 7, 2024
1 parent 5c78e7f commit e2f8889
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions src/device/manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{
collections::{hash_map::DefaultHasher, HashMap},
hash::{Hash, Hasher},
net::{Ipv4Addr, SocketAddrV4},
ops::Deref,
};
use tokio::sync::{mpsc, oneshot};
use tokio_serial::available_ports;
Expand Down Expand Up @@ -145,19 +146,32 @@ pub struct DeviceAnswer {
}

#[derive(Debug, Clone, Serialize, Deserialize, Apiv2Schema)]
#[serde(tag = "command", content = "payload")]
pub enum Request {
AutoCreate,
Create(CreateStruct),
Delete(Uuid),
Delete(UuidWrapper),
List,
Info(Uuid),
Info(UuidWrapper),
Search,
Ping(DeviceRequestStruct),
GetDeviceHandler(Uuid),
EnableContinuousMode(Uuid),
DisableContinuousMode(Uuid),
GetDeviceHandler(UuidWrapper),
EnableContinuousMode(UuidWrapper),
DisableContinuousMode(UuidWrapper),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UuidWrapper {
pub uuid: Uuid,
}

impl Deref for UuidWrapper {
type Target = Uuid;

fn deref(&self) -> &Self::Target {
&self.uuid
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateStruct {
pub source: SourceSelection,
Expand All @@ -166,8 +180,8 @@ pub struct CreateStruct {

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeviceRequestStruct {
pub target: Uuid,
pub request: crate::device::devices::PingRequest,
pub uuid: Uuid,
pub device_request: crate::device::devices::PingRequest,
}

impl DeviceManager {
Expand All @@ -187,7 +201,7 @@ impl DeviceManager {
}
}
Request::Delete(uuid) => {
let result = self.delete(uuid).await;
let result = self.delete(*uuid).await;
if let Err(e) = actor_request.respond_to.send(result) {
error!("DeviceManager: Failed to return Delete response: {e:?}");
}
Expand All @@ -199,25 +213,25 @@ impl DeviceManager {
}
}
Request::Info(device_id) => {
let result = self.info(device_id).await;
let result = self.info(*device_id).await;
if let Err(e) = actor_request.respond_to.send(result) {
error!("DeviceManager: Failed to return Info response: {:?}", e);
}
}
Request::EnableContinuousMode(uuid) => {
let result = self.continuous_mode(uuid).await;
let result = self.continuous_mode(*uuid).await;
if let Err(e) = actor_request.respond_to.send(result) {
error!("DeviceManager: Failed to return EnableContinuousMode response: {e:?}");
}
}
Request::DisableContinuousMode(uuid) => {
let result = self.continuous_mode_off(uuid).await;
let result = self.continuous_mode_off(*uuid).await;
if let Err(e) = actor_request.respond_to.send(result) {
error!("DeviceManager: Failed to return DisableContinuousMode response: {e:?}");
}
}
Request::GetDeviceHandler(id) => {
let answer = self.get_device_handler(id).await;
let answer = self.get_device_handler(*id).await;
if let Err(e) = actor_request.respond_to.send(answer) {
error!("DeviceManager: Failed to return GetDeviceHandler response: {e:?}");
}
Expand Down Expand Up @@ -515,8 +529,11 @@ impl ManagerActorHandler {
// Devices requests are forwarded directly to device and let manager handle other incoming request.
Request::Ping(request) => {
trace!("Handling Ping request: {request:?}: Forwarding request to device handler");
let get_handler_target = request.target;
let handler_request = Request::GetDeviceHandler(get_handler_target);
let get_handler_target = request.uuid;
let handler_request =
Request::GetDeviceHandler(crate::device::manager::UuidWrapper {
uuid: get_handler_target,
});
let manager_request = ManagerActorRequest {
request: handler_request,
respond_to: result_sender,
Expand All @@ -541,13 +558,13 @@ impl ManagerActorHandler {
trace!(
"Handling Ping request: {request:?}: Successfully received the handler"
);
let result = handler.send(request.request.clone()).await;
let result = handler.send(request.device_request.clone()).await;
match result {
Ok(result) => {
info!("Handling Ping request: {request:?}: Success");
Ok(Answer::DeviceMessage(DeviceAnswer {
answer: result,
device_id: request.target,
device_id: request.uuid,
}))
}
Err(err) => {
Expand Down

0 comments on commit e2f8889

Please sign in to comment.