Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
gloriacai01 committed Sep 13, 2024
2 parents d67bb9c + 698114b commit e3af17b
Show file tree
Hide file tree
Showing 34 changed files with 3,143 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.viam.sdk.core.component.powersensor;

import com.google.protobuf.Struct;
import com.viam.common.v1.Common;
import com.viam.sdk.core.component.Component;
import com.viam.sdk.core.resource.Resource;
import com.viam.sdk.core.resource.Subtype;
import com.viam.sdk.core.robot.RobotClient;

import java.util.Map.Entry;


import java.util.Map;
import java.util.Optional;

/**
* PowerSensor reports information about voltage, current and power.
*/
public abstract class PowerSensor extends Component {
public static final Subtype SUBTYPE = new Subtype(
Subtype.NAMESPACE_RDK,
Subtype.RESOURCE_TYPE_COMPONENT,
"power_sensor");

public PowerSensor(final String name) {
super(SUBTYPE, named(name));
}

/**
* Get the ResourceName of the component
*
* @param name the name of the component
* @return the component's ResourceName
*/
public static Common.ResourceName named(final String name) {
return Resource.named(SUBTYPE, name);
}

/**
* Get the component with the provided name from the provided robot.
*
* @param robot the RobotClient
* @param name the name of the component
* @return the component
*/
public static PowerSensor fromRobot(final RobotClient robot, final String name) {
return robot.getResource(PowerSensor.class, named(name));
}

/**
* Return the voltage reading of a specified device and whether it is AC or DC.
*
* @return the pair where the first double representing the voltage reading in V, the second bool indicating whether the voltage is AC (`true`) or DC
* (`false`).
*/
public abstract Entry<Double, Boolean> getVoltage(Optional<Struct> extra);

/**
* Return the current of a specified device and whether it is AC or DC.
*
* @return the pair where the first double representing the current reading in V, the second bool indicating whether the current is AC (`true`) or DC
* * (`false`).
*/
public abstract Entry<Double, Boolean> getCurrent(Optional<Struct> extra);

/**
* Return the power reading in watts.
*
* @return the power reading in watts
*/
public abstract double getPower(Optional<Struct> extra);


/**
* Get the measurements or readings that this power sensor provides. If a sensor is not configured correctly or fails to read a
* piece of data, it will not appear in the readings dictionary.
*
* @return The readings for the PowerSensor. Object should be of type Vector3, GeoPoint, Orientation, or any Value type. Includes voltage in volts (float), current in
* amperes (float), is_ac (bool), and power in watts (float).
*/
public abstract Map<String, Object> getReadings(Optional<Struct> extra);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.viam.sdk.core.component.powersensor;

import com.google.protobuf.Struct;
import com.google.protobuf.Value;
import com.viam.common.v1.Common;
import com.viam.component.powersensor.v1.PowerSensorServiceGrpc;
import com.viam.component.powersensor.v1.Powersensor;
import com.viam.sdk.core.rpc.Channel;
import com.viam.sdk.core.util.Utils;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;

public class PowerSensorRPCClient extends PowerSensor {
private final PowerSensorServiceGrpc.PowerSensorServiceBlockingStub client;

public PowerSensorRPCClient(final String name, final Channel chan) {
super(name);
final PowerSensorServiceGrpc.PowerSensorServiceBlockingStub client = PowerSensorServiceGrpc.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 Entry<Double, Boolean> getVoltage(Optional<Struct> extra) {
Powersensor.GetVoltageRequest.Builder builder = com.viam.component.powersensor.v1.Powersensor.GetVoltageRequest.newBuilder().setName(this.getName().getName());
extra.ifPresent(builder::setExtra);
Powersensor.GetVoltageResponse response = client.getVoltage(builder.build());
return Map.entry(response.getVolts(), response.getIsAc());
}

@Override
public Map.Entry<Double, Boolean> getCurrent(Optional<Struct> extra) {
Powersensor.GetCurrentRequest.Builder builder = com.viam.component.powersensor.v1.Powersensor.GetCurrentRequest.newBuilder().setName(this.getName().getName());
extra.ifPresent(builder::setExtra);
Powersensor.GetCurrentResponse response = client.getCurrent(builder.build());
return Map.entry(response.getAmperes(), response.getIsAc());
}

@Override
public double getPower(Optional<Struct> extra) {
Powersensor.GetPowerRequest.Builder builder = com.viam.component.powersensor.v1.Powersensor.GetPowerRequest.newBuilder().setName(this.getName().getName());
extra.ifPresent(builder::setExtra);
return client.getPower(builder.build()).getWatts();
}

@Override
public Map<String, Object> getReadings(Optional<Struct> extra) {
Common.GetReadingsRequest.Builder builder = Common.GetReadingsRequest.newBuilder().setName(this.getName().getName());
extra.ifPresent(builder::setExtra);
return Utils.sensorReadingsValueToNative(client.getReadings(builder.build()).getReadingsMap());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.viam.sdk.core.component.powersensor;

import com.google.protobuf.Struct;
import com.google.protobuf.Value;
import com.viam.common.v1.Common;
import com.viam.component.powersensor.v1.PowerSensorServiceGrpc;
import com.viam.component.powersensor.v1.Powersensor;
import com.viam.sdk.core.resource.ResourceManager;
import com.viam.sdk.core.resource.ResourceRPCService;
import com.viam.sdk.core.util.Utils;
import io.grpc.stub.StreamObserver;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;

public class PowerSensorRPCService extends PowerSensorServiceGrpc.PowerSensorServiceImplBase implements ResourceRPCService<PowerSensor> {

private final ResourceManager manager;

public PowerSensorRPCService(ResourceManager manager) {
this.manager = manager;
}

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

@Override
public ResourceManager getManager() {
return manager;
}


@Override
public void getVoltage(Powersensor.GetVoltageRequest request, StreamObserver<Powersensor.GetVoltageResponse> responseObserver) {
PowerSensor powerSensor = getResource(PowerSensor.named(request.getName()));
Entry<Double, Boolean> voltage = powerSensor.getVoltage(Optional.of(request.getExtra()));
responseObserver.onNext(com.viam.component.powersensor.v1.Powersensor.GetVoltageResponse.newBuilder().setVolts(voltage.getKey()).setIsAc(voltage.getValue()).build());
responseObserver.onCompleted();
}

@Override
public void getCurrent(Powersensor.GetCurrentRequest request, StreamObserver<Powersensor.GetCurrentResponse> responseObserver) {
PowerSensor powerSensor = getResource(PowerSensor.named(request.getName()));
Entry<Double, Boolean> current = powerSensor.getCurrent(Optional.of(request.getExtra()));
responseObserver.onNext(com.viam.component.powersensor.v1.Powersensor.GetCurrentResponse.newBuilder().setAmperes(current.getKey()).setIsAc(current.getValue()).build());
responseObserver.onCompleted();
}

@Override
public void getPower(Powersensor.GetPowerRequest request, StreamObserver<Powersensor.GetPowerResponse> responseObserver) {
PowerSensor powerSensor = getResource(PowerSensor.named(request.getName()));
double power = powerSensor.getPower(Optional.of(request.getExtra()));
responseObserver.onNext(com.viam.component.powersensor.v1.Powersensor.GetPowerResponse.newBuilder().setWatts(power).build());
responseObserver.onCompleted();
}

@Override
public void getReadings(Common.GetReadingsRequest request, StreamObserver<Common.GetReadingsResponse> responseObserver) {
PowerSensor powerSensor = getResource(PowerSensor.named(request.getName()));
Map<String, Value> readings = Utils.sensorReadingsNativeToValue(powerSensor.getReadings(Optional.of(request.getExtra())));
responseObserver.onNext(Common.GetReadingsResponse.newBuilder().putAllReadings(readings).build());
responseObserver.onCompleted();
}
@Override
public void doCommand(Common.DoCommandRequest request,
StreamObserver<Common.DoCommandResponse> responseObserver) {

PowerSensor powerSensor = getResource(PowerSensor.named(request.getName()));
Struct result = powerSensor.doCommand(request.getCommand().getFieldsMap());
responseObserver.onNext(Common.DoCommandResponse.newBuilder().setResult(result).build());
responseObserver.onCompleted();
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ public Sensor(final String name) {
super(SUBTYPE, named(name));
}

public Sensor(Subtype subtype, ResourceName name) {
super(subtype, name);
throw new UnsupportedOperationException();
}

/**
* Get the ResourceName of the component
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.viam.sdk.core.component.servo;

import com.google.protobuf.Struct;
import com.viam.common.v1.Common;
import com.viam.sdk.core.component.Component;
import com.viam.sdk.core.resource.Resource;
import com.viam.sdk.core.resource.Subtype;
import com.viam.sdk.core.robot.RobotClient;


/**
* Servo represents a physical servo
*/
public abstract class Servo extends Component {
public static final Subtype SUBTYPE = new Subtype(
Subtype.NAMESPACE_RDK,
Subtype.RESOURCE_TYPE_COMPONENT,
"servo");

public Servo(final String name) {
super(SUBTYPE, named(name));
}

/**
* Get the ResourceName of the component
*
* @param name the name of the component
* @return the component's ResourceName
*/
public static Common.ResourceName named(final String name) {
return Resource.named(SUBTYPE, name);
}


/**
* Get the component with the provided name from the provided robot.
* @param robot the RobotClient
* @param name the name of the component
* @return the component
*/
public static Servo fromRobot(final RobotClient robot, final String name) {
return robot.getResource(Servo.class, named(name));
}

/**
* Move the servo to the provided angle.
* @param angle the desired angle of the servo in degrees
*/
public abstract void move(int angle, Struct extra);

/**
* Move the servo to the provided angle.
* @param angle the desired angle of the servo in degrees
*/
public void move(int angle){
move(angle, Struct.getDefaultInstance());
}

/**
* Get the current angle (degrees) of the servo.
* @return the current angle of the servo in degrees
*/
public abstract int getPosition(Struct extra);

/**
* Get the current angle (degrees) of the servo.
* @return the current angle of the servo in degrees
*/
public int getPosition(){
return getPosition(Struct.getDefaultInstance());
}

/**
* Stop the servo. It is assumed that the servo stops immediately.
*/
public abstract void stop(Struct extra);

/**
* Stop the servo. It is assumed that the servo stops immediately.
*/
public void stop(){
stop(Struct.getDefaultInstance());
}


/**
* Get if the servo is currently moving.
* @return whether the servo is moving
*/
public abstract boolean isMoving();


}

Loading

0 comments on commit e3af17b

Please sign in to comment.