From bd04f61b4cffef5b7969a5ea0bcce5aaabc5f6f4 Mon Sep 17 00:00:00 2001 From: Raul Victor Trombin Date: Wed, 7 Aug 2024 16:53:26 -0300 Subject: [PATCH] first_aproach --- src/device/devices.rs | 9 +- src/device/manager/mod.rs | 2 +- src/server/protocols/v1/rest.rs | 165 +++++++++++++++++++++++++++++++- 3 files changed, 170 insertions(+), 6 deletions(-) diff --git a/src/device/devices.rs b/src/device/devices.rs index d0217811..51550d8a 100644 --- a/src/device/devices.rs +++ b/src/device/devices.rs @@ -1,4 +1,5 @@ use bluerobotics_ping::device::PingDevice; +use paperclip::actix::Apiv2Schema; use serde::{Deserialize, Serialize}; use tokio::sync::{mpsc, oneshot}; use tracing::{error, trace, warn}; @@ -267,7 +268,7 @@ pub enum UpgradeResult { Ping360, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Apiv2Schema)] pub enum PingRequest { Ping1D(Ping1DRequest), Ping360(Ping360Request), @@ -277,7 +278,7 @@ pub enum PingRequest { Stop, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Apiv2Schema)] pub enum Ping1DRequest { DeviceID, ModeAuto, @@ -308,7 +309,7 @@ pub enum Ping1DRequest { GotoBootloader, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Apiv2Schema)] pub enum Ping360Request { MotorOff, DeviceData, @@ -319,7 +320,7 @@ pub enum Ping360Request { AutoTransmit(bluerobotics_ping::ping360::AutoTransmitStruct), } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Apiv2Schema)] pub enum PingCommonRequest { DeviceInformation, ProtocolVersion, diff --git a/src/device/manager/mod.rs b/src/device/manager/mod.rs index 357e7a7a..4a693adc 100644 --- a/src/device/manager/mod.rs +++ b/src/device/manager/mod.rs @@ -159,7 +159,7 @@ pub enum Request { DisableContinuousMode(UuidWrapper), } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Apiv2Schema)] pub struct UuidWrapper { pub uuid: Uuid, } diff --git a/src/server/protocols/v1/rest.rs b/src/server/protocols/v1/rest.rs index 1b9a36f8..25a5c2d6 100644 --- a/src/server/protocols/v1/rest.rs +++ b/src/server/protocols/v1/rest.rs @@ -1,4 +1,4 @@ -use crate::device::manager::ManagerActorHandler; +use crate::device::manager::{ManagerActorHandler, UuidWrapper}; use crate::server::protocols::v1::errors::Error; use actix_web::Responder; use mime_guess::from_path; @@ -9,6 +9,7 @@ use paperclip::actix::{ }; use serde::{Deserialize, Serialize}; use serde_json::json; +use uuid::Uuid; #[derive(rust_embed::RustEmbed)] #[folder = "src/server/protocols/v1/frontend"] @@ -46,6 +47,12 @@ async fn server_metadata() -> Result, Error> { pub fn register_services(cfg: &mut web::ServiceConfig) { cfg.service(index) .service(post_request) + .service(device_manager_get) + .service(device_manager_post) + .service(device_manager_device_get) + .service(device_manager_device_ping1d_get) + .service(device_manager_device_ping360_get) + .service(device_manager_device_common_get) .service(index_files); } @@ -64,6 +71,162 @@ async fn post_request( Ok(Json(answer)) } + +#[derive(Debug, Clone, Serialize, Deserialize, Apiv2Schema)] +pub enum DeviceManagerGetOptionsV1 { + AutoCreate, + List, + Search, +} + +#[derive(Debug, Clone, Serialize, Deserialize, Apiv2Schema)] +pub enum DeviceManagerPostOptionsV1 { + Delete, + Info, + EnableContinuousMode, + DisableContinuousMode, +} + +#[derive(Debug, Clone, Serialize, Deserialize, Apiv2Schema)] +pub enum DeviceManagerDeviceRequestGetOptionsV1 { + Delete, + Info, + EnableContinuousMode, + DisableContinuousMode, +} + +#[api_v2_operation] +#[get("device_manager/{selection}")] +async fn device_manager_get( + manager_handler: web::Data, + selection: web::Path, +) -> Result, Error> { + let request = match selection.into_inner(){ + DeviceManagerGetOptionsV1::AutoCreate => crate::device::manager::Request::AutoCreate, + DeviceManagerGetOptionsV1::List => crate::device::manager::Request::List, + DeviceManagerGetOptionsV1::Search => crate::device::manager::Request::Search, + }; + + let answer = manager_handler.send(request).await?; + + // Broadcast the results to webscoket clients. + crate::server::protocols::v1::websocket::send_to_websockets(json!(answer), None); + + Ok(Json(answer)) +} + +#[api_v2_operation] +#[post("device_manager/{device}/{selection}")] +async fn device_manager_post( + manager_handler: web::Data, + info: web::Path<(Uuid, DeviceManagerPostOptionsV1)>, +) -> Result, Error> { + let info = info.into_inner(); + let uuid = info.0; + let request = info.1; + + let request = match request { + DeviceManagerPostOptionsV1::Delete => crate::device::manager::Request::Delete(uuid.into()), + DeviceManagerPostOptionsV1::Info => crate::device::manager::Request::Info(uuid.into()), + DeviceManagerPostOptionsV1::EnableContinuousMode => crate::device::manager::Request::EnableContinuousMode(uuid.into()), + DeviceManagerPostOptionsV1::DisableContinuousMode => crate::device::manager::Request::DisableContinuousMode(uuid.into()), + }; + + let answer = manager_handler.send(request).await?; + + // Broadcast the results to webscoket clients. + crate::server::protocols::v1::websocket::send_to_websockets(json!(answer), None); + + Ok(Json(answer)) +} + +#[api_v2_operation] +#[get("device_manager/{device}/{request}")] +async fn device_manager_device_get( + manager_handler: web::Data, + info: web::Path<(Uuid, crate::device::devices::PingRequest)>, +) -> Result, Error> { + let info = info.into_inner(); + let uuid = info.0; + let request = info.1; + + let request = crate::device::manager::Request::Ping(crate::device::manager::DeviceRequestStruct{uuid : uuid, device_request : request}); + + + let answer = manager_handler.send(request).await?; + + // Broadcast the results to webscoket clients. + crate::server::protocols::v1::websocket::send_to_websockets(json!(answer), None); + + Ok(Json(answer)) +} + +#[api_v2_operation] +#[get("device_manager/{device}/ping1d/{request}")] +async fn device_manager_device_ping1d_get( + manager_handler: web::Data, + info: web::Path<(Uuid, crate::device::devices::Ping1DRequest)>, +) -> Result, Error> { + let info = info.into_inner(); + let uuid = info.0; + let request = info.1; + + let request = crate::device::devices::PingRequest::Ping1D(request); + + let request = crate::device::manager::Request::Ping(crate::device::manager::DeviceRequestStruct{uuid : uuid.into(), device_request : request}); + + let answer = manager_handler.send(request).await?; + + // Broadcast the results to webscoket clients. + crate::server::protocols::v1::websocket::send_to_websockets(json!(answer), None); + + Ok(Json(answer)) +} + +#[api_v2_operation] +#[get("device_manager/{device}/ping360/{request}")] +async fn device_manager_device_ping360_get( + manager_handler: web::Data, + info: web::Path<(Uuid, crate::device::devices::Ping360Request)>, +) -> Result, Error> { + let info = info.into_inner(); + let uuid = info.0; + let request = info.1; + + let request = crate::device::devices::PingRequest::Ping360(request); + + let request = crate::device::manager::Request::Ping(crate::device::manager::DeviceRequestStruct{uuid : uuid.into(), device_request : request}); + + let answer = manager_handler.send(request).await?; + + // Broadcast the results to webscoket clients. + crate::server::protocols::v1::websocket::send_to_websockets(json!(answer), None); + + Ok(Json(answer)) +} + +#[api_v2_operation] +#[get("device_manager/{device}/common/{request}")] +async fn device_manager_device_common_get( + manager_handler: web::Data, + info: web::Path<(Uuid, crate::device::devices::PingCommonRequest)>, +) -> Result, Error> { + let info = info.into_inner(); + let uuid = info.0; + let request = info.1; + + let request = crate::device::devices::PingRequest::Common(request); + + let request = crate::device::manager::Request::Ping(crate::device::manager::DeviceRequestStruct{uuid : uuid.into(), device_request : request}); + + let answer = manager_handler.send(request).await?; + + // Broadcast the results to webscoket clients. + crate::server::protocols::v1::websocket::send_to_websockets(json!(answer), None); + + Ok(Json(answer)) +} + #[derive(Debug, Serialize, Deserialize, Apiv2Schema)] pub struct ServerMetadata { pub name: &'static str,