Skip to content

Commit

Permalink
Convert sensor reading values to types (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
njooma authored Nov 13, 2024
1 parent 254be4b commit e882c7b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
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
23 changes: 23 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,27 @@ 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':
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';
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);
});
});
});
}

0 comments on commit e882c7b

Please sign in to comment.