Skip to content

Commit

Permalink
first_aproach
Browse files Browse the repository at this point in the history
  • Loading branch information
RaulTrombin committed Aug 7, 2024
1 parent 52c24b6 commit bd04f61
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/device/devices.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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),
Expand All @@ -277,7 +278,7 @@ pub enum PingRequest {
Stop,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Apiv2Schema)]
pub enum Ping1DRequest {
DeviceID,
ModeAuto,
Expand Down Expand Up @@ -308,7 +309,7 @@ pub enum Ping1DRequest {
GotoBootloader,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Apiv2Schema)]
pub enum Ping360Request {
MotorOff,
DeviceData,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/device/manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
165 changes: 164 additions & 1 deletion src/server/protocols/v1/rest.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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"]
Expand Down Expand Up @@ -46,6 +47,12 @@ async fn server_metadata() -> Result<Json<ServerMetadata>, 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);
}

Expand All @@ -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<ManagerActorHandler>,
selection: web::Path<DeviceManagerGetOptionsV1>,
) -> Result<Json<crate::device::manager::Answer>, 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<ManagerActorHandler>,
info: web::Path<(Uuid, DeviceManagerPostOptionsV1)>,
) -> Result<Json<crate::device::manager::Answer>, 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<ManagerActorHandler>,
info: web::Path<(Uuid, crate::device::devices::PingRequest)>,
) -> Result<Json<crate::device::manager::Answer>, 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<ManagerActorHandler>,
info: web::Path<(Uuid, crate::device::devices::Ping1DRequest)>,
) -> Result<Json<crate::device::manager::Answer>, 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<ManagerActorHandler>,
info: web::Path<(Uuid, crate::device::devices::Ping360Request)>,
) -> Result<Json<crate::device::manager::Answer>, 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<ManagerActorHandler>,
info: web::Path<(Uuid, crate::device::devices::PingCommonRequest)>,
) -> Result<Json<crate::device::manager::Answer>, 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,
Expand Down

0 comments on commit bd04f61

Please sign in to comment.