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

Convert sensor reading values to types #294

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion lib/src/components/movement_sensor/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MovementSensorClient extends MovementSensor implements ResourceRPCClient {
..name = name
..extra = extra?.toStruct() ?? Struct();
final response = await client.getReadings(request);
return response.readings.map((key, value) => MapEntry(key, value.toPrimitive()));
return response.toPrimitive();
}

@override
Expand Down
2 changes: 1 addition & 1 deletion lib/src/components/sensor/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SensorClient extends Sensor implements ResourceRPCClient {
..name = name
..extra = extra?.toStruct() ?? Struct();
final response = await client.getReadings(request);
return response.readings.map((key, value) => MapEntry(key, value.toPrimitive()));
return response.toPrimitive();
}

@override
Expand Down
24 changes: 24 additions & 0 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'package:fixnum/fixnum.dart';
import 'package:grpc/grpc.dart';
import 'package:logger/logger.dart';

import 'gen/app/v1/robot.pb.dart';
import 'gen/common/v1/common.pb.dart';
import 'gen/google/protobuf/duration.pb.dart' as grpc_duration;
import 'gen/google/protobuf/struct.pb.dart';

Expand Down Expand Up @@ -92,6 +94,28 @@ grpc_duration.Duration durationToProto(Duration duration) {
..nanos = micros * 1000;
}

extension GetReadingsResponseUtils on GetReadingsResponse {
Map<String, dynamic> toPrimitive() {
return readings.map((key, value) => MapEntry(key, value.toPrimitive())).map((key, value) {
if (value is Map<String, dynamic> && value.keys.contains('_type')) {
dynamic primValue;
switch (value['_type']) {
case 'euler':
primValue = Orientation_EulerAngles(roll: value['roll'], pitch: value['pitch'], yaw: value['yaw']);
case 'vector3':
primValue = Vector3(x: value['x'], y: value['y'], z: value['z']);
case 'angular_velocity':
primValue = Vector3(x: value['x'], y: value['y'], z: value['z']);
njooma marked this conversation as resolved.
Show resolved Hide resolved
case 'geopoint':
primValue = GeoPoint(latitude: value['lat'], longitude: value['lng']);
}
return MapEntry(key, primValue);
}
return MapEntry(key, value);
});
}
}

String getVersionMetadata() {
const String sdkVersion = 'v0.0.26';
const String apiTag = 'v0.1.360';
Expand Down
21 changes: 21 additions & 0 deletions test/unit_test/utils/utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import 'package:flutter_test/flutter_test.dart';
import 'package:grpc/grpc_connection_interface.dart';
import 'package:viam_sdk/protos/app/robot.dart';
import 'package:viam_sdk/protos/common/common.dart';
import 'package:viam_sdk/src/gen/google/protobuf/struct.pb.dart';
import 'package:viam_sdk/src/utils.dart';

Expand Down Expand Up @@ -198,5 +200,24 @@ void main() {
}
});
});

group('GetReadingsResponseUtils', () {
test('toPrimitive', () {
final input = {
'velocity': Value(structValue: {'_type': 'vector3', 'x': 1, 'y': 2, 'z': 3}.toStruct()),
'geopoint': Value(structValue: {'_type': 'geopoint', 'lat': 12.3, 'lng': 45.6}.toStruct()),
'angular_velocity': Value(structValue: {'_type': 'angular_velocity', 'x': 1, 'y': 2, 'z': 3}.toStruct()),
'euler': Value(structValue: {'_type': 'euler', 'roll': 1, 'pitch': 2, 'yaw': 3}.toStruct()),
'no_change': Value(numberValue: 182),
};
final response = GetReadingsResponse(readings: input);
final output = response.toPrimitive();
expect(output['velocity'], Vector3(x: 1, y: 2, z: 3));
expect(output['geopoint'], GeoPoint(latitude: 12.3, longitude: 45.6));
expect(output['angular_velocity'], Vector3(x: 1, y: 2, z: 3));
expect(output['euler'], Orientation_EulerAngles(roll: 1, pitch: 2, yaw: 3));
expect(output['no_change'], 182);
});
});
});
}
Loading