|
| 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( |
| 32 | + position: number, |
| 33 | + extra = {}, |
| 34 | + callOptions = this.callOptions |
| 35 | + ) { |
| 36 | + const request = new SetPositionRequest({ |
| 37 | + name: this.name, |
| 38 | + position, |
| 39 | + extra: Struct.fromJson(extra), |
| 40 | + }); |
| 41 | + |
| 42 | + this.options.requestLogger?.(request); |
| 43 | + |
| 44 | + await this.client.setPosition(request, callOptions); |
| 45 | + } |
| 46 | + |
| 47 | + async getPosition(extra = {}, callOptions = this.callOptions) { |
| 48 | + const request = new GetPositionRequest({ |
| 49 | + name: this.name, |
| 50 | + extra: Struct.fromJson(extra), |
| 51 | + }); |
| 52 | + |
| 53 | + this.options.requestLogger?.(request); |
| 54 | + |
| 55 | + const resp = await this.client.getPosition(request, callOptions); |
| 56 | + return resp.position; |
| 57 | + } |
| 58 | + |
| 59 | + async getNumberOfPositions(extra = {}, callOptions = this.callOptions) { |
| 60 | + const request = new GetNumberOfPositionsRequest({ |
| 61 | + name: this.name, |
| 62 | + extra: Struct.fromJson(extra), |
| 63 | + }); |
| 64 | + |
| 65 | + this.options.requestLogger?.(request); |
| 66 | + |
| 67 | + const resp = await this.client.getNumberOfPositions(request, callOptions); |
| 68 | + return resp.numberOfPositions; |
| 69 | + } |
| 70 | + |
| 71 | + async doCommand( |
| 72 | + command: Struct, |
| 73 | + callOptions = this.callOptions |
| 74 | + ): Promise<JsonValue> { |
| 75 | + return doCommandFromClient( |
| 76 | + this.client.doCommand, |
| 77 | + this.name, |
| 78 | + command, |
| 79 | + this.options, |
| 80 | + callOptions |
| 81 | + ); |
| 82 | + } |
| 83 | +} |
0 commit comments