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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/src/resource/registry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,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) => DiscoverClient(name, channel)));
}

/// The [Subtype] available in the SDK
Expand Down
10 changes: 10 additions & 0 deletions lib/src/robot/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,14 @@ 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 response = await _client.getModelsFromModules(request);
return response.model.map((d) => ModuleModel.fromProto(d)).toList();
}
}
56 changes: 56 additions & 0 deletions lib/src/services/discovery.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'package:fixnum/fixnum.dart';
import 'package:grpc/grpc_connection_interface.dart';

import '../../protos/common/common.dart';
import '../../protos/service/discovery.dart';
import '../media/image.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.discovery;
}

@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));
}
}
39 changes: 39 additions & 0 deletions test/unit_test/services/discovery_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'package:fixnum/fixnum.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:viam_sdk/protos/service/discovery.dart';
import 'package:viam_sdk/src/gen/common/v1/common.pb.dart';
import 'package:viam_sdk/src/gen/service/discovery/v1/discovery.pbgrpc.dart';
import 'package:viam_sdk/viam_sdk.dart';

import '../mocks/mock_response_future.dart';
import '../mocks/service_clients_mocks.mocks.dart';

class FakeDiscoveryClient extends DiscoverClient {
@override
DiscoveryServiceClient get client => _client;

final MockDiscoveryServiceClient _client;

FakeDiscoveryClient(super.name, super.channel, this._client);
}

void main() {
late DiscoveryClient client;
late MockDiscoveryServiceClient serviceClient;

setUp(() {
serviceClient = MockDiscoveryServiceClient();
client = FakeDiscoveryClient('discovery', MockClientChannelBase(), serviceClient);
});

group('Discovery RPC Client Tests', () {
test('discoverResources', () async {
final expected = [ComponentConfig()];
when(serviceClient.discoverResources(any))
.thenAnswer((_) => MockResponseFuture.value(DiscoverResourcesResponse(discovery: expected)));
final response = await client.discoverResources('discoveryName');
expect(response, equals(expected));
});
});
}
Loading