diff --git a/actuator/actuator/src/server/actuator.rs b/actuator/actuator/src/server/actuator.rs new file mode 100644 index 0000000..95b2a8e --- /dev/null +++ b/actuator/actuator/src/server/actuator.rs @@ -0,0 +1,37 @@ +use crate::grpc::actuator::common::{ + actuator_controller_server::ActuatorController, ActuatorStatus, CommandActuatorsRequest, + CommandActuatorsResponse, ResponseCode, +}; +use tonic::{Request, Response, Status}; + +#[derive(Default, Clone, Copy)] +pub struct ActuatorService {} + +#[tonic::async_trait] +impl ActuatorController for ActuatorService { + async fn command_actuators( + &self, + request: Request, + ) -> Result, Status> { + let cmd = request.into_inner(); + println!("Received CommandActuatorsRequest: {:?}", cmd); + + // Create a response for each actuator in the request + let statuses: Vec = cmd + .commands + .iter() + .map(|cmd| ActuatorStatus { + actuator_id: cmd.actuator_id, + position: cmd.position, + velocity: cmd.velocity, + }) + .collect(); + + let response = CommandActuatorsResponse { + statuses, + response_code: ResponseCode::Ok as i32, + }; + + Ok(Response::new(response)) + } +} diff --git a/actuator/actuator/src/server/mod.rs b/actuator/actuator/src/server/mod.rs index b0d9126..f44e1b1 100644 --- a/actuator/actuator/src/server/mod.rs +++ b/actuator/actuator/src/server/mod.rs @@ -1,98 +1,22 @@ -use crate::grpc::actuator::common::{ - actuator_controller_server::{ActuatorController, ActuatorControllerServer}, - ActuatorStatus, CommandActuatorsRequest, CommandActuatorsResponse, ResponseCode, -}; -use crate::grpc::google::longrunning::{ - operations_server::{Operations, OperationsServer}, - CancelOperationRequest, DeleteOperationRequest, GetOperationRequest, ListOperationsRequest, - ListOperationsResponse, Operation, WaitOperationRequest, -}; -use std::net::SocketAddr; -use tonic::{transport::Server, Request, Response, Status}; - -#[derive(Default, Clone, Copy)] -pub struct ActuatorService {} - -#[tonic::async_trait] -impl Operations for ActuatorService { - async fn list_operations( - &self, - request: Request, - ) -> Result, Status> { - // Implement your logic here - Ok(Response::new(ListOperationsResponse { - operations: vec![], - next_page_token: String::new(), - })) - } - - async fn get_operation( - &self, - request: Request, - ) -> Result, Status> { - Err(Status::unimplemented("Not yet implemented")) - } - - async fn delete_operation( - &self, - request: Request, - ) -> Result, Status> { - Err(Status::unimplemented("Not yet implemented")) - } - - async fn cancel_operation( - &self, - request: Request, - ) -> Result, Status> { - Err(Status::unimplemented("Not yet implemented")) - } +mod actuator; +mod operations; - async fn wait_operation( - &self, - request: Request, - ) -> Result, Status> { - Err(Status::unimplemented("Not yet implemented")) - } -} - -#[tonic::async_trait] -impl ActuatorController for ActuatorService { - async fn command_actuators( - &self, - request: Request, - ) -> Result, Status> { - let cmd = request.into_inner(); - println!("Received CommandActuatorsRequest: {:?}", cmd); - - // Create a response for each actuator in the request - let statuses: Vec = cmd - .commands - .iter() - .map(|cmd| ActuatorStatus { - actuator_id: cmd.actuator_id, - position: cmd.position, - velocity: cmd.velocity, - }) - .collect(); +pub use actuator::ActuatorService; +pub use operations::OperationsService; - let response = CommandActuatorsResponse { - statuses, - response_code: ResponseCode::Ok as i32, - }; - - Ok(Response::new(response)) - } -} +use crate::grpc::actuator::common::actuator_controller_server::ActuatorControllerServer; +use crate::grpc::google::longrunning::operations_server::OperationsServer; +use std::net::SocketAddr; +use tonic::transport::Server; pub async fn run_server() -> Result<(), Box> { let addr: SocketAddr = "[::1]:50051".parse()?; - let service = ActuatorService::default(); println!("Starting actuator server on {}", addr); Server::builder() - .add_service(ActuatorControllerServer::new(service.clone())) - .add_service(OperationsServer::new(service)) + .add_service(ActuatorControllerServer::new(ActuatorService::default())) + .add_service(OperationsServer::new(OperationsService::default())) .serve(addr) .await?; diff --git a/actuator/actuator/src/server/operations.rs b/actuator/actuator/src/server/operations.rs new file mode 100644 index 0000000..a9ba42d --- /dev/null +++ b/actuator/actuator/src/server/operations.rs @@ -0,0 +1,51 @@ +use crate::grpc::google::longrunning::{ + operations_server::Operations, CancelOperationRequest, DeleteOperationRequest, + GetOperationRequest, ListOperationsRequest, ListOperationsResponse, Operation, + WaitOperationRequest, +}; +use tonic::{Request, Response, Status}; + +#[derive(Default, Clone, Copy)] +pub struct OperationsService {} + +#[tonic::async_trait] +impl Operations for OperationsService { + async fn list_operations( + &self, + request: Request, + ) -> Result, Status> { + // Implement your logic here + Ok(Response::new(ListOperationsResponse { + operations: vec![], + next_page_token: String::new(), + })) + } + + async fn get_operation( + &self, + request: Request, + ) -> Result, Status> { + Err(Status::unimplemented("Not yet implemented")) + } + + async fn delete_operation( + &self, + request: Request, + ) -> Result, Status> { + Err(Status::unimplemented("Not yet implemented")) + } + + async fn cancel_operation( + &self, + request: Request, + ) -> Result, Status> { + Err(Status::unimplemented("Not yet implemented")) + } + + async fn wait_operation( + &self, + request: Request, + ) -> Result, Status> { + Err(Status::unimplemented("Not yet implemented")) + } +}