@enyo-energy/sunspec-sdk 0.0.97 → 0.0.99
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/dist/cjs/connection-retry-manager.cjs +25 -1
- package/dist/cjs/connection-retry-manager.d.cts +6 -0
- package/dist/cjs/sunspec-devices.cjs +194 -41
- package/dist/cjs/sunspec-devices.d.cts +72 -5
- package/dist/cjs/sunspec-interfaces.cjs +31 -2
- package/dist/cjs/sunspec-interfaces.d.cts +140 -0
- package/dist/cjs/sunspec-modbus-client.cjs +145 -20
- package/dist/cjs/sunspec-modbus-client.d.cts +54 -6
- package/dist/cjs/version.cjs +1 -1
- package/dist/cjs/version.d.cts +1 -1
- package/dist/connection-retry-manager.d.ts +6 -0
- package/dist/connection-retry-manager.js +25 -1
- package/dist/sunspec-devices.d.ts +72 -5
- package/dist/sunspec-devices.js +195 -42
- package/dist/sunspec-interfaces.d.ts +140 -0
- package/dist/sunspec-interfaces.js +30 -1
- package/dist/sunspec-modbus-client.d.ts +54 -6
- package/dist/sunspec-modbus-client.js +144 -20
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -16,19 +16,29 @@
|
|
|
16
16
|
* - pad: 0x8000 (always returns this value)
|
|
17
17
|
* - string: all registers 0x0000 (NULL)
|
|
18
18
|
*/
|
|
19
|
-
import { type SunspecInverterControls, type SunspecInverterData, type SunspecInverterSettings, type SunspecMeterData, type SunspecModel, type SunspecMPPTData, type SunspecBatteryData, type SunspecBatteryBaseData, type SunspecBatteryControls, SunspecStorageMode, type IModbusPacingConfig } from "./sunspec-interfaces.js";
|
|
19
|
+
import { type SunspecInverterControls, type SunspecInverterData, type SunspecInverterSettings, type SunspecMeterData, type SunspecModel, type SunspecMPPTData, type SunspecBatteryData, type SunspecBatteryBaseData, type SunspecBatteryControls, SunspecStorageMode, type IModbusPacingConfig, type IModbusResilienceConfig } from "./sunspec-interfaces.js";
|
|
20
20
|
import { EnergyAppModbusDataType, IConnectionHealth } from "@enyo-energy/energy-app-sdk/dist/implementations/modbus/interfaces.js";
|
|
21
21
|
import { EnergyAppModbusInstance } from "@enyo-energy/energy-app-sdk/dist/packages/energy-app-modbus.js";
|
|
22
22
|
import { EnergyApp } from "@enyo-energy/energy-app-sdk";
|
|
23
|
+
/**
|
|
24
|
+
* Thrown when a single Modbus transaction (read/write/connect/disconnect) exceeds its configured
|
|
25
|
+
* time budget. Surfaced instead of letting a half-open socket leave the operation pending forever.
|
|
26
|
+
*/
|
|
27
|
+
export declare class ModbusOperationTimeoutError extends Error {
|
|
28
|
+
readonly operation: string;
|
|
29
|
+
readonly unitId: number;
|
|
30
|
+
readonly timeoutMs: number;
|
|
31
|
+
constructor(operation: string, unitId: number, timeoutMs: number);
|
|
32
|
+
}
|
|
23
33
|
/**
|
|
24
34
|
* Get (or create) the singleton SunspecModbusClient for this network device.
|
|
25
35
|
* Increments the refcount; pair with `releaseSunspecClient` on teardown.
|
|
26
36
|
*
|
|
27
|
-
* NOTE: `pacing`
|
|
28
|
-
* Because the client is cached and refcounted, a later caller passing
|
|
37
|
+
* NOTE: `pacing` and `resilience` are only applied when the client is FIRST created for a given
|
|
38
|
+
* host:port. Because the client is cached and refcounted, a later caller passing different config
|
|
29
39
|
* receives the already-created instance and its config is ignored.
|
|
30
40
|
*/
|
|
31
|
-
export declare function getOrCreateSunspecClient(energyApp: EnergyApp, host: string, port?: number, pacing?: IModbusPacingConfig): SunspecModbusClient;
|
|
41
|
+
export declare function getOrCreateSunspecClient(energyApp: EnergyApp, host: string, port?: number, pacing?: IModbusPacingConfig, resilience?: IModbusResilienceConfig): SunspecModbusClient;
|
|
32
42
|
/**
|
|
33
43
|
* Release a refcount on the network-device client. When the last consumer releases,
|
|
34
44
|
* the client is fully disconnected (all units closed) and removed from the registry.
|
|
@@ -53,7 +63,10 @@ export declare class SunspecModbusClient {
|
|
|
53
63
|
private closeCount;
|
|
54
64
|
private operationChain;
|
|
55
65
|
private txChains;
|
|
56
|
-
|
|
66
|
+
private resilience;
|
|
67
|
+
private poisonedUnits;
|
|
68
|
+
private lastOkAtByUnit;
|
|
69
|
+
constructor(energyApp: EnergyApp, pacing?: IModbusPacingConfig, resilience?: IModbusResilienceConfig);
|
|
57
70
|
/**
|
|
58
71
|
* Connect to a Modbus device unit. Multiple unit IDs on the same network device share this
|
|
59
72
|
* client; calling connect() with different unit IDs adds each as a separately-managed unit.
|
|
@@ -117,6 +130,12 @@ export declare class SunspecModbusClient {
|
|
|
117
130
|
* connection lock.
|
|
118
131
|
*/
|
|
119
132
|
private _closeUnit;
|
|
133
|
+
/**
|
|
134
|
+
* Close an instance under a hard time budget. A half-open socket can hang even on disconnect();
|
|
135
|
+
* left unbounded that would wedge the connection lock and stall every later connect/disconnect.
|
|
136
|
+
* Swallows all errors (including the timeout) — a close that can't complete is abandoned.
|
|
137
|
+
*/
|
|
138
|
+
private disconnectInstanceBounded;
|
|
120
139
|
/**
|
|
121
140
|
* Run `fn` with exclusive access to connection-state transitions. Subsequent calls queue
|
|
122
141
|
* behind any in-flight one. A rejected `fn` does not poison the chain for later callers.
|
|
@@ -133,6 +152,32 @@ export declare class SunspecModbusClient {
|
|
|
133
152
|
* unit — that re-enters the unit's chain and deadlocks.
|
|
134
153
|
*/
|
|
135
154
|
private runExclusive;
|
|
155
|
+
/**
|
|
156
|
+
* Wrap one serialized transaction with the per-operation timeout and half-open bookkeeping.
|
|
157
|
+
* On success, stamp the unit's freshness clock. On timeout, poison the unit so no further
|
|
158
|
+
* transaction reuses the hung socket before the reconnect path recycles it. A unit already
|
|
159
|
+
* poisoned refuses I/O outright — the abandoned in-flight response could otherwise land later
|
|
160
|
+
* and desync the next transaction's framing.
|
|
161
|
+
*/
|
|
162
|
+
private guardTransaction;
|
|
163
|
+
/**
|
|
164
|
+
* Race `fn` against a timeout. If the timeout wins, reject with a {@link ModbusOperationTimeoutError};
|
|
165
|
+
* the abandoned `fn` promise keeps running (JS can't cancel it) but is no longer awaited. The
|
|
166
|
+
* timer is always cleared so a fast `fn` doesn't keep the event loop alive.
|
|
167
|
+
*/
|
|
168
|
+
private withTimeout;
|
|
169
|
+
/**
|
|
170
|
+
* Mark a unit's socket as poisoned after a timeout and reflect the hang in the shared health
|
|
171
|
+
* gate immediately, so isHealthy() flips false this cycle instead of after the coarse
|
|
172
|
+
* 5-failure / 5-minute-staleness thresholds. Idempotent; only logs on the first poison.
|
|
173
|
+
*/
|
|
174
|
+
private poisonUnit;
|
|
175
|
+
/**
|
|
176
|
+
* A unit's socket is stale when the transport still reports it connected but no successful
|
|
177
|
+
* transaction has completed within staleSocketMs — the signature of a half-open link where
|
|
178
|
+
* the peer silently stopped answering. Units with no successful op yet are never stale.
|
|
179
|
+
*/
|
|
180
|
+
private isStale;
|
|
136
181
|
private recordOpen;
|
|
137
182
|
private recordClose;
|
|
138
183
|
/**
|
|
@@ -444,7 +489,10 @@ export declare class SunspecModbusClient {
|
|
|
444
489
|
*/
|
|
445
490
|
isConnected(unitId: number): boolean;
|
|
446
491
|
/**
|
|
447
|
-
* Check if a specific unit's connection is healthy
|
|
492
|
+
* Check if a specific unit's connection is healthy. Beyond the shared health counter, a unit is
|
|
493
|
+
* unhealthy if its socket has been poisoned by a timed-out transaction or has gone stale
|
|
494
|
+
* (half-open) — both flip false immediately so the device's readData()/tryReconnect() path
|
|
495
|
+
* recycles the socket rather than trusting a hung link.
|
|
448
496
|
*/
|
|
449
497
|
isHealthy(unitId: number): boolean;
|
|
450
498
|
/**
|
|
@@ -16,10 +16,26 @@
|
|
|
16
16
|
* - pad: 0x8000 (always returns this value)
|
|
17
17
|
* - string: all registers 0x0000 (NULL)
|
|
18
18
|
*/
|
|
19
|
-
import { SunspecModelId, SunspecBatteryChargeState, SunspecStorageControlMode, SunspecChargeSource, SunspecEnableControl, SunspecStorageMode, SunspecBatteryType, SunspecBatteryBankState, } from "./sunspec-interfaces.js";
|
|
19
|
+
import { SunspecModelId, SunspecBatteryChargeState, SunspecStorageControlMode, SunspecChargeSource, SunspecEnableControl, SunspecStorageMode, SunspecBatteryType, SunspecBatteryBankState, DEFAULT_MODBUS_RESILIENCE_CONFIG, } from "./sunspec-interfaces.js";
|
|
20
20
|
import { EnergyAppModbusConnectionHealth } from "@enyo-energy/energy-app-sdk/dist/implementations/modbus/EnergyAppModbusConnectionHealth.js";
|
|
21
21
|
import { EnergyAppModbusFaultTolerantReader } from "@enyo-energy/energy-app-sdk/dist/implementations/modbus/EnergyAppModbusFaultTolerantReader.js";
|
|
22
22
|
import { EnergyAppModbusDataTypeConverter } from "@enyo-energy/energy-app-sdk/dist/implementations/modbus/EnergyAppModbusDataTypeConverter.js";
|
|
23
|
+
/**
|
|
24
|
+
* Thrown when a single Modbus transaction (read/write/connect/disconnect) exceeds its configured
|
|
25
|
+
* time budget. Surfaced instead of letting a half-open socket leave the operation pending forever.
|
|
26
|
+
*/
|
|
27
|
+
export class ModbusOperationTimeoutError extends Error {
|
|
28
|
+
operation;
|
|
29
|
+
unitId;
|
|
30
|
+
timeoutMs;
|
|
31
|
+
constructor(operation, unitId, timeoutMs) {
|
|
32
|
+
super(`Modbus ${operation} on unit ${unitId} timed out after ${timeoutMs}ms`);
|
|
33
|
+
this.operation = operation;
|
|
34
|
+
this.unitId = unitId;
|
|
35
|
+
this.timeoutMs = timeoutMs;
|
|
36
|
+
this.name = 'ModbusOperationTimeoutError';
|
|
37
|
+
}
|
|
38
|
+
}
|
|
23
39
|
/**
|
|
24
40
|
* Module-level registry: one SunspecModbusClient per network device (host:port).
|
|
25
41
|
* Refcounted so the underlying connection only tears down when the last consumer releases.
|
|
@@ -30,15 +46,15 @@ const registryKey = (host, port) => `${host}:${port}`;
|
|
|
30
46
|
* Get (or create) the singleton SunspecModbusClient for this network device.
|
|
31
47
|
* Increments the refcount; pair with `releaseSunspecClient` on teardown.
|
|
32
48
|
*
|
|
33
|
-
* NOTE: `pacing`
|
|
34
|
-
* Because the client is cached and refcounted, a later caller passing
|
|
49
|
+
* NOTE: `pacing` and `resilience` are only applied when the client is FIRST created for a given
|
|
50
|
+
* host:port. Because the client is cached and refcounted, a later caller passing different config
|
|
35
51
|
* receives the already-created instance and its config is ignored.
|
|
36
52
|
*/
|
|
37
|
-
export function getOrCreateSunspecClient(energyApp, host, port = 502, pacing) {
|
|
53
|
+
export function getOrCreateSunspecClient(energyApp, host, port = 502, pacing, resilience) {
|
|
38
54
|
const key = registryKey(host, port);
|
|
39
55
|
let entry = CLIENT_REGISTRY.get(key);
|
|
40
56
|
if (!entry) {
|
|
41
|
-
entry = { client: new SunspecModbusClient(energyApp, pacing), refcount: 0 };
|
|
57
|
+
entry = { client: new SunspecModbusClient(energyApp, pacing, resilience), refcount: 0 };
|
|
42
58
|
CLIENT_REGISTRY.set(key, entry);
|
|
43
59
|
}
|
|
44
60
|
entry.refcount++;
|
|
@@ -111,15 +127,30 @@ export class SunspecModbusClient {
|
|
|
111
127
|
// operationChain (the connection lock) to avoid re-entrancy: a transaction never waits
|
|
112
128
|
// on a connect/disconnect, and vice versa.
|
|
113
129
|
txChains = new Map();
|
|
130
|
+
// Effective resilience config (per-op timeouts + staleness window), merged with defaults.
|
|
131
|
+
resilience;
|
|
132
|
+
// Units whose last transaction timed out. A timed-out Promise.race abandons the in-flight
|
|
133
|
+
// request but cannot cancel it — the response may still land later and desync the next
|
|
134
|
+
// transaction's framing. So a poisoned unit refuses further I/O until reconnectUnit() tears
|
|
135
|
+
// the socket down and reopens it (which clears the flag).
|
|
136
|
+
poisonedUnits = new Set();
|
|
137
|
+
// Wall-clock of each unit's last SUCCESSFUL transaction. Drives half-open detection: a socket
|
|
138
|
+
// the transport still reports connected but that has gone quiet past staleSocketMs is presumed
|
|
139
|
+
// dead. Absent entry = no successful op yet (never treated as stale — give it a chance).
|
|
140
|
+
lastOkAtByUnit = new Map();
|
|
114
141
|
constructor(energyApp,
|
|
115
142
|
// Opt-in pacing (default off). Passed straight through to `useModbus().connect()`, which
|
|
116
143
|
// serializes frames (`noParallelRequests`) and spaces them (`waitBetweenMessagesMs`) on the
|
|
117
144
|
// connection. An empty object leaves both undefined so the SDK applies its own defaults.
|
|
118
|
-
pacing = {}
|
|
145
|
+
pacing = {},
|
|
146
|
+
// Per-operation timeouts + half-open detection. Merged with DEFAULT_MODBUS_RESILIENCE_CONFIG
|
|
147
|
+
// so omitted fields keep their defaults.
|
|
148
|
+
resilience = {}) {
|
|
119
149
|
this.energyApp = energyApp;
|
|
120
150
|
this.pacing = pacing;
|
|
121
151
|
this.connectionHealth = new EnergyAppModbusConnectionHealth();
|
|
122
152
|
this.modbusDataTypeConverter = new EnergyAppModbusDataTypeConverter();
|
|
153
|
+
this.resilience = { ...DEFAULT_MODBUS_RESILIENCE_CONFIG, ...resilience };
|
|
123
154
|
}
|
|
124
155
|
/**
|
|
125
156
|
* Connect to a Modbus device unit. Multiple unit IDs on the same network device share this
|
|
@@ -314,11 +345,14 @@ export class SunspecModbusClient {
|
|
|
314
345
|
async _openUnit(host, port, unitId) {
|
|
315
346
|
let candidate = null;
|
|
316
347
|
try {
|
|
317
|
-
|
|
348
|
+
// Enforce the connect budget ourselves in addition to forwarding it as the transport's
|
|
349
|
+
// `timeout`: the transport applies it to the TCP handshake, but a sleeping/half-open peer
|
|
350
|
+
// can still leave the returned promise pending — bound it independently.
|
|
351
|
+
candidate = await this.withTimeout('connect', unitId, this.resilience.connectTimeoutMs, () => this.energyApp.useModbus().connect({
|
|
318
352
|
host,
|
|
319
353
|
port,
|
|
320
354
|
unitId,
|
|
321
|
-
timeout:
|
|
355
|
+
timeout: this.resilience.connectTimeoutMs,
|
|
322
356
|
useTls: this.connectionParams?.useTls,
|
|
323
357
|
cert: this.connectionParams?.cert,
|
|
324
358
|
key: this.connectionParams?.key,
|
|
@@ -326,22 +360,23 @@ export class SunspecModbusClient {
|
|
|
326
360
|
rejectUnauthorized: this.connectionParams?.rejectUnauthorized,
|
|
327
361
|
noParallelRequests: this.pacing.noParallelRequests,
|
|
328
362
|
waitBetweenMessagesMs: this.pacing.waitBetweenMessagesMs
|
|
329
|
-
});
|
|
363
|
+
}));
|
|
330
364
|
if (!candidate) {
|
|
331
365
|
throw new Error(`useModbus().connect returned null for ${host}:${port} unit ${unitId}`);
|
|
332
366
|
}
|
|
333
367
|
const reader = new EnergyAppModbusFaultTolerantReader(candidate, this.connectionHealth);
|
|
334
368
|
this.modbusInstances.set(unitId, candidate);
|
|
335
369
|
this.faultTolerantReaders.set(unitId, reader);
|
|
370
|
+
// Fresh socket: clear any prior poison and start the freshness clock so a just-opened
|
|
371
|
+
// unit isn't immediately judged stale.
|
|
372
|
+
this.poisonedUnits.delete(unitId);
|
|
373
|
+
this.lastOkAtByUnit.set(unitId, Date.now());
|
|
336
374
|
this.connectionHealth.recordSuccess();
|
|
337
375
|
this.recordOpen();
|
|
338
376
|
}
|
|
339
377
|
catch (err) {
|
|
340
378
|
if (candidate) {
|
|
341
|
-
|
|
342
|
-
await candidate.disconnect();
|
|
343
|
-
}
|
|
344
|
-
catch { /* ignore */ }
|
|
379
|
+
await this.disconnectInstanceBounded(candidate, unitId);
|
|
345
380
|
}
|
|
346
381
|
this.modbusInstances.delete(unitId);
|
|
347
382
|
this.faultTolerantReaders.delete(unitId);
|
|
@@ -359,15 +394,31 @@ export class SunspecModbusClient {
|
|
|
359
394
|
this.faultTolerantReaders.delete(unitId);
|
|
360
395
|
this.discoveredModelsByUnit.delete(unitId);
|
|
361
396
|
this.discoveredModelListByUnit.delete(unitId);
|
|
397
|
+
// Drop per-unit resilience state so a later reopen starts clean.
|
|
398
|
+
this.poisonedUnits.delete(unitId);
|
|
399
|
+
this.lastOkAtByUnit.delete(unitId);
|
|
362
400
|
if (instance) {
|
|
363
|
-
|
|
364
|
-
await instance.disconnect();
|
|
365
|
-
}
|
|
366
|
-
catch { /* ignore */ }
|
|
401
|
+
await this.disconnectInstanceBounded(instance, unitId);
|
|
367
402
|
}
|
|
368
403
|
if (wasOpen)
|
|
369
404
|
this.recordClose();
|
|
370
405
|
}
|
|
406
|
+
/**
|
|
407
|
+
* Close an instance under a hard time budget. A half-open socket can hang even on disconnect();
|
|
408
|
+
* left unbounded that would wedge the connection lock and stall every later connect/disconnect.
|
|
409
|
+
* Swallows all errors (including the timeout) — a close that can't complete is abandoned.
|
|
410
|
+
*/
|
|
411
|
+
async disconnectInstanceBounded(instance, unitId) {
|
|
412
|
+
try {
|
|
413
|
+
await this.withTimeout('disconnect', unitId, this.resilience.disconnectTimeoutMs, () => instance.disconnect());
|
|
414
|
+
}
|
|
415
|
+
catch (err) {
|
|
416
|
+
if (err instanceof ModbusOperationTimeoutError) {
|
|
417
|
+
console.warn(`SunspecModbusClient: disconnect of unit ${unitId} timed out after ${this.resilience.disconnectTimeoutMs}ms — abandoning socket`);
|
|
418
|
+
}
|
|
419
|
+
/* ignore: best-effort close */
|
|
420
|
+
}
|
|
421
|
+
}
|
|
371
422
|
/**
|
|
372
423
|
* Run `fn` with exclusive access to connection-state transitions. Subsequent calls queue
|
|
373
424
|
* behind any in-flight one. A rejected `fn` does not poison the chain for later callers.
|
|
@@ -389,10 +440,74 @@ export class SunspecModbusClient {
|
|
|
389
440
|
*/
|
|
390
441
|
runExclusive(unitId, fn) {
|
|
391
442
|
const prev = this.txChains.get(unitId) ?? Promise.resolve();
|
|
392
|
-
const
|
|
443
|
+
const guarded = () => this.guardTransaction(unitId, fn);
|
|
444
|
+
const run = prev.then(guarded, guarded);
|
|
393
445
|
this.txChains.set(unitId, run.catch(() => undefined));
|
|
394
446
|
return run;
|
|
395
447
|
}
|
|
448
|
+
/**
|
|
449
|
+
* Wrap one serialized transaction with the per-operation timeout and half-open bookkeeping.
|
|
450
|
+
* On success, stamp the unit's freshness clock. On timeout, poison the unit so no further
|
|
451
|
+
* transaction reuses the hung socket before the reconnect path recycles it. A unit already
|
|
452
|
+
* poisoned refuses I/O outright — the abandoned in-flight response could otherwise land later
|
|
453
|
+
* and desync the next transaction's framing.
|
|
454
|
+
*/
|
|
455
|
+
async guardTransaction(unitId, fn) {
|
|
456
|
+
if (this.poisonedUnits.has(unitId)) {
|
|
457
|
+
throw new Error(`Modbus unit ${unitId} socket is poisoned (awaiting reconnect) — transaction refused`);
|
|
458
|
+
}
|
|
459
|
+
try {
|
|
460
|
+
const result = await this.withTimeout('transaction', unitId, this.resilience.readTimeoutMs, fn);
|
|
461
|
+
this.lastOkAtByUnit.set(unitId, Date.now());
|
|
462
|
+
return result;
|
|
463
|
+
}
|
|
464
|
+
catch (error) {
|
|
465
|
+
if (error instanceof ModbusOperationTimeoutError) {
|
|
466
|
+
this.poisonUnit(unitId, error);
|
|
467
|
+
}
|
|
468
|
+
throw error;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* Race `fn` against a timeout. If the timeout wins, reject with a {@link ModbusOperationTimeoutError};
|
|
473
|
+
* the abandoned `fn` promise keeps running (JS can't cancel it) but is no longer awaited. The
|
|
474
|
+
* timer is always cleared so a fast `fn` doesn't keep the event loop alive.
|
|
475
|
+
*/
|
|
476
|
+
withTimeout(operation, unitId, timeoutMs, fn) {
|
|
477
|
+
let timer;
|
|
478
|
+
const timeout = new Promise((_, reject) => {
|
|
479
|
+
timer = setTimeout(() => reject(new ModbusOperationTimeoutError(operation, unitId, timeoutMs)), timeoutMs);
|
|
480
|
+
});
|
|
481
|
+
return Promise.race([fn(), timeout]).finally(() => {
|
|
482
|
+
if (timer !== undefined)
|
|
483
|
+
clearTimeout(timer);
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Mark a unit's socket as poisoned after a timeout and reflect the hang in the shared health
|
|
488
|
+
* gate immediately, so isHealthy() flips false this cycle instead of after the coarse
|
|
489
|
+
* 5-failure / 5-minute-staleness thresholds. Idempotent; only logs on the first poison.
|
|
490
|
+
*/
|
|
491
|
+
poisonUnit(unitId, error) {
|
|
492
|
+
if (!this.poisonedUnits.has(unitId)) {
|
|
493
|
+
console.warn(`SunspecModbusClient: ${error.message} — poisoning unit ${unitId} socket; reconnect path will recycle it`);
|
|
494
|
+
}
|
|
495
|
+
this.poisonedUnits.add(unitId);
|
|
496
|
+
this.connectionHealth.recordFailure(error);
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* A unit's socket is stale when the transport still reports it connected but no successful
|
|
500
|
+
* transaction has completed within staleSocketMs — the signature of a half-open link where
|
|
501
|
+
* the peer silently stopped answering. Units with no successful op yet are never stale.
|
|
502
|
+
*/
|
|
503
|
+
isStale(unitId) {
|
|
504
|
+
if (this.resilience.staleSocketMs <= 0)
|
|
505
|
+
return false;
|
|
506
|
+
const last = this.lastOkAtByUnit.get(unitId);
|
|
507
|
+
if (last === undefined)
|
|
508
|
+
return false;
|
|
509
|
+
return Date.now() - last > this.resilience.staleSocketMs;
|
|
510
|
+
}
|
|
396
511
|
recordOpen() {
|
|
397
512
|
this.openCount++;
|
|
398
513
|
const expected = this.modbusInstances.size;
|
|
@@ -2230,10 +2345,19 @@ export class SunspecModbusClient {
|
|
|
2230
2345
|
return this.modbusInstances.has(unitId);
|
|
2231
2346
|
}
|
|
2232
2347
|
/**
|
|
2233
|
-
* Check if a specific unit's connection is healthy
|
|
2348
|
+
* Check if a specific unit's connection is healthy. Beyond the shared health counter, a unit is
|
|
2349
|
+
* unhealthy if its socket has been poisoned by a timed-out transaction or has gone stale
|
|
2350
|
+
* (half-open) — both flip false immediately so the device's readData()/tryReconnect() path
|
|
2351
|
+
* recycles the socket rather than trusting a hung link.
|
|
2234
2352
|
*/
|
|
2235
2353
|
isHealthy(unitId) {
|
|
2236
|
-
|
|
2354
|
+
if (!this.modbusInstances.has(unitId))
|
|
2355
|
+
return false;
|
|
2356
|
+
if (this.poisonedUnits.has(unitId))
|
|
2357
|
+
return false;
|
|
2358
|
+
if (this.isStale(unitId))
|
|
2359
|
+
return false;
|
|
2360
|
+
return this.connectionHealth.isHealthy();
|
|
2237
2361
|
}
|
|
2238
2362
|
/**
|
|
2239
2363
|
* Get connection health details (shared across all units on this network device)
|
package/dist/version.d.ts
CHANGED
package/dist/version.js
CHANGED