0.0.246 • Published 8 months ago

motion-master-client v0.0.246

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

10 months ago

0.0.204

10 months ago

0.0.203

10 months ago

0.0.202

10 months ago

0.0.209

10 months ago

0.0.208

10 months ago

0.0.207

10 months ago

0.0.206

10 months ago

0.0.201

10 months ago

0.0.200

11 months ago

0.0.216

9 months ago

0.0.215

9 months ago

0.0.214

9 months ago

0.0.213

10 months ago

0.0.219

9 months ago

0.0.218

9 months ago

0.0.217

9 months ago

0.0.212

10 months ago

0.0.211

10 months ago

0.0.210

10 months ago

0.0.227

9 months ago

0.0.226

9 months ago

0.0.225

9 months ago

0.0.224

9 months ago

0.0.229

9 months ago

0.0.228

9 months ago

0.0.223

9 months ago

0.0.222

9 months ago

0.0.221

9 months ago

0.0.220

9 months ago

0.0.197

11 months ago

0.0.196

12 months ago

0.0.195

12 months ago

0.0.194

12 months ago

0.0.199

11 months ago

0.0.198

11 months ago

0.0.193

12 months ago

0.0.192

12 months ago

0.0.191

12 months ago

0.0.190

12 months ago

0.0.186

1 year ago

0.0.185

1 year ago

0.0.184

1 year ago

0.0.183

1 year ago

0.0.189

12 months ago

0.0.188

1 year ago

0.0.187

1 year ago

0.0.238

9 months ago

0.0.237

9 months ago

0.0.236

9 months ago

0.0.235

9 months ago

0.0.239

9 months ago

0.0.230

9 months ago

0.0.234

9 months ago

0.0.233

9 months ago

0.0.232

9 months ago

0.0.231

9 months ago

0.0.246

8 months ago

0.0.241

9 months ago

0.0.240

9 months ago

0.0.245

9 months ago

0.0.244

9 months ago

0.0.243

9 months ago

0.0.242

9 months ago

0.0.182

1 year ago

0.0.181

1 year ago

0.0.179

1 year ago

0.0.180

1 year ago

0.0.178

1 year ago

0.0.159

1 year ago

0.0.158

1 year ago

0.0.153

1 year ago

0.0.152

1 year ago

0.0.151

1 year ago

0.0.150

1 year ago

0.0.157

1 year ago

0.0.156

1 year ago

0.0.155

1 year ago

0.0.154

1 year ago

0.0.169

1 year ago

0.0.164

1 year ago

0.0.163

1 year ago

0.0.162

1 year ago

0.0.161

1 year ago

0.0.168

1 year ago

0.0.167

1 year ago

0.0.166

1 year ago

0.0.165

1 year ago

0.0.160

1 year ago

0.0.175

1 year ago

0.0.174

1 year ago

0.0.173

1 year ago

0.0.172

1 year ago

0.0.177

1 year ago

0.0.176

1 year ago

0.0.171

1 year ago

0.0.170

1 year ago

0.0.119

1 year ago

0.0.128

1 year ago

0.0.127

1 year ago

0.0.126

1 year ago

0.0.125

1 year ago

0.0.129

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

1 year ago

0.0.138

1 year ago

0.0.137

1 year ago

0.0.136

1 year ago

0.0.131

1 year ago

0.0.130

1 year ago

0.0.135

1 year ago

0.0.134

1 year ago

0.0.133

1 year ago

0.0.132

1 year ago

0.0.149

1 year ago

0.0.148

1 year ago

0.0.147

1 year ago

0.0.142

1 year ago

0.0.141

1 year ago

0.0.140

1 year ago

0.0.146

1 year ago

0.0.145

1 year ago

0.0.144

1 year ago

0.0.143

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

2 years ago

0.0.104

2 years ago

0.0.103

2 years ago

0.0.102

2 years ago

0.0.101

2 years ago

0.0.100

2 years ago

0.0.99

2 years ago

0.0.98

2 years ago

0.0.97

2 years ago

0.0.96

2 years ago

0.0.95

2 years ago

0.0.94

2 years ago

0.0.93

2 years ago

0.0.92

2 years ago

0.0.91

2 years ago

0.0.85

2 years ago

0.0.86

2 years ago

0.0.88

2 years ago

0.0.89

2 years ago

0.0.90

2 years ago

0.0.76

2 years ago

0.0.77

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

3 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

3 years ago

0.0.17

3 years ago

0.0.18

3 years ago

0.0.10

3 years ago

0.0.11

3 years ago

0.0.12

3 years ago

0.0.13

3 years ago

0.0.14

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.15

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.1

3 years ago