From f595ede391efb654f6b85b5699671c5383238ce3 Mon Sep 17 00:00:00 2001 From: Ethan Look-Potts Date: Tue, 28 Jan 2025 14:56:45 -0500 Subject: [PATCH] APP-7497: Add Button interface and client (#451) --- src/components/button.ts | 2 ++ src/components/button/button.ts | 8 ++++++ src/components/button/client.ts | 50 +++++++++++++++++++++++++++++++++ src/main.ts | 12 ++++++++ 4 files changed, 72 insertions(+) create mode 100644 src/components/button.ts create mode 100644 src/components/button/button.ts create mode 100644 src/components/button/client.ts diff --git a/src/components/button.ts b/src/components/button.ts new file mode 100644 index 000000000..123633e55 --- /dev/null +++ b/src/components/button.ts @@ -0,0 +1,2 @@ +export type { Button } from './button/button'; +export { ButtonClient } from './button/client'; diff --git a/src/components/button/button.ts b/src/components/button/button.ts new file mode 100644 index 000000000..80a05e9a9 --- /dev/null +++ b/src/components/button/button.ts @@ -0,0 +1,8 @@ +import type { Struct } from '@bufbuild/protobuf'; +import type { Resource } from '../../types'; + +/** Represents a physical button. */ +export interface Button extends Resource { + /** Push the button. */ + push: (extra?: Struct) => Promise; +} diff --git a/src/components/button/client.ts b/src/components/button/client.ts new file mode 100644 index 000000000..49fbde45e --- /dev/null +++ b/src/components/button/client.ts @@ -0,0 +1,50 @@ +import { Struct, type JsonValue } from '@bufbuild/protobuf'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; +import { ButtonService } from '../../gen/component/button/v1/button_connect'; +import { PushRequest } from '../../gen/component/button/v1/button_pb'; +import type { RobotClient } from '../../robot'; +import type { Options } from '../../types'; +import { doCommandFromClient } from '../../utils'; +import type { Button } from './button'; + +/** + * A gRPC-web client for the Button component. + * + * @group Clients + */ +export class ButtonClient implements Button { + private client: PromiseClient; + private readonly name: string; + private readonly options: Options; + public callOptions: CallOptions = { headers: {} as Record }; + + constructor(client: RobotClient, name: string, options: Options = {}) { + this.client = client.createServiceClient(ButtonService); + this.name = name; + this.options = options; + } + + async push(extra = {}, callOptions = this.callOptions) { + const request = new PushRequest({ + name: this.name, + extra: Struct.fromJson(extra), + }); + + this.options.requestLogger?.(request); + + await this.client.push(request, callOptions); + } + + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { + return doCommandFromClient( + this.client.doCommand, + this.name, + command, + this.options, + callOptions + ); + } +} diff --git a/src/main.ts b/src/main.ts index 08802c3d4..8a21b9a10 100644 --- a/src/main.ts +++ b/src/main.ts @@ -152,6 +152,18 @@ export { } from './components/board'; export * as boardApi from './gen/component/board/v1/board_pb'; +export { ButtonClient, type Button } from './components/button'; +/** + * Raw Protobuf interfaces for a Button component. + * + * Generated with https://github.com/connectrpc/connect-es + * + * @deprecated Use {@link ButtonClient} instead. + * @alpha + * @group Raw Protobufs + */ +export * as buttonApi from './gen/component/button/v1/button_pb'; + /** * Raw Protobuf interfaces for a Camera component. *