0.0.246 • Published 6 months ago

motion-master-client v0.0.246

Weekly downloads
-
License
-
Repository
-
Last release
6 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

7 months ago

0.0.204

8 months ago

0.0.203

8 months ago

0.0.202

8 months ago

0.0.209

7 months ago

0.0.208

7 months ago

0.0.207

7 months ago

0.0.206

7 months ago

0.0.201

8 months ago

0.0.200

9 months ago

0.0.216

7 months ago

0.0.215

7 months ago

0.0.214

7 months ago

0.0.213

7 months ago

0.0.219

7 months ago

0.0.218

7 months ago

0.0.217

7 months ago

0.0.212

7 months ago

0.0.211

7 months ago

0.0.210

7 months ago

0.0.227

7 months ago

0.0.226

7 months ago

0.0.225

7 months ago

0.0.224

7 months ago

0.0.229

7 months ago

0.0.228

7 months ago

0.0.223

7 months ago

0.0.222

7 months ago

0.0.221

7 months ago

0.0.220

7 months ago

0.0.197

9 months ago

0.0.196

9 months ago

0.0.195

9 months ago

0.0.194

9 months ago

0.0.199

9 months ago

0.0.198

9 months ago

0.0.193

9 months ago

0.0.192

9 months ago

0.0.191

9 months ago

0.0.190

9 months ago

0.0.186

10 months ago

0.0.185

10 months ago

0.0.184

10 months ago

0.0.183

10 months ago

0.0.189

9 months ago

0.0.188

10 months ago

0.0.187

10 months ago

0.0.238

6 months ago

0.0.237

6 months ago

0.0.236

6 months ago

0.0.235

6 months ago

0.0.239

6 months ago

0.0.230

7 months ago

0.0.234

6 months ago

0.0.233

7 months ago

0.0.232

7 months ago

0.0.231

7 months ago

0.0.246

6 months ago

0.0.241

6 months ago

0.0.240

6 months ago

0.0.245

6 months ago

0.0.244

6 months ago

0.0.243

6 months ago

0.0.242

6 months ago

0.0.182

10 months ago

0.0.181

11 months ago

0.0.179

11 months ago

0.0.180

11 months ago

0.0.178

11 months ago

0.0.159

12 months ago

0.0.158

12 months ago

0.0.153

12 months ago

0.0.152

12 months ago

0.0.151

12 months ago

0.0.150

12 months ago

0.0.157

12 months ago

0.0.156

12 months ago

0.0.155

12 months ago

0.0.154

12 months ago

0.0.169

11 months ago

0.0.164

11 months ago

0.0.163

11 months ago

0.0.162

11 months ago

0.0.161

11 months ago

0.0.168

11 months ago

0.0.167

11 months ago

0.0.166

11 months ago

0.0.165

11 months ago

0.0.160

11 months ago

0.0.175

11 months ago

0.0.174

11 months ago

0.0.173

11 months ago

0.0.172

11 months ago

0.0.177

11 months ago

0.0.176

11 months ago

0.0.171

11 months ago

0.0.170

11 months ago

0.0.119

1 year ago

0.0.128

12 months ago

0.0.127

1 year ago

0.0.126

1 year ago

0.0.125

1 year ago

0.0.129

12 months ago

0.0.120

1 year ago

0.0.124

1 year ago

0.0.123

1 year ago

0.0.122

1 year ago

0.0.121

1 year ago

0.0.139

12 months ago

0.0.138

12 months ago

0.0.137

12 months ago

0.0.136

12 months ago

0.0.131

12 months ago

0.0.130

12 months ago

0.0.135

12 months ago

0.0.134

12 months ago

0.0.133

12 months ago

0.0.132

12 months ago

0.0.149

12 months ago

0.0.148

12 months ago

0.0.147

12 months ago

0.0.142

12 months ago

0.0.141

12 months ago

0.0.140

12 months ago

0.0.146

12 months ago

0.0.145

12 months ago

0.0.144

12 months ago

0.0.143

12 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

1 year ago

0.0.116

1 year ago

0.0.115

1 year ago

0.0.114

1 year ago

0.0.118

1 year 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

2 years ago

0.0.74

2 years ago

0.0.75

2 years ago

0.0.72

2 years ago

0.0.71

2 years ago

0.0.70

2 years ago

0.0.68

2 years ago

0.0.69

2 years ago

0.0.67

2 years ago

0.0.66

2 years 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

3 years ago

0.0.2

3 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