Skip to content

Commit

Permalink
APP-7497: Add Switch interface and client (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanlookpotts authored Jan 28, 2025
1 parent f595ede commit 6fddc56
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/components/switch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type { Switch } from './switch/switch';
export { SwitchClient } from './switch/client';
83 changes: 83 additions & 0 deletions src/components/switch/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Struct, type JsonValue } from '@bufbuild/protobuf';
import type { CallOptions, PromiseClient } from '@connectrpc/connect';
import { SwitchService } from '../../gen/component/switch/v1/switch_connect';
import {
SetPositionRequest,
GetPositionRequest,
GetNumberOfPositionsRequest,
} from '../../gen/component/switch/v1/switch_pb';
import type { RobotClient } from '../../robot';
import type { Options } from '../../types';
import { doCommandFromClient } from '../../utils';
import type { Switch } from './switch';

/**
* A gRPC-web client for the Switch component.
*
* @group Clients
*/
export class SwitchClient implements Switch {
private client: PromiseClient<typeof SwitchService>;
private readonly name: string;
private readonly options: Options;
public callOptions: CallOptions = { headers: {} as Record<string, string> };

constructor(client: RobotClient, name: string, options: Options = {}) {
this.client = client.createServiceClient(SwitchService);
this.name = name;
this.options = options;
}

async setPosition(
position: number,
extra = {},
callOptions = this.callOptions
) {
const request = new SetPositionRequest({
name: this.name,
position,
extra: Struct.fromJson(extra),
});

this.options.requestLogger?.(request);

await this.client.setPosition(request, callOptions);
}

async getPosition(extra = {}, callOptions = this.callOptions) {
const request = new GetPositionRequest({
name: this.name,
extra: Struct.fromJson(extra),
});

this.options.requestLogger?.(request);

const resp = await this.client.getPosition(request, callOptions);
return resp.position;
}

async getNumberOfPositions(extra = {}, callOptions = this.callOptions) {
const request = new GetNumberOfPositionsRequest({
name: this.name,
extra: Struct.fromJson(extra),
});

this.options.requestLogger?.(request);

const resp = await this.client.getNumberOfPositions(request, callOptions);
return resp.numberOfPositions;
}

async doCommand(
command: Struct,
callOptions = this.callOptions
): Promise<JsonValue> {
return doCommandFromClient(
this.client.doCommand,
this.name,
command,
this.options,
callOptions
);
}
}
14 changes: 14 additions & 0 deletions src/components/switch/switch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { Struct } from '@bufbuild/protobuf';
import type { Resource } from '../../types';

/** Represents a physical switch with multiple positions. */
export interface Switch extends Resource {
/** Set the switch to a specific position. */
setPosition: (position: number, extra?: Struct) => Promise<void>;

/** Get the current position of the switch. */
getPosition: (extra?: Struct) => Promise<number>;

/** Get the total number of positions available on the switch. */
getNumberOfPositions: (extra?: Struct) => Promise<number>;
}
12 changes: 12 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,18 @@ export * as sensorApi from './gen/component/sensor/v1/sensor_connect';
export { StreamClient, type Stream } from './extra/stream';
export * as streamApi from './gen/stream/v1/stream_pb';

export { SwitchClient, type Switch } from './components/switch';
/**
* Raw Protobuf interfaces for a Switch component.
*
* Generated with https://github.com/connectrpc/connect-es
*
* @deprecated Use {@link SwitchClient} instead.
* @alpha
* @group Raw Protobufs
*/
export * as switchApi from './gen/component/switch/v1/switch_pb';

/**
* Raw Protobuf interfaces for a Generic component.
*
Expand Down

0 comments on commit 6fddc56

Please sign in to comment.