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

DOCS-2592: Add RobotClient example snippets #241

Merged
merged 1 commit into from
Jul 10, 2024
Merged
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
34 changes: 32 additions & 2 deletions lib/src/robot/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ class RobotClient {
RobotClient._();

/// Connect to a robot at the specified address with the provided options.
///
/// ```
/// // Example usage; see your machine's CONNECT tab for your machine's address and API key.
///
/// Future<void> connectToViam() async {
/// const host = '<YOUR ROBOT ADDRESS>.viam.cloud';
/// // Replace "<API-KEY-ID>" (including brackets) with your machine's API key ID
/// const apiKeyID = '<API-KEY-ID>';
/// // Replace "<API-KEY>" (including brackets) with your machine's API key
/// const apiKey = '<API-KEY>';
///
/// final machine = await RobotClient.atAddress(
/// host,
/// RobotClientOptions.withApiKey(apiKeyID, apiKey),
/// );
/// }
/// ```
static Future<RobotClient> atAddress(String url, RobotClientOptions options) async {
Logger.level = options.logLevel;
final client = RobotClient._();
Expand All @@ -85,6 +102,10 @@ class RobotClient {
}

/// Refresh the resources of this robot
///
/// ```
/// await machine.refresh();
/// ```
Future<void> refresh() async {
final ResourceNamesResponse response = await _client.resourceNames(ResourceNamesRequest());
if (setEquals(response.resources.toSet(), resourceNames.toSet())) {
Expand Down Expand Up @@ -201,6 +222,10 @@ class RobotClient {
}

/// Close the connection to the Robot. This should be done to release resources on the robot.
///
/// ```
/// await machine.close();
/// ```
Future<void> close() async {
_logger.d('Closing RobotClient connection');
try {
Expand All @@ -219,16 +244,21 @@ class RobotClient {
}
}

/// Get a connected resource by its [ResourceName]
/// Get a connected resource by its [ResourceName].
T getResource<T>(ResourceName name) {
return _manager.getResource<T>(name);
}

/// Get a WebRTC stream client with the given name
/// Get a WebRTC stream client with the given name.
StreamClient getStream(String name) {
return _streamManager.getStreamClient(name);
}

/// Get app-related information about the machine.
///
/// ```
/// var metadata = await machine.getCloudMetadata();
/// ```
Future<CloudMetadata> getCloudMetadata() async {
return await _client.getCloudMetadata(GetCloudMetadataRequest());
}
Expand Down