Skip to content

Commit

Permalink
APP-7497: Add Button interface and client (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanlookpotts authored Jan 28, 2025
1 parent 88206e2 commit f595ede
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/components/button.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type { Button } from './button/button';
export { ButtonClient } from './button/client';
8 changes: 8 additions & 0 deletions src/components/button/button.ts
Original file line number Diff line number Diff line change
@@ -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<void>;
}
50 changes: 50 additions & 0 deletions src/components/button/client.ts
Original file line number Diff line number Diff line change
@@ -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<typeof ButtonService>;
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(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<JsonValue> {
return doCommandFromClient(
this.client.doCommand,
this.name,
command,
this.options,
callOptions
);
}
}
12 changes: 12 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit f595ede

Please sign in to comment.