Skip to content

Commit

Permalink
RSDK-9622: Add Discover Service and GetModelsFromModules to Flutter (#…
Browse files Browse the repository at this point in the history
…321)

* add discovery service

* register and add tests

* add module models

* change to discoveries

* update with latest api changes

* fix mocks

* fix import'

* import common to discovery.dart

* remove content of test

* readd test

* export discovery client

---------

Co-authored-by: Naveed Jooma <[email protected]>
  • Loading branch information
martha-johnston and njooma authored Jan 22, 2025
1 parent 4c4b202 commit 5d442c7
Show file tree
Hide file tree
Showing 7 changed files with 4,257 additions and 4,544 deletions.
2 changes: 2 additions & 0 deletions lib/src/resource/registry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import '../components/sensor/sensor.dart';
import '../components/servo/client.dart';
import '../components/servo/servo.dart';
import '../resource/base.dart';
import '../services/discovery.dart';
import '../services/vision.dart';

/// {@category Viam SDK}
Expand Down Expand Up @@ -69,6 +70,7 @@ class Registry {
registerSubtype(ResourceRegistration(Sensor.subtype, (name, channel) => SensorClient(name, channel)));
registerSubtype(ResourceRegistration(Servo.subtype, (name, channel) => ServoClient(name, channel)));
registerSubtype(ResourceRegistration(VisionClient.subtype, (name, channel) => VisionClient(name, channel)));
registerSubtype(ResourceRegistration(DiscoveryClient.subtype, (name, channel) => DiscoveryClient(name, channel)));
}

/// The [Subtype] available in the SDK
Expand Down
12 changes: 12 additions & 0 deletions lib/src/robot/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:grpc/grpc_connection_interface.dart';
import 'package:logger/logger.dart';

import '../gen/common/v1/common.pb.dart';
import '../gen/robot/v1/robot.pb.dart';
import '../gen/google/protobuf/struct.pb.dart';
import '../gen/robot/v1/robot.pbgrpc.dart' as rpb;
import '../gen/stream/v1/stream.pbgrpc.dart';
Expand Down Expand Up @@ -349,4 +350,15 @@ class RobotClient {
final response = await _client.discoverComponents(request);
return response.discovery.map((d) => Discovery.fromProto(d)).toList();
}

/// GetModelsFromModules returns the list of models supported in modules on the machine.
///
/// ```
/// var modelsFromModules = await machine.getModelsFromModules();
/// ```
Future<List<ModuleModel>> getModelsFromModules() async {
final request = rpb.GetModelsFromModulesRequest();
final response = await _client.getModelsFromModules(request);
return response.models;
}
}
57 changes: 57 additions & 0 deletions lib/src/services/discovery.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'package:grpc/grpc_connection_interface.dart';

import '../../protos/common/common.dart';
import '../gen/common/v1/common.pb.dart';
import '../../protos/service/discovery.dart';
import '../gen/service/discovery/v1/discovery.pbgrpc.dart';
import '../gen/app/v1/robot.pb.dart';
import '../resource/base.dart';
import '../robot/client.dart';
import '../utils.dart';

/// {@category Services}
class DiscoveryClient extends Resource implements ResourceRPCClient {
static const Subtype subtype = Subtype(resourceNamespaceRDK, resourceTypeService, 'discovery');

@override
final String name;

@override
ClientChannelBase channel;

@override
DiscoveryServiceClient get client => DiscoveryServiceClient(channel);

DiscoveryClient(this.name, this.channel);

/// Returns a list of [ComponentConfig]s for all discovered viam resources connected to the viam-server machine.
///
/// ```
/// // Example:
/// var resources = await myDiscoveryService.discoverResources('myWebcam');
/// ```
Future<List<ComponentConfig>> discoverResources(String discoveryName, {Map<String, dynamic>? extra}) async {
final request = DiscoverResourcesRequest(name: name, extra: extra?.toStruct());
final response = await client.discoverResources(request);
return response.discoveries;
}

@override
Future<Map<String, dynamic>> doCommand(Map<String, dynamic> command) async {
final request = DoCommandRequest()
..name = name
..command = command.toStruct();
final response = await client.doCommand(request);
return response.result.toMap();
}

/// Get the [ResourceName] for this [DiscoveryClient] with the given [name]
static ResourceName getResourceName(String name) {
return DiscoveryClient.subtype.getResourceName(name);
}

/// Get the [DiscoveryClient] named [name] from the provided robot.
static DiscoveryClient fromRobot(RobotClient robot, String name) {
return robot.getResource(DiscoveryClient.getResourceName(name));
}
}
1 change: 1 addition & 0 deletions lib/viam_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export 'src/robot/client.dart';
export 'src/rpc/dial.dart';

/// Services
export 'src/services/discovery.dart';
export 'src/services/vision.dart';

/// Misc
Expand Down
2 changes: 2 additions & 0 deletions test/unit_test/mocks/service_clients_mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:viam_sdk/src/gen/app/v1/robot.pbgrpc.dart' as app_robot;
import 'package:viam_sdk/src/gen/provisioning/v1/provisioning.pbgrpc.dart';
import 'package:viam_sdk/src/gen/robot/v1/robot.pbgrpc.dart';
import 'package:viam_sdk/src/gen/service/vision/v1/vision.pbgrpc.dart';
import 'package:viam_sdk/src/gen/service/discovery/v1/discovery.pbgrpc.dart';

@GenerateNiceMocks([
MockSpec<ClientChannelBase>(),
Expand All @@ -21,5 +22,6 @@ import 'package:viam_sdk/src/gen/service/vision/v1/vision.pbgrpc.dart';
MockSpec<BillingServiceClient>(),
MockSpec<MLTrainingServiceClient>(),
MockSpec<DatasetServiceClient>(),
MockSpec<DiscoveryServiceClient>(),
])
void main() {}
Loading

0 comments on commit 5d442c7

Please sign in to comment.