Skip to main content

Robot and Cloud (node.js)

These classes and functions are available via the @transitive-sdk/utils npm package and are for use in node.js, i.e., on the robot and the cloud.

Example

const mqtt = require('mqtt');
const { MqttSync, getLogger, getPackageVersionNamespace } =
require('@transitive-sdk/utils');

// create a logger and set log level
const log = getLogger('main');
log.setLevel('debug');

// Read config.versionNamespace from parent package.json to determine which
// version namespace to use: major, minor, or patch (default).
const version = getPackageVersionNamespace();
log.debug(`using version namespace ${version}`);

const MQTT_HOST = 'mqtt://localhost'; // the mqtt server provided by robot-agent
const mqttClient = mqtt.connect(MQTT_HOST, {
// set the clientId as required the agent to identify ourselves
clientId: `${process.env.npm_package_name}/${version}`,
// Transitive abuses the username (which is not used as such) to convey
// additional information, here the full version number (for reporting).
username: JSON.stringify({
version: process.env.npm_package_version,
}),
password: process.env.PASSWORD, // is set by agent in startPackage.sh
});

mqttClient.once('connect', (connack) => {
log.debug('connected to mqtt broker', connack);

const mqttSync = new MqttSync({mqttClient, ignoreRetain: true,
// Slices off the first N fields of the topic, i.e., our client NS
// "/org/device/@scope/name/version":
sliceTopic: 5
});

// use mqttSync ..

});

Capability

Super class for all cloud capabilities.

Parameters

  • onReady
  • options (optional, default {})

fetchURL

a simple function to fetch a URL

Parameters

  • url

findPath

walk up the directory tree until we find a file or directory called basename

Parameters

  • basename

getPackageVersionNamespace

Get from package info the version namespace we should use, e.g., {version: '1.2.3', config.versionNamespace: 'minor'} => '1.2'

importCapability

Allows you to dynamically import a capability in node.js to use its API there.

Example:

import { importCapability } from '@transitive-sdk/utils';

const run = async () => {
const rosTool = await importCapability({
jwt: 'A_VALID_JWT_FOR_THE_ROS-TOOL_CAPABILITY',
});

// Use ros-tool to subscribe to the ROS /odom topic on the device of the JWT.
// Here "subscribe" is a function exported by the ros-tool capability.
rosTool.subscribe(1, '/odom');

// print data as it changes in the local cache:
rosTool.onData(() =>
console.log(JSON.stringify(
rosTool.deviceData.ros?.[1]?.messages?.odom?.pose?.pose, true, 2)),
'ros/1/messages/odom/pose/pose'
);
};

run();

Parameters

  • args

setTerminalTitle

set the title of the terminal we are running in

Parameters

  • title