-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
181 additions
and
23 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
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
2 changes: 1 addition & 1 deletion
2
example/viam_example_app/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme
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
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 |
---|---|---|
@@ -1,35 +1,47 @@ | ||
import 'package:protobuf/protobuf.dart'; | ||
|
||
import '../exceptions.dart'; | ||
import '../gen/common/v1/common.pb.dart'; | ||
import 'base.dart'; | ||
|
||
/// [ResourceManager] manages the state of all currently available resources of a robot | ||
class ResourceManager { | ||
/// The available resources | ||
Map<ResourceName, Resource> resources = {}; | ||
final Map<String, List<ResourceName>> _shortToLongName = {}; | ||
final Map<ResourceName, List<ResourceName>> _resourceNamesWithoutRemotes = {}; | ||
|
||
/// Register a new [Resource] with the manager. | ||
void register(ResourceName name, Resource resource) { | ||
if (resources.containsKey(name)) { | ||
throw Exception('Duplicate registration of resource in manager'); | ||
throw DuplicateResourceException(name); | ||
} | ||
final shortName = name.name.split(':').last; | ||
final names = _shortToLongName[shortName] ?? []; | ||
final rnWithoutRemote = name.deepCopy() | ||
..remotePath.clear() | ||
..name = name.localName; | ||
final names = _resourceNamesWithoutRemotes[rnWithoutRemote] ?? []; | ||
names.add(name); | ||
_shortToLongName[shortName] = names; | ||
_resourceNamesWithoutRemotes[rnWithoutRemote] = names; | ||
resources[name] = resource; | ||
} | ||
|
||
/// Get a resource with the given [ResourceName] | ||
T getResource<T>(ResourceName name) { | ||
final resource = resources[name]; | ||
if (resource == null) throw Exception('Resource not found in manager'); | ||
Resource? resource; | ||
if (resources.containsKey(name)) { | ||
resource = resources[name]; | ||
} else { | ||
final resourceNames = _resourceNamesWithoutRemotes[name] ?? []; | ||
// If multiple ResourceNames map to this name-without-remotes, | ||
// that means there are multiple remote resources with this same short name. | ||
// Without any means to disambiguate, we should not select any. | ||
if (resourceNames.length > 1) { | ||
throw MultipleRemoteResourcesSameNameException(resourceNames); | ||
} | ||
if (resourceNames.length == 1) { | ||
resource = resources[resourceNames.first]; | ||
} | ||
} | ||
if (resource == null) throw ResourceNotFoundException(name); | ||
return resource as T; | ||
} | ||
|
||
/// Get a resource by its name only | ||
T getResourceByName<T>(String name) { | ||
final names = _shortToLongName[name] ?? []; | ||
if (names.isEmpty) throw Exception('Resource not found in manager'); | ||
return getResource(names.first); | ||
} | ||
} |
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,30 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:viam_sdk/src/components/sensor/sensor.dart'; | ||
|
||
void main() { | ||
group('Subtype Tests', () { | ||
test('getResourceName', () { | ||
// Local | ||
final localName = 'my-sensor'; | ||
final localRN = Sensor.subtype.getResourceName(localName); | ||
expect(localRN.namespace, Sensor.subtype.namespace); | ||
expect(localRN.type, Sensor.subtype.resourceType); | ||
expect(localRN.subtype, Sensor.subtype.resourceSubtype); | ||
expect(localRN.remotePath, []); | ||
expect(localRN.name, localName); | ||
expect(localRN.localName, localName); | ||
|
||
// Remote | ||
final remoteName = 'my-sensor'; | ||
final remotePath = 'remote2:remote1'; | ||
final fullRemoteName = '$remotePath:$remoteName'; | ||
final remoteRN = Sensor.subtype.getResourceName(fullRemoteName); | ||
expect(remoteRN.namespace, Sensor.subtype.namespace); | ||
expect(remoteRN.type, Sensor.subtype.resourceType); | ||
expect(remoteRN.subtype, Sensor.subtype.resourceSubtype); | ||
expect(remoteRN.remotePath, remotePath.split(':')); | ||
expect(remoteRN.name, fullRemoteName); | ||
expect(remoteRN.localName, remoteName); | ||
}); | ||
}); | ||
} |
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,85 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:viam_sdk/src/components/sensor/sensor.dart'; | ||
import 'package:viam_sdk/src/exceptions.dart'; | ||
import 'package:viam_sdk/src/resource/manager.dart'; | ||
|
||
import '../components/sensor_test.dart'; | ||
|
||
void main() { | ||
group('ResourceManager Tests', () { | ||
group('getResource', () { | ||
test('Local', () { | ||
final localName = 'local-sensor'; | ||
final localRN = Sensor.getResourceName(localName); | ||
final localResource = FakeSensor(localName); | ||
final manager = ResourceManager(); | ||
manager.register(localRN, localResource); | ||
|
||
expect(manager.getResource<Sensor>(Sensor.getResourceName(localName)), localResource); | ||
}); | ||
|
||
test('Remote', () { | ||
final remoteName = 'remote-sensor'; | ||
final remotePath = 'remote2:remote1'; | ||
final fullRemoteName = '$remotePath:$remoteName'; | ||
final remoteRN = Sensor.subtype.getResourceName(fullRemoteName); | ||
final remoteResource = FakeSensor(fullRemoteName); | ||
final manager = ResourceManager(); | ||
manager.register(remoteRN, remoteResource); | ||
|
||
// Works with full name | ||
expect(manager.getResource<Sensor>(Sensor.getResourceName(fullRemoteName)), remoteResource); | ||
|
||
// Works with short name -- no collisions | ||
expect(manager.getResource<Sensor>(Sensor.getResourceName(remoteName)), remoteResource); | ||
}); | ||
|
||
test('Local and Remote - Same Names', () { | ||
final manager = ResourceManager(); | ||
|
||
final localName = 'my-sensor'; | ||
final localRN = Sensor.getResourceName(localName); | ||
final localResource = FakeSensor(localName); | ||
|
||
final remoteName = 'my-sensor'; | ||
final remotePath = 'remote2:remote1'; | ||
final fullRemoteName = '$remotePath:$remoteName'; | ||
final remoteRN = Sensor.subtype.getResourceName(fullRemoteName); | ||
final remoteResource = FakeSensor(fullRemoteName); | ||
|
||
manager.register(localRN, localResource); | ||
manager.register(remoteRN, remoteResource); | ||
|
||
// When using fully qualified names, it should return appropriately | ||
expect(manager.getResource<Sensor>(Sensor.getResourceName(localName)), localResource); | ||
expect(manager.getResource<Sensor>(Sensor.getResourceName(fullRemoteName)), remoteResource); | ||
|
||
// When using just `my-sensor`, it should always return the local | ||
expect(manager.getResource<Sensor>(Sensor.getResourceName(localName)), localResource); | ||
expect(manager.getResource<Sensor>(Sensor.getResourceName(remoteName)), localResource); | ||
}); | ||
|
||
test('Multiple Remotes - Same Names', () { | ||
final remoteName = 'my-sensor'; | ||
|
||
final remotePath1 = 'remote2:remote1'; | ||
final fullRemoteName1 = '$remotePath1:$remoteName'; | ||
final remoteRN1 = Sensor.subtype.getResourceName(fullRemoteName1); | ||
final remoteResource1 = FakeSensor(fullRemoteName1); | ||
|
||
final remotePath2 = 'remote4:remote3'; | ||
final fullRemoteName2 = '$remotePath2:$remoteName'; | ||
final remoteRN2 = Sensor.subtype.getResourceName(fullRemoteName2); | ||
final remoteResource2 = FakeSensor(fullRemoteName2); | ||
|
||
final manager = ResourceManager(); | ||
manager.register(remoteRN1, remoteResource1); | ||
manager.register(remoteRN2, remoteResource2); | ||
|
||
// Error when using short name only | ||
expect(() => manager.getResource<Sensor>(Sensor.getResourceName(remoteName)), | ||
throwsA(isA<MultipleRemoteResourcesSameNameException>())); | ||
}); | ||
}); | ||
}); | ||
} |