Skip to content

Commit 91e378b

Browse files
APP-7497: Add Switch interface and client
1 parent 0c20b49 commit 91e378b

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

src/components/switch.ts

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

src/components/switch/client.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { Struct, type JsonValue } from '@bufbuild/protobuf';
2+
import type { CallOptions, PromiseClient } from '@connectrpc/connect';
3+
import { SwitchService } from '../../gen/component/switch/v1/switch_connect';
4+
import {
5+
SetPositionRequest,
6+
GetPositionRequest,
7+
GetNumberOfPositionsRequest,
8+
} from '../../gen/component/switch/v1/switch_pb';
9+
import type { RobotClient } from '../../robot';
10+
import type { Options } from '../../types';
11+
import { doCommandFromClient } from '../../utils';
12+
import type { Switch } from './switch';
13+
14+
/**
15+
* A gRPC-web client for the Switch component.
16+
*
17+
* @group Clients
18+
*/
19+
export class SwitchClient implements Switch {
20+
private client: PromiseClient<typeof SwitchService>;
21+
private readonly name: string;
22+
private readonly options: Options;
23+
public callOptions: CallOptions = { headers: {} as Record<string, string> };
24+
25+
constructor(client: RobotClient, name: string, options: Options = {}) {
26+
this.client = client.createServiceClient(SwitchService);
27+
this.name = name;
28+
this.options = options;
29+
}
30+
31+
async setPosition(position: number, extra = {}, callOptions = this.callOptions) {
32+
const request = new SetPositionRequest({
33+
name: this.name,
34+
position,
35+
extra: Struct.fromJson(extra),
36+
});
37+
38+
this.options.requestLogger?.(request);
39+
40+
await this.client.setPosition(request, callOptions);
41+
}
42+
43+
async getPosition(extra = {}, callOptions = this.callOptions) {
44+
const request = new GetPositionRequest({
45+
name: this.name,
46+
extra: Struct.fromJson(extra),
47+
});
48+
49+
this.options.requestLogger?.(request);
50+
51+
const resp = await this.client.getPosition(request, callOptions);
52+
return resp.position;
53+
}
54+
55+
async getNumberOfPositions(extra = {}, callOptions = this.callOptions) {
56+
const request = new GetNumberOfPositionsRequest({
57+
name: this.name,
58+
extra: Struct.fromJson(extra),
59+
});
60+
61+
this.options.requestLogger?.(request);
62+
63+
const resp = await this.client.getNumberOfPositions(request, callOptions);
64+
return resp.numberOfPositions;
65+
}
66+
67+
async doCommand(
68+
command: Struct,
69+
callOptions = this.callOptions
70+
): Promise<JsonValue> {
71+
return doCommandFromClient(
72+
this.client.doCommand,
73+
this.name,
74+
command,
75+
this.options,
76+
callOptions
77+
);
78+
}
79+
}

src/components/switch/switch.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { Struct } from '@bufbuild/protobuf';
2+
import type { Resource } from '../../types';
3+
4+
/** Represents a physical switch with multiple positions. */
5+
export interface Switch extends Resource {
6+
/** Set the switch to a specific position. */
7+
setPosition: (position: number, extra?: Struct) => Promise<void>;
8+
9+
/** Get the current position of the switch. */
10+
getPosition: (extra?: Struct) => Promise<number>;
11+
12+
/** Get the total number of positions available on the switch. */
13+
getNumberOfPositions: (extra?: Struct) => Promise<number>;
14+
}

src/main.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,18 @@ export * as sensorApi from './gen/component/sensor/v1/sensor_connect';
269269
export { StreamClient, type Stream } from './extra/stream';
270270
export * as streamApi from './gen/stream/v1/stream_pb';
271271

272+
export { SwitchClient, type Switch } from './components/switch';
273+
/**
274+
* Raw Protobuf interfaces for a Switch component.
275+
*
276+
* Generated with https://github.com/connectrpc/connect-es
277+
*
278+
* @deprecated Use {@link SwitchClient} instead.
279+
* @alpha
280+
* @group Raw Protobufs
281+
*/
282+
export * as switchApi from './gen/component/switch/v1/switch_pb';
283+
272284
/**
273285
* Raw Protobuf interfaces for a Generic component.
274286
*

0 commit comments

Comments
 (0)