diff --git a/lib/src/components/movement_sensor/client.dart b/lib/src/components/movement_sensor/client.dart index ad1786cff9..c61dfe03e5 100644 --- a/lib/src/components/movement_sensor/client.dart +++ b/lib/src/components/movement_sensor/client.dart @@ -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 diff --git a/lib/src/components/sensor/client.dart b/lib/src/components/sensor/client.dart index 55662f5745..74a569a3db 100644 --- a/lib/src/components/sensor/client.dart +++ b/lib/src/components/sensor/client.dart @@ -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 diff --git a/lib/src/utils.dart b/lib/src/utils.dart index 327f81a9a9..1fe99cd727 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -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'; @@ -92,6 +94,27 @@ grpc_duration.Duration durationToProto(Duration duration) { ..nanos = micros * 1000; } +extension GetReadingsResponseUtils on GetReadingsResponse { + Map toPrimitive() { + return readings.map((key, value) => MapEntry(key, value.toPrimitive())).map((key, value) { + if (value is Map && 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': + case 'angular_velocity': + primValue = Vector3(x: value['x'], y: value['y'], z: value['z']); + 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.361'; diff --git a/test/unit_test/utils/utils_test.dart b/test/unit_test/utils/utils_test.dart index 8526c655fe..944a267d9f 100644 --- a/test/unit_test/utils/utils_test.dart +++ b/test/unit_test/utils/utils_test.dart @@ -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'; @@ -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); + }); + }); }); }