MCX-JavaScript API

5 minute read

Modules

motorcortex

Motorcortex Bindings for JavaScript

Classes

MessageTypes

Class for handling motorcortex data types: load proto and hash files, creates a dictionary with all available data types, resolves data types by name or by hash, performs encoding and decoding of the messages.

Request

Request/Reply communication is used to send commands to a motorcortex server.

RequestAsync

Request/Reply communication is used to send commands to a motorcortex server.

ConnectionData

Container for the session parameters

SessionManager

SessionManager manages a communication session for Request and Subscribe. SessionManager monitors if the connection is alive and tries to recover if the connection is lost.

Parameter

Parameter value with a timestamp

Subscription

Subscription class represents an active subscription group. It returns latest values and timestamps of the group parameters. Subscription class could be used as an observer, which notifies on every update or could be used as polling.

Subscribe

Subscribe class is used to receive continuous parameter updates from motorcortex server. Subscribe class simplifies creating and removing subscription groups.

Constants

SessionState

Enumerator of possible session states

Functions

encode(msg)Uint8Array

Encodes a data type from MessageTypes to a binary wire type.

Typedefs

SubscriptionClb : function

This callback is resolved when subscription is ready or failed.

SubscriptionUpdateClb : function

This callback notifies when subscription is updated with new values.

motorcortex

Motorcortex Bindings for JavaScript

Author: Alexey Zakharov zakharov@vectioneer.com
License: Copyright (C) Vectioneer - All Rights Reserved
Copyright: Vectioneer

module.exports ⏏

motorcortex namespace.

Kind: Exported member

module.exports.statusToStr(code) ⇒ string

Returns description of the Motorcortex Status Code. Status codes are defined in the StatusCode enumerator in msg/motorcortex.proto.

Kind: static method of module.exports
Returns: string - Text description of the status

Param Type Description
code number Code of the status.

Example

motorcortex.statusToStr(0x2100);

module.exports.getDataType(name) ⇒ number

Returns a сode of the Parameter Data Type. Сodes are defined in the DataType enumerator in msg/motorcortex.proto.

Kind: static method of module.exports
Returns: number - Code of the data dype

Param Type Description
name string Name of the data type.

Example

if (parameterDataType === motorcortex.getDataType('UINT8')) {
   // do something
}

module.exports.getParameterFlag(name) ⇒ number

Returns a сode of the Parameter Flag. Сodes are defined in the ParameterFlag enumerator in msg/motorcortex.proto.

Kind: static method of module.exports
Returns: number - Code of the parameter flag

Param Type Description
name string Name of the flag.

Example

if (parameterFlag === motorcortex.getParameterFlag('OVERWRITE_IS_ACTIVE')) {
   // do something
}

module.exports.getParameterType(name) ⇒ number

Returns a сode of the Parameter Type. Сodes are defined in the ParameterType enumerator in msg/motorcortex.proto.

Kind: static method of module.exports
Returns: number - Code of the parameter type

Param Type Description
name string Name of the flag.

Example

if (parameterType === motorcortex.getParameterType('INPUT')) {
   // do something
}

module.exports.getPermission(name) ⇒ number

Returns a сode of the Permision Type. Сodes are defined in the Permission enumerator in msg/motorcortex.proto.

Kind: static method of module.exports
Returns: number - Code of the permission type

Param Type Description
name string Name of the permission type.

Example

if (parameterDataType | motorcortex.getPermission('USER_WRITE')) {
   // user has a write access
}

module.exports.getStatusCode(name) ⇒ number

Returns a сode of the Status. Сodes are defined in the StatusCode enumerator in msg/motorcortex.proto.

Kind: static method of module.exports
Returns: number - Code of the status

Param Type Description
name string Name of the status code.

Example

if (replyCode === motorcortex.getStatusCode('OK')) {
   // all good
   console.log(motorcortex.statusToStr(motorcortex.getStatusCode('OK')));
}

module.exports.getUserGroup(name) ⇒ number

Returns a сode of the User Group. Сodes are defined in the UserGroup enumerator in msg/motorcortex.proto.

Kind: static method of module.exports
Returns: number - Code of the user group

Param Type Description
name string Name of the user group.

Example

if (userGroup === motorcortex.getUserGroup('OPERATOR')) {
   // operator has an access
}

ConnectionData

Container for the session parameters

Kind: global class

new ConnectionData(host, req_port, sub_port, security, request_timeout_ms, tree_timeout_ms, token_update_interval_ms, queue_length)

Creates connection data object

Param Type Description
host string Motorcortex server URL.
req_port number Request port.
sub_port number Subscribe port.
security boolean Use secured communication.
request_timeout_ms number Request timeout in milliseconds..
tree_timeout_ms number Request Parameter Tree timeout in milliseconds.
token_update_interval_ms number Toke update interval in milliseconds.
queue_length number Maximum size of the request queue.

Example

let connectionData = new ConnectionData ('192.168.2.100', 5568, 5567, false, 1000, 10000, 1000, 100)

connectionData.getRequestUri()

Returns request URI, for example wss://192.168.2.200:5568

Kind: instance method of ConnectionData

connectionData.getSubscribeUri()

Returns subscribe URI, for example wss://192.168.2.200:5567

Kind: instance method of ConnectionData

connectionData.getRequestTimeoutMs()

Returns request timeout in milliseconds.

Kind: instance method of ConnectionData

connectionData.getTreeTimeoutMs()

Returns parameter tree request timeout in milliseconds.

Kind: instance method of ConnectionData

connectionData.getTokenUpdateIntervalMs()

Returns how often session toke is updated.

Kind: instance method of ConnectionData

connectionData.getQueueLength()

Returns maximum length of the request queue.

Kind: instance method of ConnectionData

SessionState

Enumerator of possible session states

Kind: global constant

SessionState.getInfo(state) ⇒ string

Converts session state to the text

Kind: static method of SessionState
Returns: string - Text description of the session state.

Param Type Description
state number Id of the session state.

Example

console.log('Session state: ' + motorcortex.SessionState.getInfo(motorcortex.SessionState.CONNECTION_FAILED));

encode(msg) ⇒ Uint8Array

Encodes a data type from MessageTypes to a binary wire type.

Kind: global function
Returns: Uint8Array - msg A binary array with encoded message.

Param Type Description
msg Object A message created by MotorcortexTypes.

Example

let motion_script = motorcortex_types.createType('motion_spec.MotionScript', {.id = 1, name = 'Test script'});
motion_script.script = `print('Hello world');`;
let encoded_msg = req.encode(motion_script);
req.send(encoded_msg);

SubscriptionClb : function

This callback is resolved when subscription is ready or failed.

Kind: global typedef

Param Type Description
status GroupStatusMsg A status and layout of the subscribe request.

SubscriptionUpdateClb : function

This callback notifies when subscription is updated with new values.

Kind: global typedef

Param Type Description
parameters Array.<Parameter> A list of values and timestamps, ordered according to the group layout.
Last modified April 16, 2021: added js doc generator (520c0e4)