diff --git a/src/components/arm/client.ts b/src/components/arm/client.ts index 8a0fc383d..dda9eb8bb 100644 --- a/src/components/arm/client.ts +++ b/src/components/arm/client.ts @@ -1,5 +1,5 @@ import { Struct, type JsonValue } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { ArmService } from '../../gen/component/arm/v1/arm_connect'; import { GetEndPositionRequest, @@ -24,6 +24,7 @@ export class ArmClient implements Arm { 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(ArmService); @@ -31,7 +32,7 @@ export class ArmClient implements Arm { this.options = options; } - async getEndPosition(extra = {}) { + async getEndPosition(extra = {}, callOptions = this.callOptions) { const request = new GetEndPositionRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -39,7 +40,7 @@ export class ArmClient implements Arm { this.options.requestLogger?.(request); - const response = await this.client.getEndPosition(request); + const response = await this.client.getEndPosition(request, callOptions); const result = response.pose; if (!result) { throw new Error('no pose'); @@ -47,7 +48,7 @@ export class ArmClient implements Arm { return result; } - async moveToPosition(pose: Pose, extra = {}) { + async moveToPosition(pose: Pose, extra = {}, callOptions = this.callOptions) { const request = new MoveToPositionRequest({ name: this.name, to: pose, @@ -56,10 +57,14 @@ export class ArmClient implements Arm { this.options.requestLogger?.(request); - await this.client.moveToPosition(request); + await this.client.moveToPosition(request, callOptions); } - async moveToJointPositions(jointPositionsList: number[], extra = {}) { + async moveToJointPositions( + jointPositionsList: number[], + extra = {}, + callOptions = this.callOptions + ) { const newJointPositions = new JointPositions({ values: jointPositionsList, }); @@ -72,10 +77,10 @@ export class ArmClient implements Arm { this.options.requestLogger?.(request); - await this.client.moveToJointPositions(request); + await this.client.moveToJointPositions(request, callOptions); } - async getJointPositions(extra = {}) { + async getJointPositions(extra = {}, callOptions = this.callOptions) { const request = new GetJointPositionsRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -83,7 +88,7 @@ export class ArmClient implements Arm { this.options.requestLogger?.(request); - const response = await this.client.getJointPositions(request); + const response = await this.client.getJointPositions(request, callOptions); const result = response.positions; if (!result) { @@ -92,7 +97,7 @@ export class ArmClient implements Arm { return result; } - async stop(extra = {}) { + async stop(extra = {}, callOptions = this.callOptions) { const request = new StopRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -100,26 +105,30 @@ export class ArmClient implements Arm { this.options.requestLogger?.(request); - await this.client.stop(request); + await this.client.stop(request, callOptions); } - async isMoving() { + async isMoving(callOptions = this.callOptions) { const request = new IsMovingRequest({ name: this.name, }); this.options.requestLogger?.(request); - const resp = await this.client.isMoving(request); + const resp = await this.client.isMoving(request, callOptions); return resp.isMoving; } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } } diff --git a/src/components/base/base.ts b/src/components/base/base.ts index 3e99d89c3..c9ad3f29c 100644 --- a/src/components/base/base.ts +++ b/src/components/base/base.ts @@ -51,7 +51,7 @@ export interface Base extends Resource { stop(extra?: Struct): Promise; /** Return true if the base is in motion. */ - isMoving(extra?: Struct): Promise; + isMoving(): Promise; /** Return the base's properties. */ getProperties(extra?: Struct): Promise; diff --git a/src/components/base/client.ts b/src/components/base/client.ts index 6e0fa05c2..9754db38d 100644 --- a/src/components/base/client.ts +++ b/src/components/base/client.ts @@ -1,5 +1,5 @@ import { Struct, type JsonValue } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { BaseService } from '../../gen/component/base/v1/base_connect'; import { GetPropertiesRequest, @@ -24,6 +24,7 @@ export class BaseClient implements Base { 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(BaseService); @@ -31,7 +32,12 @@ export class BaseClient implements Base { this.options = options; } - async moveStraight(distanceMm: number, mmPerSec: number, extra = {}) { + async moveStraight( + distanceMm: number, + mmPerSec: number, + extra = {}, + callOptions = this.callOptions + ) { const request = new MoveStraightRequest({ name: this.name, mmPerSec, @@ -41,10 +47,15 @@ export class BaseClient implements Base { this.options.requestLogger?.(request); - await this.client.moveStraight(request); + await this.client.moveStraight(request, callOptions); } - async spin(angleDeg: number, degsPerSec: number, extra = {}) { + async spin( + angleDeg: number, + degsPerSec: number, + extra = {}, + callOptions = this.callOptions + ) { const request = new SpinRequest({ name: this.name, angleDeg, @@ -54,10 +65,15 @@ export class BaseClient implements Base { this.options.requestLogger?.(request); - await this.client.spin(request); + await this.client.spin(request, callOptions); } - async setPower(linear: Vector3, angular: Vector3, extra = {}) { + async setPower( + linear: Vector3, + angular: Vector3, + extra = {}, + callOptions = this.callOptions + ) { const request = new SetPowerRequest({ name: this.name, linear, @@ -67,10 +83,15 @@ export class BaseClient implements Base { this.options.requestLogger?.(request); - await this.client.setPower(request); + await this.client.setPower(request, callOptions); } - async setVelocity(linear: Vector3, angular: Vector3, extra = {}) { + async setVelocity( + linear: Vector3, + angular: Vector3, + extra = {}, + callOptions = this.callOptions + ) { const request = new SetVelocityRequest({ name: this.name, linear, @@ -80,10 +101,10 @@ export class BaseClient implements Base { this.options.requestLogger?.(request); - await this.client.setVelocity(request); + await this.client.setVelocity(request, callOptions); } - async stop(extra = {}) { + async stop(extra = {}, callOptions = this.callOptions) { const request = new StopRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -91,30 +112,34 @@ export class BaseClient implements Base { this.options.requestLogger?.(request); - await this.client.stop(request); + await this.client.stop(request, callOptions); } - async isMoving() { + async isMoving(callOptions = this.callOptions) { const request = new IsMovingRequest({ name: this.name, }); this.options.requestLogger?.(request); - const resp = await this.client.isMoving(request); + const resp = await this.client.isMoving(request, callOptions); return resp.isMoving; } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } - async getProperties(extra = {}) { + async getProperties(extra = {}, callOptions = this.callOptions) { const request = new GetPropertiesRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -122,6 +147,6 @@ export class BaseClient implements Base { this.options.requestLogger?.(request); - return this.client.getProperties(request); + return this.client.getProperties(request, callOptions); } } diff --git a/src/components/board/client.ts b/src/components/board/client.ts index fc0861a91..2a34a719f 100644 --- a/src/components/board/client.ts +++ b/src/components/board/client.ts @@ -2,7 +2,7 @@ import type { RobotClient } from '../../robot'; import type { Options } from '../../types'; import { Duration, Struct, type JsonValue } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { BoardService } from '../../gen/component/board/v1/board_connect'; import { GetDigitalInterruptValueRequest, @@ -29,6 +29,7 @@ export class BoardClient implements Board { 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(BoardService); @@ -36,7 +37,12 @@ export class BoardClient implements Board { this.options = options; } - async setGPIO(pin: string, high: boolean, extra = {}) { + async setGPIO( + pin: string, + high: boolean, + extra = {}, + callOptions = this.callOptions + ) { const request = new SetGPIORequest({ name: this.name, pin, @@ -46,10 +52,10 @@ export class BoardClient implements Board { this.options.requestLogger?.(request); - await this.client.setGPIO(request); + await this.client.setGPIO(request, callOptions); } - async getGPIO(pin: string, extra = {}) { + async getGPIO(pin: string, extra = {}, callOptions = this.callOptions) { const request = new GetGPIORequest({ name: this.name, pin, @@ -58,11 +64,11 @@ export class BoardClient implements Board { this.options.requestLogger?.(request); - const resp = await this.client.getGPIO(request); + const resp = await this.client.getGPIO(request, callOptions); return resp.high; } - async getPWM(pin: string, extra = {}) { + async getPWM(pin: string, extra = {}, callOptions = this.callOptions) { const request = new PWMRequest({ name: this.name, pin, @@ -71,11 +77,16 @@ export class BoardClient implements Board { this.options.requestLogger?.(request); - const resp = await this.client.pWM(request); + const resp = await this.client.pWM(request, callOptions); return resp.dutyCyclePct; } - async setPWM(pin: string, dutyCyle: number, extra = {}) { + async setPWM( + pin: string, + dutyCyle: number, + extra = {}, + callOptions = this.callOptions + ) { const request = new SetPWMRequest({ name: this.name, pin, @@ -85,10 +96,14 @@ export class BoardClient implements Board { this.options.requestLogger?.(request); - await this.client.setPWM(request); + await this.client.setPWM(request, callOptions); } - async getPWMFrequency(pin: string, extra = {}) { + async getPWMFrequency( + pin: string, + extra = {}, + callOptions = this.callOptions + ) { const request = new PWMFrequencyRequest({ name: this.name, pin, @@ -97,11 +112,16 @@ export class BoardClient implements Board { this.options.requestLogger?.(request); - const resp = await this.client.pWMFrequency(request); + const resp = await this.client.pWMFrequency(request, callOptions); return Number(resp.frequencyHz); } - async setPWMFrequency(pin: string, frequencyHz: number, extra = {}) { + async setPWMFrequency( + pin: string, + frequencyHz: number, + extra = {}, + callOptions = this.callOptions + ) { const request = new SetPWMFrequencyRequest({ name: this.name, pin, @@ -111,10 +131,14 @@ export class BoardClient implements Board { this.options.requestLogger?.(request); - await this.client.setPWMFrequency(request); + await this.client.setPWMFrequency(request, callOptions); } - async readAnalogReader(analogReader: string, extra = {}) { + async readAnalogReader( + analogReader: string, + extra = {}, + callOptions = this.callOptions + ) { const request = new ReadAnalogReaderRequest({ boardName: this.name, analogReaderName: analogReader, @@ -123,10 +147,15 @@ export class BoardClient implements Board { this.options.requestLogger?.(request); - return this.client.readAnalogReader(request); + return this.client.readAnalogReader(request, callOptions); } - async writeAnalog(pin: string, value: number, extra = {}) { + async writeAnalog( + pin: string, + value: number, + extra = {}, + callOptions = this.callOptions + ) { const request = new WriteAnalogRequest({ name: this.name, pin, @@ -136,10 +165,14 @@ export class BoardClient implements Board { this.options.requestLogger?.(request); - await this.client.writeAnalog(request); + await this.client.writeAnalog(request, callOptions); } - async getDigitalInterruptValue(digitalInterruptName: string, extra = {}) { + async getDigitalInterruptValue( + digitalInterruptName: string, + extra = {}, + callOptions = this.callOptions + ) { const request = new GetDigitalInterruptValueRequest({ boardName: this.name, digitalInterruptName, @@ -148,18 +181,26 @@ export class BoardClient implements Board { this.options.requestLogger?.(request); - const resp = await this.client.getDigitalInterruptValue(request); + const resp = await this.client.getDigitalInterruptValue( + request, + callOptions + ); return Number(resp.value); } - async streamTicks(interrupts: string[], queue: Tick[], extra = {}) { + async streamTicks( + interrupts: string[], + queue: Tick[], + extra = {}, + callOptions = this.callOptions + ) { const request = new StreamTicksRequest({ name: this.name, pinNames: interrupts, extra: Struct.fromJson(extra), }); this.options.requestLogger?.(request); - const stream = this.client.streamTicks(request); + const stream = this.client.streamTicks(request, callOptions); for await (const latest of stream) { queue.push({ @@ -170,7 +211,12 @@ export class BoardClient implements Board { } } - async setPowerMode(powerMode: PowerMode, duration?: Duration, extra = {}) { + async setPowerMode( + powerMode: PowerMode, + duration?: Duration, + extra = {}, + callOptions = this.callOptions + ) { const request = new SetPowerModeRequest({ name: this.name, powerMode, @@ -180,15 +226,19 @@ export class BoardClient implements Board { this.options.requestLogger?.(request); - await this.client.setPowerMode(request); + await this.client.setPowerMode(request, callOptions); } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } } diff --git a/src/components/camera/client.ts b/src/components/camera/client.ts index 8817483d8..26fea0ed2 100644 --- a/src/components/camera/client.ts +++ b/src/components/camera/client.ts @@ -1,5 +1,5 @@ import type { JsonValue, Struct } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { GetPropertiesRequest } from '../../gen/component/base/v1/base_pb'; import { CameraService } from '../../gen/component/camera/v1/camera_connect'; import { @@ -23,6 +23,7 @@ export class CameraClient implements Camera { 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(CameraService); @@ -30,7 +31,7 @@ export class CameraClient implements Camera { this.options = options; } - async getImage(mimeType: MimeType = '') { + async getImage(mimeType: MimeType = '', callOptions = this.callOptions) { const request = new GetImageRequest({ name: this.name, mimeType, @@ -38,11 +39,11 @@ export class CameraClient implements Camera { this.options.requestLogger?.(request); - const resp = await this.client.getImage(request); + const resp = await this.client.getImage(request, callOptions); return resp.image; } - async renderFrame(mimeType: MimeType = '') { + async renderFrame(mimeType: MimeType = '', callOptions = this.callOptions) { const request = new RenderFrameRequest({ name: this.name, mimeType, @@ -50,11 +51,11 @@ export class CameraClient implements Camera { this.options.requestLogger?.(request); - const resp = await this.client.renderFrame(request); + const resp = await this.client.renderFrame(request, callOptions); return new Blob([resp.data], { type: mimeType }); } - async getPointCloud() { + async getPointCloud(callOptions = this.callOptions) { const request = new GetPointCloudRequest({ name: this.name, mimeType: PointCloudPCD, @@ -62,26 +63,30 @@ export class CameraClient implements Camera { this.options.requestLogger?.(request); - const resp = await this.client.getPointCloud(request); + const resp = await this.client.getPointCloud(request, callOptions); return resp.pointCloud; } - async getProperties() { + async getProperties(callOptions = this.callOptions) { const request = new GetPropertiesRequest({ name: this.name, }); this.options.requestLogger?.(request); - return this.client.getProperties(request); + return this.client.getProperties(request, callOptions); } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } } diff --git a/src/components/encoder/client.ts b/src/components/encoder/client.ts index beb0fc86b..90a5d9349 100644 --- a/src/components/encoder/client.ts +++ b/src/components/encoder/client.ts @@ -1,5 +1,5 @@ import { Struct, type JsonValue } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { EncoderService } from '../../gen/component/encoder/v1/encoder_connect'; import { GetPositionRequest, @@ -20,6 +20,7 @@ export class EncoderClient implements Encoder { 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(EncoderService); @@ -27,7 +28,7 @@ export class EncoderClient implements Encoder { this.options = options; } - async resetPosition(extra = {}) { + async resetPosition(extra = {}, callOptions = this.callOptions) { const request = new ResetPositionRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -35,10 +36,10 @@ export class EncoderClient implements Encoder { this.options.requestLogger?.(request); - await this.client.resetPosition(request); + await this.client.resetPosition(request, callOptions); } - async getProperties(extra = {}) { + async getProperties(extra = {}, callOptions = this.callOptions) { const request = new GetPropertiesRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -46,12 +47,13 @@ export class EncoderClient implements Encoder { this.options.requestLogger?.(request); - return this.client.getProperties(request); + return this.client.getProperties(request, callOptions); } async getPosition( positionType: EncoderPositionType = EncoderPositionType.UNSPECIFIED, - extra = {} + extra = {}, + callOptions = this.callOptions ) { const request = new GetPositionRequest({ name: this.name, @@ -61,16 +63,20 @@ export class EncoderClient implements Encoder { this.options.requestLogger?.(request); - const response = await this.client.getPosition(request); + const response = await this.client.getPosition(request, callOptions); return [response.value, response.positionType] as const; } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } } diff --git a/src/components/gantry/client.ts b/src/components/gantry/client.ts index 162a3cc0e..d5a4d018d 100644 --- a/src/components/gantry/client.ts +++ b/src/components/gantry/client.ts @@ -1,5 +1,5 @@ import { Struct, type JsonValue } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { GantryService } from '../../gen/component/gantry/v1/gantry_connect'; import { GetLengthsRequest, @@ -23,6 +23,7 @@ export class GantryClient implements Gantry { 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(GantryService); @@ -30,7 +31,7 @@ export class GantryClient implements Gantry { this.options = options; } - async getPosition(extra = {}) { + async getPosition(extra = {}, callOptions = this.callOptions) { const request = new GetPositionRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -38,14 +39,15 @@ export class GantryClient implements Gantry { this.options.requestLogger?.(request); - const resp = await this.client.getPosition(request); + const resp = await this.client.getPosition(request, callOptions); return resp.positionsMm; } async moveToPosition( positionsMm: number[], speedsMmPerSec: number[], - extra = {} + extra = {}, + callOptions = this.callOptions ) { const request = new MoveToPositionRequest({ name: this.name, @@ -56,10 +58,10 @@ export class GantryClient implements Gantry { this.options.requestLogger?.(request); - await this.client.moveToPosition(request); + await this.client.moveToPosition(request, callOptions); } - async home(extra = {}) { + async home(extra = {}, callOptions = this.callOptions) { const request = new HomeRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -67,11 +69,11 @@ export class GantryClient implements Gantry { this.options.requestLogger?.(request); - const resp = await this.client.home(request); + const resp = await this.client.home(request, callOptions); return resp.homed; } - async getLengths(extra = {}) { + async getLengths(extra = {}, callOptions = this.callOptions) { const request = new GetLengthsRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -79,11 +81,11 @@ export class GantryClient implements Gantry { this.options.requestLogger?.(request); - const resp = await this.client.getLengths(request); + const resp = await this.client.getLengths(request, callOptions); return resp.lengthsMm; } - async stop(extra = {}) { + async stop(extra = {}, callOptions = this.callOptions) { const request = new StopRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -91,26 +93,30 @@ export class GantryClient implements Gantry { this.options.requestLogger?.(request); - await this.client.stop(request); + await this.client.stop(request, callOptions); } - async isMoving() { + async isMoving(callOptions = this.callOptions) { const request = new IsMovingRequest({ name: this.name, }); this.options.requestLogger?.(request); - const resp = await this.client.isMoving(request); + const resp = await this.client.isMoving(request, callOptions); return resp.isMoving; } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } } diff --git a/src/components/generic/client.ts b/src/components/generic/client.ts index c1a0926b8..62025e3c3 100644 --- a/src/components/generic/client.ts +++ b/src/components/generic/client.ts @@ -1,5 +1,5 @@ import type { JsonValue, Struct } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { GenericService } from '../../gen/component/generic/v1/generic_connect'; import type { RobotClient } from '../../robot'; import type { Options } from '../../types'; @@ -15,6 +15,7 @@ export class GenericClient implements Generic { 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(GenericService); @@ -22,12 +23,16 @@ export class GenericClient implements Generic { this.options = options; } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } } diff --git a/src/components/gripper/client.ts b/src/components/gripper/client.ts index 832229491..d172ea3a2 100644 --- a/src/components/gripper/client.ts +++ b/src/components/gripper/client.ts @@ -1,5 +1,5 @@ import { Struct, type JsonValue } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { GripperService } from '../../gen/component/gripper/v1/gripper_connect'; import { GrabRequest, @@ -21,6 +21,7 @@ export class GripperClient implements Gripper { 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(GripperService); @@ -28,7 +29,7 @@ export class GripperClient implements Gripper { this.options = options; } - async open(extra = {}) { + async open(extra = {}, callOptions = this.callOptions) { const request = new OpenRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -36,10 +37,10 @@ export class GripperClient implements Gripper { this.options.requestLogger?.(request); - await this.client.open(request); + await this.client.open(request, callOptions); } - async grab(extra = {}) { + async grab(extra = {}, callOptions = this.callOptions) { const request = new GrabRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -47,10 +48,10 @@ export class GripperClient implements Gripper { this.options.requestLogger?.(request); - await this.client.grab(request); + await this.client.grab(request, callOptions); } - async stop(extra = {}) { + async stop(extra = {}, callOptions = this.callOptions) { const request = new StopRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -58,26 +59,30 @@ export class GripperClient implements Gripper { this.options.requestLogger?.(request); - await this.client.stop(request); + await this.client.stop(request, callOptions); } - async isMoving() { + async isMoving(callOptions = this.callOptions) { const request = new IsMovingRequest({ name: this.name, }); this.options.requestLogger?.(request); - const resp = await this.client.isMoving(request); + const resp = await this.client.isMoving(request, callOptions); return resp.isMoving; } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } } diff --git a/src/components/input-controller/client.ts b/src/components/input-controller/client.ts index 7e5605a1f..01be98511 100644 --- a/src/components/input-controller/client.ts +++ b/src/components/input-controller/client.ts @@ -2,7 +2,7 @@ import type { RobotClient } from '../../robot'; import type { Options } from '../../types'; import { Struct, type JsonValue } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { InputControllerService } from '../../gen/component/inputcontroller/v1/input_controller_connect'; import { GetEventsRequest, @@ -20,6 +20,7 @@ export class InputControllerClient implements InputController { 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(InputControllerService); @@ -27,7 +28,7 @@ export class InputControllerClient implements InputController { this.options = options; } - async getEvents(extra = {}) { + async getEvents(extra = {}, callOptions = this.callOptions) { const request = new GetEventsRequest({ controller: this.name, extra: Struct.fromJson(extra), @@ -35,11 +36,15 @@ export class InputControllerClient implements InputController { this.options.requestLogger?.(request); - const resp = await this.client.getEvents(request); + const resp = await this.client.getEvents(request, callOptions); return resp.events; } - async triggerEvent(event: InputControllerEvent, extra = {}): Promise { + async triggerEvent( + event: InputControllerEvent, + extra = {}, + callOptions = this.callOptions + ): Promise { const request = new TriggerEventRequest({ controller: this.name, event, @@ -48,15 +53,19 @@ export class InputControllerClient implements InputController { this.options.requestLogger?.(request); - await this.client.triggerEvent(request); + await this.client.triggerEvent(request, callOptions); } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } } diff --git a/src/components/motor/client.ts b/src/components/motor/client.ts index 617387816..6530908f7 100644 --- a/src/components/motor/client.ts +++ b/src/components/motor/client.ts @@ -1,5 +1,5 @@ import { Struct, type JsonValue } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { MotorService } from '../../gen/component/motor/v1/motor_connect'; import { GetPositionRequest, @@ -27,6 +27,7 @@ export class MotorClient implements Motor { 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(MotorService); @@ -34,7 +35,7 @@ export class MotorClient implements Motor { this.options = options; } - async setPower(power: number, extra = {}) { + async setPower(power: number, extra = {}, callOptions = this.callOptions) { const request = new SetPowerRequest({ name: this.name, powerPct: power, @@ -43,10 +44,15 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - await this.client.setPower(request); + await this.client.setPower(request, callOptions); } - async goFor(rpm: number, revolutions: number, extra = {}) { + async goFor( + rpm: number, + revolutions: number, + extra = {}, + callOptions = this.callOptions + ) { const request = new GoForRequest({ name: this.name, rpm, @@ -56,10 +62,15 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - await this.client.goFor(request); + await this.client.goFor(request, callOptions); } - async goTo(rpm: number, positionRevolutions: number, extra = {}) { + async goTo( + rpm: number, + positionRevolutions: number, + extra = {}, + callOptions = this.callOptions + ) { const request = new GoToRequest({ name: this.name, rpm, @@ -69,10 +80,10 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - await this.client.goTo(request); + await this.client.goTo(request, callOptions); } - async setRPM(rpm: number, extra = {}) { + async setRPM(rpm: number, extra = {}, callOptions = this.callOptions) { const request = new SetRPMRequest({ name: this.name, rpm, @@ -81,10 +92,14 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - await this.client.setRPM(request); + await this.client.setRPM(request, callOptions); } - async resetZeroPosition(offset: number, extra = {}) { + async resetZeroPosition( + offset: number, + extra = {}, + callOptions = this.callOptions + ) { const request = new ResetZeroPositionRequest({ name: this.name, offset, @@ -93,10 +108,10 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - await this.client.resetZeroPosition(request); + await this.client.resetZeroPosition(request, callOptions); } - async stop(extra = {}) { + async stop(extra = {}, callOptions = this.callOptions) { const request = new StopRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -104,10 +119,10 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - await this.client.stop(request); + await this.client.stop(request, callOptions); } - async getProperties(extra = {}) { + async getProperties(extra = {}, callOptions = this.callOptions) { const request = new GetPropertiesRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -115,13 +130,13 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - const resp = await this.client.getProperties(request); + const resp = await this.client.getProperties(request, callOptions); return { positionReporting: resp.positionReporting, }; } - async getPosition(extra = {}) { + async getPosition(extra = {}, callOptions = this.callOptions) { const request = new GetPositionRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -129,11 +144,11 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - const resp = await this.client.getPosition(request); + const resp = await this.client.getPosition(request, callOptions); return resp.position; } - async isPowered(extra = {}) { + async isPowered(extra = {}, callOptions = this.callOptions) { const request = new IsPoweredRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -141,27 +156,31 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - const response = await this.client.isPowered(request); + const response = await this.client.isPowered(request, callOptions); return [response.isOn, response.powerPct] as const; } - async isMoving() { + async isMoving(callOptions = this.callOptions) { const request = new IsMovingRequest({ name: this.name, }); this.options.requestLogger?.(request); - const resp = await this.client.isMoving(request); + const resp = await this.client.isMoving(request, callOptions); return resp.isMoving; } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } } diff --git a/src/components/movement-sensor/client.ts b/src/components/movement-sensor/client.ts index 6dd59d4ed..a0ca31ea3 100644 --- a/src/components/movement-sensor/client.ts +++ b/src/components/movement-sensor/client.ts @@ -1,5 +1,5 @@ import { Struct, type JsonValue } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { GetReadingsRequest } from '../../gen/common/v1/common_pb'; import { MovementSensorService } from '../../gen/component/movementsensor/v1/movementsensor_connect'; import { @@ -26,6 +26,7 @@ export class MovementSensorClient implements MovementSensor { 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(MovementSensorService); @@ -33,7 +34,7 @@ export class MovementSensorClient implements MovementSensor { this.options = options; } - async getLinearVelocity(extra = {}) { + async getLinearVelocity(extra = {}, callOptions = this.callOptions) { const request = new GetLinearVelocityRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -41,7 +42,7 @@ export class MovementSensorClient implements MovementSensor { this.options.requestLogger?.(request); - const response = await this.client.getLinearVelocity(request); + const response = await this.client.getLinearVelocity(request, callOptions); const vel = response.linearVelocity; if (!vel) { @@ -51,7 +52,7 @@ export class MovementSensorClient implements MovementSensor { return vel; } - async getAngularVelocity(extra = {}) { + async getAngularVelocity(extra = {}, callOptions = this.callOptions) { const request = new GetAngularVelocityRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -59,7 +60,7 @@ export class MovementSensorClient implements MovementSensor { this.options.requestLogger?.(request); - const response = await this.client.getAngularVelocity(request); + const response = await this.client.getAngularVelocity(request, callOptions); const ang = response.angularVelocity; if (!ang) { @@ -69,7 +70,7 @@ export class MovementSensorClient implements MovementSensor { return ang; } - async getCompassHeading(extra = {}) { + async getCompassHeading(extra = {}, callOptions = this.callOptions) { const request = new GetCompassHeadingRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -77,11 +78,11 @@ export class MovementSensorClient implements MovementSensor { this.options.requestLogger?.(request); - const resp = await this.client.getCompassHeading(request); + const resp = await this.client.getCompassHeading(request, callOptions); return resp.value; } - async getOrientation(extra = {}) { + async getOrientation(extra = {}, callOptions = this.callOptions) { const request = new GetOrientationRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -89,7 +90,7 @@ export class MovementSensorClient implements MovementSensor { this.options.requestLogger?.(request); - const response = await this.client.getOrientation(request); + const response = await this.client.getOrientation(request, callOptions); const ori = response.orientation; if (!ori) { @@ -99,7 +100,7 @@ export class MovementSensorClient implements MovementSensor { return ori; } - async getPosition(extra = {}) { + async getPosition(extra = {}, callOptions = this.callOptions) { const request = new GetPositionRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -107,10 +108,10 @@ export class MovementSensorClient implements MovementSensor { this.options.requestLogger?.(request); - return this.client.getPosition(request); + return this.client.getPosition(request, callOptions); } - async getProperties(extra = {}) { + async getProperties(extra = {}, callOptions = this.callOptions) { const request = new GetPropertiesRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -118,10 +119,10 @@ export class MovementSensorClient implements MovementSensor { this.options.requestLogger?.(request); - return this.client.getProperties(request); + return this.client.getProperties(request, callOptions); } - async getAccuracy(extra = {}) { + async getAccuracy(extra = {}, callOptions = this.callOptions) { const request = new GetAccuracyRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -129,10 +130,10 @@ export class MovementSensorClient implements MovementSensor { this.options.requestLogger?.(request); - return this.client.getAccuracy(request); + return this.client.getAccuracy(request, callOptions); } - async getLinearAcceleration(extra = {}) { + async getLinearAcceleration(extra = {}, callOptions = this.callOptions) { const request = new GetLinearAccelerationRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -140,7 +141,10 @@ export class MovementSensorClient implements MovementSensor { this.options.requestLogger?.(request); - const response = await this.client.getLinearAcceleration(request); + const response = await this.client.getLinearAcceleration( + request, + callOptions + ); const acc = response.linearAcceleration; if (!acc) { @@ -150,7 +154,7 @@ export class MovementSensorClient implements MovementSensor { return acc; } - async getReadings(extra = {}) { + async getReadings(extra = {}, callOptions = this.callOptions) { const request = new GetReadingsRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -158,7 +162,7 @@ export class MovementSensorClient implements MovementSensor { this.options.requestLogger?.(request); - const response = await this.client.getReadings(request); + const response = await this.client.getReadings(request, callOptions); const result: Record = {}; for (const key of Object.keys(response.readings)) { @@ -171,12 +175,16 @@ export class MovementSensorClient implements MovementSensor { return result; } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } } diff --git a/src/components/power-sensor/client.ts b/src/components/power-sensor/client.ts index 14b3c9a18..e8035f065 100644 --- a/src/components/power-sensor/client.ts +++ b/src/components/power-sensor/client.ts @@ -1,5 +1,5 @@ import { Struct, type JsonValue } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { GetReadingsRequest } from '../../gen/common/v1/common_pb'; import { PowerSensorService } from '../../gen/component/powersensor/v1/powersensor_connect'; import { @@ -22,6 +22,7 @@ export class PowerSensorClient implements PowerSensor { 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(PowerSensorService); @@ -29,7 +30,7 @@ export class PowerSensorClient implements PowerSensor { this.options = options; } - async getVoltage(extra = {}) { + async getVoltage(extra = {}, callOptions = this.callOptions) { const request = new GetVoltageRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -37,12 +38,12 @@ export class PowerSensorClient implements PowerSensor { this.options.requestLogger?.(request); - const response = await this.client.getVoltage(request); + const response = await this.client.getVoltage(request, callOptions); return [response.volts, response.isAc] as const; } - async getCurrent(extra = {}) { + async getCurrent(extra = {}, callOptions = this.callOptions) { const request = new GetCurrentRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -50,12 +51,12 @@ export class PowerSensorClient implements PowerSensor { this.options.requestLogger?.(request); - const response = await this.client.getCurrent(request); + const response = await this.client.getCurrent(request, callOptions); return [response.amperes, response.isAc] as const; } - async getPower(extra = {}) { + async getPower(extra = {}, callOptions = this.callOptions) { const request = new GetPowerRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -63,11 +64,11 @@ export class PowerSensorClient implements PowerSensor { this.options.requestLogger?.(request); - const resp = await this.client.getPower(request); + const resp = await this.client.getPower(request, callOptions); return resp.watts; } - async getReadings(extra = {}) { + async getReadings(extra = {}, callOptions = this.callOptions) { const request = new GetReadingsRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -75,7 +76,7 @@ export class PowerSensorClient implements PowerSensor { this.options.requestLogger?.(request); - const response = await this.client.getReadings(request); + const response = await this.client.getReadings(request, callOptions); const result: Record = {}; for (const key of Object.keys(response.readings)) { @@ -88,12 +89,16 @@ export class PowerSensorClient implements PowerSensor { return result; } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } } diff --git a/src/components/sensor/client.ts b/src/components/sensor/client.ts index d68bd05e1..65072930d 100644 --- a/src/components/sensor/client.ts +++ b/src/components/sensor/client.ts @@ -2,7 +2,7 @@ import type { RobotClient } from '../../robot'; import type { Options } from '../../types'; import { Struct, type JsonValue } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { GetReadingsRequest } from '../../gen/common/v1/common_pb'; import { SensorService } from '../../gen/component/sensor/v1/sensor_connect'; import { doCommandFromClient } from '../../utils'; @@ -17,6 +17,7 @@ export class SensorClient implements Sensor { 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(SensorService); @@ -24,7 +25,7 @@ export class SensorClient implements Sensor { this.options = options; } - async getReadings(extra = {}) { + async getReadings(extra = {}, callOptions = this.callOptions) { const request = new GetReadingsRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -32,7 +33,7 @@ export class SensorClient implements Sensor { this.options.requestLogger?.(request); - const response = await this.client.getReadings(request); + const response = await this.client.getReadings(request, callOptions); const result: Record = {}; for (const key of Object.keys(response.readings)) { @@ -45,12 +46,16 @@ export class SensorClient implements Sensor { return result; } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } } diff --git a/src/components/servo/client.ts b/src/components/servo/client.ts index 03c280844..b91a5fddd 100644 --- a/src/components/servo/client.ts +++ b/src/components/servo/client.ts @@ -1,5 +1,5 @@ import { Struct, type JsonValue } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { ServoService } from '../../gen/component/servo/v1/servo_connect'; import { GetPositionRequest, @@ -21,6 +21,7 @@ export class ServoClient implements Servo { 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(ServoService); @@ -28,7 +29,7 @@ export class ServoClient implements Servo { this.options = options; } - async move(angleDeg: number, extra = {}) { + async move(angleDeg: number, extra = {}, callOptions = this.callOptions) { const request = new MoveRequest({ name: this.name, angleDeg, @@ -37,10 +38,10 @@ export class ServoClient implements Servo { this.options.requestLogger?.(request); - await this.client.move(request); + await this.client.move(request, callOptions); } - async getPosition(extra = {}) { + async getPosition(extra = {}, callOptions = this.callOptions) { const request = new GetPositionRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -48,11 +49,11 @@ export class ServoClient implements Servo { this.options.requestLogger?.(request); - const resp = await this.client.getPosition(request); + const resp = await this.client.getPosition(request, callOptions); return resp.positionDeg; } - async stop(extra = {}) { + async stop(extra = {}, callOptions = this.callOptions) { const request = new StopRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -60,26 +61,30 @@ export class ServoClient implements Servo { this.options.requestLogger?.(request); - await this.client.stop(request); + await this.client.stop(request, callOptions); } - async isMoving() { + async isMoving(callOptions = this.callOptions) { const request = new IsMovingRequest({ name: this.name, }); this.options.requestLogger?.(request); - const resp = await this.client.isMoving(request); + const resp = await this.client.isMoving(request, callOptions); return resp.isMoving; } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } } diff --git a/src/main.ts b/src/main.ts index 7975f5c4f..9de610370 100644 --- a/src/main.ts +++ b/src/main.ts @@ -439,6 +439,12 @@ export * as commonApi from './gen/common/v1/common_pb'; export * from './types'; -export { doCommandFromClient } from './utils'; +export { + addMetadata, + deleteMetadata, + disableDebugLogging, + doCommandFromClient, + enableDebugLogging, +} from './utils'; export { MachineConnectionEvent } from './events'; diff --git a/src/services/data-manager/client.ts b/src/services/data-manager/client.ts index 42aeb7b73..0fd3913e2 100644 --- a/src/services/data-manager/client.ts +++ b/src/services/data-manager/client.ts @@ -1,5 +1,5 @@ import { Struct, type JsonValue } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { DataManagerService } from '../../gen/service/datamanager/v1/data_manager_connect.js'; import { SyncRequest } from '../../gen/service/datamanager/v1/data_manager_pb.js'; import type { RobotClient } from '../../robot'; @@ -11,6 +11,7 @@ export class DataManagerClient implements DataManager { 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(DataManagerService); @@ -18,7 +19,7 @@ export class DataManagerClient implements DataManager { this.options = options; } - async sync(extra = {}) { + async sync(extra = {}, callOptions = this.callOptions) { const request = new SyncRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -26,15 +27,19 @@ export class DataManagerClient implements DataManager { this.options.requestLogger?.(request); - await this.client.sync(request); + await this.client.sync(request, callOptions); } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } } diff --git a/src/services/generic/client.ts b/src/services/generic/client.ts index 3539d9b70..2ceec0891 100644 --- a/src/services/generic/client.ts +++ b/src/services/generic/client.ts @@ -1,5 +1,5 @@ import type { JsonValue, Struct } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { GenericService } from '../../gen/component/generic/v1/generic_connect'; import { RobotClient } from '../../robot'; import type { Options } from '../../types'; @@ -15,6 +15,7 @@ export class GenericClient implements Generic { 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(GenericService); @@ -22,12 +23,16 @@ export class GenericClient implements Generic { this.options = options; } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } } diff --git a/src/services/motion/client.ts b/src/services/motion/client.ts index a1a523844..9f0c949fb 100644 --- a/src/services/motion/client.ts +++ b/src/services/motion/client.ts @@ -1,5 +1,5 @@ import { Struct, type JsonValue } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { MotionService } from '../../gen/service/motion/v1/motion_connect'; import { GetPlanRequest, @@ -35,6 +35,7 @@ export class MotionClient implements Motion { 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(MotionService); @@ -47,7 +48,8 @@ export class MotionClient implements Motion { componentName: ResourceName, worldState?: WorldState, constraints?: Constraints, - extra = {} + extra = {}, + callOptions = this.callOptions ) { const request = new MoveRequest({ name: this.name, @@ -60,7 +62,7 @@ export class MotionClient implements Motion { this.options.requestLogger?.(request); - const resp = await this.client.move(request); + const resp = await this.client.move(request, callOptions); return resp.success; } @@ -70,7 +72,8 @@ export class MotionClient implements Motion { slamServiceName: ResourceName, motionConfig?: MotionConfiguration, obstacles?: Geometry[], - extra = {} + extra = {}, + callOptions = this.callOptions ) { const request = new MoveOnMapRequest({ name: this.name, @@ -84,7 +87,7 @@ export class MotionClient implements Motion { this.options.requestLogger?.(request); - const resp = await this.client.moveOnMap(request); + const resp = await this.client.moveOnMap(request, callOptions); return resp.executionId; } @@ -96,7 +99,8 @@ export class MotionClient implements Motion { obstaclesList?: GeoGeometry[], motionConfig?: MotionConfiguration, boundingRegionsList?: GeoGeometry[], - extra = {} + extra = {}, + callOptions = this.callOptions ) { const request = new MoveOnGlobeRequest({ name: this.name, @@ -112,11 +116,15 @@ export class MotionClient implements Motion { this.options.requestLogger?.(request); - const resp = await this.client.moveOnGlobe(request); + const resp = await this.client.moveOnGlobe(request, callOptions); return resp.executionId; } - async stopPlan(componentName: ResourceName, extra = {}) { + async stopPlan( + componentName: ResourceName, + extra = {}, + callOptions = this.callOptions + ) { const request = new StopPlanRequest({ name: this.name, componentName, @@ -125,7 +133,7 @@ export class MotionClient implements Motion { this.options.requestLogger?.(request); - await this.client.stopPlan(request); + await this.client.stopPlan(request, callOptions); return null; } @@ -133,7 +141,8 @@ export class MotionClient implements Motion { componentName: ResourceName, lastPlanOnly?: boolean, executionId?: string, - extra = {} + extra = {}, + callOptions = this.callOptions ) { const request = new GetPlanRequest({ name: this.name, @@ -145,10 +154,14 @@ export class MotionClient implements Motion { this.options.requestLogger?.(request); - return this.client.getPlan(request); + return this.client.getPlan(request, callOptions); } - async listPlanStatuses(onlyActivePlans?: boolean, extra = {}) { + async listPlanStatuses( + onlyActivePlans?: boolean, + extra = {}, + callOptions = this.callOptions + ) { const request = new ListPlanStatusesRequest({ name: this.name, onlyActivePlans, @@ -157,14 +170,15 @@ export class MotionClient implements Motion { this.options.requestLogger?.(request); - return this.client.listPlanStatuses(request); + return this.client.listPlanStatuses(request, callOptions); } async getPose( componentName: ResourceName, destinationFrame: string, supplementalTransforms: Transform[], - extra = {} + extra = {}, + callOptions = this.callOptions ) { const request = new GetPoseRequest({ name: this.name, @@ -176,7 +190,7 @@ export class MotionClient implements Motion { this.options.requestLogger?.(request); - const response = await this.client.getPose(request); + const response = await this.client.getPose(request, callOptions); const result = response.pose; @@ -187,12 +201,16 @@ export class MotionClient implements Motion { return result; } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } } diff --git a/src/services/navigation/client.ts b/src/services/navigation/client.ts index 671b79a1d..d92d48ba9 100644 --- a/src/services/navigation/client.ts +++ b/src/services/navigation/client.ts @@ -1,5 +1,5 @@ import { Struct, type JsonValue } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { NavigationService } from '../../gen/service/navigation/v1/navigation_connect'; import { AddWaypointRequest, @@ -28,6 +28,7 @@ export class NavigationClient implements Navigation { 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(NavigationService); @@ -35,7 +36,7 @@ export class NavigationClient implements Navigation { this.options = options; } - async getMode(extra = {}) { + async getMode(extra = {}, callOptions = this.callOptions) { const request = new GetModeRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -43,11 +44,11 @@ export class NavigationClient implements Navigation { this.options.requestLogger?.(request); - const resp = await this.client.getMode(request); + const resp = await this.client.getMode(request, callOptions); return resp.mode; } - async setMode(mode: Mode, extra = {}) { + async setMode(mode: Mode, extra = {}, callOptions = this.callOptions) { const request = new SetModeRequest({ name: this.name, mode, @@ -56,10 +57,10 @@ export class NavigationClient implements Navigation { this.options.requestLogger?.(request); - await this.client.setMode(request); + await this.client.setMode(request, callOptions); } - async getLocation(extra = {}) { + async getLocation(extra = {}, callOptions = this.callOptions) { const request = new GetLocationRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -67,7 +68,7 @@ export class NavigationClient implements Navigation { this.options.requestLogger?.(request); - const response = await this.client.getLocation(request); + const response = await this.client.getLocation(request, callOptions); if (!response.location) { throw new Error('no location'); @@ -78,7 +79,7 @@ export class NavigationClient implements Navigation { return response; } - async getWayPoints(extra = {}) { + async getWayPoints(extra = {}, callOptions = this.callOptions) { const request = new GetWaypointsRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -86,11 +87,15 @@ export class NavigationClient implements Navigation { this.options.requestLogger?.(request); - const resp = await this.client.getWaypoints(request); + const resp = await this.client.getWaypoints(request, callOptions); return resp.waypoints; } - async addWayPoint(location: GeoPoint, extra = {}) { + async addWayPoint( + location: GeoPoint, + extra = {}, + callOptions = this.callOptions + ) { const request = new AddWaypointRequest({ name: this.name, location, @@ -99,10 +104,10 @@ export class NavigationClient implements Navigation { this.options.requestLogger?.(request); - await this.client.addWaypoint(request); + await this.client.addWaypoint(request, callOptions); } - async removeWayPoint(id: string, extra = {}) { + async removeWayPoint(id: string, extra = {}, callOptions = this.callOptions) { const request = new RemoveWaypointRequest({ name: this.name, id, @@ -111,10 +116,10 @@ export class NavigationClient implements Navigation { this.options.requestLogger?.(request); - await this.client.removeWaypoint(request); + await this.client.removeWaypoint(request, callOptions); } - async getObstacles(extra = {}) { + async getObstacles(extra = {}, callOptions = this.callOptions) { const request = new GetObstaclesRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -122,11 +127,11 @@ export class NavigationClient implements Navigation { this.options.requestLogger?.(request); - const resp = await this.client.getObstacles(request); + const resp = await this.client.getObstacles(request, callOptions); return resp.obstacles; } - async getPaths(extra = {}) { + async getPaths(extra = {}, callOptions = this.callOptions) { const request = new GetPathsRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -134,26 +139,30 @@ export class NavigationClient implements Navigation { this.options.requestLogger?.(request); - const resp = await this.client.getPaths(request); + const resp = await this.client.getPaths(request, callOptions); return resp.paths; } - async getProperties() { + async getProperties(callOptions = this.callOptions) { const request = new GetPropertiesRequest({ name: this.name, }); this.options.requestLogger?.(request); - return this.client.getProperties(request); + return this.client.getProperties(request, callOptions); } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } } diff --git a/src/services/slam/client.ts b/src/services/slam/client.ts index 719858704..50edbfa18 100644 --- a/src/services/slam/client.ts +++ b/src/services/slam/client.ts @@ -1,5 +1,5 @@ import type { JsonValue, Struct } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { SLAMService } from '../../gen/service/slam/v1/slam_connect'; import { GetInternalStateRequest, @@ -21,6 +21,7 @@ export class SlamClient implements Slam { 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(SLAMService); @@ -28,17 +29,20 @@ export class SlamClient implements Slam { this.options = options; } - async getPosition() { + async getPosition(callOptions = this.callOptions) { const request = new GetPositionRequest({ name: this.name, }); this.options.requestLogger?.(request); - return this.client.getPosition(request); + return this.client.getPosition(request, callOptions); } - getPointCloudMap = async (returnEditedMap?: boolean): Promise => { + getPointCloudMap = async ( + returnEditedMap?: boolean, + callOptions = this.callOptions + ): Promise => { const request = new GetPointCloudMapRequest({ name: this.name, returnEditedMap, @@ -46,43 +50,49 @@ export class SlamClient implements Slam { this.options.requestLogger?.(request); const chunks: Uint8Array[] = []; - const stream = this.client.getPointCloudMap(request); + const stream = this.client.getPointCloudMap(request, callOptions); for await (const chunk of stream) { chunks.push(chunk.pointCloudPcdChunk); } return concatArrayU8(chunks); }; - getInternalState = async (): Promise => { + getInternalState = async ( + callOptions = this.callOptions + ): Promise => { const request = new GetInternalStateRequest({ name: this.name, }); this.options.requestLogger?.(request); const chunks: Uint8Array[] = []; - const stream = this.client.getInternalState(request); + const stream = this.client.getInternalState(request, callOptions); for await (const chunk of stream) { chunks.push(chunk.internalStateChunk); } return concatArrayU8(chunks); }; - async getProperties() { + async getProperties(callOptions = this.callOptions) { const request = new GetPropertiesRequest({ name: this.name, }); this.options.requestLogger?.(request); - return this.client.getProperties(request); + return this.client.getProperties(request, callOptions); } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } } diff --git a/src/services/vision/client.ts b/src/services/vision/client.ts index 24243e3ed..875d62a3c 100644 --- a/src/services/vision/client.ts +++ b/src/services/vision/client.ts @@ -1,5 +1,5 @@ import { Struct, type JsonValue } from '@bufbuild/protobuf'; -import type { PromiseClient } from '@connectrpc/connect'; +import type { CallOptions, PromiseClient } from '@connectrpc/connect'; import { VisionService } from '../../gen/service/vision/v1/vision_connect'; import { CaptureAllFromCameraRequest, @@ -26,6 +26,7 @@ export class VisionClient implements Vision { 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(VisionService); @@ -33,7 +34,11 @@ export class VisionClient implements Vision { this.options = options; } - async getDetectionsFromCamera(cameraName: string, extra = {}) { + async getDetectionsFromCamera( + cameraName: string, + extra = {}, + callOptions = this.callOptions + ) { const request = new GetDetectionsFromCameraRequest({ name: this.name, cameraName, @@ -42,7 +47,10 @@ export class VisionClient implements Vision { this.options.requestLogger?.(request); - const resp = await this.client.getDetectionsFromCamera(request); + const resp = await this.client.getDetectionsFromCamera( + request, + callOptions + ); return resp.detections; } @@ -51,7 +59,8 @@ export class VisionClient implements Vision { width: number, height: number, mimeType: MimeType, - extra = {} + extra = {}, + callOptions = this.callOptions ) { const request = new GetDetectionsRequest({ name: this.name, @@ -64,14 +73,15 @@ export class VisionClient implements Vision { this.options.requestLogger?.(request); - const resp = await this.client.getDetections(request); + const resp = await this.client.getDetections(request, callOptions); return resp.detections; } async getClassificationsFromCamera( cameraName: string, count: number, - extra = {} + extra = {}, + callOptions = this.callOptions ) { const request = new GetClassificationsFromCameraRequest({ name: this.name, @@ -82,7 +92,10 @@ export class VisionClient implements Vision { this.options.requestLogger?.(request); - const resp = await this.client.getClassificationsFromCamera(request); + const resp = await this.client.getClassificationsFromCamera( + request, + callOptions + ); return resp.classifications; } @@ -92,7 +105,8 @@ export class VisionClient implements Vision { height: number, mimeType: MimeType, count: number, - extra = {} + extra = {}, + callOptions = this.callOptions ) { const request = new GetClassificationsRequest({ name: this.name, @@ -106,11 +120,15 @@ export class VisionClient implements Vision { this.options.requestLogger?.(request); - const resp = await this.client.getClassifications(request); + const resp = await this.client.getClassifications(request, callOptions); return resp.classifications; } - async getObjectPointClouds(cameraName: string, extra = {}) { + async getObjectPointClouds( + cameraName: string, + extra = {}, + callOptions = this.callOptions + ) { const request = new GetObjectPointCloudsRequest({ name: this.name, cameraName, @@ -119,11 +137,11 @@ export class VisionClient implements Vision { this.options.requestLogger?.(request); - const resp = await this.client.getObjectPointClouds(request); + const resp = await this.client.getObjectPointClouds(request, callOptions); return resp.objects; } - async getProperties(extra = {}) { + async getProperties(extra = {}, callOptions = this.callOptions) { const request = new GetPropertiesRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -131,7 +149,7 @@ export class VisionClient implements Vision { this.options.requestLogger?.(request); - const response = await this.client.getProperties(request); + const response = await this.client.getProperties(request, callOptions); return { classificationsSupported: response.classificationsSupported, detectionsSupported: response.detectionsSupported, @@ -147,7 +165,8 @@ export class VisionClient implements Vision { returnDetections, returnObjectPointClouds, }: CaptureAllOptions, - extra = {} + extra = {}, + callOptions = this.callOptions ) { const request = new CaptureAllFromCameraRequest({ name: this.name, @@ -161,7 +180,10 @@ export class VisionClient implements Vision { this.options.requestLogger?.(request); - const response = await this.client.captureAllFromCamera(request); + const response = await this.client.captureAllFromCamera( + request, + callOptions + ); return { image: response.image, @@ -171,12 +193,16 @@ export class VisionClient implements Vision { }; } - async doCommand(command: Struct): Promise { + async doCommand( + command: Struct, + callOptions = this.callOptions + ): Promise { return doCommandFromClient( this.client.doCommand, this.name, command, - this.options + this.options, + callOptions ); } } diff --git a/src/utils.ts b/src/utils.ts index 0dceadd94..0b4662d35 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -22,7 +22,8 @@ export const doCommandFromClient = async function doCommandFromClient( doCommander: doCommand, name: string, command: Struct, - options: Options = {} + options: Options = {}, + callOptions: CallOptions = {} ): Promise { const request = new DoCommandRequest({ name, @@ -31,10 +32,57 @@ export const doCommandFromClient = async function doCommandFromClient( options.requestLogger?.(request); - const response = await doCommander(request); + const response = await doCommander(request, callOptions); const result = response.result?.toJson(); if (result === undefined) { return {}; } return result; }; + +export const enableDebugLogging = ( + key?: string, + opts?: CallOptions +): CallOptions => { + const finalOpts = opts ?? { headers: {} as Record }; + let finalKey = ''; + if (key === undefined) { + const letters = 'abcdefghijklmnopqrstuvwxyz'; + for (let i = 0; i < 6; i += 1) { + finalKey += letters[Math.floor(Math.random() * 26)]; + } + } else { + finalKey = key; + } + (finalOpts.headers as Record).dtname = finalKey; + return finalOpts; +}; + +export const disableDebugLogging = (opts: CallOptions): void => { + if (opts.headers) { + const { dtname, ...remainingHeaders } = opts.headers as Record< + string, + string + >; + opts.headers = remainingHeaders; + } +}; + +export const addMetadata = ( + key: string, + value: string, + opts?: CallOptions +): CallOptions => { + const finalOpts = + opts ?? ({ headers: {} as Record } as CallOptions); + (finalOpts.headers as Record)[key] = value; + return finalOpts; +}; + +export const deleteMetadata = (opts: CallOptions, key: string): void => { + const { [key]: _, ...remainingHeaders } = opts.headers as Record< + string, + string + >; + opts.headers = remainingHeaders; +};