Skip to content

Commit

Permalink
RSDK-4791: Add Readings to Flutter SDK (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
martha-johnston authored Oct 19, 2023
1 parent ec21efb commit 5ba9602
Show file tree
Hide file tree
Showing 16 changed files with 247 additions and 266 deletions.
9 changes: 9 additions & 0 deletions lib/src/components/movement_sensor/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ class MovementSensorClient extends MovementSensor implements ResourceRPCClient {

MovementSensorClient(this.name, this.channel);

@override
Future<Map<String, dynamic>> readings({Map<String, dynamic>? extra}) async {
final request = GetReadingsRequest()
..name = name
..extra = extra?.toStruct() ?? Struct();
final response = await client.getReadings(request);
return response.readings.map((key, value) => MapEntry(key, value.toPrimitive()));
}

@override
Future<Position> position({Map<String, dynamic>? extra}) async {
final request = GetPositionRequest()
Expand Down
66 changes: 5 additions & 61 deletions lib/src/components/movement_sensor/movement_sensor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ class Position {
typedef Properties = GetPropertiesResponse;

/// MovementSensor reports information about the robot's direction, position and speed.
abstract class MovementSensor extends Sensor {
abstract class MovementSensor extends Resource {
static const Subtype subtype = Subtype(resourceNamespaceRDK, resourceTypeComponent, 'movement_sensor');

/// Obtain the measurements/data specific to this [MovementSensor]
/// If a sensor is not configured to have a measurement or fails to read a piece of data, it will not appear in the readings dictionary.
Future<Map<String, dynamic>> readings({Map<String, dynamic>? extra});

/// Get the current [GeoPoint] (latitude, longitude) and altitude (mm)
Future<Position> position({Map<String, dynamic>? extra});

Expand All @@ -43,66 +47,6 @@ abstract class MovementSensor extends Sensor {
/// Get the accuracy of the various sensors
Future<Map<String, double>> accuracy({Map<String, dynamic>? extra});

/// Obtain the measurements/data specific to this [MovementSensor].
/// If a sensor is not configured to have a measurement or fails to read a piece of data, it will not appear in the readings dictionary.
/// The returns dictionary contains the following readings and values:
/// position: [GeoPoint],
/// altitude: [double],
/// linear_velocity: [Vector3],
/// angular_velocity: [Vector3],
/// linear_acceleration: [Vector3],
/// compass: [double],
/// orientation: [Orientation],
@override
Future<Map<String, dynamic>> readings({Map<String, dynamic>? extra}) async {
final Map<String, dynamic> readings = {};
try {
final Position pos = await position(extra: extra);
readings['position'] = pos.coordinates;
readings['altitude'] = pos.altitude;
} catch (exception) {
// TODO: Check if the exception is of a specific type and ignore or rethrow
Logger().e(exception);
}

try {
final lv = await linearVelocity(extra: extra);
readings['linear_velocity'] = lv;
} catch (exception) {
// TODO: Check if the exception is of a specific type and ignore or rethrow
}

try {
final av = await angularVelocity(extra: extra);
readings['angular_velocity'] = av;
} catch (exception) {
// TODO: Check if the exception is of a specific type and ignore or rethrow
}

try {
final la = await linearAcceleration(extra: extra);
readings['linear_acceleration'] = la;
} catch (exception) {
// TODO: Check if the exception is of a specific type and ignore or rethrow
}

try {
final comp = await compassHeading(extra: extra);
readings['compass'] = comp;
} catch (exception) {
// TODO: Check if the exception is of a specific type and ignore or rethrow
}

try {
final orient = await orientation(extra: extra);
readings['orientation'] = orient;
} catch (exception) {
// TODO: Check if the exception is of a specific type and ignore or rethrow
}

return readings;
}

/// Get the [ResourceName] for this [MovementSensor] with the given [name]
static ResourceName getResourceName(String name) {
return MovementSensor.subtype.getResourceName(name);
Expand Down
7 changes: 7 additions & 0 deletions lib/src/components/movement_sensor/service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class MovementSensorService extends MovementSensorServiceBase {
}
}

@override
Future<GetReadingsResponse> getReadings(ServiceCall call, GetReadingsRequest request) async {
final movementSensor = _fromManager(request.name);
final result = await movementSensor.readings(extra: request.extra.toMap());
return GetReadingsResponse()..readings.addAll(result.toStruct().fields);
}

@override
Future<DoCommandResponse> doCommand(ServiceCall call, DoCommandRequest request) async {
final movementSensor = _fromManager(request.name);
Expand Down
9 changes: 9 additions & 0 deletions lib/src/components/power_sensor/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ class PowerSensorClient extends PowerSensor implements ResourceRPCClient {

PowerSensorClient(this.name, this.channel);

@override
Future<Map<String, dynamic>> readings({Map<String, dynamic>? extra}) async {
final request = GetReadingsRequest()
..name = name
..extra = extra?.toStruct() ?? Struct();
final response = await client.getReadings(request);
return response.readings.map((key, value) => MapEntry(key, value.toPrimitive()));
}

@override
Future<Voltage> voltage({Map<String, dynamic>? extra}) async {
final request = GetVoltageRequest()
Expand Down
39 changes: 5 additions & 34 deletions lib/src/components/power_sensor/power_sensor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ typedef Voltage = GetVoltageResponse;
typedef Current = GetCurrentResponse;

/// PowerSensor reports information about voltage, current, and power.
abstract class PowerSensor extends Sensor {
abstract class PowerSensor extends Resource {
static const Subtype subtype = Subtype(resourceNamespaceRDK, resourceTypeComponent, 'power_sensor');

/// Obtain the measurements/data specific to this [Sensor]
/// If a sensor is not configured to have a measurement or fails to read a piece of data, it will not appear in the readings dictionary.
Future<Map<String, dynamic>> readings({Map<String, dynamic>? extra});

/// Get the voltage (volts) and boolean isAC
Future<Voltage> voltage({Map<String, dynamic>? extra});

Expand All @@ -20,39 +24,6 @@ abstract class PowerSensor extends Sensor {
/// Get the power (watts)
Future<double> power({Map<String, dynamic>? extra});

/// Obtain the measurements/data specific to this [PowerSensor].
/// If a sensor is not configured to have a measurement or fails to read a piece of data, it will not appear in the readings dictionary.
/// The returns dictionary contains the following readings and values:
/// voltage: [double],
/// current: [double],
/// is_ac: [bool],
/// power: [double],
@override
Future<Map<String, dynamic>> readings({Map<String, dynamic>? extra}) async {
final Map<String, dynamic> readings = {};
try {
final vol = await voltage(extra: extra);
readings['voltage'] = vol.volts;
readings['is_ac'] = vol.isAc;
} catch (exception) {
// TODO: Check if the exception is of a specific type and ignore or rethrow
}
try {
final cur = await current(extra: extra);
readings['current'] = cur.amperes;
readings['is_ac'] = cur.isAc;
} catch (exception) {
// TODO: Check if the exception is of a specific type and ignore or rethrow;
}
try {
final pow = await power(extra: extra);
readings['power'] = pow;
} catch (exception) {
// TODO: Check if the exception is of a specific type and ignore or rethrow
}
return readings;
}

/// Get the [ResourceName] for this [PowerSensor] with the given [name]
static ResourceName getResourceName(String name) {
return PowerSensor.subtype.getResourceName(name);
Expand Down
7 changes: 7 additions & 0 deletions lib/src/components/power_sensor/service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class PowerSensorService extends PowerSensorServiceBase {
}
}

@override
Future<GetReadingsResponse> getReadings(ServiceCall call, GetReadingsRequest request) async {
final sensor = _fromManager(request.name);
final result = await sensor.readings(extra: request.extra.toMap());
return GetReadingsResponse()..readings.addAll(result.toStruct().fields);
}

@override
Future<DoCommandResponse> doCommand(ServiceCall call, DoCommandRequest request) async {
final powerSensor = _fromManager(request.name);
Expand Down
90 changes: 90 additions & 0 deletions lib/src/gen/common/v1/common.pb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,96 @@ class GetGeometriesResponse extends $pb.GeneratedMessage {
$core.List<Geometry> get geometries => $_getList(0);
}

class GetReadingsRequest extends $pb.GeneratedMessage {
factory GetReadingsRequest() => create();
GetReadingsRequest._() : super();
factory GetReadingsRequest.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
factory GetReadingsRequest.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);

static final $pb.BuilderInfo _i = $pb.BuilderInfo(_omitMessageNames ? '' : 'GetReadingsRequest', package: const $pb.PackageName(_omitMessageNames ? '' : 'viam.common.v1'), createEmptyInstance: create)
..aOS(1, _omitFieldNames ? '' : 'name')
..aOM<$1.Struct>(99, _omitFieldNames ? '' : 'extra', subBuilder: $1.Struct.create)
..hasRequiredFields = false
;

@$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.deepCopy] instead. '
'Will be removed in next major version')
GetReadingsRequest clone() => GetReadingsRequest()..mergeFromMessage(this);
@$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.rebuild] instead. '
'Will be removed in next major version')
GetReadingsRequest copyWith(void Function(GetReadingsRequest) updates) => super.copyWith((message) => updates(message as GetReadingsRequest)) as GetReadingsRequest;

$pb.BuilderInfo get info_ => _i;

@$core.pragma('dart2js:noInline')
static GetReadingsRequest create() => GetReadingsRequest._();
GetReadingsRequest createEmptyInstance() => create();
static $pb.PbList<GetReadingsRequest> createRepeated() => $pb.PbList<GetReadingsRequest>();
@$core.pragma('dart2js:noInline')
static GetReadingsRequest getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor<GetReadingsRequest>(create);
static GetReadingsRequest? _defaultInstance;

@$pb.TagNumber(1)
$core.String get name => $_getSZ(0);
@$pb.TagNumber(1)
set name($core.String v) { $_setString(0, v); }
@$pb.TagNumber(1)
$core.bool hasName() => $_has(0);
@$pb.TagNumber(1)
void clearName() => clearField(1);

@$pb.TagNumber(99)
$1.Struct get extra => $_getN(1);
@$pb.TagNumber(99)
set extra($1.Struct v) { setField(99, v); }
@$pb.TagNumber(99)
$core.bool hasExtra() => $_has(1);
@$pb.TagNumber(99)
void clearExtra() => clearField(99);
@$pb.TagNumber(99)
$1.Struct ensureExtra() => $_ensure(1);
}

class GetReadingsResponse extends $pb.GeneratedMessage {
factory GetReadingsResponse() => create();
GetReadingsResponse._() : super();
factory GetReadingsResponse.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
factory GetReadingsResponse.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);

static final $pb.BuilderInfo _i = $pb.BuilderInfo(_omitMessageNames ? '' : 'GetReadingsResponse', package: const $pb.PackageName(_omitMessageNames ? '' : 'viam.common.v1'), createEmptyInstance: create)
..m<$core.String, $1.Value>(1, _omitFieldNames ? '' : 'readings', entryClassName: 'GetReadingsResponse.ReadingsEntry', keyFieldType: $pb.PbFieldType.OS, valueFieldType: $pb.PbFieldType.OM, valueCreator: $1.Value.create, valueDefaultOrMaker: $1.Value.getDefault, packageName: const $pb.PackageName('viam.common.v1'))
..hasRequiredFields = false
;

@$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.deepCopy] instead. '
'Will be removed in next major version')
GetReadingsResponse clone() => GetReadingsResponse()..mergeFromMessage(this);
@$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.rebuild] instead. '
'Will be removed in next major version')
GetReadingsResponse copyWith(void Function(GetReadingsResponse) updates) => super.copyWith((message) => updates(message as GetReadingsResponse)) as GetReadingsResponse;

$pb.BuilderInfo get info_ => _i;

@$core.pragma('dart2js:noInline')
static GetReadingsResponse create() => GetReadingsResponse._();
GetReadingsResponse createEmptyInstance() => create();
static $pb.PbList<GetReadingsResponse> createRepeated() => $pb.PbList<GetReadingsResponse>();
@$core.pragma('dart2js:noInline')
static GetReadingsResponse getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor<GetReadingsResponse>(create);
static GetReadingsResponse? _defaultInstance;

@$pb.TagNumber(1)
$core.Map<$core.String, $1.Value> get readings => $_getMap(0);
}

class Common {
static final safetyHeartbeatMonitored = $pb.Extension<$core.bool>(_omitMessageNames ? '' : 'google.protobuf.MethodOptions', _omitFieldNames ? '' : 'safetyHeartbeatMonitored', 84260, $pb.PbFieldType.OB);
static void registerAllExtensions($pb.ExtensionRegistry registry) {
Expand Down
40 changes: 40 additions & 0 deletions lib/src/gen/common/v1/common.pbjson.dart
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,43 @@ final $typed_data.Uint8List getGeometriesResponseDescriptor = $convert.base64Dec
'ChVHZXRHZW9tZXRyaWVzUmVzcG9uc2USOAoKZ2VvbWV0cmllcxgBIAMoCzIYLnZpYW0uY29tbW'
'9uLnYxLkdlb21ldHJ5UgpnZW9tZXRyaWVz');

@$core.Deprecated('Use getReadingsRequestDescriptor instead')
const GetReadingsRequest$json = {
'1': 'GetReadingsRequest',
'2': [
{'1': 'name', '3': 1, '4': 1, '5': 9, '10': 'name'},
{'1': 'extra', '3': 99, '4': 1, '5': 11, '6': '.google.protobuf.Struct', '10': 'extra'},
],
};

/// Descriptor for `GetReadingsRequest`. Decode as a `google.protobuf.DescriptorProto`.
final $typed_data.Uint8List getReadingsRequestDescriptor = $convert.base64Decode(
'ChJHZXRSZWFkaW5nc1JlcXVlc3QSEgoEbmFtZRgBIAEoCVIEbmFtZRItCgVleHRyYRhjIAEoCz'
'IXLmdvb2dsZS5wcm90b2J1Zi5TdHJ1Y3RSBWV4dHJh');

@$core.Deprecated('Use getReadingsResponseDescriptor instead')
const GetReadingsResponse$json = {
'1': 'GetReadingsResponse',
'2': [
{'1': 'readings', '3': 1, '4': 3, '5': 11, '6': '.viam.common.v1.GetReadingsResponse.ReadingsEntry', '10': 'readings'},
],
'3': [GetReadingsResponse_ReadingsEntry$json],
};

@$core.Deprecated('Use getReadingsResponseDescriptor instead')
const GetReadingsResponse_ReadingsEntry$json = {
'1': 'ReadingsEntry',
'2': [
{'1': 'key', '3': 1, '4': 1, '5': 9, '10': 'key'},
{'1': 'value', '3': 2, '4': 1, '5': 11, '6': '.google.protobuf.Value', '10': 'value'},
],
'7': {'7': true},
};

/// Descriptor for `GetReadingsResponse`. Decode as a `google.protobuf.DescriptorProto`.
final $typed_data.Uint8List getReadingsResponseDescriptor = $convert.base64Decode(
'ChNHZXRSZWFkaW5nc1Jlc3BvbnNlEk0KCHJlYWRpbmdzGAEgAygLMjEudmlhbS5jb21tb24udj'
'EuR2V0UmVhZGluZ3NSZXNwb25zZS5SZWFkaW5nc0VudHJ5UghyZWFkaW5ncxpTCg1SZWFkaW5n'
'c0VudHJ5EhAKA2tleRgBIAEoCVIDa2V5EiwKBXZhbHVlGAIgASgLMhYuZ29vZ2xlLnByb3RvYn'
'VmLlZhbHVlUgV2YWx1ZToCOAE=');

20 changes: 20 additions & 0 deletions lib/src/gen/component/movementsensor/v1/movementsensor.pbgrpc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class MovementSensorServiceClient extends $grpc.Client {
'/viam.component.movementsensor.v1.MovementSensorService/GetGeometries',
($1.GetGeometriesRequest value) => value.writeToBuffer(),
($core.List<$core.int> value) => $1.GetGeometriesResponse.fromBuffer(value));
static final _$getReadings = $grpc.ClientMethod<$1.GetReadingsRequest, $1.GetReadingsResponse>(
'/viam.component.movementsensor.v1.MovementSensorService/GetReadings',
($1.GetReadingsRequest value) => value.writeToBuffer(),
($core.List<$core.int> value) => $1.GetReadingsResponse.fromBuffer(value));

MovementSensorServiceClient($grpc.ClientChannel channel,
{$grpc.CallOptions? options,
Expand Down Expand Up @@ -108,6 +112,10 @@ class MovementSensorServiceClient extends $grpc.Client {
$grpc.ResponseFuture<$1.GetGeometriesResponse> getGeometries($1.GetGeometriesRequest request, {$grpc.CallOptions? options}) {
return $createUnaryCall(_$getGeometries, request, options: options);
}

$grpc.ResponseFuture<$1.GetReadingsResponse> getReadings($1.GetReadingsRequest request, {$grpc.CallOptions? options}) {
return $createUnaryCall(_$getReadings, request, options: options);
}
}

@$pb.GrpcServiceName('viam.component.movementsensor.v1.MovementSensorService')
Expand Down Expand Up @@ -185,6 +193,13 @@ abstract class MovementSensorServiceBase extends $grpc.Service {
false,
($core.List<$core.int> value) => $1.GetGeometriesRequest.fromBuffer(value),
($1.GetGeometriesResponse value) => value.writeToBuffer()));
$addMethod($grpc.ServiceMethod<$1.GetReadingsRequest, $1.GetReadingsResponse>(
'GetReadings',
getReadings_Pre,
false,
false,
($core.List<$core.int> value) => $1.GetReadingsRequest.fromBuffer(value),
($1.GetReadingsResponse value) => value.writeToBuffer()));
}

$async.Future<$0.GetLinearVelocityResponse> getLinearVelocity_Pre($grpc.ServiceCall call, $async.Future<$0.GetLinearVelocityRequest> request) async {
Expand Down Expand Up @@ -227,6 +242,10 @@ abstract class MovementSensorServiceBase extends $grpc.Service {
return getGeometries(call, await request);
}

$async.Future<$1.GetReadingsResponse> getReadings_Pre($grpc.ServiceCall call, $async.Future<$1.GetReadingsRequest> request) async {
return getReadings(call, await request);
}

$async.Future<$0.GetLinearVelocityResponse> getLinearVelocity($grpc.ServiceCall call, $0.GetLinearVelocityRequest request);
$async.Future<$0.GetAngularVelocityResponse> getAngularVelocity($grpc.ServiceCall call, $0.GetAngularVelocityRequest request);
$async.Future<$0.GetCompassHeadingResponse> getCompassHeading($grpc.ServiceCall call, $0.GetCompassHeadingRequest request);
Expand All @@ -237,4 +256,5 @@ abstract class MovementSensorServiceBase extends $grpc.Service {
$async.Future<$0.GetLinearAccelerationResponse> getLinearAcceleration($grpc.ServiceCall call, $0.GetLinearAccelerationRequest request);
$async.Future<$1.DoCommandResponse> doCommand($grpc.ServiceCall call, $1.DoCommandRequest request);
$async.Future<$1.GetGeometriesResponse> getGeometries($grpc.ServiceCall call, $1.GetGeometriesRequest request);
$async.Future<$1.GetReadingsResponse> getReadings($grpc.ServiceCall call, $1.GetReadingsRequest request);
}
Loading

0 comments on commit 5ba9602

Please sign in to comment.