Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSDK-9622: Add Discover Service and GetModelsFromModules to Flutter #321

Merged
merged 12 commits into from
Jan 22, 2025
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
Loading