0.0.103 • Published 7 days ago

motion-master-client v0.0.103

Weekly downloads
-
License
-
Repository
-
Last release
7 days ago

Motion Master Client

Motion Master Client is a library and CLI program written in TypeScript. It is used for communicating with Motion Master process over WebSocket connections by exchanging Protocol Buffers serialized messages.

Usage

The Motion Master Client library can be equally used on a server with Node.js or in browsers.

Server usage

Initialize a project:

mkdir hello-somanet
cd hello-somanet
npm init --yes

Install the required dependencies:

npm install --save motion-master-client rxjs ws
npm install --save-dev ts-node typescript

Create a TypeScript configuration:

npx tsc --init

Create a main program file:

touch main.ts

Add the following content to the main.ts file:

import { createMotionMasterClient } from 'motion-master-client';
import { lastValueFrom } from 'rxjs';

// Ensure that Node.js has the global WebSocket object available.
Object.assign(globalThis, { WebSocket: require('ws') });

const client = createMotionMasterClient('192.168.200.253');

client.whenReady().then(async () => {
  const devices = await lastValueFrom(client.request.getDevices());
  const message = JSON.stringify(devices, null, 2);
  console.log(message);
  client.closeSockets();
});

Run the program:

npx ts-node main.ts

The program will:

  1. Create an instance of the client and connect to a Motion Master process running at 192.168.200.253.
  2. The client needs some time to connect, so it must be ready before making requests. Most of the requests are implemented and documented in the MotionMasterReqResClient class.
  3. The client requests a list of devices, which are then printed to the console.
  4. The client closes opened sockets.

Here's another example that demonstrates how to get and set device parameter values:

import { createMotionMasterClient } from 'motion-master-client';

// Ensure that Node.js has the global WebSocket object available.
Object.assign(globalThis, { WebSocket: require('ws') });

const client = createMotionMasterClient('192.168.200.253');

client.whenReady().then(async () => {
  // Device is referenced by a position (0).
  const motorRatedCurrent = await client.request.upload(0, 0x6075, 0);
  console.log(`Motor rated current: ${motorRatedCurrent}`);

  // The device parameter is referenced by an ID, which consists of
  // the index (0x6076), subindex (0x00), and the device serial number (8504-03-0002369-2329).
  const motorRatedTorque = await client.request.upload('0x6076:00.8504-03-0002369-2329');
  console.log(`Motor rated current: ${motorRatedTorque}`);

  // Update the Max current parameter value to 2000.
  await client.request.download(0, 0x6073, 0, 2000);

  client.closeSockets();
});

Understanding requests

When the client is instantiated, it connects to the Motion Master process using the WebSocket protocol. As it is a bi-directional, full-duplex communication protocol, there needs to be a mechanism that couples requests with responses. This is solved by having a message ID that is a mandatory attribute of each request; responses produced by that request will include the same message ID.

For some requests, such as firmware installation, Motion Master will send progress messages until the firmware installation is complete. Therefore, when making a request with the client library, functions will return an Observable of request statuses. This means that you can subscribe to an Observable and receive status messages over time. These observables are asynchronous, meaning your program can continue to do other things while the request executes. For the purpose of utilizing Observables and implementing reactive programming, the client library uses RxJs.

To demonstrate the use of reactive programming, let's create a program that monitors the drive and core temperatures:

import { createMotionMasterClient } from 'motion-master-client';
import { map, mergeMap } from 'rxjs';

// Ensure that Node.js has the global WebSocket object available.
Object.assign(globalThis, { WebSocket: require('ws') });

const client = createMotionMasterClient('192.168.200.253');

const subscription = client.onceReady$
  .pipe(
    mergeMap(() =>
      client.startMonitoring(
        [
          [0, 0x2030, 1], // Core temperature
          [0, 0x2031, 1], // Drive temperature
        ],
        1000000, // microseconds
        { topic: 'temperatures-monitoring', distinct: true }
      )
    ),
    map((values) => `core=${values[0]} drive=${values[1]}`)
  )
  .subscribe(console.log);

process.on('SIGINT', function () {
  console.log('\nGracefully shutting down from SIGINT (Ctrl-C).\nStopping monitoring and closing sockets.');
  subscription?.unsubscribe();
  client.closeSockets();
  process.exit(0);
});

Class Diagrams

---
title: Client & socket classes
---
classDiagram
  class MotionMasterClient {
    +MotionMasterReqResClient request
    +MotionMasterPubSubClient monitor
    +startMonitoring()
    +stopMonitoring()
  }

  class MotionMasterReqResClient {
    +MotionMasterReqResSocket socket
    note "Has all the request methods\ncorresponding to the proto file."
  }

  class MotionMasterReqResSocket
  <<interface>> MotionMasterReqResSocket
  MotionMasterReqResSocket: +BehaviorSubject~boolean~ opened\$
  MotionMasterReqResSocket: +BehaviorSubject~boolean~ alive\$
  MotionMasterReqResSocket: +Observable~MotionMasterMessage~ message\$
  MotionMasterReqResSocket: +open(string url, number pingSystemInterval?, number systemAliveTimeout?)
  MotionMasterReqResSocket: +close()
  MotionMasterReqResSocket: +send(MotionMasterMessage message)

  class MotionMasterReqResWebSocket {
    note "Keeps the connection alive by\nsending the ping system messages\nin regular intervals."
  }

  class MotionMasterReqResWorkerSocket {
    +Worker worker
    note "Passes messages between\nthe UI and worker thread."
  }

  class MotionMasterPubSubClient {
    +MotionMasterPubSubSocket socket
    +Subject<[string, MotionMasterMessage[]]> data\$
    +Observable~MotionMasterMessage~ notification\$
    +Observable~MotionMasterMessage~ systemEvent\$
    +Observable~MotionMasterMessage~ deviceEvent\$
    +subscribe(MonitoringConfig config)
    +unsubscribe(string messageId)
    +unsubscribeAll()
  }

  class MotionMasterPubSubSocket
  <<interface>> MotionMasterPubSubSocket
  MotionMasterPubSubSocket: +BehaviorSubject~boolean~ opened\$
  MotionMasterPubSubSocket: +Observable<[string, MotionMasterMessage]> data\$
  MotionMasterPubSubSocket: +open(string url)
  MotionMasterPubSubSocket: +close()

  class MotionMasterPubSubWebSocket {
    note "Deserializes incoming data into\na tuple of topic and message."
  }

  class MotionMasterPubSubWorkerSocket {
    +Worker worker
    note "Passes messages between\nthe UI and worker thread."
  }

  MotionMasterClient *-- MotionMasterReqResClient
  MotionMasterClient *-- MotionMasterPubSubClient

  MotionMasterReqResClient *-- MotionMasterReqResSocket
  MotionMasterReqResWebSocket <|-- MotionMasterReqResSocket
  MotionMasterReqResWorkerSocket <|-- MotionMasterReqResSocket

  MotionMasterPubSubClient *-- MotionMasterPubSubSocket
  MotionMasterPubSubWebSocket <|-- MotionMasterPubSubSocket
  MotionMasterPubSubWorkerSocket <|-- MotionMasterPubSubSocket

Build & Publish

To update the motion-master-client version, first modify the version in libs/motion-master-client/package.json. Then, from the repository root, build the library by executing the following command:

npm run build motion-master-client

After building the library, proceed to publish it by following these steps:

cd dist/libs/motion-master-client
npm publish
0.0.103

7 days ago

0.0.102

7 days ago

0.0.101

7 days ago

0.0.100

10 days ago

0.0.99

14 days ago

0.0.98

19 days ago

0.0.97

28 days ago

0.0.96

29 days ago

0.0.95

1 month ago

0.0.94

1 month ago

0.0.93

1 month ago

0.0.92

1 month ago

0.0.91

1 month ago

0.0.85

1 month ago

0.0.86

1 month ago

0.0.88

1 month ago

0.0.89

1 month ago

0.0.90

1 month ago

0.0.76

2 months ago

0.0.77

2 months ago

0.0.73

2 months ago

0.0.74

2 months ago

0.0.75

2 months ago

0.0.72

2 months ago

0.0.71

2 months ago

0.0.70

2 months ago

0.0.68

3 months ago

0.0.69

3 months ago

0.0.67

3 months ago

0.0.66

4 months ago

0.0.62

6 months ago

0.0.63

6 months ago

0.0.64

6 months ago

0.0.65

6 months ago

0.0.60

6 months ago

0.0.61

6 months ago

0.0.59

6 months ago

0.0.56

6 months ago

0.0.57

6 months ago

0.0.58

6 months ago

0.0.40

11 months ago

0.0.41

11 months ago

0.0.42

11 months ago

0.0.43

10 months ago

0.0.44

10 months ago

0.0.45

10 months ago

0.0.46

10 months ago

0.0.47

10 months ago

0.0.37

11 months ago

0.0.38

11 months ago

0.0.39

11 months ago

0.0.30

11 months ago

0.0.31

11 months ago

0.0.32

11 months ago

0.0.33

11 months ago

0.0.34

11 months ago

0.0.35

11 months ago

0.0.36

11 months ago

0.0.26

11 months ago

0.0.27

11 months ago

0.0.29

11 months ago

0.0.20

11 months ago

0.0.21

11 months ago

0.0.22

11 months ago

0.0.23

11 months ago

0.0.25

11 months ago

0.0.19

12 months ago

0.0.52

10 months ago

0.0.53

10 months ago

0.0.54

10 months ago

0.0.55

10 months ago

0.0.50

10 months ago

0.0.49

10 months ago

0.0.16

1 year ago

0.0.17

1 year ago

0.0.18

12 months ago

0.0.10

1 year ago

0.0.11

1 year ago

0.0.12

1 year ago

0.0.13

1 year ago

0.0.14

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.15

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.1

1 year ago