@enyo-energy/sunspec-sdk 0.0.102 → 0.0.103
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/sunspec-devices.cjs +46 -3
- package/dist/cjs/sunspec-devices.d.cts +27 -2
- package/dist/cjs/sunspec-modbus-client.cjs +45 -5
- package/dist/cjs/sunspec-modbus-client.d.cts +17 -0
- package/dist/cjs/version.cjs +1 -1
- package/dist/cjs/version.d.cts +1 -1
- package/dist/sunspec-devices.d.ts +27 -2
- package/dist/sunspec-devices.js +46 -3
- package/dist/sunspec-modbus-client.d.ts +17 -0
- package/dist/sunspec-modbus-client.js +45 -5
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +3 -3
|
@@ -117,6 +117,19 @@ class BaseSunspecDevice {
|
|
|
117
117
|
* would otherwise stay Offline forever despite reading data fine.
|
|
118
118
|
*/
|
|
119
119
|
appliedOffline = false;
|
|
120
|
+
/**
|
|
121
|
+
* True once {@link reassertConnectedIfRecovered} has confirmed (via the
|
|
122
|
+
* appliance store) that this device is Connected, so it can stop probing
|
|
123
|
+
* for an *externally* applied Offline state. Distinct from
|
|
124
|
+
* {@link appliedOffline}, which only tracks Offline states this SDK object
|
|
125
|
+
* applied itself. An appliance can be marked Offline by a path outside the
|
|
126
|
+
* SDK object — e.g. the energy-app registering a boot-time offline
|
|
127
|
+
* placeholder before this device came up on a shared socket — which never
|
|
128
|
+
* sets appliedOffline; without reconciling against actual state, such a
|
|
129
|
+
* device stays Offline forever despite reading data fine. Reset in
|
|
130
|
+
* {@link markOffline} so a later Offline episode is re-checked.
|
|
131
|
+
*/
|
|
132
|
+
reconciledExternalOffline = false;
|
|
120
133
|
/**
|
|
121
134
|
* Prefix used when persisting calibration snapshots via the library's
|
|
122
135
|
* {@link SnapshotService}. Kept identical to the key the SDK used before
|
|
@@ -337,6 +350,7 @@ class BaseSunspecDevice {
|
|
|
337
350
|
try {
|
|
338
351
|
await this.applianceManager.updateApplianceState(this.applianceId, enyo_appliance_js_1.EnyoApplianceConnectionType.Connector, enyo_appliance_js_1.EnyoApplianceStateEnum.Offline);
|
|
339
352
|
this.appliedOffline = true;
|
|
353
|
+
this.reconciledExternalOffline = false;
|
|
340
354
|
}
|
|
341
355
|
catch (error) {
|
|
342
356
|
console.error(`${this.constructor.name} ${this.applianceId}: Failed to mark appliance offline: ${error}`);
|
|
@@ -352,17 +366,45 @@ class BaseSunspecDevice {
|
|
|
352
366
|
* readData() and skip tryReconnect() entirely, so without this they would
|
|
353
367
|
* stay Offline forever despite reading data fine.
|
|
354
368
|
*
|
|
369
|
+
* Heals two ways an appliance can be stuck Offline while reads succeed:
|
|
370
|
+
* 1. This SDK object marked itself offline ({@link appliedOffline}) and a
|
|
371
|
+
* sibling won the reconnect race — the original shared-socket case.
|
|
372
|
+
* 2. The appliance was marked Offline by a path *outside* this object —
|
|
373
|
+
* e.g. the energy-app registering a boot-time offline placeholder
|
|
374
|
+
* before this device came up on the shared socket — which never sets
|
|
375
|
+
* appliedOffline. We reconcile against the actual stored state so the
|
|
376
|
+
* invariant "healthy reads ⇒ Connected" holds regardless of who
|
|
377
|
+
* applied the Offline.
|
|
378
|
+
*
|
|
355
379
|
* Call this on the success path of readData() (after the
|
|
356
|
-
* markOfflineIfUnhealthy() guard has confirmed the unit is healthy).
|
|
357
|
-
* a
|
|
380
|
+
* markOfflineIfUnhealthy() guard has confirmed the unit is healthy). Cheap:
|
|
381
|
+
* a no-op via the appliedOffline fast path, and the external-state lookup
|
|
382
|
+
* runs at most once per Offline episode (latched by
|
|
383
|
+
* {@link reconciledExternalOffline} once Connected is confirmed).
|
|
358
384
|
*/
|
|
359
385
|
async reassertConnectedIfRecovered() {
|
|
360
|
-
if (!this.
|
|
386
|
+
if (!this.applianceId) {
|
|
361
387
|
return;
|
|
362
388
|
}
|
|
363
389
|
if (!this.sunspecClient.isHealthy(this.unitId)) {
|
|
364
390
|
return;
|
|
365
391
|
}
|
|
392
|
+
let shouldReassert = this.appliedOffline;
|
|
393
|
+
// Not offline by our own hand: it may still be Offline from an
|
|
394
|
+
// externally applied state. Probe the store once until we confirm
|
|
395
|
+
// Connected, then latch so the steady state adds no extra lookups.
|
|
396
|
+
if (!shouldReassert && !this.reconciledExternalOffline) {
|
|
397
|
+
const appliance = await this.applianceManager.findApplianceById(this.applianceId);
|
|
398
|
+
if (appliance?.metadata?.state === enyo_appliance_js_1.EnyoApplianceStateEnum.Offline) {
|
|
399
|
+
shouldReassert = true;
|
|
400
|
+
}
|
|
401
|
+
else {
|
|
402
|
+
this.reconciledExternalOffline = true;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
if (!shouldReassert) {
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
366
408
|
console.log(`${this.constructor.name} ${this.applianceId}: connection recovered via shared socket — re-asserting Connected`);
|
|
367
409
|
try {
|
|
368
410
|
await this.applianceManager.updateApplianceState(this.applianceId, enyo_appliance_js_1.EnyoApplianceConnectionType.Connector, enyo_appliance_js_1.EnyoApplianceStateEnum.Connected);
|
|
@@ -372,6 +414,7 @@ class BaseSunspecDevice {
|
|
|
372
414
|
return;
|
|
373
415
|
}
|
|
374
416
|
this.appliedOffline = false;
|
|
417
|
+
this.reconciledExternalOffline = true;
|
|
375
418
|
this.retryManager.reset();
|
|
376
419
|
this.consecutiveReconnectFailures = 0;
|
|
377
420
|
await this.onConnectionRestored();
|
|
@@ -37,6 +37,19 @@ export declare abstract class BaseSunspecDevice {
|
|
|
37
37
|
* would otherwise stay Offline forever despite reading data fine.
|
|
38
38
|
*/
|
|
39
39
|
private appliedOffline;
|
|
40
|
+
/**
|
|
41
|
+
* True once {@link reassertConnectedIfRecovered} has confirmed (via the
|
|
42
|
+
* appliance store) that this device is Connected, so it can stop probing
|
|
43
|
+
* for an *externally* applied Offline state. Distinct from
|
|
44
|
+
* {@link appliedOffline}, which only tracks Offline states this SDK object
|
|
45
|
+
* applied itself. An appliance can be marked Offline by a path outside the
|
|
46
|
+
* SDK object — e.g. the energy-app registering a boot-time offline
|
|
47
|
+
* placeholder before this device came up on a shared socket — which never
|
|
48
|
+
* sets appliedOffline; without reconciling against actual state, such a
|
|
49
|
+
* device stays Offline forever despite reading data fine. Reset in
|
|
50
|
+
* {@link markOffline} so a later Offline episode is re-checked.
|
|
51
|
+
*/
|
|
52
|
+
private reconciledExternalOffline;
|
|
40
53
|
/**
|
|
41
54
|
* Prefix used when persisting calibration snapshots via the library's
|
|
42
55
|
* {@link SnapshotService}. Kept identical to the key the SDK used before
|
|
@@ -152,9 +165,21 @@ export declare abstract class BaseSunspecDevice {
|
|
|
152
165
|
* readData() and skip tryReconnect() entirely, so without this they would
|
|
153
166
|
* stay Offline forever despite reading data fine.
|
|
154
167
|
*
|
|
168
|
+
* Heals two ways an appliance can be stuck Offline while reads succeed:
|
|
169
|
+
* 1. This SDK object marked itself offline ({@link appliedOffline}) and a
|
|
170
|
+
* sibling won the reconnect race — the original shared-socket case.
|
|
171
|
+
* 2. The appliance was marked Offline by a path *outside* this object —
|
|
172
|
+
* e.g. the energy-app registering a boot-time offline placeholder
|
|
173
|
+
* before this device came up on the shared socket — which never sets
|
|
174
|
+
* appliedOffline. We reconcile against the actual stored state so the
|
|
175
|
+
* invariant "healthy reads ⇒ Connected" holds regardless of who
|
|
176
|
+
* applied the Offline.
|
|
177
|
+
*
|
|
155
178
|
* Call this on the success path of readData() (after the
|
|
156
|
-
* markOfflineIfUnhealthy() guard has confirmed the unit is healthy).
|
|
157
|
-
* a
|
|
179
|
+
* markOfflineIfUnhealthy() guard has confirmed the unit is healthy). Cheap:
|
|
180
|
+
* a no-op via the appliedOffline fast path, and the external-state lookup
|
|
181
|
+
* runs at most once per Offline episode (latched by
|
|
182
|
+
* {@link reconciledExternalOffline} once Connected is confirmed).
|
|
158
183
|
*/
|
|
159
184
|
protected reassertConnectedIfRecovered(): Promise<void>;
|
|
160
185
|
protected sendCommandAcknowledge(messageId: string, acknowledgeMessage: EnyoDataBusMessageEnum | string, answer: EnyoCommandAcknowledgeAnswerEnum, rejectionReason?: string): void;
|
|
@@ -231,6 +231,10 @@ class SunspecModbusClient {
|
|
|
231
231
|
await this._openUnit(host, port, unitId, {
|
|
232
232
|
connectRetries: 0,
|
|
233
233
|
connectTimeoutMs: opts.timeoutMs ?? this.resilience.connectTimeoutMs,
|
|
234
|
+
// Fast-fail scan: a mostly-dead RS485 range must cost ~one timeout per unit, so do
|
|
235
|
+
// NOT let the transport also walk a fallback host (which would double the per-unit
|
|
236
|
+
// cost). Warm connects/reconnects still get the in-band fallback.
|
|
237
|
+
useFallbackHosts: false,
|
|
234
238
|
});
|
|
235
239
|
console.log(`Probed Sunspec unit ${unitId} at ${host}:${port} — open (opens=${this.openCount}, closes=${this.closeCount}, openUnits=${this.modbusInstances.size})`);
|
|
236
240
|
});
|
|
@@ -402,6 +406,25 @@ class SunspecModbusClient {
|
|
|
402
406
|
return false;
|
|
403
407
|
}
|
|
404
408
|
}
|
|
409
|
+
/**
|
|
410
|
+
* Build the ordered host list handed to the transport's `host` option for a single connect.
|
|
411
|
+
* The attempt's primary target leads, followed by every OTHER known host (the other of
|
|
412
|
+
* primaryHost/secondaryHost) as an in-band fallback the transport walks in order — so a stale
|
|
413
|
+
* DNS name or a DHCP-changed IP on the first host is tolerated within one connect() instead of
|
|
414
|
+
* only via the client's sequential primary→secondary retry. Deduped; returns a bare string when
|
|
415
|
+
* no distinct fallback exists, leaving single-host devices (and their tests) unaffected.
|
|
416
|
+
*/
|
|
417
|
+
buildConnectHosts(primaryTarget) {
|
|
418
|
+
const hosts = [primaryTarget];
|
|
419
|
+
const params = this.connectionParams;
|
|
420
|
+
if (params) {
|
|
421
|
+
for (const h of [params.primaryHost, params.secondaryHost]) {
|
|
422
|
+
if (h && !hosts.includes(h))
|
|
423
|
+
hosts.push(h);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return hosts.length === 1 ? hosts[0] : hosts;
|
|
427
|
+
}
|
|
405
428
|
/**
|
|
406
429
|
* Open a unit, retrying the connect on a transient failure per the resilience config
|
|
407
430
|
* ({@link IModbusResilienceConfig.connectRetries}). Each attempt is a full {@link _openUnitOnce}
|
|
@@ -415,7 +438,11 @@ class SunspecModbusClient {
|
|
|
415
438
|
async _openUnit(host, port, unitId, override) {
|
|
416
439
|
const connectTimeoutMs = override?.connectTimeoutMs ?? this.resilience.connectTimeoutMs;
|
|
417
440
|
const attempts = (override?.connectRetries ?? this.resilience.connectRetries) + 1;
|
|
418
|
-
|
|
441
|
+
// Default ON: a normal connect/reconnect hands the transport BOTH known hosts so a single
|
|
442
|
+
// attempt tolerates DNS/DHCP drift on the first one. probeUnit opts out to stay a fast-fail
|
|
443
|
+
// single-host scan (see probeUnit / _openUnitOnce).
|
|
444
|
+
const useFallbackHosts = override?.useFallbackHosts ?? true;
|
|
445
|
+
await retryAsync(() => this._openUnitOnce(host, port, unitId, connectTimeoutMs, useFallbackHosts), {
|
|
419
446
|
attempts,
|
|
420
447
|
delayMs: this.resilience.connectRetryDelayMs,
|
|
421
448
|
onRetry: (error, attempt) => console.warn(`SunspecModbusClient: connect to ${host}:${port} unit ${unitId} failed ` +
|
|
@@ -427,15 +454,28 @@ class SunspecModbusClient {
|
|
|
427
454
|
* Open a new Modbus instance for one unit ID and wire up its fault-tolerant reader. Bounds the
|
|
428
455
|
* connect at `connectTimeoutMs`. On any failure, the just-opened instance (if any) is closed so
|
|
429
456
|
* we never leak. Caller must hold the connection lock and ensure the unit is not already open.
|
|
430
|
-
|
|
431
|
-
|
|
457
|
+
*
|
|
458
|
+
* When `useFallbackHosts` is true (the connect/reconnect default), the transport is handed the
|
|
459
|
+
* full host list from {@link buildConnectHosts} — the attempt's primary target plus the other
|
|
460
|
+
* known host — so one connect() tolerates DNS/DHCP drift on the first host in-band, rather than
|
|
461
|
+
* waiting for the client's own sequential primary→secondary retry. The transport walks the list
|
|
462
|
+
* in order, each host bounded by `timeout` (connectTimeoutMs); we scale our own outer budget by
|
|
463
|
+
* the host count so a legitimate multi-host walk isn't aborted mid-fallback. probeUnit passes
|
|
464
|
+
* false to keep speculative RS485 scans a fast-fail single-host probe.
|
|
465
|
+
*/
|
|
466
|
+
async _openUnitOnce(host, port, unitId, connectTimeoutMs = this.resilience.connectTimeoutMs, useFallbackHosts = true) {
|
|
432
467
|
let candidate = null;
|
|
468
|
+
const hosts = useFallbackHosts ? this.buildConnectHosts(host) : host;
|
|
469
|
+
const hostCount = Array.isArray(hosts) ? hosts.length : 1;
|
|
470
|
+
// Each host in the list gets its own `timeout`; bound the whole walk at count × budget so we
|
|
471
|
+
// don't kill a genuine fallback to the second host with the single-host budget.
|
|
472
|
+
const overallBudgetMs = connectTimeoutMs * hostCount;
|
|
433
473
|
try {
|
|
434
474
|
// Enforce the connect budget ourselves in addition to forwarding it as the transport's
|
|
435
475
|
// `timeout`: the transport applies it to the TCP handshake, but a sleeping/half-open peer
|
|
436
476
|
// can still leave the returned promise pending — bound it independently.
|
|
437
|
-
candidate = await this.withTimeout('connect', unitId,
|
|
438
|
-
host,
|
|
477
|
+
candidate = await this.withTimeout('connect', unitId, overallBudgetMs, () => this.energyApp.useModbus().connect({
|
|
478
|
+
host: hosts,
|
|
439
479
|
port,
|
|
440
480
|
unitId,
|
|
441
481
|
timeout: connectTimeoutMs,
|
|
@@ -152,6 +152,15 @@ export declare class SunspecModbusClient {
|
|
|
152
152
|
* if every unit succeeded.
|
|
153
153
|
*/
|
|
154
154
|
private attemptConnection;
|
|
155
|
+
/**
|
|
156
|
+
* Build the ordered host list handed to the transport's `host` option for a single connect.
|
|
157
|
+
* The attempt's primary target leads, followed by every OTHER known host (the other of
|
|
158
|
+
* primaryHost/secondaryHost) as an in-band fallback the transport walks in order — so a stale
|
|
159
|
+
* DNS name or a DHCP-changed IP on the first host is tolerated within one connect() instead of
|
|
160
|
+
* only via the client's sequential primary→secondary retry. Deduped; returns a bare string when
|
|
161
|
+
* no distinct fallback exists, leaving single-host devices (and their tests) unaffected.
|
|
162
|
+
*/
|
|
163
|
+
private buildConnectHosts;
|
|
155
164
|
/**
|
|
156
165
|
* Open a unit, retrying the connect on a transient failure per the resilience config
|
|
157
166
|
* ({@link IModbusResilienceConfig.connectRetries}). Each attempt is a full {@link _openUnitOnce}
|
|
@@ -167,6 +176,14 @@ export declare class SunspecModbusClient {
|
|
|
167
176
|
* Open a new Modbus instance for one unit ID and wire up its fault-tolerant reader. Bounds the
|
|
168
177
|
* connect at `connectTimeoutMs`. On any failure, the just-opened instance (if any) is closed so
|
|
169
178
|
* we never leak. Caller must hold the connection lock and ensure the unit is not already open.
|
|
179
|
+
*
|
|
180
|
+
* When `useFallbackHosts` is true (the connect/reconnect default), the transport is handed the
|
|
181
|
+
* full host list from {@link buildConnectHosts} — the attempt's primary target plus the other
|
|
182
|
+
* known host — so one connect() tolerates DNS/DHCP drift on the first host in-band, rather than
|
|
183
|
+
* waiting for the client's own sequential primary→secondary retry. The transport walks the list
|
|
184
|
+
* in order, each host bounded by `timeout` (connectTimeoutMs); we scale our own outer budget by
|
|
185
|
+
* the host count so a legitimate multi-host walk isn't aborted mid-fallback. probeUnit passes
|
|
186
|
+
* false to keep speculative RS485 scans a fast-fail single-host probe.
|
|
170
187
|
*/
|
|
171
188
|
private _openUnitOnce;
|
|
172
189
|
/**
|
package/dist/cjs/version.cjs
CHANGED
|
@@ -9,7 +9,7 @@ exports.getSdkVersion = getSdkVersion;
|
|
|
9
9
|
/**
|
|
10
10
|
* Current version of the enyo Energy App SDK.
|
|
11
11
|
*/
|
|
12
|
-
exports.SDK_VERSION = '0.0.
|
|
12
|
+
exports.SDK_VERSION = '0.0.103';
|
|
13
13
|
/**
|
|
14
14
|
* Gets the current SDK version.
|
|
15
15
|
* @returns The semantic version string of the SDK
|
package/dist/cjs/version.d.cts
CHANGED
|
@@ -37,6 +37,19 @@ export declare abstract class BaseSunspecDevice {
|
|
|
37
37
|
* would otherwise stay Offline forever despite reading data fine.
|
|
38
38
|
*/
|
|
39
39
|
private appliedOffline;
|
|
40
|
+
/**
|
|
41
|
+
* True once {@link reassertConnectedIfRecovered} has confirmed (via the
|
|
42
|
+
* appliance store) that this device is Connected, so it can stop probing
|
|
43
|
+
* for an *externally* applied Offline state. Distinct from
|
|
44
|
+
* {@link appliedOffline}, which only tracks Offline states this SDK object
|
|
45
|
+
* applied itself. An appliance can be marked Offline by a path outside the
|
|
46
|
+
* SDK object — e.g. the energy-app registering a boot-time offline
|
|
47
|
+
* placeholder before this device came up on a shared socket — which never
|
|
48
|
+
* sets appliedOffline; without reconciling against actual state, such a
|
|
49
|
+
* device stays Offline forever despite reading data fine. Reset in
|
|
50
|
+
* {@link markOffline} so a later Offline episode is re-checked.
|
|
51
|
+
*/
|
|
52
|
+
private reconciledExternalOffline;
|
|
40
53
|
/**
|
|
41
54
|
* Prefix used when persisting calibration snapshots via the library's
|
|
42
55
|
* {@link SnapshotService}. Kept identical to the key the SDK used before
|
|
@@ -152,9 +165,21 @@ export declare abstract class BaseSunspecDevice {
|
|
|
152
165
|
* readData() and skip tryReconnect() entirely, so without this they would
|
|
153
166
|
* stay Offline forever despite reading data fine.
|
|
154
167
|
*
|
|
168
|
+
* Heals two ways an appliance can be stuck Offline while reads succeed:
|
|
169
|
+
* 1. This SDK object marked itself offline ({@link appliedOffline}) and a
|
|
170
|
+
* sibling won the reconnect race — the original shared-socket case.
|
|
171
|
+
* 2. The appliance was marked Offline by a path *outside* this object —
|
|
172
|
+
* e.g. the energy-app registering a boot-time offline placeholder
|
|
173
|
+
* before this device came up on the shared socket — which never sets
|
|
174
|
+
* appliedOffline. We reconcile against the actual stored state so the
|
|
175
|
+
* invariant "healthy reads ⇒ Connected" holds regardless of who
|
|
176
|
+
* applied the Offline.
|
|
177
|
+
*
|
|
155
178
|
* Call this on the success path of readData() (after the
|
|
156
|
-
* markOfflineIfUnhealthy() guard has confirmed the unit is healthy).
|
|
157
|
-
* a
|
|
179
|
+
* markOfflineIfUnhealthy() guard has confirmed the unit is healthy). Cheap:
|
|
180
|
+
* a no-op via the appliedOffline fast path, and the external-state lookup
|
|
181
|
+
* runs at most once per Offline episode (latched by
|
|
182
|
+
* {@link reconciledExternalOffline} once Connected is confirmed).
|
|
158
183
|
*/
|
|
159
184
|
protected reassertConnectedIfRecovered(): Promise<void>;
|
|
160
185
|
protected sendCommandAcknowledge(messageId: string, acknowledgeMessage: EnyoDataBusMessageEnum | string, answer: EnyoCommandAcknowledgeAnswerEnum, rejectionReason?: string): void;
|
package/dist/sunspec-devices.js
CHANGED
|
@@ -110,6 +110,19 @@ export class BaseSunspecDevice {
|
|
|
110
110
|
* would otherwise stay Offline forever despite reading data fine.
|
|
111
111
|
*/
|
|
112
112
|
appliedOffline = false;
|
|
113
|
+
/**
|
|
114
|
+
* True once {@link reassertConnectedIfRecovered} has confirmed (via the
|
|
115
|
+
* appliance store) that this device is Connected, so it can stop probing
|
|
116
|
+
* for an *externally* applied Offline state. Distinct from
|
|
117
|
+
* {@link appliedOffline}, which only tracks Offline states this SDK object
|
|
118
|
+
* applied itself. An appliance can be marked Offline by a path outside the
|
|
119
|
+
* SDK object — e.g. the energy-app registering a boot-time offline
|
|
120
|
+
* placeholder before this device came up on a shared socket — which never
|
|
121
|
+
* sets appliedOffline; without reconciling against actual state, such a
|
|
122
|
+
* device stays Offline forever despite reading data fine. Reset in
|
|
123
|
+
* {@link markOffline} so a later Offline episode is re-checked.
|
|
124
|
+
*/
|
|
125
|
+
reconciledExternalOffline = false;
|
|
113
126
|
/**
|
|
114
127
|
* Prefix used when persisting calibration snapshots via the library's
|
|
115
128
|
* {@link SnapshotService}. Kept identical to the key the SDK used before
|
|
@@ -330,6 +343,7 @@ export class BaseSunspecDevice {
|
|
|
330
343
|
try {
|
|
331
344
|
await this.applianceManager.updateApplianceState(this.applianceId, EnyoApplianceConnectionType.Connector, EnyoApplianceStateEnum.Offline);
|
|
332
345
|
this.appliedOffline = true;
|
|
346
|
+
this.reconciledExternalOffline = false;
|
|
333
347
|
}
|
|
334
348
|
catch (error) {
|
|
335
349
|
console.error(`${this.constructor.name} ${this.applianceId}: Failed to mark appliance offline: ${error}`);
|
|
@@ -345,17 +359,45 @@ export class BaseSunspecDevice {
|
|
|
345
359
|
* readData() and skip tryReconnect() entirely, so without this they would
|
|
346
360
|
* stay Offline forever despite reading data fine.
|
|
347
361
|
*
|
|
362
|
+
* Heals two ways an appliance can be stuck Offline while reads succeed:
|
|
363
|
+
* 1. This SDK object marked itself offline ({@link appliedOffline}) and a
|
|
364
|
+
* sibling won the reconnect race — the original shared-socket case.
|
|
365
|
+
* 2. The appliance was marked Offline by a path *outside* this object —
|
|
366
|
+
* e.g. the energy-app registering a boot-time offline placeholder
|
|
367
|
+
* before this device came up on the shared socket — which never sets
|
|
368
|
+
* appliedOffline. We reconcile against the actual stored state so the
|
|
369
|
+
* invariant "healthy reads ⇒ Connected" holds regardless of who
|
|
370
|
+
* applied the Offline.
|
|
371
|
+
*
|
|
348
372
|
* Call this on the success path of readData() (after the
|
|
349
|
-
* markOfflineIfUnhealthy() guard has confirmed the unit is healthy).
|
|
350
|
-
* a
|
|
373
|
+
* markOfflineIfUnhealthy() guard has confirmed the unit is healthy). Cheap:
|
|
374
|
+
* a no-op via the appliedOffline fast path, and the external-state lookup
|
|
375
|
+
* runs at most once per Offline episode (latched by
|
|
376
|
+
* {@link reconciledExternalOffline} once Connected is confirmed).
|
|
351
377
|
*/
|
|
352
378
|
async reassertConnectedIfRecovered() {
|
|
353
|
-
if (!this.
|
|
379
|
+
if (!this.applianceId) {
|
|
354
380
|
return;
|
|
355
381
|
}
|
|
356
382
|
if (!this.sunspecClient.isHealthy(this.unitId)) {
|
|
357
383
|
return;
|
|
358
384
|
}
|
|
385
|
+
let shouldReassert = this.appliedOffline;
|
|
386
|
+
// Not offline by our own hand: it may still be Offline from an
|
|
387
|
+
// externally applied state. Probe the store once until we confirm
|
|
388
|
+
// Connected, then latch so the steady state adds no extra lookups.
|
|
389
|
+
if (!shouldReassert && !this.reconciledExternalOffline) {
|
|
390
|
+
const appliance = await this.applianceManager.findApplianceById(this.applianceId);
|
|
391
|
+
if (appliance?.metadata?.state === EnyoApplianceStateEnum.Offline) {
|
|
392
|
+
shouldReassert = true;
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
this.reconciledExternalOffline = true;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
if (!shouldReassert) {
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
359
401
|
console.log(`${this.constructor.name} ${this.applianceId}: connection recovered via shared socket — re-asserting Connected`);
|
|
360
402
|
try {
|
|
361
403
|
await this.applianceManager.updateApplianceState(this.applianceId, EnyoApplianceConnectionType.Connector, EnyoApplianceStateEnum.Connected);
|
|
@@ -365,6 +407,7 @@ export class BaseSunspecDevice {
|
|
|
365
407
|
return;
|
|
366
408
|
}
|
|
367
409
|
this.appliedOffline = false;
|
|
410
|
+
this.reconciledExternalOffline = true;
|
|
368
411
|
this.retryManager.reset();
|
|
369
412
|
this.consecutiveReconnectFailures = 0;
|
|
370
413
|
await this.onConnectionRestored();
|
|
@@ -152,6 +152,15 @@ export declare class SunspecModbusClient {
|
|
|
152
152
|
* if every unit succeeded.
|
|
153
153
|
*/
|
|
154
154
|
private attemptConnection;
|
|
155
|
+
/**
|
|
156
|
+
* Build the ordered host list handed to the transport's `host` option for a single connect.
|
|
157
|
+
* The attempt's primary target leads, followed by every OTHER known host (the other of
|
|
158
|
+
* primaryHost/secondaryHost) as an in-band fallback the transport walks in order — so a stale
|
|
159
|
+
* DNS name or a DHCP-changed IP on the first host is tolerated within one connect() instead of
|
|
160
|
+
* only via the client's sequential primary→secondary retry. Deduped; returns a bare string when
|
|
161
|
+
* no distinct fallback exists, leaving single-host devices (and their tests) unaffected.
|
|
162
|
+
*/
|
|
163
|
+
private buildConnectHosts;
|
|
155
164
|
/**
|
|
156
165
|
* Open a unit, retrying the connect on a transient failure per the resilience config
|
|
157
166
|
* ({@link IModbusResilienceConfig.connectRetries}). Each attempt is a full {@link _openUnitOnce}
|
|
@@ -167,6 +176,14 @@ export declare class SunspecModbusClient {
|
|
|
167
176
|
* Open a new Modbus instance for one unit ID and wire up its fault-tolerant reader. Bounds the
|
|
168
177
|
* connect at `connectTimeoutMs`. On any failure, the just-opened instance (if any) is closed so
|
|
169
178
|
* we never leak. Caller must hold the connection lock and ensure the unit is not already open.
|
|
179
|
+
*
|
|
180
|
+
* When `useFallbackHosts` is true (the connect/reconnect default), the transport is handed the
|
|
181
|
+
* full host list from {@link buildConnectHosts} — the attempt's primary target plus the other
|
|
182
|
+
* known host — so one connect() tolerates DNS/DHCP drift on the first host in-band, rather than
|
|
183
|
+
* waiting for the client's own sequential primary→secondary retry. The transport walks the list
|
|
184
|
+
* in order, each host bounded by `timeout` (connectTimeoutMs); we scale our own outer budget by
|
|
185
|
+
* the host count so a legitimate multi-host walk isn't aborted mid-fallback. probeUnit passes
|
|
186
|
+
* false to keep speculative RS485 scans a fast-fail single-host probe.
|
|
170
187
|
*/
|
|
171
188
|
private _openUnitOnce;
|
|
172
189
|
/**
|
|
@@ -224,6 +224,10 @@ export class SunspecModbusClient {
|
|
|
224
224
|
await this._openUnit(host, port, unitId, {
|
|
225
225
|
connectRetries: 0,
|
|
226
226
|
connectTimeoutMs: opts.timeoutMs ?? this.resilience.connectTimeoutMs,
|
|
227
|
+
// Fast-fail scan: a mostly-dead RS485 range must cost ~one timeout per unit, so do
|
|
228
|
+
// NOT let the transport also walk a fallback host (which would double the per-unit
|
|
229
|
+
// cost). Warm connects/reconnects still get the in-band fallback.
|
|
230
|
+
useFallbackHosts: false,
|
|
227
231
|
});
|
|
228
232
|
console.log(`Probed Sunspec unit ${unitId} at ${host}:${port} — open (opens=${this.openCount}, closes=${this.closeCount}, openUnits=${this.modbusInstances.size})`);
|
|
229
233
|
});
|
|
@@ -395,6 +399,25 @@ export class SunspecModbusClient {
|
|
|
395
399
|
return false;
|
|
396
400
|
}
|
|
397
401
|
}
|
|
402
|
+
/**
|
|
403
|
+
* Build the ordered host list handed to the transport's `host` option for a single connect.
|
|
404
|
+
* The attempt's primary target leads, followed by every OTHER known host (the other of
|
|
405
|
+
* primaryHost/secondaryHost) as an in-band fallback the transport walks in order — so a stale
|
|
406
|
+
* DNS name or a DHCP-changed IP on the first host is tolerated within one connect() instead of
|
|
407
|
+
* only via the client's sequential primary→secondary retry. Deduped; returns a bare string when
|
|
408
|
+
* no distinct fallback exists, leaving single-host devices (and their tests) unaffected.
|
|
409
|
+
*/
|
|
410
|
+
buildConnectHosts(primaryTarget) {
|
|
411
|
+
const hosts = [primaryTarget];
|
|
412
|
+
const params = this.connectionParams;
|
|
413
|
+
if (params) {
|
|
414
|
+
for (const h of [params.primaryHost, params.secondaryHost]) {
|
|
415
|
+
if (h && !hosts.includes(h))
|
|
416
|
+
hosts.push(h);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return hosts.length === 1 ? hosts[0] : hosts;
|
|
420
|
+
}
|
|
398
421
|
/**
|
|
399
422
|
* Open a unit, retrying the connect on a transient failure per the resilience config
|
|
400
423
|
* ({@link IModbusResilienceConfig.connectRetries}). Each attempt is a full {@link _openUnitOnce}
|
|
@@ -408,7 +431,11 @@ export class SunspecModbusClient {
|
|
|
408
431
|
async _openUnit(host, port, unitId, override) {
|
|
409
432
|
const connectTimeoutMs = override?.connectTimeoutMs ?? this.resilience.connectTimeoutMs;
|
|
410
433
|
const attempts = (override?.connectRetries ?? this.resilience.connectRetries) + 1;
|
|
411
|
-
|
|
434
|
+
// Default ON: a normal connect/reconnect hands the transport BOTH known hosts so a single
|
|
435
|
+
// attempt tolerates DNS/DHCP drift on the first one. probeUnit opts out to stay a fast-fail
|
|
436
|
+
// single-host scan (see probeUnit / _openUnitOnce).
|
|
437
|
+
const useFallbackHosts = override?.useFallbackHosts ?? true;
|
|
438
|
+
await retryAsync(() => this._openUnitOnce(host, port, unitId, connectTimeoutMs, useFallbackHosts), {
|
|
412
439
|
attempts,
|
|
413
440
|
delayMs: this.resilience.connectRetryDelayMs,
|
|
414
441
|
onRetry: (error, attempt) => console.warn(`SunspecModbusClient: connect to ${host}:${port} unit ${unitId} failed ` +
|
|
@@ -420,15 +447,28 @@ export class SunspecModbusClient {
|
|
|
420
447
|
* Open a new Modbus instance for one unit ID and wire up its fault-tolerant reader. Bounds the
|
|
421
448
|
* connect at `connectTimeoutMs`. On any failure, the just-opened instance (if any) is closed so
|
|
422
449
|
* we never leak. Caller must hold the connection lock and ensure the unit is not already open.
|
|
423
|
-
|
|
424
|
-
|
|
450
|
+
*
|
|
451
|
+
* When `useFallbackHosts` is true (the connect/reconnect default), the transport is handed the
|
|
452
|
+
* full host list from {@link buildConnectHosts} — the attempt's primary target plus the other
|
|
453
|
+
* known host — so one connect() tolerates DNS/DHCP drift on the first host in-band, rather than
|
|
454
|
+
* waiting for the client's own sequential primary→secondary retry. The transport walks the list
|
|
455
|
+
* in order, each host bounded by `timeout` (connectTimeoutMs); we scale our own outer budget by
|
|
456
|
+
* the host count so a legitimate multi-host walk isn't aborted mid-fallback. probeUnit passes
|
|
457
|
+
* false to keep speculative RS485 scans a fast-fail single-host probe.
|
|
458
|
+
*/
|
|
459
|
+
async _openUnitOnce(host, port, unitId, connectTimeoutMs = this.resilience.connectTimeoutMs, useFallbackHosts = true) {
|
|
425
460
|
let candidate = null;
|
|
461
|
+
const hosts = useFallbackHosts ? this.buildConnectHosts(host) : host;
|
|
462
|
+
const hostCount = Array.isArray(hosts) ? hosts.length : 1;
|
|
463
|
+
// Each host in the list gets its own `timeout`; bound the whole walk at count × budget so we
|
|
464
|
+
// don't kill a genuine fallback to the second host with the single-host budget.
|
|
465
|
+
const overallBudgetMs = connectTimeoutMs * hostCount;
|
|
426
466
|
try {
|
|
427
467
|
// Enforce the connect budget ourselves in addition to forwarding it as the transport's
|
|
428
468
|
// `timeout`: the transport applies it to the TCP handshake, but a sleeping/half-open peer
|
|
429
469
|
// can still leave the returned promise pending — bound it independently.
|
|
430
|
-
candidate = await this.withTimeout('connect', unitId,
|
|
431
|
-
host,
|
|
470
|
+
candidate = await this.withTimeout('connect', unitId, overallBudgetMs, () => this.energyApp.useModbus().connect({
|
|
471
|
+
host: hosts,
|
|
432
472
|
port,
|
|
433
473
|
unitId,
|
|
434
474
|
timeout: connectTimeoutMs,
|
package/dist/version.d.ts
CHANGED
package/dist/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@enyo-energy/sunspec-sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.103",
|
|
4
4
|
"description": "enyo Energy Sunspec SDK",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -40,8 +40,8 @@
|
|
|
40
40
|
"vitest": "^2.1.9"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@enyo-energy/appliance-calibration": "0.0.
|
|
44
|
-
"@enyo-energy/energy-app-sdk": "^0.0.
|
|
43
|
+
"@enyo-energy/appliance-calibration": "0.0.3",
|
|
44
|
+
"@enyo-energy/energy-app-sdk": "^0.0.180"
|
|
45
45
|
},
|
|
46
46
|
"volta": {
|
|
47
47
|
"node": "22.17.0"
|