Source

bosdyn-client/arm_surface_contact.js

'use strict';

const { BaseClient } = require('./common');
const { add_lease_wallet_processors } = require('./lease');
const { NoTimeSyncError, _TimeConverter, _edit_proto } = require('./robot_command');
const arm_surface_contact_service_grpc_pb = require('../bosdyn/api/arm_surface_contact_service_grpc_pb');

const EDIT_TREE_CONVERT_LOCAL_TIME_TO_ROBOT_TIME = {
  request: {
    poseTrajectoryInTask: {
      referenceTime: null,
    },
    gripperCommand: {
      trajectory: {
        referenceTime: null,
      },
    },
  },
};

/**
 * Client for the ArmSurfaceContact service.
 * @class {ArmSurfaceContactClient}
 * @extends {BaseClient}
 */
class ArmSurfaceContactClient extends BaseClient {
  static default_service_name = 'arm-surface-contact';
  static service_type = 'bosdyn.api.ArmSurfaceContactService';

  /**
   * Create an instance of ArmSurfaceContactClient's class.
   * @param {string} [name=null] Name of the Class.
   */
  constructor(name = null) {
    super(arm_surface_contact_service_grpc_pb.ArmSurfaceContactServiceClient, name);
    this._timesync_endpoint = null;
  }

  /**
   * Update instance from another object.
   * @param {Object} other The object where to copy from.
   * @returns {Promise<void>}
   */
  async update_from(other) {
    super.update_from(other);
    if (this.lease_wallet) add_lease_wallet_processors(this, this.lease_wallet);

    try {
      this._timesync_endpoint = (await other.time_sync).endpoint;
    } catch (e) {
      // Pass
    }
  }

  /**
   * Set or convert fields of the command proto that need timestamps in the robot's clock.
   * @param {arm_surface_contact_pb.ArmSurfaceContactRequest} command Command message to update.
   * @returns {void}
   * @private
   */
  _update_command_timestamps(command) {
    if (this._timesync_endpoint === null) throw new NoTimeSyncError();

    const converter = new _TimeConverter(this, this._timesync_endpoint);

    function _to_robot_time(key, proto) {
      if (!(key in proto)) return;
      const timestamp = proto[key];
      converter.convert_timestamp_from_local_to_robot(timestamp);
    }

    _edit_proto(command, EDIT_TREE_CONVERT_LOCAL_TIME_TO_ROBOT_TIME, _to_robot_time);
  }

  /**
   * Issue an arm surface contact command to the robot.
   * @param {arm_surface_contact_pb.ArmSurfaceContactRequest} request The command request.
   * @param {Object} args Extra arguments for controlling RPC details.
   * @returns {Promise<arm_surface_contact_pb.ArmSurfaceContactResponse>} The full arm surface contact response message.
   */
  arm_surface_contact_command(request, args) {
    this._update_command_timestamps(request);
    return this.call(this._stub.armSurfaceContact, request, null, null, args);
  }
}

module.exports = {
  ArmSurfaceContactClient,
};