Skip to content

Commit

Permalink
RSDK-2235: add power management api (#174)
Browse files Browse the repository at this point in the history
Co-authored-by: Maxim Pertsov <[email protected]>
  • Loading branch information
purplenicole730 and maximpertsov authored Oct 4, 2023
1 parent c622785 commit e05927f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/components/board.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export type { Board } from './board/board';
export { type Board, type Duration, PowerMode } from './board/board';
export { BoardClient } from './board/client';
21 changes: 21 additions & 0 deletions src/components/board/board.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { type Duration as PBDuration } from 'google-protobuf/google/protobuf/duration_pb';
import pb from '../../gen/component/board/v1/board_pb';
import type { Resource, StructType } from '../../types';

interface Status {
analogs: Record<string, number>;
digitalInterrupts: Record<string, number>;
}

type ValueOf<T> = T[keyof T];
export const { PowerMode } = pb;
export type PowerMode = ValueOf<typeof pb.PowerMode>;

export type Duration = PBDuration.AsObject;

/**
* Represents a physical general purpose compute board that contains various
* components such as analog readers, and digital interrupts.
Expand Down Expand Up @@ -74,4 +82,17 @@ export interface Board extends Resource {
digitalInterruptName: string,
extra?: StructType
): Promise<number>;
/**
* Set power mode of the board.
*
* @param name - The name of the board.
* @param powerMode - The requested power mode.
* @param duration - The requested duration to stay in power mode.
*/
setPowerMode(
name: string,
powerMode: PowerMode,
duration: Duration,
extra?: StructType
): Promise<void>;
}
29 changes: 28 additions & 1 deletion src/components/board/client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Struct } from 'google-protobuf/google/protobuf/struct_pb';

import { Duration as PBDuration } from 'google-protobuf/google/protobuf/duration_pb';
import { BoardServiceClient } from '../../gen/component/board/v1/board_pb_service';
import type { RobotClient } from '../../robot';
import type { Options, StructType } from '../../types';

import pb from '../../gen/component/board/v1/board_pb';
import { promisify, doCommandFromClient } from '../../utils';
import type { Board } from './board';
import type { Board, Duration, PowerMode } from './board';

/**
* A gRPC-web client for the Board component.
Expand Down Expand Up @@ -202,6 +203,32 @@ export class BoardClient implements Board {
return response.getValue();
}

async setPowerMode(
name: string,
powerMode: PowerMode,
duration?: Duration,
extra = {}
) {
const { boardService } = this;
const request = new pb.SetPowerModeRequest();
request.setName(name);
request.setPowerMode(powerMode);
if (duration) {
const pbDuration = new PBDuration();
pbDuration.setNanos(duration.nanos);
pbDuration.setSeconds(duration.seconds);
request.setDuration(pbDuration);
}
request.setExtra(Struct.fromJavaScript(extra));

this.options.requestLogger?.(request);

await promisify<pb.SetPowerModeRequest, pb.SetPowerModeResponse>(
boardService.setPowerMode.bind(boardService),
request
);
}

async doCommand(command: StructType): Promise<StructType> {
const { boardService } = this;
return doCommandFromClient(boardService, this.name, command, this.options);
Expand Down
7 changes: 6 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ export { type Base, type BaseProperties, BaseClient } from './components/base';
* @group Raw Protobufs
*/
export { default as boardApi } from './gen/component/board/v1/board_pb';
export { type Board, BoardClient } from './components/board';
export {
type Board,
BoardClient,
type Duration,
PowerMode,
} from './components/board';

/**
* Raw Protobuf interfaces for a Camera component.
Expand Down
8 changes: 7 additions & 1 deletion src/robot/dial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,13 @@ export const createRobotClient = async (
): Promise<RobotClient> => {
let client;
if (conf.authEntity) {
conf.authEntity = new URL(conf.authEntity).host;
try {
conf.authEntity = new URL(conf.authEntity).host;
} catch (error) {
if (!(error instanceof TypeError)) {
throw error;
}
}
}

if (conf.reconnectMaxAttempts && !isPosInt(conf.reconnectMaxAttempts)) {
Expand Down

0 comments on commit e05927f

Please sign in to comment.