-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add generic component support (#166)
- Loading branch information
Showing
6 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'package:grpc/grpc_connection_interface.dart'; | ||
|
||
import '../../gen/common/v1/common.pb.dart'; | ||
import '../../gen/component/generic/v1/generic.pbgrpc.dart'; | ||
import '../../resource/base.dart'; | ||
import '../../utils.dart'; | ||
import 'generic.dart'; | ||
|
||
/// gRPC client for the [Generic] component. | ||
class GenericClient extends Generic implements ResourceRPCClient { | ||
@override | ||
String name; | ||
|
||
@override | ||
ClientChannelBase channel; | ||
|
||
@override | ||
GenericServiceClient get client => GenericServiceClient(channel); | ||
|
||
GenericClient(this.name, this.channel); | ||
|
||
@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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import '../../gen/common/v1/common.pb.dart'; | ||
import '../../resource/base.dart'; | ||
import '../../robot/client.dart'; | ||
|
||
/// Generic represents a generic component that executes doCommand. | ||
abstract class Generic extends Resource { | ||
static const Subtype subtype = Subtype(resourceNamespaceRDK, resourceTypeComponent, 'generic'); | ||
|
||
/// Get the [ResourceName] for this [Generic] with the given [name] | ||
static ResourceName getResourceName(String name) { | ||
return Generic.subtype.getResourceName(name); | ||
} | ||
|
||
/// Get the [Generic] named [name] from the provided robot. | ||
static Generic fromRobot(RobotClient robot, String name) { | ||
return robot.getResource(Generic.getResourceName(name)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'package:grpc/grpc.dart'; | ||
|
||
import '../../gen/common/v1/common.pb.dart'; | ||
import '../../gen/component/generic/v1/generic.pbgrpc.dart'; | ||
import '../../resource/manager.dart'; | ||
import '../../utils.dart'; | ||
import 'generic.dart'; | ||
|
||
/// gRPC Service for a generic [Generic] | ||
class GenericService extends GenericServiceBase { | ||
final ResourceManager _manager; | ||
|
||
GenericService(this._manager); | ||
|
||
Generic _fromManager(String name) { | ||
try { | ||
return _manager.getResource(Generic.getResourceName(name)); | ||
} catch (e) { | ||
throw (GrpcError.notFound(e.toString())); | ||
} | ||
} | ||
|
||
@override | ||
Future<DoCommandResponse> doCommand(ServiceCall call, DoCommandRequest request) async { | ||
final generic = _fromManager(request.name); | ||
final result = await generic.doCommand(request.command.toMap()); | ||
return DoCommandResponse()..result = result.toStruct(); | ||
} | ||
|
||
@override | ||
Future<GetGeometriesResponse> getGeometries(ServiceCall call, GetGeometriesRequest request) { | ||
// TODO: implement getGeometries | ||
throw UnimplementedError(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:grpc/grpc.dart'; | ||
import 'package:viam_sdk/src/components/generic/service.dart'; | ||
import 'package:viam_sdk/src/gen/component/generic/v1/generic.pbgrpc.dart'; | ||
import 'package:viam_sdk/src/resource/manager.dart'; | ||
import 'package:viam_sdk/src/utils.dart'; | ||
import 'package:viam_sdk/viam_sdk.dart'; | ||
|
||
class FakeGeneric extends Generic { | ||
@override | ||
String name; | ||
|
||
FakeGeneric(this.name); | ||
|
||
@override | ||
Future<Map<String, dynamic>> doCommand(Map<String, dynamic>? command) async { | ||
return {'command': command}; | ||
} | ||
} | ||
|
||
void main() { | ||
group('FakeGeneric Tests', () { | ||
const String name = 'generic'; | ||
late FakeGeneric generic; | ||
|
||
setUp(() { | ||
generic = FakeGeneric(name); | ||
}); | ||
|
||
test('doCommand', () async { | ||
final cmd = {'foo': 'bar'}; | ||
final resp = await generic.doCommand(cmd); | ||
expect(resp['command'], cmd); | ||
}); | ||
}); | ||
|
||
group('Generic RPC Tests', () { | ||
const String name = 'generic'; | ||
late FakeGeneric generic; | ||
late ClientChannel channel; | ||
late GenericService service; | ||
late Server server; | ||
|
||
setUp(() async { | ||
final port = 50000 + (name.hashCode % 10000); | ||
generic = FakeGeneric(name); | ||
final ResourceManager manager = ResourceManager(); | ||
manager.register(Generic.getResourceName(name), generic); | ||
service = GenericService(manager); | ||
channel = ClientChannel('localhost', port: port, options: const ChannelOptions(credentials: ChannelCredentials.insecure())); | ||
server = Server.create(services: [service]); | ||
await server.serve(port: port); | ||
}); | ||
|
||
tearDown(() async { | ||
await channel.shutdown(); | ||
await server.shutdown(); | ||
}); | ||
|
||
group('Generic Service Tests', () { | ||
test('doCommand', () async { | ||
final cmd = {'foo': 'bar'}; | ||
|
||
final client = GenericServiceClient(channel); | ||
final resp = await client.doCommand(DoCommandRequest() | ||
..name = name | ||
..command = cmd.toStruct()); | ||
expect(resp.result.toMap()['command'], cmd); | ||
}); | ||
}); | ||
|
||
group('Generic Client Tests', () { | ||
test('doCommand', () async { | ||
final cmd = {'foo': 'bar'}; | ||
final client = GenericClient(name, channel); | ||
final resp = await client.doCommand(cmd); | ||
expect(resp['command'], cmd); | ||
}); | ||
}); | ||
}); | ||
} |