|
| 1 | +import { Struct } from 'google-protobuf/google/protobuf/struct_pb'; |
| 2 | +import type { RobotClient } from '../../robot'; |
| 3 | +import type { Encoder } from './Encoder'; |
| 4 | +import { EncoderServiceClient } from '../../gen/component/encoder/v1/encoder_pb_service'; |
| 5 | +import { type Options, PositionType } from '../../types'; |
| 6 | +import encoderApi from '../../gen/component/encoder/v1/encoder_pb'; |
| 7 | +import { promisify } from '../../utils'; |
| 8 | + |
| 9 | +/** |
| 10 | + * A gRPC-web client for the Encoder component. |
| 11 | + * |
| 12 | + * @group Clients |
| 13 | + */ |
| 14 | +export class EncoderClient implements Encoder { |
| 15 | + private client: EncoderServiceClient; |
| 16 | + private readonly name: string; |
| 17 | + private readonly options: Options; |
| 18 | + |
| 19 | + constructor(client: RobotClient, name: string, options: Options = {}) { |
| 20 | + this.client = client.createServiceClient(EncoderServiceClient); |
| 21 | + this.name = name; |
| 22 | + this.options = options; |
| 23 | + } |
| 24 | + |
| 25 | + private get encoderService() { |
| 26 | + return this.client; |
| 27 | + } |
| 28 | + |
| 29 | + async resetPosition(extra = {}) { |
| 30 | + const encoderService = this.encoderService; |
| 31 | + const request = new encoderApi.ResetPositionRequest(); |
| 32 | + request.setName(this.name); |
| 33 | + request.setExtra(Struct.fromJavaScript(extra)); |
| 34 | + |
| 35 | + this.options.requestLogger?.(request); |
| 36 | + |
| 37 | + await promisify< |
| 38 | + encoderApi.ResetPositionRequest, |
| 39 | + encoderApi.ResetPositionResponse |
| 40 | + >(encoderService.resetPosition.bind(encoderService), request); |
| 41 | + } |
| 42 | + |
| 43 | + async getProperties(extra = {}) { |
| 44 | + const encoderService = this.encoderService; |
| 45 | + const request = new encoderApi.GetPropertiesRequest(); |
| 46 | + request.setName(this.name); |
| 47 | + request.setExtra(Struct.fromJavaScript(extra)); |
| 48 | + |
| 49 | + this.options.requestLogger?.(request); |
| 50 | + |
| 51 | + const response = await promisify< |
| 52 | + encoderApi.GetPropertiesRequest, |
| 53 | + encoderApi.GetPropertiesResponse |
| 54 | + >(encoderService.getProperties.bind(encoderService), request); |
| 55 | + return response.toObject(); |
| 56 | + } |
| 57 | + |
| 58 | + async getPosition( |
| 59 | + positionType: PositionType = PositionType.POSITION_TYPE_UNSPECIFIED, |
| 60 | + extra = {} |
| 61 | + ) { |
| 62 | + const encoderService = this.encoderService; |
| 63 | + const request = new encoderApi.GetPositionRequest(); |
| 64 | + request.setName(this.name); |
| 65 | + request.setPositionType(positionType); |
| 66 | + request.setExtra(Struct.fromJavaScript(extra)); |
| 67 | + |
| 68 | + this.options.requestLogger?.(request); |
| 69 | + |
| 70 | + const response = await promisify< |
| 71 | + encoderApi.GetPositionRequest, |
| 72 | + encoderApi.GetPositionResponse |
| 73 | + >(encoderService.getPosition.bind(encoderService), request); |
| 74 | + return [response.getValue(), response.getPositionType()] as const; |
| 75 | + } |
| 76 | +} |
0 commit comments