@ya-modbus/transport 0.10.1 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/README.md +16 -0
- package/dist/cjs/src/connection.d.ts +18 -3
- package/dist/cjs/src/connection.d.ts.map +1 -1
- package/dist/cjs/src/connection.js +43 -23
- package/dist/cjs/src/connection.js.map +1 -1
- package/dist/cjs/src/index.d.ts +1 -1
- package/dist/cjs/src/index.d.ts.map +1 -1
- package/dist/cjs/src/index.js.map +1 -1
- package/dist/cjs/src/manager.d.ts +37 -0
- package/dist/cjs/src/manager.d.ts.map +1 -1
- package/dist/cjs/src/manager.js +57 -14
- package/dist/cjs/src/manager.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/connection.d.ts +18 -3
- package/dist/esm/src/connection.d.ts.map +1 -1
- package/dist/esm/src/connection.js +43 -23
- package/dist/esm/src/connection.js.map +1 -1
- package/dist/esm/src/index.d.ts +1 -1
- package/dist/esm/src/index.d.ts.map +1 -1
- package/dist/esm/src/index.js.map +1 -1
- package/dist/esm/src/manager.d.ts +37 -0
- package/dist/esm/src/manager.d.ts.map +1 -1
- package/dist/esm/src/manager.js +57 -14
- package/dist/esm/src/manager.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [0.11.0](https://github.com/groupsky/ya-modbus/compare/@ya-modbus/transport@0.10.1...@ya-modbus/transport@0.11.0) (2026-10-12)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
- **transport:** let the mqtt-bridge start when a serial adapter is missing ([#587](https://github.com/groupsky/ya-modbus/issues/587)) ([38a2c9b](https://github.com/groupsky/ya-modbus/commit/38a2c9b4dc07b987c474afec995f6bc0a67ed20f)), closes [#549](https://github.com/groupsky/ya-modbus/issues/549) [#568](https://github.com/groupsky/ya-modbus/issues/568)
|
|
11
|
+
|
|
6
12
|
## [0.10.1](https://github.com/groupsky/ya-modbus/compare/@ya-modbus/transport@0.10.0...@ya-modbus/transport@0.10.1) (2026-10-12)
|
|
7
13
|
|
|
8
14
|
### Bug Fixes
|
package/README.md
CHANGED
|
@@ -100,6 +100,17 @@ Requests between the attempts reject with the last error. The delay also doubles
|
|
|
100
100
|
opens but is lost again before a request succeeds. A successful request sets the delay back to
|
|
101
101
|
1 second. `TransportManager` opens one new client for all devices on the bus.
|
|
102
102
|
|
|
103
|
+
`createTransport` and `TransportManager.getTransport()` reject when the port cannot be opened,
|
|
104
|
+
for example when a USB adapter is not plugged in. A long-running program can start anyway with
|
|
105
|
+
`new TransportManager({ startLost: true })`. Then `getTransport()` returns the transport, and
|
|
106
|
+
its connection starts lost with the error of the failed open. Requests reject with a
|
|
107
|
+
`ConnectionLostError` with a message like
|
|
108
|
+
`Could not open /dev/ttyUSB0: Error: No such file or directory, cannot open /dev/ttyUSB0`, until
|
|
109
|
+
the port opens for the first time. The port is opened again after the same delays as above.
|
|
110
|
+
`getConnectionError(config)` returns the error while the connection is lost, and `undefined`
|
|
111
|
+
while it works. The MQTT bridge uses this, so one missing adapter does not stop the other
|
|
112
|
+
devices.
|
|
113
|
+
|
|
103
114
|
A short-lived program, like a CLI command, can stop at the first `ConnectionLostError`.
|
|
104
115
|
|
|
105
116
|
```typescript
|
|
@@ -143,6 +154,11 @@ const transport = createModbusTransport(connection)
|
|
|
143
154
|
Without the `open` function, a lost connection works again only when you open the same client
|
|
144
155
|
again.
|
|
145
156
|
|
|
157
|
+
When the first `open()` fails, you can pass its error instead of the client:
|
|
158
|
+
`new ModbusConnection(error, '/dev/ttyUSB0', open)`. The connection then starts lost with that
|
|
159
|
+
error, and opens the port later in the same way. `connection.error` is the error while the
|
|
160
|
+
connection is lost, and `undefined` while it works.
|
|
161
|
+
|
|
146
162
|
## License
|
|
147
163
|
|
|
148
164
|
GPL-3.0-or-later
|
|
@@ -34,13 +34,15 @@ declare const CONNECTION: unique symbol;
|
|
|
34
34
|
* not lead to a successful request, up to MAX_REOPEN_DELAY_MS.
|
|
35
35
|
* - without an `open` function, requests reject until the owner of the client
|
|
36
36
|
* opens it again
|
|
37
|
+
*
|
|
38
|
+
* A connection created with the error of a failed open (for example a USB serial
|
|
39
|
+
* adapter that is not plugged in) starts lost in the same way.
|
|
37
40
|
*/
|
|
38
41
|
export declare class ModbusConnection {
|
|
39
42
|
private readonly target?;
|
|
40
43
|
private readonly open?;
|
|
41
44
|
readonly [CONNECTION] = true;
|
|
42
|
-
private
|
|
43
|
-
private lostError;
|
|
45
|
+
private state;
|
|
44
46
|
private reopenDelay;
|
|
45
47
|
private reopenAt;
|
|
46
48
|
private reopening;
|
|
@@ -51,7 +53,20 @@ export declare class ModbusConnection {
|
|
|
51
53
|
* @param target - Port path or host:port, used in error messages
|
|
52
54
|
* @param open - Opens a new client after the connection is lost
|
|
53
55
|
*/
|
|
54
|
-
constructor(client: ModbusRTU, target?: string
|
|
56
|
+
constructor(client: ModbusRTU, target?: string, open?: OpenClient);
|
|
57
|
+
/**
|
|
58
|
+
* @param openError - Error of a failed attempt to open the port. The connection starts
|
|
59
|
+
* lost: requests reject with a ConnectionLostError, and the first request after
|
|
60
|
+
* REOPEN_DELAY_MS calls `open`.
|
|
61
|
+
* @param target - Port path or host:port, used in error messages
|
|
62
|
+
* @param open - Opens a new client
|
|
63
|
+
*/
|
|
64
|
+
constructor(openError: Error, target: string | undefined, open: OpenClient);
|
|
65
|
+
/**
|
|
66
|
+
* The error that requests reject with while the connection is lost, or undefined while it
|
|
67
|
+
* works. The request that opens the port again clears it.
|
|
68
|
+
*/
|
|
69
|
+
get error(): ModbusError | undefined;
|
|
55
70
|
/**
|
|
56
71
|
* Run a request with the client
|
|
57
72
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../../src/connection.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,OAAO,KAAK,SAAS,MAAM,eAAe,CAAA;AAE1C,OAAO,EAAwB,KAAK,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAE7E,kFAAkF;AAClF,eAAO,MAAM,eAAe,OAAO,CAAA;AAEnC,kFAAkF;AAClF,eAAO,MAAM,mBAAmB,QAAS,CAAA;AAEzC;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,MAAM,OAAO,CAAC,SAAS,CAAC,CAAA;AAEjD;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,WAAW,CAE1E;AAeD,8EAA8E;AAC9E,QAAA,MAAM,UAAU,eAAsD,CAAA;
|
|
1
|
+
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../../src/connection.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,OAAO,KAAK,SAAS,MAAM,eAAe,CAAA;AAE1C,OAAO,EAAwB,KAAK,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAE7E,kFAAkF;AAClF,eAAO,MAAM,eAAe,OAAO,CAAA;AAEnC,kFAAkF;AAClF,eAAO,MAAM,mBAAmB,QAAS,CAAA;AAEzC;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,MAAM,OAAO,CAAC,SAAS,CAAC,CAAA;AAEjD;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,WAAW,CAE1E;AAeD,8EAA8E;AAC9E,QAAA,MAAM,UAAU,eAAsD,CAAA;AAmCtE;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,gBAAgB;IAyBzB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC;IACxB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC;IAzBxB,QAAQ,CAAC,CAAC,UAAU,CAAC,QAAO;IAC5B,OAAO,CAAC,KAAK,CAAiB;IAC9B,OAAO,CAAC,WAAW,CAAkB;IACrC,OAAO,CAAC,QAAQ,CAAI;IACpB,OAAO,CAAC,SAAS,CAAgC;IACjD,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA0C;IAEnE;;;;OAIG;gBACS,MAAM,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,UAAU;IACjE;;;;;;OAMG;gBACS,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,GAAG,SAAS,EAAE,IAAI,EAAE,UAAU;IAe1E;;;OAGG;IACH,IAAI,KAAK,IAAI,WAAW,GAAG,SAAS,CAEnC;IAED;;;;;;OAMG;IACG,GAAG,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,MAAM,EAAE,SAAS,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IA2BpE;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAa5B,OAAO,CAAC,KAAK;IAOb,OAAO,CAAC,IAAI;IAaZ,8EAA8E;IAC9E,OAAO,CAAC,WAAW;IAKnB;;;;OAIG;IACH,OAAO,CAAC,MAAM;YAmBA,UAAU;CAqBzB;AAED;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,SAAS,GAAG,gBAAgB,GAAG,gBAAgB,CAEnF"}
|
|
@@ -38,11 +38,16 @@ function closeClient(client, callback) {
|
|
|
38
38
|
/** Marks ModbusConnection objects, the same for all copies of this package */
|
|
39
39
|
const CONNECTION = Symbol.for('@ya-modbus/transport/ModbusConnection');
|
|
40
40
|
/**
|
|
41
|
-
* Error for a lost connection, with code and errno of a
|
|
41
|
+
* Error for a lost connection, or for a port that was never open, with code and errno of a
|
|
42
|
+
* Node.js system error cause
|
|
42
43
|
*/
|
|
43
|
-
function connectionLostError(target, cause) {
|
|
44
|
+
function connectionLostError(target, cause, wasOpen = true) {
|
|
44
45
|
const reason = (0, normalize_error_js_1.normalizeModbusError)(cause);
|
|
45
|
-
|
|
46
|
+
// A port that was never open was not lost
|
|
47
|
+
const message = wasOpen
|
|
48
|
+
? `Connection ${target === undefined ? '' : `to ${target} `}lost: ${reason.message}`
|
|
49
|
+
: `Could not open ${target ?? 'the connection'}: ${reason.message}`;
|
|
50
|
+
const error = new Error(message, { cause: reason });
|
|
46
51
|
error.name = 'ConnectionLostError';
|
|
47
52
|
// modbus-serial gives all its port errors errno 'ECONNREFUSED', also an unplugged
|
|
48
53
|
// serial adapter. Only Node.js system errors (they have a code) give a real errno.
|
|
@@ -66,13 +71,11 @@ function connectionLostError(target, cause) {
|
|
|
66
71
|
* not lead to a successful request, up to MAX_REOPEN_DELAY_MS.
|
|
67
72
|
* - without an `open` function, requests reject until the owner of the client
|
|
68
73
|
* opens it again
|
|
74
|
+
*
|
|
75
|
+
* A connection created with the error of a failed open (for example a USB serial
|
|
76
|
+
* adapter that is not plugged in) starts lost in the same way.
|
|
69
77
|
*/
|
|
70
78
|
class ModbusConnection {
|
|
71
|
-
/**
|
|
72
|
-
* @param client - Connected modbus-serial client
|
|
73
|
-
* @param target - Port path or host:port, used in error messages
|
|
74
|
-
* @param open - Opens a new client after the connection is lost
|
|
75
|
-
*/
|
|
76
79
|
constructor(client, target, open) {
|
|
77
80
|
this.target = target;
|
|
78
81
|
this.open = open;
|
|
@@ -81,8 +84,21 @@ class ModbusConnection {
|
|
|
81
84
|
this.reopenAt = 0;
|
|
82
85
|
this.closed = false;
|
|
83
86
|
this.inFlight = new Set();
|
|
84
|
-
|
|
85
|
-
|
|
87
|
+
if (client instanceof Error) {
|
|
88
|
+
this.state = { client: undefined, lostError: connectionLostError(target, client, false) };
|
|
89
|
+
this.delayReopen();
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
this.state = { client };
|
|
93
|
+
this.watch(client);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The error that requests reject with while the connection is lost, or undefined while it
|
|
98
|
+
* works. The request that opens the port again clears it.
|
|
99
|
+
*/
|
|
100
|
+
get error() {
|
|
101
|
+
return this.state.lostError;
|
|
86
102
|
}
|
|
87
103
|
/**
|
|
88
104
|
* Run a request with the client
|
|
@@ -92,8 +108,9 @@ class ModbusConnection {
|
|
|
92
108
|
* @throws ConnectionLostError when the connection is lost before or during the request
|
|
93
109
|
*/
|
|
94
110
|
async run(request) {
|
|
111
|
+
const { state } = this;
|
|
95
112
|
// No await while the connection works, so the request starts at once
|
|
96
|
-
const client =
|
|
113
|
+
const client = state.lostError === undefined ? state.client : await this.reopen(state.lostError);
|
|
97
114
|
// Assigned at once: the promise executor runs synchronously
|
|
98
115
|
let rejectOnLoss;
|
|
99
116
|
const lost = new Promise((_resolve, reject) => {
|
|
@@ -123,11 +140,12 @@ class ModbusConnection {
|
|
|
123
140
|
*/
|
|
124
141
|
async close() {
|
|
125
142
|
this.closed = true;
|
|
143
|
+
const { state } = this;
|
|
126
144
|
// A lost client or a client that is not open needs no close. modbus-serial never
|
|
127
145
|
// calls back when it closes a TCP client whose socket is closed.
|
|
128
|
-
if (
|
|
146
|
+
if (state.lostError !== undefined || !state.client.isOpen)
|
|
129
147
|
return;
|
|
130
|
-
const client =
|
|
148
|
+
const { client } = state;
|
|
131
149
|
await new Promise((resolve) => {
|
|
132
150
|
closeClient(client, () => resolve());
|
|
133
151
|
});
|
|
@@ -140,12 +158,13 @@ class ModbusConnection {
|
|
|
140
158
|
}
|
|
141
159
|
lose(client, cause) {
|
|
142
160
|
// Ignore events from clients that were replaced, and repeated events
|
|
143
|
-
if (client !== this.client || this.lostError !== undefined || this.closed)
|
|
161
|
+
if (client !== this.state.client || this.state.lostError !== undefined || this.closed)
|
|
144
162
|
return;
|
|
145
|
-
|
|
163
|
+
const lostError = connectionLostError(this.target, cause);
|
|
164
|
+
this.state = { client, lostError };
|
|
146
165
|
this.delayReopen();
|
|
147
166
|
for (const reject of this.inFlight)
|
|
148
|
-
reject(
|
|
167
|
+
reject(lostError);
|
|
149
168
|
// serialport closes the port on a disconnect. A port that is still open keeps
|
|
150
169
|
// its lock, and the reopen would fail.
|
|
151
170
|
if (client.isOpen)
|
|
@@ -164,9 +183,10 @@ class ModbusConnection {
|
|
|
164
183
|
reopen(lostError) {
|
|
165
184
|
if (this.open === undefined) {
|
|
166
185
|
// The owner of the client can open it again
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
186
|
+
const { client } = this.state;
|
|
187
|
+
if (client?.isOpen && !this.closed) {
|
|
188
|
+
this.state = { client };
|
|
189
|
+
return Promise.resolve(client);
|
|
170
190
|
}
|
|
171
191
|
return Promise.reject(lostError);
|
|
172
192
|
}
|
|
@@ -184,8 +204,9 @@ class ModbusConnection {
|
|
|
184
204
|
client = await open();
|
|
185
205
|
}
|
|
186
206
|
catch (error) {
|
|
187
|
-
|
|
188
|
-
this.
|
|
207
|
+
// Without a client, the port has never been open
|
|
208
|
+
const openError = connectionLostError(this.target, error, this.state.client !== undefined);
|
|
209
|
+
this.state = { client: this.state.client, lostError: openError };
|
|
189
210
|
this.delayReopen();
|
|
190
211
|
throw openError;
|
|
191
212
|
}
|
|
@@ -194,8 +215,7 @@ class ModbusConnection {
|
|
|
194
215
|
closeClient(client, () => undefined);
|
|
195
216
|
throw lostError;
|
|
196
217
|
}
|
|
197
|
-
this.
|
|
198
|
-
this.lostError = undefined;
|
|
218
|
+
this.state = { client };
|
|
199
219
|
this.watch(client);
|
|
200
220
|
return client;
|
|
201
221
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection.js","sourceRoot":"","sources":["../../../src/connection.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;AAyBH,sDAEC;
|
|
1
|
+
{"version":3,"file":"connection.js","sourceRoot":"","sources":["../../../src/connection.ts"],"names":[],"mappings":";AAA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,CAAA;YAC1F,IAAI,CAAC,KAAK,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,CAAA;YAChE,IAAI,CAAC,WAAW,EAAE,CAAA;YAClB,MAAM,SAAS,CAAA;QACjB,CAAC;QACD,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YAChB,kDAAkD;YAClD,WAAW,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,CAAA;YACpC,MAAM,SAAS,CAAA;QACjB,CAAC;QAED,IAAI,CAAC,KAAK,GAAG,EAAE,MAAM,EAAE,CAAA;QACvB,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;QAClB,OAAO,MAAM,CAAA;IACf,CAAC;CACF;AAtKD,4CAsKC;KArKW,UAAU;AAuKtB;;;;;;;;GAQG;AACH,SAAgB,YAAY,CAAC,MAAoC;IAC/D,OAAO,UAAU,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,gBAAgB,CAAC,MAAM,CAAC,CAAA;AACrE,CAAC"}
|
package/dist/cjs/src/index.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export { createModbusTransport } from './create-modbus-transport.js';
|
|
|
11
11
|
export { withRetry, MAX_RETRIES, RETRY_DELAY_MS, type RetryLogger } from './retry.js';
|
|
12
12
|
export { normalizeModbusError, type ModbusError } from './normalize-error.js';
|
|
13
13
|
export { isConnectionLostError, ModbusConnection, type OpenClient } from './connection.js';
|
|
14
|
-
export { TransportManager, type TransportStats } from './manager.js';
|
|
14
|
+
export { TransportManager, type TransportManagerOptions, type TransportStats } from './manager.js';
|
|
15
15
|
export { MutexTransport } from './mutex-transport.js';
|
|
16
16
|
export { SlaveTransport } from './slave-transport.js';
|
|
17
17
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/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AAE,gBAAgB,EAAE,KAAK,uBAAuB,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAA;AAClG,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,2CAAoE;AAA3D,6GAAA,eAAe,OAAA;AACxB,uDAAuE;AAA9D,sHAAA,kBAAkB,OAAA;AAC3B,uDAAuE;AAA9D,sHAAA,kBAAkB,OAAA;AAC3B,2EAAoE;AAA3D,mIAAA,qBAAqB,OAAA;AAC9B,uCAAqF;AAA5E,qGAAA,SAAS,OAAA;AAAE,uGAAA,WAAW,OAAA;AAAE,0GAAA,cAAc,OAAA;AAC/C,2DAA6E;AAApE,0HAAA,oBAAoB,OAAA;AAC7B,iDAA0F;AAAjF,sHAAA,qBAAqB,OAAA;AAAE,iHAAA,gBAAgB,OAAA;AAChD,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,2CAAoE;AAA3D,6GAAA,eAAe,OAAA;AACxB,uDAAuE;AAA9D,sHAAA,kBAAkB,OAAA;AAC3B,uDAAuE;AAA9D,sHAAA,kBAAkB,OAAA;AAC3B,2EAAoE;AAA3D,mIAAA,qBAAqB,OAAA;AAC9B,uCAAqF;AAA5E,qGAAA,SAAS,OAAA;AAAE,uGAAA,WAAW,OAAA;AAAE,0GAAA,cAAc,OAAA;AAC/C,2DAA6E;AAApE,0HAAA,oBAAoB,OAAA;AAC7B,iDAA0F;AAAjF,sHAAA,qBAAqB,OAAA;AAAE,iHAAA,gBAAgB,OAAA;AAChD,2CAAkG;AAAzF,8GAAA,gBAAgB,OAAA;AACzB,2DAAqD;AAA5C,oHAAA,cAAc,OAAA;AACvB,2DAAqD;AAA5C,oHAAA,cAAc,OAAA"}
|
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
import type { Transport } from '@ya-modbus/driver-types';
|
|
2
2
|
import type { TransportConfig } from './factory.js';
|
|
3
|
+
import { type ModbusError } from './normalize-error.js';
|
|
4
|
+
/**
|
|
5
|
+
* Options for TransportManager
|
|
6
|
+
*/
|
|
7
|
+
export interface TransportManagerOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Return a transport also when its port cannot be opened (for example a USB serial adapter
|
|
10
|
+
* that is not plugged in). The connection then starts lost: requests reject with a
|
|
11
|
+
* ConnectionLostError, and a request after the reopen delay opens the port (see
|
|
12
|
+
* ModbusConnection). getConnectionError() returns the error.
|
|
13
|
+
*
|
|
14
|
+
* Default: false, getTransport() rejects when the port cannot be opened.
|
|
15
|
+
*/
|
|
16
|
+
startLost?: boolean;
|
|
17
|
+
}
|
|
3
18
|
/**
|
|
4
19
|
* Statistics about managed transports
|
|
5
20
|
*/
|
|
@@ -21,6 +36,8 @@ export interface TransportStats {
|
|
|
21
36
|
* - A lost connection (USB adapter unplugged, TCP connection closed) drops the client.
|
|
22
37
|
* The first request after a backoff opens a new client for all devices on the
|
|
23
38
|
* connection (see ModbusConnection)
|
|
39
|
+
* - With the `startLost` option, a port that cannot be opened (USB adapter missing at
|
|
40
|
+
* start) gives a lost connection instead of an error
|
|
24
41
|
*
|
|
25
42
|
* This architecture solves the multi-device problem:
|
|
26
43
|
* - Each device gets its own SlaveTransport with its own slave ID
|
|
@@ -36,7 +53,10 @@ export interface TransportStats {
|
|
|
36
53
|
* - SlaveTransport.close(): Does not close shared client (use closeAll() for cleanup)
|
|
37
54
|
*/
|
|
38
55
|
export declare class TransportManager {
|
|
56
|
+
private readonly options;
|
|
39
57
|
private readonly connections;
|
|
58
|
+
private readonly opening;
|
|
59
|
+
constructor(options?: TransportManagerOptions);
|
|
40
60
|
/**
|
|
41
61
|
* Get or create a transport for the given configuration.
|
|
42
62
|
* Returns a new SlaveTransport instance bound to the slave ID in the config.
|
|
@@ -44,8 +64,25 @@ export declare class TransportManager {
|
|
|
44
64
|
*
|
|
45
65
|
* @param config - RTU or TCP transport configuration
|
|
46
66
|
* @returns SlaveTransport instance for the specific slave device
|
|
67
|
+
* @throws When the port cannot be opened, unless the `startLost` option is set
|
|
47
68
|
*/
|
|
48
69
|
getTransport(config: TransportConfig): Promise<Transport>;
|
|
70
|
+
/**
|
|
71
|
+
* Open the shared connection for a physical connection and add it to the pool
|
|
72
|
+
*
|
|
73
|
+
* @param key - Physical connection key
|
|
74
|
+
* @param config - Transport configuration
|
|
75
|
+
* @returns The pool entry
|
|
76
|
+
*/
|
|
77
|
+
private openConnection;
|
|
78
|
+
/**
|
|
79
|
+
* Get the error of a lost connection
|
|
80
|
+
*
|
|
81
|
+
* @param config - Configuration of a transport on the connection
|
|
82
|
+
* @returns The error that requests on the connection reject with while it is lost.
|
|
83
|
+
* Undefined while the connection works, or when there is no connection for the config.
|
|
84
|
+
*/
|
|
85
|
+
getConnectionError(config: TransportConfig): ModbusError | undefined;
|
|
49
86
|
/**
|
|
50
87
|
* Get statistics about managed connections
|
|
51
88
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../../../src/manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAMxD,OAAO,KAAK,EAAwB,eAAe,EAAE,MAAM,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../../../src/manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAMxD,OAAO,KAAK,EAAwB,eAAe,EAAE,MAAM,cAAc,CAAA;AACzE,OAAO,EAAwB,KAAK,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAa7E;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC;;;;;;;OAOG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,eAAe,EAAE,MAAM,CAAA;IACvB,aAAa,EAAE,MAAM,CAAA;IACrB,aAAa,EAAE,MAAM,CAAA;CACtB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,qBAAa,gBAAgB;IAKf,OAAO,CAAC,QAAQ,CAAC,OAAO;IAJpC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAqC;IAEjE,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA8C;gBAEzC,OAAO,GAAE,uBAA4B;IAElE;;;;;;;;OAQG;IACG,YAAY,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,SAAS,CAAC;IAwB/D;;;;;;OAMG;YACW,cAAc;IAkB5B;;;;;;OAMG;IACH,kBAAkB,CAAC,MAAM,EAAE,eAAe,GAAG,WAAW,GAAG,SAAS;IAIpE;;;;OAIG;IACH,QAAQ,IAAI,cAAc;IAS1B;;;OAGG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAsB/B;;;;;;;;OAQG;IACH,OAAO,CAAC,wBAAwB;IAShC;;;;;OAKG;IACH,OAAO,CAAC,SAAS;IAIjB;;;;;;OAMG;YACW,eAAe;IAe7B;;;;;;OAMG;YACW,eAAe;IAU7B;;;;;OAKG;IACH,OAAO,CAAC,WAAW;CAGpB"}
|
package/dist/cjs/src/manager.js
CHANGED
|
@@ -6,6 +6,7 @@ const async_mutex_1 = require("async-mutex");
|
|
|
6
6
|
const modbus_serial_1 = tslib_1.__importDefault(require("modbus-serial"));
|
|
7
7
|
const client_config_js_1 = require("./client-config.js");
|
|
8
8
|
const connection_js_1 = require("./connection.js");
|
|
9
|
+
const normalize_error_js_1 = require("./normalize-error.js");
|
|
9
10
|
const slave_transport_js_1 = require("./slave-transport.js");
|
|
10
11
|
/**
|
|
11
12
|
* TransportManager pools modbus-serial clients and provides slave-specific transports.
|
|
@@ -20,6 +21,8 @@ const slave_transport_js_1 = require("./slave-transport.js");
|
|
|
20
21
|
* - A lost connection (USB adapter unplugged, TCP connection closed) drops the client.
|
|
21
22
|
* The first request after a backoff opens a new client for all devices on the
|
|
22
23
|
* connection (see ModbusConnection)
|
|
24
|
+
* - With the `startLost` option, a port that cannot be opened (USB adapter missing at
|
|
25
|
+
* start) gives a lost connection instead of an error
|
|
23
26
|
*
|
|
24
27
|
* This architecture solves the multi-device problem:
|
|
25
28
|
* - Each device gets its own SlaveTransport with its own slave ID
|
|
@@ -35,8 +38,11 @@ const slave_transport_js_1 = require("./slave-transport.js");
|
|
|
35
38
|
* - SlaveTransport.close(): Does not close shared client (use closeAll() for cleanup)
|
|
36
39
|
*/
|
|
37
40
|
class TransportManager {
|
|
38
|
-
constructor() {
|
|
41
|
+
constructor(options = {}) {
|
|
42
|
+
this.options = options;
|
|
39
43
|
this.connections = new Map();
|
|
44
|
+
// Connections that are opening, so that devices added at the same time share one client
|
|
45
|
+
this.opening = new Map();
|
|
40
46
|
}
|
|
41
47
|
/**
|
|
42
48
|
* Get or create a transport for the given configuration.
|
|
@@ -45,27 +51,57 @@ class TransportManager {
|
|
|
45
51
|
*
|
|
46
52
|
* @param config - RTU or TCP transport configuration
|
|
47
53
|
* @returns SlaveTransport instance for the specific slave device
|
|
54
|
+
* @throws When the port cannot be opened, unless the `startLost` option is set
|
|
48
55
|
*/
|
|
49
56
|
async getTransport(config) {
|
|
50
57
|
const key = this.getPhysicalConnectionKey(config);
|
|
51
|
-
const isRTU = this.isRTUConfig(config);
|
|
52
58
|
// Get or create the shared connection
|
|
53
59
|
let entry = this.connections.get(key);
|
|
54
60
|
if (!entry) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
config,
|
|
62
|
-
isRTU,
|
|
63
|
-
};
|
|
64
|
-
this.connections.set(key, entry);
|
|
61
|
+
let opening = this.opening.get(key);
|
|
62
|
+
if (!opening) {
|
|
63
|
+
opening = this.openConnection(key, config).finally(() => this.opening.delete(key));
|
|
64
|
+
this.opening.set(key, opening);
|
|
65
|
+
}
|
|
66
|
+
entry = await opening;
|
|
65
67
|
}
|
|
66
68
|
// Return a new SlaveTransport for this specific slave
|
|
67
69
|
return new slave_transport_js_1.SlaveTransport(config.slaveId, entry.connection, entry.mutex, config.maxRetries ?? 3, config.logger);
|
|
68
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Open the shared connection for a physical connection and add it to the pool
|
|
73
|
+
*
|
|
74
|
+
* @param key - Physical connection key
|
|
75
|
+
* @param config - Transport configuration
|
|
76
|
+
* @returns The pool entry
|
|
77
|
+
*/
|
|
78
|
+
async openConnection(key, config) {
|
|
79
|
+
const isRTU = this.isRTUConfig(config);
|
|
80
|
+
const open = isRTU ? () => this.createRTUClient(config) : () => this.createTCPClient(config);
|
|
81
|
+
const target = this.getTarget(config);
|
|
82
|
+
let connection;
|
|
83
|
+
try {
|
|
84
|
+
connection = new connection_js_1.ModbusConnection(await open(), target, open);
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
if (!this.options.startLost)
|
|
88
|
+
throw error;
|
|
89
|
+
connection = new connection_js_1.ModbusConnection((0, normalize_error_js_1.normalizeModbusError)(error), target, open);
|
|
90
|
+
}
|
|
91
|
+
const entry = { connection, mutex: new async_mutex_1.Mutex(), config, isRTU };
|
|
92
|
+
this.connections.set(key, entry);
|
|
93
|
+
return entry;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Get the error of a lost connection
|
|
97
|
+
*
|
|
98
|
+
* @param config - Configuration of a transport on the connection
|
|
99
|
+
* @returns The error that requests on the connection reject with while it is lost.
|
|
100
|
+
* Undefined while the connection works, or when there is no connection for the config.
|
|
101
|
+
*/
|
|
102
|
+
getConnectionError(config) {
|
|
103
|
+
return this.connections.get(this.getPhysicalConnectionKey(config))?.connection.error;
|
|
104
|
+
}
|
|
69
105
|
/**
|
|
70
106
|
* Get statistics about managed connections
|
|
71
107
|
*
|
|
@@ -84,7 +120,15 @@ class TransportManager {
|
|
|
84
120
|
* Errors during close are logged but do not stop the process.
|
|
85
121
|
*/
|
|
86
122
|
async closeAll() {
|
|
87
|
-
|
|
123
|
+
// A connection that is still opening is added to the pool when it opens. More opens can
|
|
124
|
+
// start while this waits.
|
|
125
|
+
while (this.opening.size > 0)
|
|
126
|
+
await Promise.allSettled(this.opening.values());
|
|
127
|
+
// Clear the pool first: a connection that opens while these close stays in the pool,
|
|
128
|
+
// so the next closeAll() closes it
|
|
129
|
+
const entries = Array.from(this.connections.values());
|
|
130
|
+
this.connections.clear();
|
|
131
|
+
const closePromises = entries.map(async (entry) => {
|
|
88
132
|
try {
|
|
89
133
|
await entry.connection.close();
|
|
90
134
|
}
|
|
@@ -94,7 +138,6 @@ class TransportManager {
|
|
|
94
138
|
}
|
|
95
139
|
});
|
|
96
140
|
await Promise.all(closePromises);
|
|
97
|
-
this.connections.clear();
|
|
98
141
|
}
|
|
99
142
|
/**
|
|
100
143
|
* Generate a unique key for a physical connection.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manager.js","sourceRoot":"","sources":["../../../src/manager.ts"],"names":[],"mappings":";;;;AACA,6CAAmC;AACnC,0EAAqC;AAErC,yDAAmE;AACnE,mDAAkD;AAElD,6DAAqD;
|
|
1
|
+
{"version":3,"file":"manager.js","sourceRoot":"","sources":["../../../src/manager.ts"],"names":[],"mappings":";;;;AACA,6CAAmC;AACnC,0EAAqC;AAErC,yDAAmE;AACnE,mDAAkD;AAElD,6DAA6E;AAC7E,6DAAqD;AAoCrD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAa,gBAAgB;IAK3B,YAA6B,UAAmC,EAAE;QAArC,YAAO,GAAP,OAAO,CAA8B;QAJjD,gBAAW,GAAG,IAAI,GAAG,EAA2B,CAAA;QACjE,wFAAwF;QACvE,YAAO,GAAG,I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