Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
gvaradarajan committed Oct 23, 2024
1 parent 039e301 commit b9da694
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Cargo.lock
.vscode
components_esp32*.lock
.envrc
/build
7 changes: 6 additions & 1 deletion micro-rdk/src/common/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,12 @@ impl<'a> GrpcServerInner<'a> {
}

fn robot_get_cloud_metadata(&mut self) -> Result<Bytes, ServerError> {
let resp = self.robot.lock().unwrap().get_cloud_metadata();
let resp = self
.robot
.lock()
.unwrap()
.get_cloud_metadata()
.map_err(|err| ServerError::new(GrpcError::RpcInternal, Some(err.into())))?;
self.encode_message(resp)
}

Expand Down
49 changes: 27 additions & 22 deletions micro-rdk/src/common/robot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ impl ResourceType {
}
}

#[derive(Debug, Clone)]
pub struct CloudMetadata {
org_id: String,
location_id: String,
machine_id: String,
}

pub struct LocalRobot {
pub(crate) part_id: String,
resources: ResourceMap,
Expand All @@ -106,9 +113,7 @@ pub struct LocalRobot {
// is NOT a valid timestamp. For actual timestamps, the real time should be set on the system
// at some point using settimeofday (or something equivalent) and referenced thereof.
pub(crate) start_time: Instant,
org_id: String,
location_id: String,
machine_id: String,
cloud_metadata: Option<CloudMetadata>,
}

#[derive(Error, Debug)]
Expand All @@ -133,6 +138,8 @@ pub enum RobotError {
RobotActuatorError(#[from] ActuatorError),
#[error("resource not found with name {0} and component_type {1}")]
ResourceNotFound(String, String),
#[error("missing cloud metadata")]
RobotMissingCloudMetadata,
#[cfg(feature = "data")]
#[error(transparent)]
DataCollectorInitError(#[from] DataCollectionError),
Expand Down Expand Up @@ -173,9 +180,7 @@ impl LocalRobot {
start_time: Instant::now(),
executor: Default::default(),
part_id: Default::default(),
org_id: Default::default(),
location_id: Default::default(),
machine_id: Default::default(),
cloud_metadata: None,
resources: Default::default(),
build_time: Default::default(),
data_manager_collection_task: Default::default(),
Expand Down Expand Up @@ -260,9 +265,11 @@ impl LocalRobot {
let mut robot = LocalRobot {
executor: exec,
part_id,
org_id: Default::default(),
location_id: Default::default(),
machine_id: Default::default(),
cloud_metadata: config.cloud.as_ref().map(|cfg| CloudMetadata {
org_id: cfg.primary_org_id.clone(),
location_id: cfg.location_id.clone(),
machine_id: cfg.machine_id.clone(),
}),
resources: ResourceMap::new(),
// Use date time pulled off gRPC header as the `build_time` returned in the status of
// every resource as `last_reconfigured`.
Expand All @@ -274,11 +281,6 @@ impl LocalRobot {
data_manager_collection_task: None,
start_time: Instant::now(),
};
if let Some(cloud_cfg) = config.cloud.as_ref() {
robot.org_id = cloud_cfg.primary_org_id.clone();
robot.location_id = cloud_cfg.location_id.clone();
robot.machine_id = cloud_cfg.machine_id.clone();
}

let components: Result<Vec<Option<DynamicComponentConfig>>, AttributeError> = config
.components
Expand Down Expand Up @@ -882,14 +884,17 @@ impl LocalRobot {
Ok(())
}

pub fn get_cloud_metadata(&self) -> robot::v1::GetCloudMetadataResponse {
robot::v1::GetCloudMetadataResponse {
machine_part_id: self.part_id.clone(),
primary_org_id: self.org_id.clone(),
location_id: self.location_id.clone(),
machine_id: self.machine_id.clone(),
..Default::default()
}
pub fn get_cloud_metadata(&self) -> Result<robot::v1::GetCloudMetadataResponse, RobotError> {
self.cloud_metadata
.as_ref()
.ok_or(RobotError::RobotMissingCloudMetadata)
.map(|md| robot::v1::GetCloudMetadataResponse {
machine_part_id: self.part_id.clone(),
primary_org_id: md.org_id.clone(),
location_id: md.location_id.clone(),
machine_id: md.machine_id.clone(),
..Default::default()
})
}
}

Expand Down

0 comments on commit b9da694

Please sign in to comment.