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

6 months ago

0.0.204

6 months ago

0.0.203

7 months ago

0.0.202

7 months ago

0.0.209

6 months ago

0.0.208

6 months ago

0.0.207

6 months ago

0.0.206

6 months ago

0.0.201

7 months ago

0.0.200

7 months ago

0.0.216

6 months ago

0.0.215

6 months ago

0.0.214

6 months ago

0.0.213

6 months ago

0.0.219

6 months ago

0.0.218

6 months ago

0.0.217

6 months ago

0.0.212

6 months ago

0.0.211

6 months ago

0.0.210

6 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

6 months ago

0.0.220

6 months ago

0.0.197

7 months ago

0.0.196

8 months ago

0.0.195

8 months ago

0.0.194

8 months ago

0.0.199

7 months ago

0.0.198

7 months ago

0.0.193

8 months ago

0.0.192

8 months ago

0.0.191

8 months ago

0.0.190

8 months ago

0.0.186

9 months ago

0.0.185

9 months ago

0.0.184

9 months ago

0.0.183

9 months ago

0.0.189

8 months ago

0.0.188

9 months ago

0.0.187

9 months ago

0.0.238

5 months ago

0.0.237

5 months ago

0.0.236

5 months ago

0.0.235

5 months ago

0.0.239

5 months ago

0.0.230

5 months ago

0.0.234

5 months ago

0.0.233

5 months ago

0.0.232

5 months ago

0.0.231

5 months ago

0.0.246

4 months ago

0.0.241

5 months ago

0.0.240

5 months ago

0.0.245

5 months ago

0.0.244

5 months ago

0.0.243

5 months ago

0.0.242

5 months ago

0.0.182

9 months ago

0.0.181

9 months ago

0.0.179

9 months ago

0.0.180

9 months ago

0.0.178

9 months ago

0.0.159

10 months ago

0.0.158

10 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

10 months ago

0.0.164

10 months ago

0.0.163

10 months ago

0.0.162

10 months ago

0.0.161

10 months ago

0.0.168

10 months ago

0.0.167

10 months ago

0.0.166

10 months ago

0.0.165

10 months ago

0.0.160

10 months ago

0.0.175

9 months ago

0.0.174

10 months ago

0.0.173

10 months ago

0.0.172

10 months ago

0.0.177

9 months ago

0.0.176

9 months ago

0.0.171

10 months ago

0.0.170

10 months ago

0.0.119

11 months ago

0.0.128

11 months ago

0.0.127

11 months ago

0.0.126

11 months ago

0.0.125

11 months ago

0.0.129

11 months ago

0.0.120

11 months ago

0.0.124

11 months ago

0.0.123

11 months ago

0.0.122

11 months ago

0.0.121

11 months ago

0.0.139

11 months ago

0.0.138

11 months ago

0.0.137

11 months ago

0.0.136

11 months ago

0.0.131

11 months ago

0.0.130

11 months ago

0.0.135

11 months ago

0.0.134

11 months ago

0.0.133

11 months ago

0.0.132

11 months ago

0.0.149

10 months ago

0.0.148

10 months ago

0.0.147

10 months ago

0.0.142

11 months ago

0.0.141

11 months ago

0.0.140

11 months ago

0.0.146

10 months ago

0.0.145

10 months ago

0.0.144

11 months ago

0.0.143

11 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

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

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

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