Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sensor rpc #12

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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<Struct> extra);
}
Original file line number Diff line number Diff line change
@@ -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<String, Value> command) {
return client.doCommand(Common.DoCommandRequest.newBuilder().
setName(getName().getName()).
setCommand(Struct.newBuilder().putAllFields(command).build()).
build()).getResult();
}

@Override
public GetReadingsResponse getReadings(final Optional<Struct> extra) {
final GetReadingsRequest.Builder builder = GetReadingsRequest.newBuilder().
setName(getName().getName());
extra.ifPresent(builder::setExtra);
return client.getReadings(builder.build());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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<Sensor> {

private final ResourceManager manager;

public SensorRPCService(final ResourceManager manager) {
this.manager = manager;
}

@Override
public void getReadings(GetReadingsRequest request,
StreamObserver<GetReadingsResponse> 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();
}

@Override
public Class<Sensor> getResourceClass() {
return Sensor.class;
}

@Override
public ResourceManager getManager() {
return manager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down