-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathutils.dart
More file actions
176 lines (152 loc) · 5.84 KB
/
utils.dart
File metadata and controls
176 lines (152 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import 'package:collection/collection.dart';
import 'package:fixnum/fixnum.dart';
import 'package:flutter/material.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.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';
final _logger = Logger();
class Kinematics {
final KinematicsFileFormat format;
final List<int> raw;
final Map<String, Mesh> meshesByUrdfFilepath;
static const ListEquality<int> _rawEquality = ListEquality<int>();
static const MapEquality<String, Mesh> _meshEquality = MapEquality<String, Mesh>();
const Kinematics(this.format, this.raw, {this.meshesByUrdfFilepath = const {}});
factory Kinematics.fromProto(GetKinematicsResponse gkResponse) {
return Kinematics(
gkResponse.format,
gkResponse.kinematicsData,
meshesByUrdfFilepath: gkResponse.meshesByUrdfFilepath,
);
}
GetKinematicsResponse toProto() {
return GetKinematicsResponse()
..format = format
..kinematicsData = raw
..meshesByUrdfFilepath.addAll(meshesByUrdfFilepath);
}
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Kinematics &&
runtimeType == other.runtimeType &&
format == other.format &&
_rawEquality.equals(raw, other.raw) &&
_meshEquality.equals(meshesByUrdfFilepath, other.meshesByUrdfFilepath);
@override
int get hashCode => Object.hash(format, _rawEquality.hash(raw), _meshEquality.hash(meshesByUrdfFilepath));
}
extension NullableStringUtils on String? {
bool get isNullOrEmpty {
return this?.isEmpty ?? true;
}
bool get isNotNullNorEmpty {
return this?.isNotEmpty ?? false;
}
}
extension ValueUtils on Value {
dynamic toPrimitive() {
if (hasBoolValue()) return boolValue;
if (hasListValue()) return listValue.values.map((e) => e.toPrimitive()).toList();
if (hasNullValue()) return null;
if (hasNumberValue()) return numberValue;
if (hasStringValue()) return stringValue;
if (hasStructValue()) return structValue.fields.map((key, value) => MapEntry(key, value.toPrimitive()));
_logger.e('Value does not contain valid type');
throw const GrpcError.invalidArgument('value does not contain valid type');
}
}
extension StructUtils on Struct {
Map<String, dynamic> toMap() {
return fields.map((key, value) => MapEntry(key, value.toPrimitive()));
}
}
extension ListValueUtils<T> on List<T> {
Value toValue() {
final values = map((e) {
if (e is bool) return Value()..boolValue = e;
if (e is List<dynamic>) return e.toValue();
if (e is Map<String, dynamic>) return e.toValue();
if (e is num) return Value()..numberValue = e.toDouble();
if (e is String) return Value()..stringValue = e;
if (e == null) return Value()..nullValue = NullValue.NULL_VALUE;
_logger.e('Error converting the List to a Value');
throw const GrpcError.invalidArgument('List contains unsupported type');
});
return Value()..listValue = (ListValue()..values.addAll(values));
}
}
extension MapStructUtils on Map<String, dynamic> {
Struct toStruct() {
final Map<String, Value> result = {};
for (final entry in entries) {
final value = entry.value;
if (value is num) {
result[entry.key] = Value()..numberValue = value.toDouble();
} else if (value is String) {
result[entry.key] = Value()..stringValue = value;
} else if (value is bool) {
result[entry.key] = Value()..boolValue = value;
} else if (value is List<dynamic>) {
result[entry.key] = value.toValue();
} else if (value is Map<String, dynamic>) {
result[entry.key] = value.toValue();
} else if (value == null) {
result[entry.key] = Value()..nullValue = NullValue.NULL_VALUE;
} else {
_logger.e('Error converting the Map to a Struct');
throw const GrpcError.invalidArgument('Unsupported type');
}
}
return Struct()..fields.addAll(result);
}
Value toValue() {
return Value()..structValue = toStruct();
}
}
grpc_duration.Duration durationToProto(Duration duration) {
final micros = duration.inMicroseconds % Duration.microsecondsPerSecond;
return grpc_duration.Duration()
..seconds = Int64(duration.inSeconds)
..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.18.0';
const String apiTag = 'v0.1.530';
return 'flutter;$sdkVersion;$apiTag';
}
/// Show an error dialog with one action: OK, which simply dismisses the dialog
Future<void> showErrorDialog(BuildContext context, {String title = 'An Error Occurred', String? error}) async {
return showAdaptiveDialog(
context: context,
builder: (context) => AlertDialog.adaptive(
title: Text(title),
content: error == null ? null : Text(error),
actions: [PlatformDialogAction(onPressed: Navigator.of(context).pop, child: Text('OK'))],
),
);
}