@ya-modbus/transport 0.10.0 → 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 +12 -0
- package/README.md +22 -0
- package/dist/cjs/src/client-config.d.ts +15 -0
- package/dist/cjs/src/client-config.d.ts.map +1 -1
- package/dist/cjs/src/client-config.js +26 -0
- package/dist/cjs/src/client-config.js.map +1 -1
- 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 +58 -15
- package/dist/cjs/src/manager.js.map +1 -1
- package/dist/cjs/src/tcp-transport.d.ts +1 -1
- package/dist/cjs/src/tcp-transport.d.ts.map +1 -1
- package/dist/cjs/src/tcp-transport.js +1 -1
- package/dist/cjs/src/tcp-transport.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/client-config.d.ts +15 -0
- package/dist/esm/src/client-config.d.ts.map +1 -1
- package/dist/esm/src/client-config.js +25 -0
- package/dist/esm/src/client-config.js.map +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 +59 -16
- package/dist/esm/src/manager.js.map +1 -1
- package/dist/esm/src/tcp-transport.d.ts +1 -1
- package/dist/esm/src/tcp-transport.d.ts.map +1 -1
- package/dist/esm/src/tcp-transport.js +2 -2
- package/dist/esm/src/tcp-transport.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,18 @@
|
|
|
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
|
+
|
|
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)
|
|
13
|
+
|
|
14
|
+
### Bug Fixes
|
|
15
|
+
|
|
16
|
+
- **transport:** time out TCP connects that get no answer ([#588](https://github.com/groupsky/ya-modbus/issues/588)) ([2eeaf08](https://github.com/groupsky/ya-modbus/commit/2eeaf08e1c04c17953591bb3000a63339823bffc)), closes [#569](https://github.com/groupsky/ya-modbus/issues/569)
|
|
17
|
+
|
|
6
18
|
# [0.10.0](https://github.com/groupsky/ya-modbus/compare/@ya-modbus/transport@0.9.1...@ya-modbus/transport@0.10.0) (2026-10-12)
|
|
7
19
|
|
|
8
20
|
### Features
|
package/README.md
CHANGED
|
@@ -87,6 +87,12 @@ so the process does not crash. The connection is then lost:
|
|
|
87
87
|
After a TCP socket error, `modbus-serial` emits no event. The next request fails with the error of
|
|
88
88
|
`modbus-serial` (for example `Port Not Open`), and the connection is lost from then on.
|
|
89
89
|
|
|
90
|
+
Opening a TCP connection takes at most `timeout` ms (the response timeout, default 1000 ms).
|
|
91
|
+
When the host does not answer, the open fails with `TCP Connection Timed Out`. Without this
|
|
92
|
+
limit it would wait for the TCP timeout of the system (about 2 minutes), and the other devices
|
|
93
|
+
on the connection would wait too. On a slow network, or one that loses packets, use a larger
|
|
94
|
+
`timeout` (for example 3000).
|
|
95
|
+
|
|
90
96
|
`createTransport` and `TransportManager` open the port again on a later request. There are no
|
|
91
97
|
attempts in the background. The first request 1 second after the loss opens the port again. If
|
|
92
98
|
this fails, the next attempt waits 2 seconds, then 4 seconds, and so on, up to 60 seconds.
|
|
@@ -94,6 +100,17 @@ Requests between the attempts reject with the last error. The delay also doubles
|
|
|
94
100
|
opens but is lost again before a request succeeds. A successful request sets the delay back to
|
|
95
101
|
1 second. `TransportManager` opens one new client for all devices on the bus.
|
|
96
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
|
+
|
|
97
114
|
A short-lived program, like a CLI command, can stop at the first `ConnectionLostError`.
|
|
98
115
|
|
|
99
116
|
```typescript
|
|
@@ -137,6 +154,11 @@ const transport = createModbusTransport(connection)
|
|
|
137
154
|
Without the `open` function, a lost connection works again only when you open the same client
|
|
138
155
|
again.
|
|
139
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
|
+
|
|
140
162
|
## License
|
|
141
163
|
|
|
142
164
|
GPL-3.0-or-later
|
|
@@ -12,4 +12,19 @@ export declare const DEFAULT_TIMEOUT_MS = 1000;
|
|
|
12
12
|
* @param timeout - Response timeout in milliseconds (default: DEFAULT_TIMEOUT_MS)
|
|
13
13
|
*/
|
|
14
14
|
export declare function configureClient(client: ModbusRTU, slaveId: number, timeout?: number): void;
|
|
15
|
+
/**
|
|
16
|
+
* Connect a modbus-serial client to a Modbus TCP server, and give up after `timeout` ms when
|
|
17
|
+
* the host does not answer
|
|
18
|
+
*
|
|
19
|
+
* Without a timeout, the connect waits for the TCP timeout of the system (about 2 minutes on
|
|
20
|
+
* Linux). modbus-serial uses the `timeout` option as the socket timeout, so a connect that
|
|
21
|
+
* takes longer fails with 'TCP Connection Timed Out'. After the connect, the socket keeps
|
|
22
|
+
* the timeout, but then it does not close the socket.
|
|
23
|
+
*
|
|
24
|
+
* @param client - New modbus-serial client
|
|
25
|
+
* @param host - TCP host (IP address or hostname)
|
|
26
|
+
* @param port - TCP port
|
|
27
|
+
* @param timeout - Connect timeout in milliseconds (default: DEFAULT_TIMEOUT_MS)
|
|
28
|
+
*/
|
|
29
|
+
export declare function connectTCP(client: ModbusRTU, host: string, port: number, timeout?: number): Promise<void>;
|
|
15
30
|
//# sourceMappingURL=client-config.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client-config.d.ts","sourceRoot":"","sources":["../../../src/client-config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,SAAS,MAAM,eAAe,CAAA;AAE1C,qEAAqE;AACrE,eAAO,MAAM,kBAAkB,OAAO,CAAA;AAEtC;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAG1F"}
|
|
1
|
+
{"version":3,"file":"client-config.d.ts","sourceRoot":"","sources":["../../../src/client-config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,SAAS,MAAM,eAAe,CAAA;AAE1C,qEAAqE;AACrE,eAAO,MAAM,kBAAkB,OAAO,CAAA;AAEtC;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAG1F;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAsB,UAAU,CAC9B,MAAM,EAAE,SAAS,EACjB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,OAAO,SAAqB,GAC3B,OAAO,CAAC,IAAI,CAAC,CASf"}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.DEFAULT_TIMEOUT_MS = void 0;
|
|
7
7
|
exports.configureClient = configureClient;
|
|
8
|
+
exports.connectTCP = connectTCP;
|
|
8
9
|
/** Default response timeout for Modbus operations in milliseconds */
|
|
9
10
|
exports.DEFAULT_TIMEOUT_MS = 1000;
|
|
10
11
|
/**
|
|
@@ -18,4 +19,29 @@ function configureClient(client, slaveId, timeout) {
|
|
|
18
19
|
client.setID(slaveId);
|
|
19
20
|
client.setTimeout(timeout ?? exports.DEFAULT_TIMEOUT_MS);
|
|
20
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Connect a modbus-serial client to a Modbus TCP server, and give up after `timeout` ms when
|
|
24
|
+
* the host does not answer
|
|
25
|
+
*
|
|
26
|
+
* Without a timeout, the connect waits for the TCP timeout of the system (about 2 minutes on
|
|
27
|
+
* Linux). modbus-serial uses the `timeout` option as the socket timeout, so a connect that
|
|
28
|
+
* takes longer fails with 'TCP Connection Timed Out'. After the connect, the socket keeps
|
|
29
|
+
* the timeout, but then it does not close the socket.
|
|
30
|
+
*
|
|
31
|
+
* @param client - New modbus-serial client
|
|
32
|
+
* @param host - TCP host (IP address or hostname)
|
|
33
|
+
* @param port - TCP port
|
|
34
|
+
* @param timeout - Connect timeout in milliseconds (default: DEFAULT_TIMEOUT_MS)
|
|
35
|
+
*/
|
|
36
|
+
async function connectTCP(client, host, port, timeout = exports.DEFAULT_TIMEOUT_MS) {
|
|
37
|
+
try {
|
|
38
|
+
await client.connectTCP(host, { port, timeout });
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
// After a timeout the socket still tries to connect. A connection that it opens later
|
|
42
|
+
// would stay open with nothing using it.
|
|
43
|
+
client.destroy(() => undefined);
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
21
47
|
//# sourceMappingURL=client-config.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client-config.js","sourceRoot":"","sources":["../../../src/client-config.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAcH,0CAGC;
|
|
1
|
+
{"version":3,"file":"client-config.js","sourceRoot":"","sources":["../../../src/client-config.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAcH,0CAGC;AAgBD,gCAcC;AA3CD,qEAAqE;AACxD,QAAA,kBAAkB,GAAG,IAAI,CAAA;AAEtC;;;;;;GAMG;AACH,SAAgB,eAAe,CAAC,MAAiB,EAAE,OAAe,EAAE,OAAgB;IAClF,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;IACrB,MAAM,CAAC,UAAU,CAAC,OAAO,IAAI,0BAAkB,CAAC,CAAA;AAClD,CAAC;AAED;;;;;;;;;;;;;GAaG;AACI,KAAK,UAAU,UAAU,CAC9B,MAAiB,EACjB,IAAY,EACZ,IAAY,EACZ,OAAO,GAAG,0BAAkB;IAE5B,IAAI,CAAC;QACH,MAAM,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAA;IAClD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,sFAAsF;QACtF,yCAAyC;QACzC,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAA;QAC/B,MAAM,KAAK,CAAA;IACb,CAAC;AACH,CAAC"}
|
|
@@ -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,CAE1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usConnection 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":";AAAA;;GAEG;;;;AAyBH,sDAEC;AAsPD,oCAEC;AAjRD,6CAA0C;AAI1C,6DAA6E;AAE7E,kFAAkF;AACrE,QAAA,eAAe,GAAG,IAAI,CAAA;AAEnC,kFAAkF;AACrE,QAAA,mBAAmB,GAAG,KAAM,CAAA;AAOzC;;;;;GAKG;AACH,SAAgB,qBAAqB,CAAC,KAAc;IAClD,OAAO,KAAK,YAAY,KAAK,IAAI,KAAK,CAAC,IAAI,KAAK,qBAAqB,CAAA;AACvE,CAAC;AAED;;;;;;GAMG;AACH,SAAS,WAAW,CAAC,MAAiB,EAAE,QAAoB;IAC1D,MAAM,IAAI,GAAY,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAClD,IAAI,IAAI,YAAY,0BAAY;QAAE,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,CAAA;IACnE,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAA;AACxB,CAAC;AAED,8EAA8E;AAC9E,MAAM,UAAU,GAAG,MAAM,CAAC,GAAG,CAAC,uCAAuC,CAAC,CAAA;AAEtE;;;GAGG;AACH,SAAS,mBAAmB,CAC1B,MAA0B,EAC1B,KAAc,EACd,OAAO,GAAG,IAAI;IAEd,MAAM,MAAM,GAAG,IAAA,yCAAoB,EAAC,KAAK,CAAC,CAAA;IAC1C,0CAA0C;IAC1C,MAAM,OAAO,GAAG,OAAO;QACrB,CAAC,CAAC,cAAc,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,MAAM,GAAG,SAAS,MAAM,CAAC,OAAO,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MAAM,IAAI,EAAE,CAAA;QACvB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,iDAAiD;YACjD,MAAM,SAAS,GAAG,mBAAmB,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM,KAAK,SAAS,CAAC,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/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,eAAe,EAAE,KAAK,eAAe,EAAE,MAAM,cAAc,CAAA;AACpE,OAAO,EAAE,kBAAkB,EAAE,KAAK,SAAS,EAAE,MAAM,oBAAoB,CAAA;AACvE,OAAO,EAAE,kBAAkB,EAAE,KAAK,SAAS,EAAE,MAAM,oBAAoB,CAAA;AACvE,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAA;AACpE,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,cAAc,EAAE,KAAK,WAAW,EAAE,MAAM,YAAY,CAAA;AACrF,OAAO,EAAE,oBAAoB,EAAE,KAAK,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAC7E,OAAO,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAC1F,OAAO,EAAE,gBAAgB,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,eAAe,EAAE,KAAK,eAAe,EAAE,MAAM,cAAc,CAAA;AACpE,OAAO,EAAE,kBAAkB,EAAE,KAAK,SAAS,EAAE,MAAM,oBAAoB,CAAA;AACvE,OAAO,EAAE,kBAAkB,EAAE,KAAK,SAAS,EAAE,MAAM,oBAAoB,CAAA;AACvE,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAA;AACpE,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,cAAc,EAAE,KAAK,WAAW,EAAE,MAAM,YAAY,CAAA;AACrF,OAAO,EAAE,oBAAoB,EAAE,KAAK,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAC7E,OAAO,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAC1F,OAAO,EAAE,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.
|
|
@@ -148,7 +191,7 @@ class TransportManager {
|
|
|
148
191
|
*/
|
|
149
192
|
async createTCPClient(config) {
|
|
150
193
|
const client = new modbus_serial_1.default();
|
|
151
|
-
await
|
|
194
|
+
await (0, client_config_js_1.connectTCP)(client, config.host, config.port ?? 502, config.timeout);
|
|
152
195
|
client.setTimeout(config.timeout ?? client_config_js_1.DEFAULT_TIMEOUT_MS);
|
|
153
196
|
return client;
|
|
154
197
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manager.js","sourceRoot":"","sources":["../../../src/manager.ts"],"names":[],"mappings":";;;;AACA,6CAAmC;AACnC,0EAAqC;AAErC,
|
|
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,IAAI,GAAG,EAAoC,CAAA;IAED,CAAC;IAEtE;;;;;;;;OAQG;IACH,KAAK,CAAC,YAAY,CAAC,MAAuB;QACxC,MAAM,GAAG,GAAG,IAAI,CAAC,wBAAwB,CAAC,MAAM,CAAC,CAAA;QAEjD,sCAAsC;QACtC,IAAI,KAAK,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QACrC,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,IAAI,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;YACnC,IAAI,CAAC,OAAO,EAAE,CAAC;gBACb,OAAO,GAAG,IAAI,CAAC,cAAc,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAA;gBAClF,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;YAChC,CAAC;YACD,KAAK,GAAG,MAAM,OAAO,CAAA;QACvB,CAAC;QAED,sDAAsD;QACtD,OAAO,IAAI,mCAAc,CACvB,MAAM,CAAC,OAAO,EACd,KAAK,CAAC,UAAU,EACh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AC,CAAA;QAEzE,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,OAAO,IAAI,qCAAkB,CAAC,CAAA;QAEvD,OAAO,MAAM,CAAA;IACf,CAAC;IAED;;;;;OAKG;IACK,WAAW,CAAC,MAAuB;QACzC,OAAO,MAAM,IAAI,MAAM,IAAI,OAAO,MAAM,CAAC,IAAI,KAAK,QAAQ,IAAI,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,CAAA;IACnF,CAAC;CACF;AAhMD,4CAgMC"}
|
|
@@ -10,7 +10,7 @@ export interface TCPConfig {
|
|
|
10
10
|
port?: number | undefined;
|
|
11
11
|
/** Modbus slave ID (1-247) */
|
|
12
12
|
slaveId: number;
|
|
13
|
-
/** Response timeout in milliseconds (default: 1000) */
|
|
13
|
+
/** Response timeout in milliseconds, also used as the connect timeout (default: 1000) */
|
|
14
14
|
timeout?: number | undefined;
|
|
15
15
|
/** Maximum retry attempts (default: 3, use 1 to disable retries) */
|
|
16
16
|
maxRetries?: number | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tcp-transport.d.ts","sourceRoot":"","sources":["../../../src/tcp-transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAMxD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAE7C;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,wCAAwC;IACxC,IAAI,EAAE,MAAM,CAAA;IACZ,8BAA8B;IAC9B,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IACzB,8BAA8B;IAC9B,OAAO,EAAE,MAAM,CAAA;IACf,
|
|
1
|
+
{"version":3,"file":"tcp-transport.d.ts","sourceRoot":"","sources":["../../../src/tcp-transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAMxD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAE7C;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,wCAAwC;IACxC,IAAI,EAAE,MAAM,CAAA;IACZ,8BAA8B;IAC9B,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IACzB,8BAA8B;IAC9B,OAAO,EAAE,MAAM,CAAA;IACf,yFAAyF;IACzF,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC5B,oEAAoE;IACpE,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC/B,4DAA4D;IAC5D,MAAM,CAAC,EAAE,WAAW,GAAG,SAAS,CAAA;CACjC;AAED;;;;;;;;GAQG;AACH,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,CAc9E"}
|
|
@@ -20,7 +20,7 @@ async function createTCPTransport(config) {
|
|
|
20
20
|
const open = async () => {
|
|
21
21
|
const client = new modbus_serial_1.default();
|
|
22
22
|
// Connect to TCP server
|
|
23
|
-
await
|
|
23
|
+
await (0, client_config_js_1.connectTCP)(client, config.host, port, config.timeout);
|
|
24
24
|
(0, client_config_js_1.configureClient)(client, config.slaveId, config.timeout);
|
|
25
25
|
return client;
|
|
26
26
|
};
|