0.0.246 • Published 4 months ago

motion-master-client v0.0.246

Weekly downloads
-
License
-
Repository
-
Last release
4 months ago

Motion Master Client

The Motion Master Client is a TypeScript library designed to facilitate communication with the Motion Master process via WebSocket connections. It utilizes Protocol Buffers for efficient message serialization during data exchange.

You can find various examples in the official Motion Master Client Examples GitHub repository.

Usage

The Motion Master Client library is versatile and can be used both in server environments with Node.js and in browsers.

The key classes for making requests are:

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);
  })
  .finally(() => 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);
  })
  .finally(() => 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);
});
0.0.205

5 months ago

0.0.204

5 months ago

0.0.203

6 months ago

0.0.202

6 months ago

0.0.209

5 months ago

0.0.208

5 months ago

0.0.207

5 months ago

0.0.206

5 months ago

0.0.201

6 months ago

0.0.200

6 months ago

0.0.216

5 months ago

0.0.215

5 months ago

0.0.214

5 months ago

0.0.213

5 months ago

0.0.219

5 months ago

0.0.218

5 months ago

0.0.217

5 months ago

0.0.212

5 months ago

0.0.211

5 months ago

0.0.210

5 months ago

0.0.227

5 months ago

0.0.226

5 months ago

0.0.225

5 months ago

0.0.224

5 months ago

0.0.229

5 months ago

0.0.228

5 months ago

0.0.223

5 months ago

0.0.222

5 months ago

0.0.221

5 months ago

0.0.220

5 months ago

0.0.197

7 months ago

0.0.196

7 months ago

0.0.195

7 months ago

0.0.194

7 months ago

0.0.199

7 months ago

0.0.198

7 months ago

0.0.193

7 months ago

0.0.192

7 months ago

0.0.191

7 months ago

0.0.190

7 months ago

0.0.186

8 months ago

0.0.185

8 months ago

0.0.184

8 months ago

0.0.183

8 months ago

0.0.189

7 months ago

0.0.188

8 months ago

0.0.187

8 months ago

0.0.238

4 months ago

0.0.237

4 months ago

0.0.236

4 months ago

0.0.235

4 months ago

0.0.239

4 months ago

0.0.230

5 months ago

0.0.234

4 months ago

0.0.233

4 months ago

0.0.232

4 months ago

0.0.231

4 months ago

0.0.246

4 months ago

0.0.241

4 months ago

0.0.240

4 months ago

0.0.245

4 months ago

0.0.244

4 months ago

0.0.243

4 months ago

0.0.242

4 months ago

0.0.182

8 months ago

0.0.181

8 months ago

0.0.179

8 months ago

0.0.180

8 months ago

0.0.178

8 months ago

0.0.159

9 months ago

0.0.158

9 months ago

0.0.153

10 months ago

0.0.152

10 months ago

0.0.151

10 months ago

0.0.150

10 months ago

0.0.157

10 months ago

0.0.156

10 months ago

0.0.155

10 months ago

0.0.154

10 months ago

0.0.169

9 months ago

0.0.164

9 months ago

0.0.163

9 months ago

0.0.162

9 months ago

0.0.161

9 months ago

0.0.168

9 months ago

0.0.167

9 months ago

0.0.166

9 months ago

0.0.165

9 months ago

0.0.160

9 months ago

0.0.175

9 months ago

0.0.174

9 months ago

0.0.173

9 months ago

0.0.172

9 months ago

0.0.177

9 months ago

0.0.176

9 months ago

0.0.171

9 months ago

0.0.170

9 months ago

0.0.119

10 months ago

0.0.128

10 months ago

0.0.127

10 months ago

0.0.126

10 months ago

0.0.125

10 months ago

0.0.129

10 months ago

0.0.120

10 months ago

0.0.124

10 months ago

0.0.123

10 months ago

0.0.122

10 months ago

0.0.121

10 months ago

0.0.139

10 months ago

0.0.138

10 months ago

0.0.137

10 months ago

0.0.136

10 months ago

0.0.131

10 months ago

0.0.130

10 months ago

0.0.135

10 months ago

0.0.134

10 months ago

0.0.133

10 months ago

0.0.132

10 months ago

0.0.149

10 months ago

0.0.148

10 months ago

0.0.147

10 months ago

0.0.142

10 months ago

0.0.141

10 months ago

0.0.140

10 months ago

0.0.146

10 months ago

0.0.145

10 months ago

0.0.144

10 months ago

0.0.143

10 months ago

0.0.106

1 year ago

0.0.109

1 year ago

0.0.108

1 year ago

0.0.107

1 year ago

0.0.117

12 months ago

0.0.116

12 months ago

0.0.115

12 months ago

0.0.114

1 year ago

0.0.118

12 months ago

0.0.113

1 year ago

0.0.112

1 year ago

0.0.111

1 year ago

0.0.110

1 year ago

0.0.105

1 year ago

0.0.104

1 year ago

0.0.103

1 year ago

0.0.102

1 year ago

0.0.101

1 year ago

0.0.100

1 year ago

0.0.99

1 year ago

0.0.98

1 year ago

0.0.97

1 year ago

0.0.96

1 year ago

0.0.95

1 year ago

0.0.94

1 year ago

0.0.93

1 year ago

0.0.92

1 year ago

0.0.91

1 year ago

0.0.85

1 year ago

0.0.86

1 year ago

0.0.88

1 year ago

0.0.89

1 year ago

0.0.90

1 year ago

0.0.76

1 year ago

0.0.77

1 year ago

0.0.73

1 year ago

0.0.74

1 year ago

0.0.75

1 year ago

0.0.72

1 year ago

0.0.71

1 year ago

0.0.70

1 year ago

0.0.68

1 year ago

0.0.69

1 year ago

0.0.67

1 year ago

0.0.66

1 year ago

0.0.62

2 years ago

0.0.63

2 years ago

0.0.64

2 years ago

0.0.65

2 years ago

0.0.60

2 years ago

0.0.61

2 years ago

0.0.59

2 years ago

0.0.56

2 years ago

0.0.57

2 years ago

0.0.58

2 years ago

0.0.40

2 years ago

0.0.41

2 years ago

0.0.42

2 years ago

0.0.43

2 years ago

0.0.44

2 years ago

0.0.45

2 years ago

0.0.46

2 years ago

0.0.47

2 years ago

0.0.37

2 years ago

0.0.38

2 years ago

0.0.39

2 years ago

0.0.30

2 years ago

0.0.31

2 years ago

0.0.32

2 years ago

0.0.33

2 years ago

0.0.34

2 years ago

0.0.35

2 years ago

0.0.36

2 years ago

0.0.26

2 years ago

0.0.27

2 years ago

0.0.29

2 years ago

0.0.20

2 years ago

0.0.21

2 years ago

0.0.22

2 years ago

0.0.23

2 years ago

0.0.25

2 years ago

0.0.19

2 years ago

0.0.52

2 years ago

0.0.53

2 years ago

0.0.54

2 years ago

0.0.55

2 years ago

0.0.50

2 years ago

0.0.49

2 years ago

0.0.16

2 years ago

0.0.17

2 years ago

0.0.18

2 years ago

0.0.10

2 years ago

0.0.11

2 years ago

0.0.12

2 years ago

0.0.13

2 years ago

0.0.14

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.15

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.1

3 years ago