From f66643f33d9cf9803e05f028a8c6bedf73ce627e Mon Sep 17 00:00:00 2001 From: shawnbmccarthy Date: Thu, 4 Apr 2024 14:13:58 -0400 Subject: [PATCH] sensor rpc --- .../sdk/core/component/sensor/Sensor.java | 7 ++ .../component/sensor/SensorRPCClient.java | 43 ++++++++++++ .../component/sensor/SensorRPCService.java | 67 +++++++++++++++++++ .../sdk/core/resource/ResourceManager.java | 11 ++- 4 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 core/sdk/src/main/java/com/viam/sdk/core/component/sensor/SensorRPCClient.java create mode 100644 core/sdk/src/main/java/com/viam/sdk/core/component/sensor/SensorRPCService.java diff --git a/core/sdk/src/main/java/com/viam/sdk/core/component/sensor/Sensor.java b/core/sdk/src/main/java/com/viam/sdk/core/component/sensor/Sensor.java index 4d81fc873..0ff1cf4fc 100644 --- a/core/sdk/src/main/java/com/viam/sdk/core/component/sensor/Sensor.java +++ b/core/sdk/src/main/java/com/viam/sdk/core/component/sensor/Sensor.java @@ -3,6 +3,7 @@ import com.google.protobuf.Struct; import com.google.protobuf.Value; import com.viam.common.v1.Common; +import com.viam.common.v1.Common.GetReadingsResponse; import com.viam.common.v1.Common.ResourceName; import com.viam.sdk.core.component.Component; import com.viam.sdk.core.component.generic.Generic; @@ -23,6 +24,10 @@ public abstract class Sensor extends Component { Subtype.RESOURCE_TYPE_COMPONENT, "sensor"); + public Sensor(final String name) { + super(SUBTYPE, named(name)); + } + public Sensor(Subtype subtype, ResourceName name) { super(subtype, name); throw new UnsupportedOperationException(); @@ -47,4 +52,6 @@ public static Common.ResourceName named(final String name) { public static Sensor fromRobot(final RobotClient robot, final String name) { return robot.getResource(Sensor.class, named(name)); } + + public abstract GetReadingsResponse getReadings(final Optional extra); } diff --git a/core/sdk/src/main/java/com/viam/sdk/core/component/sensor/SensorRPCClient.java b/core/sdk/src/main/java/com/viam/sdk/core/component/sensor/SensorRPCClient.java new file mode 100644 index 000000000..e48701c5b --- /dev/null +++ b/core/sdk/src/main/java/com/viam/sdk/core/component/sensor/SensorRPCClient.java @@ -0,0 +1,43 @@ +package com.viam.sdk.core.component.sensor; + +import com.google.protobuf.Struct; +import com.google.protobuf.Value; +import com.viam.common.v1.Common; +import com.viam.common.v1.Common.GetReadingsRequest; +import com.viam.common.v1.Common.GetReadingsResponse; +import com.viam.component.sensor.v1.SensorServiceGrpc; + +import com.viam.sdk.core.rpc.Channel; + +import java.util.Map; +import java.util.Optional; + +public class SensorRPCClient extends Sensor { + private final SensorServiceGrpc.SensorServiceBlockingStub client; + + public SensorRPCClient(final String name, final Channel chan) { + super(name); + final SensorServiceGrpc.SensorServiceBlockingStub client = SensorServiceGrpc.newBlockingStub(chan); + if (chan.getCallCredentials().isPresent()) { + this.client = client.withCallCredentials(chan.getCallCredentials().get()); + } else { + this.client = client; + } + } + + @Override + public Struct doCommand(final Map command) { + return client.doCommand(Common.DoCommandRequest.newBuilder(). + setName(getName().getName()). + setCommand(Struct.newBuilder().putAllFields(command).build()). + build()).getResult(); + } + + @Override + public GetReadingsResponse getReadings(final Optional extra) { + final GetReadingsRequest.Builder builder = GetReadingsRequest.newBuilder(). + setName(getName().getName()); + extra.ifPresent(builder::setExtra); + return client.getReadings(builder.build()); + } +} diff --git a/core/sdk/src/main/java/com/viam/sdk/core/component/sensor/SensorRPCService.java b/core/sdk/src/main/java/com/viam/sdk/core/component/sensor/SensorRPCService.java new file mode 100644 index 000000000..617db74ec --- /dev/null +++ b/core/sdk/src/main/java/com/viam/sdk/core/component/sensor/SensorRPCService.java @@ -0,0 +1,67 @@ +package com.viam.sdk.core.component.sensor; + +import com.google.protobuf.Struct; +import com.google.protobuf.Value; +import com.viam.common.v1.Common; +import com.viam.common.v1.Common.Geometry; +import com.viam.common.v1.Common.GetGeometriesResponse; +import com.viam.common.v1.Common.GetReadingsRequest; +import com.viam.common.v1.Common.GetReadingsResponse; +import com.viam.common.v1.Common.Orientation; +import com.viam.common.v1.Common.Vector3; +import com.viam.component.movementsensor.v1.MovementSensorServiceGrpc; +import com.viam.component.movementsensor.v1.Movementsensor.GetAccuracyRequest; +import com.viam.component.movementsensor.v1.Movementsensor.GetAccuracyResponse; +import com.viam.component.movementsensor.v1.Movementsensor.GetAngularVelocityRequest; +import com.viam.component.movementsensor.v1.Movementsensor.GetAngularVelocityResponse; +import com.viam.component.movementsensor.v1.Movementsensor.GetCompassHeadingRequest; +import com.viam.component.movementsensor.v1.Movementsensor.GetCompassHeadingResponse; +import com.viam.component.movementsensor.v1.Movementsensor.GetLinearAccelerationRequest; +import com.viam.component.movementsensor.v1.Movementsensor.GetLinearAccelerationResponse; +import com.viam.component.movementsensor.v1.Movementsensor.GetLinearVelocityRequest; +import com.viam.component.movementsensor.v1.Movementsensor.GetLinearVelocityResponse; +import com.viam.component.movementsensor.v1.Movementsensor.GetOrientationRequest; +import com.viam.component.movementsensor.v1.Movementsensor.GetOrientationResponse; +import com.viam.component.movementsensor.v1.Movementsensor.GetPositionRequest; +import com.viam.component.movementsensor.v1.Movementsensor.GetPositionResponse; +import com.viam.component.movementsensor.v1.Movementsensor.GetPropertiesRequest; +import com.viam.component.movementsensor.v1.Movementsensor.GetPropertiesResponse; +import com.viam.component.sensor.v1.SensorServiceGrpc; +import com.viam.sdk.core.resource.ResourceManager; +import com.viam.sdk.core.resource.ResourceRPCService; +import io.grpc.stub.StreamObserver; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +public class SensorRPCService extends SensorServiceGrpc.SensorServiceImplBase + implements ResourceRPCService { + + private final ResourceManager manager; + + public SensorRPCService(final ResourceManager manager) { + this.manager = manager; + } + + @Override + public void getReadings(GetReadingsRequest request, + StreamObserver responseObserver) { + final com.viam.sdk.core.component.sensor.Sensor sensor = getResource( + com.viam.sdk.core.component.sensor.Sensor.named(request.getName()) + ); + final GetReadingsResponse result = sensor.getReadings(Optional.of(request.getExtra())); + responseObserver.onNext(GetReadingsResponse.newBuilder().putAllReadings(result.getReadingsMap()).build()); + responseObserver.onCompleted(); + responseObserver.onCompleted(); + } + + @Override + public Class getResourceClass() { + return Sensor.class; + } + + @Override + public ResourceManager getManager() { + return manager; + } +} diff --git a/core/sdk/src/main/java/com/viam/sdk/core/resource/ResourceManager.java b/core/sdk/src/main/java/com/viam/sdk/core/resource/ResourceManager.java index d96d0d459..e0ff5072f 100644 --- a/core/sdk/src/main/java/com/viam/sdk/core/resource/ResourceManager.java +++ b/core/sdk/src/main/java/com/viam/sdk/core/resource/ResourceManager.java @@ -5,6 +5,7 @@ import com.viam.component.generic.v1.GenericServiceGrpc; import com.viam.component.gripper.v1.GripperServiceGrpc; import com.viam.component.movementsensor.v1.MovementSensorServiceGrpc; +import com.viam.component.sensor.v1.SensorServiceGrpc; import com.viam.sdk.core.component.generic.Generic; import com.viam.sdk.core.component.generic.GenericRPCClient; import com.viam.sdk.core.component.generic.GenericRPCService; @@ -14,6 +15,9 @@ import com.viam.sdk.core.component.movementsensor.MovementSensor; import com.viam.sdk.core.component.movementsensor.MovementSensorRPCClient; import com.viam.sdk.core.component.movementsensor.MovementSensorRPCService; +import com.viam.sdk.core.component.sensor.Sensor; +import com.viam.sdk.core.component.sensor.SensorRPCClient; +import com.viam.sdk.core.component.sensor.SensorRPCService; import com.viam.sdk.core.exception.DuplicateResourceException; import com.viam.sdk.core.exception.ResourceNotFoundException; import com.viam.sdk.core.service.datamanager.DataManager; @@ -163,7 +167,12 @@ public synchronized void close() { MovementSensorRPCService::new, MovementSensorRPCClient::new )); - + Registry.registerSubtype(new ResourceRegistration<>( + Sensor.SUBTYPE, + SensorServiceGrpc.SERVICE_NAME, + SensorRPCService::new, + SensorRPCClient::new + )); Registry.registerSubtype(new ResourceRegistration<>( Sensors.SUBTYPE, SensorsServiceGrpc.SERVICE_NAME,