Skip to content

Add labels to switch #577

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

Merged
merged 1 commit into from
Jun 26, 2025
Merged
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
14 changes: 11 additions & 3 deletions src/components/switch/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Struct, type JsonValue } from '@bufbuild/protobuf';
import type { CallOptions, PromiseClient } from '@connectrpc/connect';
import type { CallOptions, Client } from '@connectrpc/connect';
import { SwitchService } from '../../gen/component/switch/v1/switch_connect';
import {
SetPositionRequest,
Expand All @@ -17,7 +17,7 @@ import type { Switch } from './switch';
* @group Clients
*/
export class SwitchClient implements Switch {
private client: PromiseClient<typeof SwitchService>;
private client: Client<typeof SwitchService>;
public readonly name: string;
private readonly options: Options;
public callOptions: CallOptions = { headers: {} as Record<string, string> };
Expand Down Expand Up @@ -65,7 +65,15 @@ export class SwitchClient implements Switch {
this.options.requestLogger?.(request);

const resp = await this.client.getNumberOfPositions(request, callOptions);
return resp.numberOfPositions;
if (
resp.labels.length > 0 &&
resp.labels.length !== resp.numberOfPositions
) {
throw new Error(
'the number of labels does not match the number of positions'
);
}
return [resp.numberOfPositions, resp.labels] as [number, string[]];
}

async doCommand(
Expand Down
6 changes: 4 additions & 2 deletions src/components/switch/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export interface Switch extends Resource {
getPosition: (extra?: Struct) => Promise<number>;

/**
* Get the total number of positions available on the switch.
* Get the total number of positions available on the switch, along with their
* labels. Labels should either be null, undefined, empty, or the same length
* has the number of positions.
*
* @example
*
Expand All @@ -65,5 +67,5 @@ export interface Switch extends Resource {
* For more information, see [Switch
* API](https://docs.viam.com/dev/reference/apis/components/switch/#getnumberofpositions).
*/
getNumberOfPositions: (extra?: Struct) => Promise<number>;
getNumberOfPositions: (extra?: Struct) => Promise<[number, string[]]>;
}