From 7209a3ce9c25611295956d9bf522a6a908d72c3c Mon Sep 17 00:00:00 2001 From: purplenicole730 Date: Wed, 9 Oct 2024 08:35:11 -0400 Subject: [PATCH 1/9] working example in motor --- src/components/motor/client.ts | 72 ++++++++++++++++++++++++---------- src/main.ts | 8 +++- src/utils.ts | 34 ++++++++++++++++ 3 files changed, 92 insertions(+), 22 deletions(-) diff --git a/src/components/motor/client.ts b/src/components/motor/client.ts index 617387816..c152a104a 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?: 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 || this.callOptions); } - async goFor(rpm: number, revolutions: number, extra = {}) { + async goFor( + rpm: number, + revolutions: number, + extra = {}, + callOptions?: 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 || this.callOptions); } - async goTo(rpm: number, positionRevolutions: number, extra = {}) { + async goTo( + rpm: number, + positionRevolutions: number, + extra = {}, + callOptions?: 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 || this.callOptions); } - async setRPM(rpm: number, extra = {}) { + async setRPM(rpm: number, extra = {}, callOptions?: 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 || this.callOptions); } - async resetZeroPosition(offset: number, extra = {}) { + async resetZeroPosition( + offset: number, + extra = {}, + callOptions?: CallOptions + ) { const request = new ResetZeroPositionRequest({ name: this.name, offset, @@ -93,10 +108,13 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - await this.client.resetZeroPosition(request); + await this.client.resetZeroPosition( + request, + callOptions || this.callOptions + ); } - async stop(extra = {}) { + async stop(extra = {}, callOptions?: CallOptions) { const request = new StopRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -104,10 +122,10 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - await this.client.stop(request); + await this.client.stop(request, callOptions || this.callOptions); } - async getProperties(extra = {}) { + async getProperties(extra = {}, callOptions?: CallOptions) { const request = new GetPropertiesRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -115,13 +133,16 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - const resp = await this.client.getProperties(request); + const resp = await this.client.getProperties( + request, + callOptions || this.callOptions + ); return { positionReporting: resp.positionReporting, }; } - async getPosition(extra = {}) { + async getPosition(extra = {}, callOptions?: CallOptions) { const request = new GetPositionRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -129,11 +150,14 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - const resp = await this.client.getPosition(request); + const resp = await this.client.getPosition( + request, + callOptions || this.callOptions + ); return resp.position; } - async isPowered(extra = {}) { + async isPowered(extra = {}, callOptions?: CallOptions) { const request = new IsPoweredRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -141,18 +165,24 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - const response = await this.client.isPowered(request); + const response = await this.client.isPowered( + request, + callOptions || this.callOptions + ); return [response.isOn, response.powerPct] as const; } - async isMoving() { + async isMoving(callOptions?: 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 || this.callOptions + ); return resp.isMoving; } 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/utils.ts b/src/utils.ts index 408a7ee24..31b187da0 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -38,3 +38,37 @@ export const doCommandFromClient = async function doCommandFromClient( } return result; }; + +export const enableDebugLogging = ( + key?: string, + opts?: CallOptions +): CallOptions => { + opts ??= { headers: {} as Record } as CallOptions; + if (!key) { + key = ''; + const letters = 'abcdefghijklmnopqrstuvwxyz'; + for (let i = 0; i < 6; i++) { + key += letters[Math.floor(Math.random() * 26)]; + } + } + (opts.headers as Record)['dtname'] = key; + return opts; +}; + +export const disableDebugLogging = (opts: CallOptions): void => { + delete (opts.headers as Record)['dtname']; +}; + +export const addMetadata = ( + key: string, + value: string, + opts?: CallOptions +): CallOptions => { + opts ??= { headers: {} as Record } as CallOptions; + (opts.headers as Record)[key] = value; + return opts; +}; + +export const deleteMetadata = (opts: CallOptions, key: string): void => { + delete (opts.headers as Record)[key]; +}; From be605555ba6a0602b4b5036f777986b80980d545 Mon Sep 17 00:00:00 2001 From: purplenicole730 Date: Wed, 9 Oct 2024 09:20:21 -0400 Subject: [PATCH 2/9] add calloptions to all component client classes and methods --- src/components/arm/client.ts | 31 ++++---- src/components/base/base.ts | 2 +- src/components/base/client.ts | 51 +++++++++---- src/components/board/client.ts | 92 +++++++++++++++++------ src/components/camera/client.ts | 19 ++--- src/components/encoder/client.ts | 16 ++-- src/components/gantry/client.ts | 28 +++---- src/components/generic/client.ts | 3 +- src/components/gripper/client.ts | 19 ++--- src/components/input-controller/client.ts | 15 ++-- src/components/motor/client.ts | 55 +++++--------- src/components/movement-sensor/client.ts | 42 ++++++----- src/components/power-sensor/client.ts | 19 ++--- src/components/sensor/client.ts | 7 +- src/components/servo/client.ts | 19 ++--- 15 files changed, 247 insertions(+), 171 deletions(-) diff --git a/src/components/arm/client.ts b/src/components/arm/client.ts index 8a0fc383d..0a5e2d423 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,17 +105,17 @@ 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; } 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..bcc6df7ee 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,17 +112,17 @@ 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; } @@ -114,7 +135,7 @@ export class BaseClient implements Base { ); } - async getProperties(extra = {}) { + async getProperties(extra = {}, callOptions = this.callOptions) { const request = new GetPropertiesRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -122,6 +143,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..c8db72f32 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,7 +226,7 @@ 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 { diff --git a/src/components/camera/client.ts b/src/components/camera/client.ts index 8817483d8..59bb9efb3 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,18 +63,18 @@ 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 { diff --git a/src/components/encoder/client.ts b/src/components/encoder/client.ts index beb0fc86b..1e115ba59 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,7 +63,7 @@ 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; } diff --git a/src/components/gantry/client.ts b/src/components/gantry/client.ts index 162a3cc0e..b215e1741 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,17 +93,17 @@ 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; } diff --git a/src/components/generic/client.ts b/src/components/generic/client.ts index c1a0926b8..bdd743fa1 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); diff --git a/src/components/gripper/client.ts b/src/components/gripper/client.ts index 832229491..d5d572d9e 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,17 +59,17 @@ 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; } diff --git a/src/components/input-controller/client.ts b/src/components/input-controller/client.ts index 7e5605a1f..9c76506c4 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,7 +53,7 @@ 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 { diff --git a/src/components/motor/client.ts b/src/components/motor/client.ts index c152a104a..d3bb2df80 100644 --- a/src/components/motor/client.ts +++ b/src/components/motor/client.ts @@ -35,7 +35,7 @@ export class MotorClient implements Motor { this.options = options; } - async setPower(power: number, extra = {}, callOptions?: CallOptions) { + async setPower(power: number, extra = {}, callOptions = this.callOptions) { const request = new SetPowerRequest({ name: this.name, powerPct: power, @@ -44,14 +44,14 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - await this.client.setPower(request, callOptions || this.callOptions); + await this.client.setPower(request, callOptions); } async goFor( rpm: number, revolutions: number, extra = {}, - callOptions?: CallOptions + callOptions = this.callOptions ) { const request = new GoForRequest({ name: this.name, @@ -62,14 +62,14 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - await this.client.goFor(request, callOptions || this.callOptions); + await this.client.goFor(request, callOptions); } async goTo( rpm: number, positionRevolutions: number, extra = {}, - callOptions?: CallOptions + callOptions = this.callOptions ) { const request = new GoToRequest({ name: this.name, @@ -80,10 +80,10 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - await this.client.goTo(request, callOptions || this.callOptions); + await this.client.goTo(request, callOptions); } - async setRPM(rpm: number, extra = {}, callOptions?: CallOptions) { + async setRPM(rpm: number, extra = {}, callOptions = this.callOptions) { const request = new SetRPMRequest({ name: this.name, rpm, @@ -92,13 +92,13 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - await this.client.setRPM(request, callOptions || this.callOptions); + await this.client.setRPM(request, callOptions); } async resetZeroPosition( offset: number, extra = {}, - callOptions?: CallOptions + callOptions = this.callOptions ) { const request = new ResetZeroPositionRequest({ name: this.name, @@ -108,13 +108,10 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - await this.client.resetZeroPosition( - request, - callOptions || this.callOptions - ); + await this.client.resetZeroPosition(request, callOptions); } - async stop(extra = {}, callOptions?: CallOptions) { + async stop(extra = {}, callOptions = this.callOptions) { const request = new StopRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -122,10 +119,10 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - await this.client.stop(request, callOptions || this.callOptions); + await this.client.stop(request, callOptions); } - async getProperties(extra = {}, callOptions?: CallOptions) { + async getProperties(extra = {}, callOptions = this.callOptions) { const request = new GetPropertiesRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -133,16 +130,13 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - const resp = await this.client.getProperties( - request, - callOptions || this.callOptions - ); + const resp = await this.client.getProperties(request, callOptions); return { positionReporting: resp.positionReporting, }; } - async getPosition(extra = {}, callOptions?: CallOptions) { + async getPosition(extra = {}, callOptions = this.callOptions) { const request = new GetPositionRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -150,14 +144,11 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - const resp = await this.client.getPosition( - request, - callOptions || this.callOptions - ); + const resp = await this.client.getPosition(request, callOptions); return resp.position; } - async isPowered(extra = {}, callOptions?: CallOptions) { + async isPowered(extra = {}, callOptions = this.callOptions) { const request = new IsPoweredRequest({ name: this.name, extra: Struct.fromJson(extra), @@ -165,24 +156,18 @@ export class MotorClient implements Motor { this.options.requestLogger?.(request); - const response = await this.client.isPowered( - request, - callOptions || this.callOptions - ); + const response = await this.client.isPowered(request, callOptions); return [response.isOn, response.powerPct] as const; } - async isMoving(callOptions?: CallOptions) { + async isMoving(callOptions = this.callOptions) { const request = new IsMovingRequest({ name: this.name, }); this.options.requestLogger?.(request); - const resp = await this.client.isMoving( - request, - callOptions || this.callOptions - ); + const resp = await this.client.isMoving(request, callOptions); return resp.isMoving; } diff --git a/src/components/movement-sensor/client.ts b/src/components/movement-sensor/client.ts index 6dd59d4ed..a465bc371 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)) { diff --git a/src/components/power-sensor/client.ts b/src/components/power-sensor/client.ts index 14b3c9a18..aa32d6524 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)) { diff --git a/src/components/sensor/client.ts b/src/components/sensor/client.ts index d68bd05e1..15da87558 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)) { diff --git a/src/components/servo/client.ts b/src/components/servo/client.ts index 03c280844..339597413 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,17 +61,17 @@ 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; } From 3adf43a7412f9c6731d6c79030ab58ea643abe76 Mon Sep 17 00:00:00 2001 From: purplenicole730 Date: Wed, 9 Oct 2024 12:11:36 -0400 Subject: [PATCH 3/9] make lint --- src/utils.ts | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 31b187da0..52b3de97c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -43,20 +43,28 @@ export const enableDebugLogging = ( key?: string, opts?: CallOptions ): CallOptions => { - opts ??= { headers: {} as Record } as CallOptions; - if (!key) { - key = ''; + const finalOpts = opts ?? { headers: {} as Record }; + let finalKey = ''; + if (key) { + finalKey = key; + } else { const letters = 'abcdefghijklmnopqrstuvwxyz'; - for (let i = 0; i < 6; i++) { - key += letters[Math.floor(Math.random() * 26)]; + for (let i = 0; i < 6; i += 1) { + finalKey += letters[Math.floor(Math.random() * 26)]; } } - (opts.headers as Record)['dtname'] = key; - return opts; + (finalOpts.headers as Record).dtname = finalKey; + return finalOpts; }; export const disableDebugLogging = (opts: CallOptions): void => { - delete (opts.headers as Record)['dtname']; + if (opts.headers) { + const { dtname, ...remainingHeaders } = opts.headers as Record< + string, + string + >; + opts.headers = remainingHeaders; + } }; export const addMetadata = ( @@ -64,11 +72,16 @@ export const addMetadata = ( value: string, opts?: CallOptions ): CallOptions => { - opts ??= { headers: {} as Record } as CallOptions; - (opts.headers as Record)[key] = value; - return opts; + const finalOpts = + opts ?? ({ headers: {} as Record } as CallOptions); + (finalOpts.headers as Record)[key] = value; + return finalOpts; }; export const deleteMetadata = (opts: CallOptions, key: string): void => { - delete (opts.headers as Record)[key]; + const { [key]: _, ...remainingHeaders } = opts.headers as Record< + string, + string + >; + opts.headers = remainingHeaders; }; From 0f540197b07cb4c15e7b92da6e142791e53f844c Mon Sep 17 00:00:00 2001 From: purplenicole730 Date: Thu, 10 Oct 2024 11:22:33 -0400 Subject: [PATCH 4/9] add calloptions to all services --- src/services/data-manager/client.ts | 7 ++-- src/services/generic/client.ts | 3 +- src/services/motion/client.ts | 44 +++++++++++++++--------- src/services/navigation/client.ts | 43 +++++++++++++----------- src/services/slam/client.ts | 24 ++++++++----- src/services/vision/client.ts | 52 ++++++++++++++++++++--------- 6 files changed, 111 insertions(+), 62 deletions(-) diff --git a/src/services/data-manager/client.ts b/src/services/data-manager/client.ts index 42aeb7b73..85c182241 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,7 +27,7 @@ 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 { diff --git a/src/services/generic/client.ts b/src/services/generic/client.ts index 3539d9b70..755376b62 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); diff --git a/src/services/motion/client.ts b/src/services/motion/client.ts index a1a523844..d3ab4aa87 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; diff --git a/src/services/navigation/client.ts b/src/services/navigation/client.ts index 671b79a1d..ef178cce1 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,18 +139,18 @@ 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 { diff --git a/src/services/slam/client.ts b/src/services/slam/client.ts index 719858704..95ba30f36 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,35 +50,37 @@ 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 { diff --git a/src/services/vision/client.ts b/src/services/vision/client.ts index 24243e3ed..621335f38 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, From 6562ba81486dd2494c438b8a4c750c8330b69fc2 Mon Sep 17 00:00:00 2001 From: purplenicole730 Date: Thu, 10 Oct 2024 11:29:39 -0400 Subject: [PATCH 5/9] make lint update connectrpc and exponential-backoff --- package-lock.json | 18 +++++++++--------- package.json | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index 51e517855..3668164b3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,8 +10,8 @@ "license": "Apache-2.0", "dependencies": { "@bufbuild/protobuf": "^1.10.0", - "@connectrpc/connect": "^1.5.0", - "@connectrpc/connect-web": "^1.5.0", + "@connectrpc/connect": "^1.6.0", + "@connectrpc/connect-web": "^1.6.0", "exponential-backoff": "^3.1.1" }, "devDependencies": { @@ -525,22 +525,22 @@ "license": "(Apache-2.0 AND BSD-3-Clause)" }, "node_modules/@connectrpc/connect": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/@connectrpc/connect/-/connect-1.5.0.tgz", - "integrity": "sha512-1gGg0M6c2Y3lnr5itis9dNj9r8hbOIuBMqoGSbUy7L7Vjw4MAttjJzJfj9HCDgytGCJkGanYEYI6MQVDijdVQw==", + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/@connectrpc/connect/-/connect-1.6.0.tgz", + "integrity": "sha512-0WEnWOTtYbjlfi71cjiTle8FdmzdD4A6D9N/9Ky6KlsHB2hPvh2FQ3jpzzXma09rZBbJezHF9vvy29qZqeWtwQ==", "license": "Apache-2.0", "peerDependencies": { "@bufbuild/protobuf": "^1.10.0" } }, "node_modules/@connectrpc/connect-web": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/@connectrpc/connect-web/-/connect-web-1.5.0.tgz", - "integrity": "sha512-xjiiQ932Kibddaka18fGZ6yQL7xjXuLcYFYh/cU+q1WWEIrFPkZfViG/Ee6yrZbrlZkjcBuDibng+q7baTndfg==", + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/@connectrpc/connect-web/-/connect-web-1.6.0.tgz", + "integrity": "sha512-dyjkdSgNKoUSjNyXOkD4r/d5d8gJxgR1q8zinmCZDFKgRKPFZdjzHLRvd4k4s1Fw54R7Zam5aMMj/t7B0/4CIA==", "license": "Apache-2.0", "peerDependencies": { "@bufbuild/protobuf": "^1.10.0", - "@connectrpc/connect": "1.5.0" + "@connectrpc/connect": "1.6.0" } }, "node_modules/@cspotcode/source-map-support": { diff --git a/package.json b/package.json index c10f2cf16..1e9a06603 100644 --- a/package.json +++ b/package.json @@ -46,10 +46,10 @@ }, "homepage": "https://github.com/viamrobotics/viam-typescript-sdk#readme", "dependencies": { - "exponential-backoff": "^3.1.1", "@bufbuild/protobuf": "^1.10.0", - "@connectrpc/connect": "^1.5.0", - "@connectrpc/connect-web": "^1.5.0" + "@connectrpc/connect": "^1.6.0", + "@connectrpc/connect-web": "^1.6.0", + "exponential-backoff": "^3.1.1" }, "devDependencies": { "@bufbuild/buf": "^1.15.0-1", From fdc24fca27170f7fcc10ddcc4f354adeea0e350f Mon Sep 17 00:00:00 2001 From: purplenicole730 Date: Thu, 10 Oct 2024 12:18:04 -0400 Subject: [PATCH 6/9] add calloptions to docommand --- src/components/arm/client.ts | 8 ++++++-- src/components/base/client.ts | 8 ++++++-- src/components/board/client.ts | 8 ++++++-- src/components/camera/client.ts | 8 ++++++-- src/components/encoder/client.ts | 8 ++++++-- src/components/gantry/client.ts | 8 ++++++-- src/components/generic/client.ts | 8 ++++++-- src/components/gripper/client.ts | 8 ++++++-- src/components/input-controller/client.ts | 8 ++++++-- src/components/motor/client.ts | 8 ++++++-- src/components/movement-sensor/client.ts | 8 ++++++-- src/components/power-sensor/client.ts | 8 ++++++-- src/components/sensor/client.ts | 8 ++++++-- src/components/servo/client.ts | 8 ++++++-- src/services/data-manager/client.ts | 8 ++++++-- src/services/generic/client.ts | 8 ++++++-- src/services/motion/client.ts | 8 ++++++-- src/services/navigation/client.ts | 8 ++++++-- src/services/slam/client.ts | 8 ++++++-- src/services/vision/client.ts | 8 ++++++-- src/utils.ts | 5 +++-- 21 files changed, 123 insertions(+), 42 deletions(-) diff --git a/src/components/arm/client.ts b/src/components/arm/client.ts index 0a5e2d423..dda9eb8bb 100644 --- a/src/components/arm/client.ts +++ b/src/components/arm/client.ts @@ -119,12 +119,16 @@ export class ArmClient implements Arm { 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/client.ts b/src/components/base/client.ts index bcc6df7ee..9754db38d 100644 --- a/src/components/base/client.ts +++ b/src/components/base/client.ts @@ -126,12 +126,16 @@ export class BaseClient implements Base { 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/board/client.ts b/src/components/board/client.ts index c8db72f32..2a34a719f 100644 --- a/src/components/board/client.ts +++ b/src/components/board/client.ts @@ -229,12 +229,16 @@ export class BoardClient implements Board { 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 59bb9efb3..26fea0ed2 100644 --- a/src/components/camera/client.ts +++ b/src/components/camera/client.ts @@ -77,12 +77,16 @@ export class CameraClient implements Camera { 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 1e115ba59..90a5d9349 100644 --- a/src/components/encoder/client.ts +++ b/src/components/encoder/client.ts @@ -67,12 +67,16 @@ export class EncoderClient implements Encoder { 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 b215e1741..d5a4d018d 100644 --- a/src/components/gantry/client.ts +++ b/src/components/gantry/client.ts @@ -107,12 +107,16 @@ export class GantryClient implements Gantry { 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 bdd743fa1..62025e3c3 100644 --- a/src/components/generic/client.ts +++ b/src/components/generic/client.ts @@ -23,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 d5d572d9e..d172ea3a2 100644 --- a/src/components/gripper/client.ts +++ b/src/components/gripper/client.ts @@ -73,12 +73,16 @@ export class GripperClient implements Gripper { 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 9c76506c4..01be98511 100644 --- a/src/components/input-controller/client.ts +++ b/src/components/input-controller/client.ts @@ -56,12 +56,16 @@ export class InputControllerClient implements InputController { 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 d3bb2df80..6530908f7 100644 --- a/src/components/motor/client.ts +++ b/src/components/motor/client.ts @@ -171,12 +171,16 @@ export class MotorClient implements Motor { 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 a465bc371..a0ca31ea3 100644 --- a/src/components/movement-sensor/client.ts +++ b/src/components/movement-sensor/client.ts @@ -175,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 aa32d6524..e8035f065 100644 --- a/src/components/power-sensor/client.ts +++ b/src/components/power-sensor/client.ts @@ -89,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 15da87558..65072930d 100644 --- a/src/components/sensor/client.ts +++ b/src/components/sensor/client.ts @@ -46,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 339597413..b91a5fddd 100644 --- a/src/components/servo/client.ts +++ b/src/components/servo/client.ts @@ -75,12 +75,16 @@ export class ServoClient implements Servo { 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/services/data-manager/client.ts b/src/services/data-manager/client.ts index 85c182241..0fd3913e2 100644 --- a/src/services/data-manager/client.ts +++ b/src/services/data-manager/client.ts @@ -30,12 +30,16 @@ export class DataManagerClient implements DataManager { 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 755376b62..2ceec0891 100644 --- a/src/services/generic/client.ts +++ b/src/services/generic/client.ts @@ -23,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 d3ab4aa87..9f0c949fb 100644 --- a/src/services/motion/client.ts +++ b/src/services/motion/client.ts @@ -201,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 ef178cce1..d92d48ba9 100644 --- a/src/services/navigation/client.ts +++ b/src/services/navigation/client.ts @@ -153,12 +153,16 @@ export class NavigationClient implements Navigation { 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 95ba30f36..50edbfa18 100644 --- a/src/services/slam/client.ts +++ b/src/services/slam/client.ts @@ -83,12 +83,16 @@ export class SlamClient implements Slam { 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 621335f38..875d62a3c 100644 --- a/src/services/vision/client.ts +++ b/src/services/vision/client.ts @@ -193,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 52b3de97c..27df846bc 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,7 +32,7 @@ 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) { return {}; From 239965e608b634ee3b79bd7dfb06d04cf416a949 Mon Sep 17 00:00:00 2001 From: purplenicole730 Date: Thu, 10 Oct 2024 12:22:53 -0400 Subject: [PATCH 7/9] make callopts a default param --- src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 27df846bc..8930ce223 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -23,7 +23,7 @@ export const doCommandFromClient = async function doCommandFromClient( name: string, command: Struct, options: Options = {}, - callOptions: CallOptions + callOptions: CallOptions = { headers: {} as Record } ): Promise { const request = new DoCommandRequest({ name, From be59b6fea919d3d885bcf45be34274dd4ece824c Mon Sep 17 00:00:00 2001 From: purplenicole730 Date: Thu, 10 Oct 2024 12:26:18 -0400 Subject: [PATCH 8/9] do not use object literal --- src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 8930ce223..746be1fbc 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -23,7 +23,7 @@ export const doCommandFromClient = async function doCommandFromClient( name: string, command: Struct, options: Options = {}, - callOptions: CallOptions = { headers: {} as Record } + callOptions: CallOptions = {} ): Promise { const request = new DoCommandRequest({ name, From 97f646ad9f52d3365a0f28a9c13bc2a28f6119a5 Mon Sep 17 00:00:00 2001 From: purplenicole730 Date: Fri, 11 Oct 2024 16:38:03 -0400 Subject: [PATCH 9/9] make lint --- src/utils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index f051ab188..0b4662d35 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -46,13 +46,13 @@ export const enableDebugLogging = ( ): CallOptions => { const finalOpts = opts ?? { headers: {} as Record }; let finalKey = ''; - if (key) { - finalKey = key; - } else { + 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;