Skip to content

Commit

Permalink
[RSDK-8870] add readings endpoint for MovementSensor and PowerSensor (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gvaradarajan authored Sep 26, 2024
1 parent 1c0c335 commit 1300553
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions micro-rdk/src/common/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ impl<'a> GrpcServerInner<'a> {
"/viam.component.movementsensor.v1.MovementSensorService/DoCommand" => {
self.movement_sensor_do_command(payload)
}
"/viam.component.movementsensor.v1.MovementSensorService/GetReadings" => {
self.movement_sensor_get_readings(payload)
}
"/viam.component.encoder.v1.EncoderService/GetPosition" => {
self.encoder_get_position(payload)
}
Expand All @@ -340,6 +343,9 @@ impl<'a> GrpcServerInner<'a> {
"/viam.component.powersensor.v1.PowerSensorService/GetPower" => {
self.power_sensor_get_power(payload)
}
"/viam.component.powersensor.v1.PowerSensorService/GetReadings" => {
self.power_sensor_get_readings(payload)
}
"/viam.component.powersensor.v1.PowerSensorService/DoCommand" => {
self.power_sensor_do_command(payload)
}
Expand Down Expand Up @@ -995,6 +1001,28 @@ impl<'a> GrpcServerInner<'a> {
Err(ServerError::from(GrpcError::RpcUnimplemented))
}

fn movement_sensor_get_readings(&mut self, message: &[u8]) -> Result<Bytes, ServerError> {
let req = proto::common::v1::GetReadingsRequest::decode(message)
.map_err(|_| ServerError::from(GrpcError::RpcInvalidArgument))?;
let m_sensor = match self
.robot
.lock()
.unwrap()
.get_movement_sensor_by_name(req.name)
{
Some(b) => b,
None => return Err(ServerError::from(GrpcError::RpcUnavailable)),
};

let readings = m_sensor
.lock()
.unwrap()
.get_generic_readings()
.map_err(|err| ServerError::new(GrpcError::RpcInternal, Some(err.into())))?;
let resp = proto::common::v1::GetReadingsResponse { readings };
self.encode_message(resp)
}

fn movement_sensor_do_command(&mut self, message: &[u8]) -> Result<Bytes, ServerError> {
let req = proto::common::v1::DoCommandRequest::decode(message)
.map_err(|_| ServerError::from(GrpcError::RpcInvalidArgument))?;
Expand Down Expand Up @@ -1205,6 +1233,28 @@ impl<'a> GrpcServerInner<'a> {
self.encode_message(resp)
}

fn power_sensor_get_readings(&mut self, message: &[u8]) -> Result<Bytes, ServerError> {
let req = proto::common::v1::GetReadingsRequest::decode(message)
.map_err(|_| ServerError::from(GrpcError::RpcInvalidArgument))?;
let power_sensor = match self
.robot
.lock()
.unwrap()
.get_power_sensor_by_name(req.name)
{
Some(s) => s,
None => return Err(ServerError::from(GrpcError::RpcUnavailable)),
};

let readings = power_sensor
.lock()
.unwrap()
.get_generic_readings()
.map_err(|err| ServerError::new(GrpcError::RpcInternal, Some(err.into())))?;
let resp = proto::common::v1::GetReadingsResponse { readings };
self.encode_message(resp)
}

fn power_sensor_do_command(&mut self, message: &[u8]) -> Result<Bytes, ServerError> {
let req = proto::common::v1::DoCommandRequest::decode(message)
.map_err(|_| ServerError::from(GrpcError::RpcInvalidArgument))?;
Expand Down

0 comments on commit 1300553

Please sign in to comment.