Skip to content

Commit f595ede

Browse files
APP-7497: Add Button interface and client (#451)
1 parent 88206e2 commit f595ede

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

src/components/button.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export type { Button } from './button/button';
2+
export { ButtonClient } from './button/client';

src/components/button/button.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { Struct } from '@bufbuild/protobuf';
2+
import type { Resource } from '../../types';
3+
4+
/** Represents a physical button. */
5+
export interface Button extends Resource {
6+
/** Push the button. */
7+
push: (extra?: Struct) => Promise<void>;
8+
}

src/components/button/client.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Struct, type JsonValue } from '@bufbuild/protobuf';
2+
import type { CallOptions, PromiseClient } from '@connectrpc/connect';
3+
import { ButtonService } from '../../gen/component/button/v1/button_connect';
4+
import { PushRequest } from '../../gen/component/button/v1/button_pb';
5+
import type { RobotClient } from '../../robot';
6+
import type { Options } from '../../types';
7+
import { doCommandFromClient } from '../../utils';
8+
import type { Button } from './button';
9+
10+
/**
11+
* A gRPC-web client for the Button component.
12+
*
13+
* @group Clients
14+
*/
15+
export class ButtonClient implements Button {
16+
private client: PromiseClient<typeof ButtonService>;
17+
private readonly name: string;
18+
private readonly options: Options;
19+
public callOptions: CallOptions = { headers: {} as Record<string, string> };
20+
21+
constructor(client: RobotClient, name: string, options: Options = {}) {
22+
this.client = client.createServiceClient(ButtonService);
23+
this.name = name;
24+
this.options = options;
25+
}
26+
27+
async push(extra = {}, callOptions = this.callOptions) {
28+
const request = new PushRequest({
29+
name: this.name,
30+
extra: Struct.fromJson(extra),
31+
});
32+
33+
this.options.requestLogger?.(request);
34+
35+
await this.client.push(request, callOptions);
36+
}
37+
38+
async doCommand(
39+
command: Struct,
40+
callOptions = this.callOptions
41+
): Promise<JsonValue> {
42+
return doCommandFromClient(
43+
this.client.doCommand,
44+
this.name,
45+
command,
46+
this.options,
47+
callOptions
48+
);
49+
}
50+
}

src/main.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ export {
152152
} from './components/board';
153153
export * as boardApi from './gen/component/board/v1/board_pb';
154154

155+
export { ButtonClient, type Button } from './components/button';
156+
/**
157+
* Raw Protobuf interfaces for a Button component.
158+
*
159+
* Generated with https://github.com/connectrpc/connect-es
160+
*
161+
* @deprecated Use {@link ButtonClient} instead.
162+
* @alpha
163+
* @group Raw Protobufs
164+
*/
165+
export * as buttonApi from './gen/component/button/v1/button_pb';
166+
155167
/**
156168
* Raw Protobuf interfaces for a Camera component.
157169
*

0 commit comments

Comments
 (0)