From ee6ec7635325545969f1dbbb58314cc99a1fb1bc Mon Sep 17 00:00:00 2001 From: Naveed Jooma Date: Mon, 11 Nov 2024 17:15:22 -0500 Subject: [PATCH 1/3] Convert sensor reading values to types --- .../components/movement_sensor/client.dart | 2 +- lib/src/components/sensor/client.dart | 2 +- lib/src/utils.dart | 24 +++++++++++++++++++ test/unit_test/utils/utils_test.dart | 19 +++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/src/components/movement_sensor/client.dart b/lib/src/components/movement_sensor/client.dart index ad1786cff98..c61dfe03e5f 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 55662f57451..74a569a3dbe 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 432360e51fa..cb2db6818a9 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,28 @@ 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': + 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']); + 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'; diff --git a/test/unit_test/utils/utils_test.dart b/test/unit_test/utils/utils_test.dart index 8526c655fe2..ab8a7bdcdfb 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,22 @@ 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()), + }; + 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)); + }); + }); }); } From 1e94864ce3e522b225ad43912bc81bcf7172afde Mon Sep 17 00:00:00 2001 From: Naveed Jooma Date: Mon, 11 Nov 2024 17:17:17 -0500 Subject: [PATCH 2/3] Add extra test case --- test/unit_test/utils/utils_test.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/unit_test/utils/utils_test.dart b/test/unit_test/utils/utils_test.dart index ab8a7bdcdfb..944a267d9fd 100644 --- a/test/unit_test/utils/utils_test.dart +++ b/test/unit_test/utils/utils_test.dart @@ -208,6 +208,7 @@ void main() { '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(); @@ -215,6 +216,7 @@ void main() { 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); }); }); }); From b8ee86aab245d2b64517fcd19bfb78900d2ca2da Mon Sep 17 00:00:00 2001 From: Naveed Jooma Date: Wed, 13 Nov 2024 10:46:20 -0500 Subject: [PATCH 3/3] Merge similar cases Co-authored-by: Ethan --- lib/src/utils.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/src/utils.dart b/lib/src/utils.dart index cb2db6818a9..a07f88a86dd 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -103,7 +103,6 @@ extension GetReadingsResponseUtils on GetReadingsResponse { 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']); case 'geopoint':