Skip to content

Commit

Permalink
fix: patch bugs TypeError
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoPierne committed Oct 27, 2022
1 parent ff29807 commit bc1b896
Showing 1 changed file with 84 additions and 22 deletions.
106 changes: 84 additions & 22 deletions bosdyn-client/power.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const { setTimeout: sleep } = require('node:timers/promises');
const { Duration } = require('google-protobuf/google/protobuf/duration_pb');
const { BaseClient, error_factory } = require('./common');
const { ResponseError, InternalServerError, LicenseError, TimedOutError } = require('./exceptions');
const { add_lease_wallet_processors } = require('./lease');
Expand All @@ -14,61 +15,75 @@ const robot_command_pb = require('../bosdyn/api/robot_command_pb');
const robot_state_pb = require('../bosdyn/api/robot_state_pb');

class PowerResponseError extends ResponseError {
constructor(msg) {
super(null, msg);
constructor(response, msg) {
super(response, msg);
this.name = 'PowerResponseError';
}
}

class ShorePowerConnectedError extends PowerResponseError {
constructor(msg) {
super(msg);
constructor(response, msg) {
super(response, msg);
this.name = 'ShorePowerConnectedError';
}
}

class BatteryMissingError extends PowerResponseError {
constructor(msg) {
super(msg);
constructor(response, msg) {
super(response, msg);
this.name = 'BatteryMissingError';
}
}

class CommandInProgressError extends PowerResponseError {
constructor(msg) {
super(msg);
constructor(response, msg) {
super(response, msg);
this.name = 'CommandInProgressError';
}
}

class EstoppedError extends PowerResponseError {
constructor(msg) {
super(msg);
constructor(response, msg) {
super(response, msg);
this.name = 'EstoppedError';
}
}

class OverriddenError extends PowerResponseError {
constructor(response, msg) {
super(response, msg);
this.name = 'OverriddenError';
}
}

class FaultedError extends PowerResponseError {
constructor(msg) {
super(msg);
constructor(response, msg) {
super(response, msg);
this.name = 'FaultedError';
}
}

class PowerError extends PowerResponseError {
class PowerError extends Error {
constructor(msg) {
super(msg);
this.name = 'PowerError';
}
}

class CommandTimedOutError extends PowerResponseError {
class CommandTimedOutError extends PowerError {
constructor(msg) {
super(msg);
this.name = 'CommandTimedOutError';
}
}

class FanControlTemperatureError extends PowerResponseError {
constructor(response, msg) {
super(response, msg);
this.name = 'FanControlTemperatureError';
}
}

/**
* A client for enabling / disabling robot motor power.
* Commands are non blocking. Clients are expected to issue a power command and then periodically
Expand Down Expand Up @@ -105,20 +120,41 @@ class PowerClient extends BaseClient {
);
}

fan_power_command(percent_power, duration, lease = null, args) {
const req = PowerClient._fan_power_command_request(lease, percent_power, duration);
return this.call(this._stub.fanPowerCommand, req, null, _fan_power_command_error_from_response, args);
}

fan_power_command_feedback(command_id, args) {
const req = PowerClient._fan_power_command_feedback_request(command_id);
return this.call(this._stub.fanPowerCommandFeedback, req, null, _fan_power_feedback_error_from_response, args);
}

static _power_command_request(lease, request) {
return new power_pb.PowerCommandRequest().setLease(lease).setRequest(request);
}

static _power_command_feedback_request(power_command_id) {
return new power_pb.PowerCommandFeedbackRequest().setPowerCommandId(power_command_id);
}

static _fan_power_command_request(lease, percent_power, duration) {
const duration_pb = new Duration().setSeconds(duration);
return new power_pb.FanPowerCommandRequest()
.setLease(lease)
.setPercentPower(percent_power)
.setDuration(duration_pb);
}

static _fan_power_command_feedback_request(command_id) {
return new power_pb.FanPowerCommandFeedbackRequest().setCommandId(command_id);
}
}

function _handle_license_errors(func) {
function wrapper(args, kwargs) {
return function (args, kwargs) {
return _common_license_errors(args) || func(args, kwargs);
}
return wrapper;
};
}

function _common_license_errors(response) {
Expand Down Expand Up @@ -162,12 +198,38 @@ const _STATUS_TO_ERROR = {
LicenseError,
'Request was rejected due to using an invalid license.',
],
[power_pb.PowerCommandStatus.STATUS_OVERRIDDEN]: [
OverriddenError,
'The command was overridden and is no longer valid.',
],
};

const _FAN_STATUS_TO_ERROR = {
[power_pb.FanPowerCommandResponse.Status.STATUS_OK]: [null, null],
[power_pb.FanPowerCommandResponse.Status.STATUS_TEMPERATURE_TOO_HIGH]: [
FanControlTemperatureError,
'Current measured robot temperatures are too high to accept user fan command.',
],
};

function _fan_power_command_error_from_response(response) {
return error_factory(
response,
response.getStatus(),
Object.keys(power_pb.FanPowerCommandResponse.Status),
_FAN_STATUS_TO_ERROR,
);
}

function _power_command_error_from_response(response) {
return error_factory(response, response.getStatus(), Object.keys(power_pb.PowerCommandStatus), _STATUS_TO_ERROR);
}

// eslint-disable-next-line
function _fan_power_feedback_error_from_response(response){
return null;
}

// eslint-disable-next-line
function _power_feedback_error_from_response(response) {
return null;
Expand Down Expand Up @@ -200,7 +262,7 @@ async function safe_power_off(command_client, state_client, timeout_msec = 30_00
new basic_command_pb.SafePowerOffCommand.Request(),
);
const command = new robot_command_pb.RobotCommand().setFullBodyCommand(full_body_command);
await command_client.robot_command(command, args);
await command_client.robot_command(command, null, null, null, args);

/* eslint-disable no-await-in-loop */
while (Date.now() < end_time) {
Expand Down Expand Up @@ -421,9 +483,9 @@ async function _power_command(
);
if (response === power_pb.PowerCommandStatus.STATUS_SUCCESS) return;
if (response !== power_pb.PowerCommandStatus.STATUS_IN_PROGRESS) {
const error_type = _STATUS_TO_ERROR[response][0];
const message = _STATUS_TO_ERROR[response][1];
throw new error_type(message);
const [error_type, message] = _STATUS_TO_ERROR[response];
if (!error_type && !message) throw new Error('Unknown error');
throw new error_type(null, message);
}
} catch (e) {
const errorClass = Object.values(_STATUS_TO_ERROR)
Expand All @@ -443,7 +505,7 @@ async function _power_command(
}
const call_time = Date.now() - start_call_time;
const sleep_time = Math.max(0.0, update_time - call_time);
console.log(update_time - call_time);
console.log(sleep_time);
await sleep(sleep_time);
}
/* eslint-enable no-await-in-loop */
Expand Down

0 comments on commit bc1b896

Please sign in to comment.