@enyo-energy/sunspec-sdk 0.0.103 → 0.0.104
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/index.d.cts +1 -0
- package/dist/cjs/sunspec-modbus-client.cjs +40 -8
- package/dist/cjs/sunspec-modbus-client.d.cts +17 -6
- package/dist/cjs/version.cjs +1 -1
- package/dist/cjs/version.d.cts +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/sunspec-modbus-client.d.ts +17 -6
- package/dist/sunspec-modbus-client.js +40 -8
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -2
package/dist/cjs/index.d.cts
CHANGED
|
@@ -7,5 +7,6 @@ export { SunspecCalibrationStorage, createSunspecCalibrationStorage } from './su
|
|
|
7
7
|
export { SunspecBatteryCalibrationDriver } from './sunspec-battery-calibration-driver.cjs';
|
|
8
8
|
export { SunspecBatteryFeatureCalibrator, type SunspecBatteryFeatureCalibratorOptions, decodeFeatureResults, } from './sunspec-battery-feature-calibrator.cjs';
|
|
9
9
|
export { SunspecBatteryScheduleHandler, type SunspecScheduleRegisters, type SunspecBatteryScheduleHandlerOptions, } from './sunspec-battery-schedule-handler.cjs';
|
|
10
|
+
export { type EnergyAppModbus, type EnergyAppModbusInstance, type ModbusOptions, } from '@enyo-energy/energy-app-sdk/dist/packages/energy-app-modbus.js';
|
|
10
11
|
export { type EnyoDataBusSetStorageScheduleV1, type EnyoStorageScheduleEntry, EnyoStorageScheduleModeEnum, EnyoStorageScheduleDirectionEnum, } from '@enyo-energy/energy-app-sdk/dist/types/enyo-data-bus-value.js';
|
|
11
12
|
export { AbstractCalibrationStorage, InMemoryCalibrationStorage, type AckResult, type CalibrationResult, type CalibrationState, type CalibrationSnapshot, type CalibrationRestoreCallback, type RestoreReason, SnapshotService, type SnapshotServiceOptions, DEFAULT_AUTO_STOP_MS, AbstractBatteryCalibrationDriver, BatteryCalibrator, type BatteryCalibratorOptions, type BatteryCalibratorConfig, DEFAULT_BATTERY_CALIBRATOR_CONFIG, CalibrationResultStore, type CalibrationResultStoreData, CalibrationTrigger, type CalibrationTriggerOptions, } from '@enyo-energy/appliance-calibration';
|
|
@@ -53,15 +53,19 @@ const registryKey = (host, port) => `${host}:${port}`;
|
|
|
53
53
|
* Get (or create) the singleton SunspecModbusClient for this network device.
|
|
54
54
|
* Increments the refcount; pair with `releaseSunspecClient` on teardown.
|
|
55
55
|
*
|
|
56
|
-
* NOTE: `pacing` and `
|
|
57
|
-
* host:port. Because the client is cached and refcounted, a later caller
|
|
58
|
-
* receives the already-created instance and its config is ignored.
|
|
56
|
+
* NOTE: `pacing`, `resilience` and `modbusFactory` are only applied when the client is FIRST
|
|
57
|
+
* created for a given host:port. Because the client is cached and refcounted, a later caller
|
|
58
|
+
* passing different config receives the already-created instance and its config is ignored.
|
|
59
|
+
*
|
|
60
|
+
* `modbusFactory` lets a caller supply their own Modbus backend (the factory whose `connect()`
|
|
61
|
+
* opens a per-unit connection) instead of the one from `energyApp.useModbus()` — e.g. a mock for
|
|
62
|
+
* tests, or an alternative transport. When provided, `energyApp` may be passed as `undefined`.
|
|
59
63
|
*/
|
|
60
|
-
function getOrCreateSunspecClient(energyApp, host, port = 502, pacing, resilience) {
|
|
64
|
+
function getOrCreateSunspecClient(energyApp, host, port = 502, pacing, resilience, modbusFactory) {
|
|
61
65
|
const key = registryKey(host, port);
|
|
62
66
|
let entry = CLIENT_REGISTRY.get(key);
|
|
63
67
|
if (!entry) {
|
|
64
|
-
entry = { client: new SunspecModbusClient(energyApp, pacing, resilience), refcount: 0 };
|
|
68
|
+
entry = { client: new SunspecModbusClient(energyApp, pacing, resilience, modbusFactory), refcount: 0 };
|
|
65
69
|
CLIENT_REGISTRY.set(key, entry);
|
|
66
70
|
}
|
|
67
71
|
entry.refcount++;
|
|
@@ -127,6 +131,7 @@ async function retryAsync(fn, opts) {
|
|
|
127
131
|
class SunspecModbusClient {
|
|
128
132
|
energyApp;
|
|
129
133
|
pacing;
|
|
134
|
+
modbusFactory;
|
|
130
135
|
// Per-unit Modbus instance/reader/discovered-models maps. One client = one network device,
|
|
131
136
|
// many unit IDs share that client (each unit ID is one EnergyAppModbusInstance underneath).
|
|
132
137
|
modbusInstances = new Map();
|
|
@@ -170,20 +175,47 @@ class SunspecModbusClient {
|
|
|
170
175
|
// the transport still reports connected but that has gone quiet past staleSocketMs is presumed
|
|
171
176
|
// dead. Absent entry = no successful op yet (never treated as stale — give it a chance).
|
|
172
177
|
lastOkAtByUnit = new Map();
|
|
173
|
-
constructor(
|
|
178
|
+
constructor(
|
|
179
|
+
// The host application. Only used to obtain the default Modbus backend via
|
|
180
|
+
// `energyApp.useModbus()`. May be omitted (`undefined`) when a `modbusFactory` is supplied.
|
|
181
|
+
energyApp,
|
|
174
182
|
// Opt-in pacing (default off). Passed straight through to `useModbus().connect()`, which
|
|
175
183
|
// serializes frames (`noParallelRequests`) and spaces them (`waitBetweenMessagesMs`) on the
|
|
176
184
|
// connection. An empty object leaves both undefined so the SDK applies its own defaults.
|
|
177
185
|
pacing = {},
|
|
178
186
|
// Per-operation timeouts + half-open detection. Merged with DEFAULT_MODBUS_RESILIENCE_CONFIG
|
|
179
187
|
// so omitted fields keep their defaults.
|
|
180
|
-
resilience = {}
|
|
188
|
+
resilience = {},
|
|
189
|
+
// Optional custom Modbus backend. When provided, its `connect()` is used to open every
|
|
190
|
+
// per-unit connection instead of `energyApp.useModbus()` — letting callers plug in their own
|
|
191
|
+
// Modbus implementation (a mock for tests, or an alternative transport). All the resilience
|
|
192
|
+
// machinery (per-unit connect/reconnect, TLS, pacing, timeouts) is driven through this
|
|
193
|
+
// factory, which is why an already-connected instance would not do: the client re-opens
|
|
194
|
+
// connections on its own schedule and needs a factory to call.
|
|
195
|
+
modbusFactory) {
|
|
181
196
|
this.energyApp = energyApp;
|
|
182
197
|
this.pacing = pacing;
|
|
198
|
+
this.modbusFactory = modbusFactory;
|
|
199
|
+
if (!energyApp && !modbusFactory) {
|
|
200
|
+
throw new Error("SunspecModbusClient requires either an EnergyApp or a modbusFactory");
|
|
201
|
+
}
|
|
183
202
|
this.connectionHealth = new EnergyAppModbusConnectionHealth_js_1.EnergyAppModbusConnectionHealth();
|
|
184
203
|
this.modbusDataTypeConverter = new EnergyAppModbusDataTypeConverter_js_1.EnergyAppModbusDataTypeConverter();
|
|
185
204
|
this.resilience = { ...sunspec_interfaces_js_1.DEFAULT_MODBUS_RESILIENCE_CONFIG, ...resilience };
|
|
186
205
|
}
|
|
206
|
+
/**
|
|
207
|
+
* The Modbus backend used to open per-unit connections: the caller-supplied {@link modbusFactory}
|
|
208
|
+
* if one was given, otherwise the host application's default (`energyApp.useModbus()`). The
|
|
209
|
+
* constructor guarantees at least one of the two is present.
|
|
210
|
+
*/
|
|
211
|
+
useModbus() {
|
|
212
|
+
if (this.modbusFactory)
|
|
213
|
+
return this.modbusFactory;
|
|
214
|
+
if (!this.energyApp) {
|
|
215
|
+
throw new Error("SunspecModbusClient has no Modbus backend: neither energyApp nor modbusFactory is set");
|
|
216
|
+
}
|
|
217
|
+
return this.energyApp.useModbus();
|
|
218
|
+
}
|
|
187
219
|
/**
|
|
188
220
|
* Connect to a Modbus device unit. Multiple unit IDs on the same network device share this
|
|
189
221
|
* client; calling connect() with different unit IDs adds each as a separately-managed unit.
|
|
@@ -474,7 +506,7 @@ class SunspecModbusClient {
|
|
|
474
506
|
// Enforce the connect budget ourselves in addition to forwarding it as the transport's
|
|
475
507
|
// `timeout`: the transport applies it to the TCP handshake, but a sleeping/half-open peer
|
|
476
508
|
// can still leave the returned promise pending — bound it independently.
|
|
477
|
-
candidate = await this.withTimeout('connect', unitId, overallBudgetMs, () => this.
|
|
509
|
+
candidate = await this.withTimeout('connect', unitId, overallBudgetMs, () => this.useModbus().connect({
|
|
478
510
|
host: hosts,
|
|
479
511
|
port,
|
|
480
512
|
unitId,
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
*/
|
|
19
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.cjs";
|
|
20
20
|
import { EnergyAppModbusDataType, IConnectionHealth } from "@enyo-energy/energy-app-sdk/dist/implementations/modbus/interfaces.js";
|
|
21
|
-
import { EnergyAppModbusInstance } from "@enyo-energy/energy-app-sdk/dist/packages/energy-app-modbus.js";
|
|
21
|
+
import { EnergyAppModbus, EnergyAppModbusInstance } from "@enyo-energy/energy-app-sdk/dist/packages/energy-app-modbus.js";
|
|
22
22
|
import { EnergyApp } from "@enyo-energy/energy-app-sdk";
|
|
23
23
|
/**
|
|
24
24
|
* Thrown when a single Modbus transaction (read/write/connect/disconnect) exceeds its configured
|
|
@@ -34,11 +34,15 @@ export declare class ModbusOperationTimeoutError extends Error {
|
|
|
34
34
|
* Get (or create) the singleton SunspecModbusClient for this network device.
|
|
35
35
|
* Increments the refcount; pair with `releaseSunspecClient` on teardown.
|
|
36
36
|
*
|
|
37
|
-
* NOTE: `pacing` and `
|
|
38
|
-
* host:port. Because the client is cached and refcounted, a later caller
|
|
39
|
-
* receives the already-created instance and its config is ignored.
|
|
37
|
+
* NOTE: `pacing`, `resilience` and `modbusFactory` are only applied when the client is FIRST
|
|
38
|
+
* created for a given host:port. Because the client is cached and refcounted, a later caller
|
|
39
|
+
* passing different config receives the already-created instance and its config is ignored.
|
|
40
|
+
*
|
|
41
|
+
* `modbusFactory` lets a caller supply their own Modbus backend (the factory whose `connect()`
|
|
42
|
+
* opens a per-unit connection) instead of the one from `energyApp.useModbus()` — e.g. a mock for
|
|
43
|
+
* tests, or an alternative transport. When provided, `energyApp` may be passed as `undefined`.
|
|
40
44
|
*/
|
|
41
|
-
export declare function getOrCreateSunspecClient(energyApp: EnergyApp, host: string, port?: number, pacing?: IModbusPacingConfig, resilience?: IModbusResilienceConfig): SunspecModbusClient;
|
|
45
|
+
export declare function getOrCreateSunspecClient(energyApp: EnergyApp | undefined, host: string, port?: number, pacing?: IModbusPacingConfig, resilience?: IModbusResilienceConfig, modbusFactory?: EnergyAppModbus): SunspecModbusClient;
|
|
42
46
|
/**
|
|
43
47
|
* Release a refcount on the network-device client. When the last consumer releases,
|
|
44
48
|
* the client is fully disconnected (all units closed) and removed from the registry.
|
|
@@ -63,6 +67,7 @@ export declare function retryAsync<T>(fn: (attempt: number) => Promise<T>, opts:
|
|
|
63
67
|
export declare class SunspecModbusClient {
|
|
64
68
|
private energyApp;
|
|
65
69
|
private pacing;
|
|
70
|
+
private modbusFactory?;
|
|
66
71
|
private modbusInstances;
|
|
67
72
|
private faultTolerantReaders;
|
|
68
73
|
private discoveredModelsByUnit;
|
|
@@ -79,7 +84,13 @@ export declare class SunspecModbusClient {
|
|
|
79
84
|
private resilience;
|
|
80
85
|
private poisonedUnits;
|
|
81
86
|
private lastOkAtByUnit;
|
|
82
|
-
constructor(energyApp: EnergyApp, pacing?: IModbusPacingConfig, resilience?: IModbusResilienceConfig);
|
|
87
|
+
constructor(energyApp: EnergyApp | undefined, pacing?: IModbusPacingConfig, resilience?: IModbusResilienceConfig, modbusFactory?: EnergyAppModbus | undefined);
|
|
88
|
+
/**
|
|
89
|
+
* The Modbus backend used to open per-unit connections: the caller-supplied {@link modbusFactory}
|
|
90
|
+
* if one was given, otherwise the host application's default (`energyApp.useModbus()`). The
|
|
91
|
+
* constructor guarantees at least one of the two is present.
|
|
92
|
+
*/
|
|
93
|
+
private useModbus;
|
|
83
94
|
/**
|
|
84
95
|
* Connect to a Modbus device unit. Multiple unit IDs on the same network device share this
|
|
85
96
|
* client; calling connect() with different unit IDs adds each as a separately-managed unit.
|
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.104';
|
|
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
package/dist/index.d.ts
CHANGED
|
@@ -7,5 +7,6 @@ export { SunspecCalibrationStorage, createSunspecCalibrationStorage } from './su
|
|
|
7
7
|
export { SunspecBatteryCalibrationDriver } from './sunspec-battery-calibration-driver.js';
|
|
8
8
|
export { SunspecBatteryFeatureCalibrator, type SunspecBatteryFeatureCalibratorOptions, decodeFeatureResults, } from './sunspec-battery-feature-calibrator.js';
|
|
9
9
|
export { SunspecBatteryScheduleHandler, type SunspecScheduleRegisters, type SunspecBatteryScheduleHandlerOptions, } from './sunspec-battery-schedule-handler.js';
|
|
10
|
+
export { type EnergyAppModbus, type EnergyAppModbusInstance, type ModbusOptions, } from '@enyo-energy/energy-app-sdk/dist/packages/energy-app-modbus.js';
|
|
10
11
|
export { type EnyoDataBusSetStorageScheduleV1, type EnyoStorageScheduleEntry, EnyoStorageScheduleModeEnum, EnyoStorageScheduleDirectionEnum, } from '@enyo-energy/energy-app-sdk/dist/types/enyo-data-bus-value.js';
|
|
11
12
|
export { AbstractCalibrationStorage, InMemoryCalibrationStorage, type AckResult, type CalibrationResult, type CalibrationState, type CalibrationSnapshot, type CalibrationRestoreCallback, type RestoreReason, SnapshotService, type SnapshotServiceOptions, DEFAULT_AUTO_STOP_MS, AbstractBatteryCalibrationDriver, BatteryCalibrator, type BatteryCalibratorOptions, type BatteryCalibratorConfig, DEFAULT_BATTERY_CALIBRATOR_CONFIG, CalibrationResultStore, type CalibrationResultStoreData, CalibrationTrigger, type CalibrationTriggerOptions, } from '@enyo-energy/appliance-calibration';
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
*/
|
|
19
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
|
-
import { EnergyAppModbusInstance } from "@enyo-energy/energy-app-sdk/dist/packages/energy-app-modbus.js";
|
|
21
|
+
import { EnergyAppModbus, EnergyAppModbusInstance } from "@enyo-energy/energy-app-sdk/dist/packages/energy-app-modbus.js";
|
|
22
22
|
import { EnergyApp } from "@enyo-energy/energy-app-sdk";
|
|
23
23
|
/**
|
|
24
24
|
* Thrown when a single Modbus transaction (read/write/connect/disconnect) exceeds its configured
|
|
@@ -34,11 +34,15 @@ export declare class ModbusOperationTimeoutError extends Error {
|
|
|
34
34
|
* Get (or create) the singleton SunspecModbusClient for this network device.
|
|
35
35
|
* Increments the refcount; pair with `releaseSunspecClient` on teardown.
|
|
36
36
|
*
|
|
37
|
-
* NOTE: `pacing` and `
|
|
38
|
-
* host:port. Because the client is cached and refcounted, a later caller
|
|
39
|
-
* receives the already-created instance and its config is ignored.
|
|
37
|
+
* NOTE: `pacing`, `resilience` and `modbusFactory` are only applied when the client is FIRST
|
|
38
|
+
* created for a given host:port. Because the client is cached and refcounted, a later caller
|
|
39
|
+
* passing different config receives the already-created instance and its config is ignored.
|
|
40
|
+
*
|
|
41
|
+
* `modbusFactory` lets a caller supply their own Modbus backend (the factory whose `connect()`
|
|
42
|
+
* opens a per-unit connection) instead of the one from `energyApp.useModbus()` — e.g. a mock for
|
|
43
|
+
* tests, or an alternative transport. When provided, `energyApp` may be passed as `undefined`.
|
|
40
44
|
*/
|
|
41
|
-
export declare function getOrCreateSunspecClient(energyApp: EnergyApp, host: string, port?: number, pacing?: IModbusPacingConfig, resilience?: IModbusResilienceConfig): SunspecModbusClient;
|
|
45
|
+
export declare function getOrCreateSunspecClient(energyApp: EnergyApp | undefined, host: string, port?: number, pacing?: IModbusPacingConfig, resilience?: IModbusResilienceConfig, modbusFactory?: EnergyAppModbus): SunspecModbusClient;
|
|
42
46
|
/**
|
|
43
47
|
* Release a refcount on the network-device client. When the last consumer releases,
|
|
44
48
|
* the client is fully disconnected (all units closed) and removed from the registry.
|
|
@@ -63,6 +67,7 @@ export declare function retryAsync<T>(fn: (attempt: number) => Promise<T>, opts:
|
|
|
63
67
|
export declare class SunspecModbusClient {
|
|
64
68
|
private energyApp;
|
|
65
69
|
private pacing;
|
|
70
|
+
private modbusFactory?;
|
|
66
71
|
private modbusInstances;
|
|
67
72
|
private faultTolerantReaders;
|
|
68
73
|
private discoveredModelsByUnit;
|
|
@@ -79,7 +84,13 @@ export declare class SunspecModbusClient {
|
|
|
79
84
|
private resilience;
|
|
80
85
|
private poisonedUnits;
|
|
81
86
|
private lastOkAtByUnit;
|
|
82
|
-
constructor(energyApp: EnergyApp, pacing?: IModbusPacingConfig, resilience?: IModbusResilienceConfig);
|
|
87
|
+
constructor(energyApp: EnergyApp | undefined, pacing?: IModbusPacingConfig, resilience?: IModbusResilienceConfig, modbusFactory?: EnergyAppModbus | undefined);
|
|
88
|
+
/**
|
|
89
|
+
* The Modbus backend used to open per-unit connections: the caller-supplied {@link modbusFactory}
|
|
90
|
+
* if one was given, otherwise the host application's default (`energyApp.useModbus()`). The
|
|
91
|
+
* constructor guarantees at least one of the two is present.
|
|
92
|
+
*/
|
|
93
|
+
private useModbus;
|
|
83
94
|
/**
|
|
84
95
|
* Connect to a Modbus device unit. Multiple unit IDs on the same network device share this
|
|
85
96
|
* client; calling connect() with different unit IDs adds each as a separately-managed unit.
|
|
@@ -46,15 +46,19 @@ const registryKey = (host, port) => `${host}:${port}`;
|
|
|
46
46
|
* Get (or create) the singleton SunspecModbusClient for this network device.
|
|
47
47
|
* Increments the refcount; pair with `releaseSunspecClient` on teardown.
|
|
48
48
|
*
|
|
49
|
-
* NOTE: `pacing` and `
|
|
50
|
-
* host:port. Because the client is cached and refcounted, a later caller
|
|
51
|
-
* receives the already-created instance and its config is ignored.
|
|
49
|
+
* NOTE: `pacing`, `resilience` and `modbusFactory` are only applied when the client is FIRST
|
|
50
|
+
* created for a given host:port. Because the client is cached and refcounted, a later caller
|
|
51
|
+
* passing different config receives the already-created instance and its config is ignored.
|
|
52
|
+
*
|
|
53
|
+
* `modbusFactory` lets a caller supply their own Modbus backend (the factory whose `connect()`
|
|
54
|
+
* opens a per-unit connection) instead of the one from `energyApp.useModbus()` — e.g. a mock for
|
|
55
|
+
* tests, or an alternative transport. When provided, `energyApp` may be passed as `undefined`.
|
|
52
56
|
*/
|
|
53
|
-
export function getOrCreateSunspecClient(energyApp, host, port = 502, pacing, resilience) {
|
|
57
|
+
export function getOrCreateSunspecClient(energyApp, host, port = 502, pacing, resilience, modbusFactory) {
|
|
54
58
|
const key = registryKey(host, port);
|
|
55
59
|
let entry = CLIENT_REGISTRY.get(key);
|
|
56
60
|
if (!entry) {
|
|
57
|
-
entry = { client: new SunspecModbusClient(energyApp, pacing, resilience), refcount: 0 };
|
|
61
|
+
entry = { client: new SunspecModbusClient(energyApp, pacing, resilience, modbusFactory), refcount: 0 };
|
|
58
62
|
CLIENT_REGISTRY.set(key, entry);
|
|
59
63
|
}
|
|
60
64
|
entry.refcount++;
|
|
@@ -120,6 +124,7 @@ export async function retryAsync(fn, opts) {
|
|
|
120
124
|
export class SunspecModbusClient {
|
|
121
125
|
energyApp;
|
|
122
126
|
pacing;
|
|
127
|
+
modbusFactory;
|
|
123
128
|
// Per-unit Modbus instance/reader/discovered-models maps. One client = one network device,
|
|
124
129
|
// many unit IDs share that client (each unit ID is one EnergyAppModbusInstance underneath).
|
|
125
130
|
modbusInstances = new Map();
|
|
@@ -163,20 +168,47 @@ export class SunspecModbusClient {
|
|
|
163
168
|
// the transport still reports connected but that has gone quiet past staleSocketMs is presumed
|
|
164
169
|
// dead. Absent entry = no successful op yet (never treated as stale — give it a chance).
|
|
165
170
|
lastOkAtByUnit = new Map();
|
|
166
|
-
constructor(
|
|
171
|
+
constructor(
|
|
172
|
+
// The host application. Only used to obtain the default Modbus backend via
|
|
173
|
+
// `energyApp.useModbus()`. May be omitted (`undefined`) when a `modbusFactory` is supplied.
|
|
174
|
+
energyApp,
|
|
167
175
|
// Opt-in pacing (default off). Passed straight through to `useModbus().connect()`, which
|
|
168
176
|
// serializes frames (`noParallelRequests`) and spaces them (`waitBetweenMessagesMs`) on the
|
|
169
177
|
// connection. An empty object leaves both undefined so the SDK applies its own defaults.
|
|
170
178
|
pacing = {},
|
|
171
179
|
// Per-operation timeouts + half-open detection. Merged with DEFAULT_MODBUS_RESILIENCE_CONFIG
|
|
172
180
|
// so omitted fields keep their defaults.
|
|
173
|
-
resilience = {}
|
|
181
|
+
resilience = {},
|
|
182
|
+
// Optional custom Modbus backend. When provided, its `connect()` is used to open every
|
|
183
|
+
// per-unit connection instead of `energyApp.useModbus()` — letting callers plug in their own
|
|
184
|
+
// Modbus implementation (a mock for tests, or an alternative transport). All the resilience
|
|
185
|
+
// machinery (per-unit connect/reconnect, TLS, pacing, timeouts) is driven through this
|
|
186
|
+
// factory, which is why an already-connected instance would not do: the client re-opens
|
|
187
|
+
// connections on its own schedule and needs a factory to call.
|
|
188
|
+
modbusFactory) {
|
|
174
189
|
this.energyApp = energyApp;
|
|
175
190
|
this.pacing = pacing;
|
|
191
|
+
this.modbusFactory = modbusFactory;
|
|
192
|
+
if (!energyApp && !modbusFactory) {
|
|
193
|
+
throw new Error("SunspecModbusClient requires either an EnergyApp or a modbusFactory");
|
|
194
|
+
}
|
|
176
195
|
this.connectionHealth = new EnergyAppModbusConnectionHealth();
|
|
177
196
|
this.modbusDataTypeConverter = new EnergyAppModbusDataTypeConverter();
|
|
178
197
|
this.resilience = { ...DEFAULT_MODBUS_RESILIENCE_CONFIG, ...resilience };
|
|
179
198
|
}
|
|
199
|
+
/**
|
|
200
|
+
* The Modbus backend used to open per-unit connections: the caller-supplied {@link modbusFactory}
|
|
201
|
+
* if one was given, otherwise the host application's default (`energyApp.useModbus()`). The
|
|
202
|
+
* constructor guarantees at least one of the two is present.
|
|
203
|
+
*/
|
|
204
|
+
useModbus() {
|
|
205
|
+
if (this.modbusFactory)
|
|
206
|
+
return this.modbusFactory;
|
|
207
|
+
if (!this.energyApp) {
|
|
208
|
+
throw new Error("SunspecModbusClient has no Modbus backend: neither energyApp nor modbusFactory is set");
|
|
209
|
+
}
|
|
210
|
+
return this.energyApp.useModbus();
|
|
211
|
+
}
|
|
180
212
|
/**
|
|
181
213
|
* Connect to a Modbus device unit. Multiple unit IDs on the same network device share this
|
|
182
214
|
* client; calling connect() with different unit IDs adds each as a separately-managed unit.
|
|
@@ -467,7 +499,7 @@ export class SunspecModbusClient {
|
|
|
467
499
|
// Enforce the connect budget ourselves in addition to forwarding it as the transport's
|
|
468
500
|
// `timeout`: the transport applies it to the TCP handshake, but a sleeping/half-open peer
|
|
469
501
|
// can still leave the returned promise pending — bound it independently.
|
|
470
|
-
candidate = await this.withTimeout('connect', unitId, overallBudgetMs, () => this.
|
|
502
|
+
candidate = await this.withTimeout('connect', unitId, overallBudgetMs, () => this.useModbus().connect({
|
|
471
503
|
host: hosts,
|
|
472
504
|
port,
|
|
473
505
|
unitId,
|
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.104",
|
|
4
4
|
"description": "enyo Energy Sunspec SDK",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@enyo-energy/appliance-calibration": "0.0.3",
|
|
44
|
-
"@enyo-energy/energy-app-sdk": "^0.0.
|
|
44
|
+
"@enyo-energy/energy-app-sdk": "^0.0.188"
|
|
45
45
|
},
|
|
46
46
|
"volta": {
|
|
47
47
|
"node": "22.17.0"
|