Skip to content

Commit

Permalink
RSDK-6171-add-client-api-for-debugging-grpc-calls (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
purplenicole730 authored Oct 14, 2024
1 parent ded23f6 commit 1f8e7f4
Show file tree
Hide file tree
Showing 23 changed files with 551 additions and 262 deletions.
39 changes: 24 additions & 15 deletions src/components/arm/client.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -24,30 +24,31 @@ export class ArmClient implements Arm {
private client: PromiseClient<typeof ArmService>;
private readonly name: string;
private readonly options: Options;
public callOptions: CallOptions = { headers: {} as Record<string, string> };

constructor(client: RobotClient, name: string, options: Options = {}) {
this.client = client.createServiceClient(ArmService);
this.name = name;
this.options = options;
}

async getEndPosition(extra = {}) {
async getEndPosition(extra = {}, callOptions = this.callOptions) {
const request = new GetEndPositionRequest({
name: this.name,
extra: Struct.fromJson(extra),
});

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');
}
return result;
}

async moveToPosition(pose: Pose, extra = {}) {
async moveToPosition(pose: Pose, extra = {}, callOptions = this.callOptions) {
const request = new MoveToPositionRequest({
name: this.name,
to: pose,
Expand All @@ -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,
});
Expand All @@ -72,18 +77,18 @@ 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),
});

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) {
Expand All @@ -92,34 +97,38 @@ 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),
});

this.options.requestLogger?.(request);

await this.client.stop(request);
await this.client.stop(request, callOptions);
}

async isMoving() {
async isMoving(callOptions = this.callOptions) {
const request = new IsMovingRequest({
name: this.name,
});

this.options.requestLogger?.(request);

const resp = await this.client.isMoving(request);
const resp = await this.client.isMoving(request, callOptions);
return resp.isMoving;
}

async doCommand(command: Struct): Promise<JsonValue> {
async doCommand(
command: Struct,
callOptions = this.callOptions
): Promise<JsonValue> {
return doCommandFromClient(
this.client.doCommand,
this.name,
command,
this.options
this.options,
callOptions
);
}
}
2 changes: 1 addition & 1 deletion src/components/base/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface Base extends Resource {
stop(extra?: Struct): Promise<void>;

/** Return true if the base is in motion. */
isMoving(extra?: Struct): Promise<boolean>;
isMoving(): Promise<boolean>;

/** Return the base's properties. */
getProperties(extra?: Struct): Promise<BaseProperties>;
Expand Down
59 changes: 42 additions & 17 deletions src/components/base/client.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -24,14 +24,20 @@ export class BaseClient implements Base {
private client: PromiseClient<typeof BaseService>;
private readonly name: string;
private readonly options: Options;
public callOptions: CallOptions = { headers: {} as Record<string, string> };

constructor(client: RobotClient, name: string, options: Options = {}) {
this.client = client.createServiceClient(BaseService);
this.name = name;
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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -80,48 +101,52 @@ 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),
});

this.options.requestLogger?.(request);

await this.client.stop(request);
await this.client.stop(request, callOptions);
}

async isMoving() {
async isMoving(callOptions = this.callOptions) {
const request = new IsMovingRequest({
name: this.name,
});

this.options.requestLogger?.(request);

const resp = await this.client.isMoving(request);
const resp = await this.client.isMoving(request, callOptions);
return resp.isMoving;
}

async doCommand(command: Struct): Promise<JsonValue> {
async doCommand(
command: Struct,
callOptions = this.callOptions
): Promise<JsonValue> {
return doCommandFromClient(
this.client.doCommand,
this.name,
command,
this.options
this.options,
callOptions
);
}

async getProperties(extra = {}) {
async getProperties(extra = {}, callOptions = this.callOptions) {
const request = new GetPropertiesRequest({
name: this.name,
extra: Struct.fromJson(extra),
});

this.options.requestLogger?.(request);

return this.client.getProperties(request);
return this.client.getProperties(request, callOptions);
}
}
Loading

0 comments on commit 1f8e7f4

Please sign in to comment.