Skip to content

Commit

Permalink
[RSDK-9281] Add native type return to Flutter SDK DiscoverComponents (#…
Browse files Browse the repository at this point in the history
…299)

* Add native type return

* Add unwrapper

* Improve naming

* Revert unwrapper
  • Loading branch information
hexbabe authored Nov 19, 2024
1 parent b2bc45c commit 68f593b
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions lib/src/robot/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,33 @@ class DiscoveryQuery {
Struct get extraStruct => extra.toStruct();
}

/// {@category Viam SDK}
/// Represents the result of a discovery query.
class Discovery {
final String subtype;
final String model;
final Map<String, dynamic> results;

Discovery({
required this.subtype,
required this.model,
required this.results,
});

factory Discovery.fromProto(rpb.Discovery proto) {
return Discovery(
subtype: proto.query.subtype,
model: proto.query.model,
results: proto.results.toMap(),
);
}

@override
String toString() {
return 'Discovery(subtype: $subtype, model: $model, results: $results)';
}
}

/// {@category Viam SDK}
/// gRPC client for a Robot. This class should be used for all interactions with a robot.
///
Expand Down Expand Up @@ -287,13 +314,14 @@ class RobotClient {
/// var queries = [DiscoveryQuery(subtype: 'camera', model: 'webcam', extra: {'username': 'admin', 'password': 'admin'})];
/// var discoveredComponents = await machine.discoverComponents(queries);
/// ```
Future<rpb.DiscoverComponentsResponse> discoverComponents([List<DiscoveryQuery> queries = const []]) async {
Future<List<Discovery>> discoverComponents([List<DiscoveryQuery> queries = const []]) async {
final request = rpb.DiscoverComponentsRequest()
..queries.addAll(queries.map((sdkQuery) => rpb.DiscoveryQuery()
..subtype = sdkQuery.subtype
..model = sdkQuery.model
..extra = sdkQuery.extraStruct));

return await _client.discoverComponents(request);
final response = await _client.discoverComponents(request);
return response.discovery.map((d) => Discovery.fromProto(d)).toList();
}
}

0 comments on commit 68f593b

Please sign in to comment.