@enyo-energy/sunspec-sdk 0.0.103 → 0.0.105
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-battery-schedule-handler.cjs +76 -28
- package/dist/cjs/sunspec-battery-schedule-handler.d.cts +33 -6
- package/dist/cjs/sunspec-devices.cjs +36 -20
- package/dist/cjs/sunspec-devices.d.cts +15 -0
- package/dist/cjs/sunspec-interfaces.cjs +9 -2
- package/dist/cjs/sunspec-interfaces.d.cts +11 -4
- package/dist/cjs/sunspec-modbus-client.cjs +64 -19
- 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-battery-schedule-handler.d.ts +33 -6
- package/dist/sunspec-battery-schedule-handler.js +76 -28
- package/dist/sunspec-devices.d.ts +15 -0
- package/dist/sunspec-devices.js +35 -20
- package/dist/sunspec-interfaces.d.ts +11 -4
- package/dist/sunspec-interfaces.js +9 -2
- package/dist/sunspec-modbus-client.d.ts +17 -6
- package/dist/sunspec-modbus-client.js +64 -19
- 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';
|
|
@@ -22,8 +22,9 @@ class SunspecBatteryScheduleHandler extends storage_schedule_handler_js_1.Storag
|
|
|
22
22
|
* Sticky pre-schedule snapshot, captured once on the first `onInit` and
|
|
23
23
|
* held across every subsequent schedule-to-schedule replacement. Cleared
|
|
24
24
|
* only when a real rollback fires (`mode: auto` from the data bus or
|
|
25
|
-
* `dispose`).
|
|
26
|
-
*
|
|
25
|
+
* `dispose`). Supplies the `wChaMax` denominator for the percentage math
|
|
26
|
+
* and the charge source to hand back on release — taken from the *original*
|
|
27
|
+
* pre-schedule read rather than the register set the device happens to be
|
|
27
28
|
* in at the moment of replacement.
|
|
28
29
|
*/
|
|
29
30
|
originalBaseline;
|
|
@@ -35,6 +36,11 @@ class SunspecBatteryScheduleHandler extends storage_schedule_handler_js_1.Storag
|
|
|
35
36
|
* write the snapshot back during a replacement.
|
|
36
37
|
*/
|
|
37
38
|
suppressNextRollbackWrite = false;
|
|
39
|
+
/**
|
|
40
|
+
* Mirrors the base class's private `disposed` flag so the auto-release
|
|
41
|
+
* path can tell "released and gone" from "released and still live".
|
|
42
|
+
*/
|
|
43
|
+
isDisposed = false;
|
|
38
44
|
constructor(opts) {
|
|
39
45
|
super(opts);
|
|
40
46
|
this.sunspecClient = opts.sunspecClient;
|
|
@@ -52,11 +58,15 @@ class SunspecBatteryScheduleHandler extends storage_schedule_handler_js_1.Storag
|
|
|
52
58
|
* normally there.
|
|
53
59
|
*/
|
|
54
60
|
async applyMessage(msg) {
|
|
55
|
-
|
|
61
|
+
const isForThisAppliance = msg.applianceId === this.applianceIdForLog;
|
|
62
|
+
if (isForThisAppliance
|
|
56
63
|
&& msg.data.mode === enyo_data_bus_value_js_1.EnyoStorageScheduleModeEnum.Schedule
|
|
57
64
|
&& this.getActiveEntry() !== undefined) {
|
|
58
65
|
this.suppressNextRollbackWrite = true;
|
|
59
66
|
}
|
|
67
|
+
// Captured before the base class runs: with a schedule active, its
|
|
68
|
+
// `doReleaseSchedule → onRollback` already performs the auto release.
|
|
69
|
+
const hadActiveSchedule = this.getActiveEntry() !== undefined;
|
|
60
70
|
try {
|
|
61
71
|
await super.applyMessage(msg);
|
|
62
72
|
}
|
|
@@ -66,6 +76,30 @@ class SunspecBatteryScheduleHandler extends storage_schedule_handler_js_1.Storag
|
|
|
66
76
|
// otherwise stay set and silently swallow the next real rollback.
|
|
67
77
|
this.suppressNextRollbackWrite = false;
|
|
68
78
|
}
|
|
79
|
+
if (isForThisAppliance
|
|
80
|
+
&& msg.data.mode === enyo_data_bus_value_js_1.EnyoStorageScheduleModeEnum.Auto
|
|
81
|
+
&& !hadActiveSchedule
|
|
82
|
+
&& !this.isDisposed) {
|
|
83
|
+
// `mode: auto` with no schedule running is a no-op in the base
|
|
84
|
+
// library (`doReleaseSchedule` returns early when idle), so nothing
|
|
85
|
+
// would reach the device. That is not harmless: Model 124 setpoints
|
|
86
|
+
// latch — an interrupted schedule (modbus drop, package restart)
|
|
87
|
+
// leaves StorCtl_Mod and InWRte set, and with InOutWRte_RvrtTms = 0
|
|
88
|
+
// the inverter never reverts on its own. Seen in production as a
|
|
89
|
+
// battery pinned at ~2 % charge rate for hours while the energy
|
|
90
|
+
// manager sent `auto` every 40 s and got an `Accepted` back.
|
|
91
|
+
//
|
|
92
|
+
// Writing the release unconditionally is safe because it is
|
|
93
|
+
// idempotent: it is exactly the register set that means
|
|
94
|
+
// "no external control".
|
|
95
|
+
await this.releaseToAuto().catch(err => {
|
|
96
|
+
console.error(`SunspecBatteryScheduleHandler ${this.applianceIdForLog}: auto release failed: ${err}`);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
dispose() {
|
|
101
|
+
this.isDisposed = true;
|
|
102
|
+
super.dispose();
|
|
69
103
|
}
|
|
70
104
|
async onInit(_active) {
|
|
71
105
|
if (this.originalBaseline !== undefined) {
|
|
@@ -104,7 +138,7 @@ class SunspecBatteryScheduleHandler extends storage_schedule_handler_js_1.Storag
|
|
|
104
138
|
console.error(`SunspecBatteryScheduleHandler ${this.applianceIdForLog}: onChange write failed for entry ${active.indexInSchedule}: ${err}`);
|
|
105
139
|
});
|
|
106
140
|
}
|
|
107
|
-
onRollback(
|
|
141
|
+
onRollback(_registers) {
|
|
108
142
|
if (this.suppressNextRollbackWrite) {
|
|
109
143
|
// Schedule-to-schedule replacement. Do NOT write the snapshot
|
|
110
144
|
// back; the new schedule's first `onChange` will own the
|
|
@@ -122,19 +156,41 @@ class SunspecBatteryScheduleHandler extends storage_schedule_handler_js_1.Storag
|
|
|
122
156
|
// that follows — so a concurrent `onInit` queued behind this one
|
|
123
157
|
// (impossible today, but defensive) cannot observe a half-cleared
|
|
124
158
|
// state.
|
|
159
|
+
const chargeSource = this.originalBaseline?.chaGriSet;
|
|
125
160
|
this.originalBaseline = undefined;
|
|
126
161
|
this.installedWChaMax = undefined;
|
|
127
|
-
void (
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
162
|
+
void this.releaseToAuto(chargeSource).catch(err => {
|
|
163
|
+
console.error(`SunspecBatteryScheduleHandler ${this.applianceIdForLog}: onRollback failed: ${err}`);
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Hand the battery back to its own logic: clear StorCtl_Mod and open both
|
|
168
|
+
* rate setpoints back up to 100 %.
|
|
169
|
+
*
|
|
170
|
+
* This deliberately does *not* replay the snapshot captured at `onInit`.
|
|
171
|
+
* That snapshot is whatever the registers happened to hold when the
|
|
172
|
+
* schedule started — and if a previous schedule had already stranded a
|
|
173
|
+
* limit there, restoring it re-latches the limit and the battery can never
|
|
174
|
+
* climb out. The only correct end state for "we are no longer controlling
|
|
175
|
+
* this battery" is the one written here, and it is the same set of
|
|
176
|
+
* registers a schedule entry touches, so nothing else is left dangling.
|
|
177
|
+
*
|
|
178
|
+
* `wChaMax` is not restored because a schedule never writes it — the
|
|
179
|
+
* percentage math divides against it instead.
|
|
180
|
+
*
|
|
181
|
+
* @param chargeSource - `chaGriSet` to restore; falls back to PV-only,
|
|
182
|
+
* since grid charging is only ever switched on by a Charge entry.
|
|
183
|
+
*/
|
|
184
|
+
async releaseToAuto(chargeSource) {
|
|
185
|
+
await this.sunspecClient.writeBatteryControls(this.unitId, {
|
|
186
|
+
storCtlMod: sunspec_interfaces_js_1.SunspecStorageControlMode.NONE,
|
|
187
|
+
chaGriSet: chargeSource ?? sunspec_interfaces_js_1.SunspecChargeSource.PV,
|
|
188
|
+
inWRte: 100,
|
|
189
|
+
outWRte: 100,
|
|
190
|
+
});
|
|
191
|
+
await this.getSnapshotService()?.recordModification([
|
|
192
|
+
'storCtlMod', 'chaGriSet', 'inWRte', 'outWRte',
|
|
193
|
+
]);
|
|
138
194
|
}
|
|
139
195
|
onError(err) {
|
|
140
196
|
this.onErrorCallback?.(err);
|
|
@@ -142,19 +198,11 @@ class SunspecBatteryScheduleHandler extends storage_schedule_handler_js_1.Storag
|
|
|
142
198
|
async applyEntry(active) {
|
|
143
199
|
const { direction, powerW } = active.entry;
|
|
144
200
|
if (direction === enyo_data_bus_value_js_1.EnyoStorageScheduleDirectionEnum.Idle) {
|
|
145
|
-
// Idle entry: hand the battery back to its own autonomous logic
|
|
146
|
-
//
|
|
147
|
-
//
|
|
148
|
-
//
|
|
149
|
-
|
|
150
|
-
const baselineRegisters = this.originalBaseline;
|
|
151
|
-
if (!baselineRegisters) {
|
|
152
|
-
throw new Error(`no usable baseline for idle entry (originalBaseline=undefined)`);
|
|
153
|
-
}
|
|
154
|
-
await this.sunspecClient.writeBatteryControls(this.unitId, baselineRegisters);
|
|
155
|
-
await this.getSnapshotService()?.recordModification([
|
|
156
|
-
'storCtlMod', 'chaGriSet', 'wChaMax', 'inWRte', 'outWRte',
|
|
157
|
-
]);
|
|
201
|
+
// Idle entry: hand the battery back to its own autonomous logic —
|
|
202
|
+
// the same release the `mode: auto` path performs. The schedule
|
|
203
|
+
// itself keeps running, so a subsequent Charge / Discharge entry
|
|
204
|
+
// can resume external control.
|
|
205
|
+
await this.releaseToAuto(this.originalBaseline?.chaGriSet);
|
|
158
206
|
return;
|
|
159
207
|
}
|
|
160
208
|
const baseline = this.installedWChaMax;
|
|
@@ -19,9 +19,10 @@ export interface BatteryControlsClient {
|
|
|
19
19
|
*/
|
|
20
20
|
export type BatteryModificationRecorder = Pick<SnapshotService<SunspecBatteryControls>, 'recordModification'>;
|
|
21
21
|
/**
|
|
22
|
-
* Snapshot of the writable Model 124 registers taken at schedule install time
|
|
23
|
-
*
|
|
24
|
-
*
|
|
22
|
+
* Snapshot of the writable Model 124 registers taken at schedule install time.
|
|
23
|
+
* The base library hands it back to `onRollback`, but the release path does not
|
|
24
|
+
* replay it — see `releaseToAuto`. It survives as the record of the installed
|
|
25
|
+
* `wChaMax` and the pre-schedule charge source. JSON-serialisable because
|
|
25
26
|
* `EnergyAppStorage` round-trips it.
|
|
26
27
|
*
|
|
27
28
|
* The snapshotted `wChaMax` doubles as the denominator for the `inWRte` /
|
|
@@ -63,8 +64,9 @@ export declare class SunspecBatteryScheduleHandler extends StorageScheduleHandle
|
|
|
63
64
|
* Sticky pre-schedule snapshot, captured once on the first `onInit` and
|
|
64
65
|
* held across every subsequent schedule-to-schedule replacement. Cleared
|
|
65
66
|
* only when a real rollback fires (`mode: auto` from the data bus or
|
|
66
|
-
* `dispose`).
|
|
67
|
-
*
|
|
67
|
+
* `dispose`). Supplies the `wChaMax` denominator for the percentage math
|
|
68
|
+
* and the charge source to hand back on release — taken from the *original*
|
|
69
|
+
* pre-schedule read rather than the register set the device happens to be
|
|
68
70
|
* in at the moment of replacement.
|
|
69
71
|
*/
|
|
70
72
|
private originalBaseline?;
|
|
@@ -76,6 +78,11 @@ export declare class SunspecBatteryScheduleHandler extends StorageScheduleHandle
|
|
|
76
78
|
* write the snapshot back during a replacement.
|
|
77
79
|
*/
|
|
78
80
|
private suppressNextRollbackWrite;
|
|
81
|
+
/**
|
|
82
|
+
* Mirrors the base class's private `disposed` flag so the auto-release
|
|
83
|
+
* path can tell "released and gone" from "released and still live".
|
|
84
|
+
*/
|
|
85
|
+
private isDisposed;
|
|
79
86
|
constructor(opts: SunspecBatteryScheduleHandlerOptions);
|
|
80
87
|
/**
|
|
81
88
|
* Override the base-class data-bus router so a schedule-to-schedule
|
|
@@ -86,9 +93,29 @@ export declare class SunspecBatteryScheduleHandler extends StorageScheduleHandle
|
|
|
86
93
|
* normally there.
|
|
87
94
|
*/
|
|
88
95
|
applyMessage(msg: EnyoDataBusSetStorageScheduleV1): Promise<void>;
|
|
96
|
+
dispose(): void;
|
|
89
97
|
protected onInit(_active: ActiveStorageScheduleEntry): Promise<SunspecScheduleRegisters>;
|
|
90
98
|
protected onChange(active: ActiveStorageScheduleEntry, _previous: ActiveStorageScheduleEntry | undefined): void;
|
|
91
|
-
protected onRollback(
|
|
99
|
+
protected onRollback(_registers: SunspecScheduleRegisters): void;
|
|
100
|
+
/**
|
|
101
|
+
* Hand the battery back to its own logic: clear StorCtl_Mod and open both
|
|
102
|
+
* rate setpoints back up to 100 %.
|
|
103
|
+
*
|
|
104
|
+
* This deliberately does *not* replay the snapshot captured at `onInit`.
|
|
105
|
+
* That snapshot is whatever the registers happened to hold when the
|
|
106
|
+
* schedule started — and if a previous schedule had already stranded a
|
|
107
|
+
* limit there, restoring it re-latches the limit and the battery can never
|
|
108
|
+
* climb out. The only correct end state for "we are no longer controlling
|
|
109
|
+
* this battery" is the one written here, and it is the same set of
|
|
110
|
+
* registers a schedule entry touches, so nothing else is left dangling.
|
|
111
|
+
*
|
|
112
|
+
* `wChaMax` is not restored because a schedule never writes it — the
|
|
113
|
+
* percentage math divides against it instead.
|
|
114
|
+
*
|
|
115
|
+
* @param chargeSource - `chaGriSet` to restore; falls back to PV-only,
|
|
116
|
+
* since grid charging is only ever switched on by a Charge entry.
|
|
117
|
+
*/
|
|
118
|
+
private releaseToAuto;
|
|
92
119
|
protected onError(err: Error): void;
|
|
93
120
|
private applyEntry;
|
|
94
121
|
}
|
|
@@ -5,6 +5,7 @@ exports.detectFeaturesFromRegisters = detectFeaturesFromRegisters;
|
|
|
5
5
|
exports.sumMpptBatteryPowerW = sumMpptBatteryPowerW;
|
|
6
6
|
exports.selectLiveBatteryPowerW = selectLiveBatteryPowerW;
|
|
7
7
|
exports.filterFeaturesByCalibrationResult = filterFeaturesByCalibrationResult;
|
|
8
|
+
exports.deriveStorageMode = deriveStorageMode;
|
|
8
9
|
const sunspec_interfaces_js_1 = require("./sunspec-interfaces.cjs");
|
|
9
10
|
const node_crypto_1 = require("node:crypto");
|
|
10
11
|
const enyo_appliance_js_1 = require("@enyo-energy/energy-app-sdk/dist/types/enyo-appliance.js");
|
|
@@ -1434,6 +1435,37 @@ function filterFeaturesByCalibrationResult(detected, result, controllable) {
|
|
|
1434
1435
|
/**
|
|
1435
1436
|
* Sunspec Battery implementation
|
|
1436
1437
|
*/
|
|
1438
|
+
/**
|
|
1439
|
+
* Derive the storage mode from Model 124's StorCtl_Mod bitfield.
|
|
1440
|
+
*
|
|
1441
|
+
* The bits mark *which rate setpoint the device is enforcing* — bit 0 for
|
|
1442
|
+
* InWRte, bit 1 for OutWRte — not which direction is permitted. So no bit set
|
|
1443
|
+
* is the autonomous mode, and both bits set is the most externally controlled
|
|
1444
|
+
* state there is, which is the opposite of what the naming suggests. Reading
|
|
1445
|
+
* them the other way round meant a battery running its own logic reported
|
|
1446
|
+
* `holding`, a fully clamped one reported `auto`, and no appliance could ever
|
|
1447
|
+
* report that it had been handed back to itself.
|
|
1448
|
+
*
|
|
1449
|
+
* @param controls - Model 124 control registers, or null when unreadable.
|
|
1450
|
+
* @returns The mode, or null when StorCtl_Mod could not be read.
|
|
1451
|
+
*/
|
|
1452
|
+
function deriveStorageMode(controls) {
|
|
1453
|
+
if (!controls || controls.storCtlMod === undefined) {
|
|
1454
|
+
return null;
|
|
1455
|
+
}
|
|
1456
|
+
const chargeLimited = (controls.storCtlMod & 0x0001) !== 0;
|
|
1457
|
+
const dischargeLimited = (controls.storCtlMod & 0x0002) !== 0;
|
|
1458
|
+
if (!chargeLimited && !dischargeLimited) {
|
|
1459
|
+
return sunspec_interfaces_js_1.SunspecStorageMode.AUTO;
|
|
1460
|
+
}
|
|
1461
|
+
// Both directions clamped to zero — the battery is parked, not idling.
|
|
1462
|
+
if (chargeLimited && dischargeLimited && controls.inWRte === 0 && controls.outWRte === 0) {
|
|
1463
|
+
return sunspec_interfaces_js_1.SunspecStorageMode.HOLDING;
|
|
1464
|
+
}
|
|
1465
|
+
return dischargeLimited && !chargeLimited
|
|
1466
|
+
? sunspec_interfaces_js_1.SunspecStorageMode.DISCHARGE
|
|
1467
|
+
: sunspec_interfaces_js_1.SunspecStorageMode.CHARGE;
|
|
1468
|
+
}
|
|
1437
1469
|
class SunspecBattery extends BaseSunspecDevice {
|
|
1438
1470
|
featureMode;
|
|
1439
1471
|
capabilities;
|
|
@@ -1588,7 +1620,7 @@ class SunspecBattery extends BaseSunspecDevice {
|
|
|
1588
1620
|
battery: {
|
|
1589
1621
|
connectedToApplianceId: inverterApplianceId,
|
|
1590
1622
|
storageMode: this.mapToEnyoStorageMode(storageMode),
|
|
1591
|
-
maxChargingPowerW: batteryData
|
|
1623
|
+
maxChargingPowerW: batteryData?.wChaMax,
|
|
1592
1624
|
maxDischargePowerW: batteryData && batteryData.wChaMax !== undefined && batteryData.outWRte !== undefined ? batteryData.wChaMax * batteryData.outWRte : undefined,
|
|
1593
1625
|
activeChargeLimitW: activeChargeLimitW,
|
|
1594
1626
|
activeDischargeLimitW: activeDischargeLimitW,
|
|
@@ -1616,7 +1648,7 @@ class SunspecBattery extends BaseSunspecDevice {
|
|
|
1616
1648
|
...existingAppliance?.battery,
|
|
1617
1649
|
connectedToApplianceId: inverterApplianceId,
|
|
1618
1650
|
storageMode: this.mapToEnyoStorageMode(storageMode),
|
|
1619
|
-
maxChargingPowerW: batteryData
|
|
1651
|
+
maxChargingPowerW: batteryData?.wChaMax,
|
|
1620
1652
|
maxDischargePowerW: batteryData && batteryData.wChaMax !== undefined && batteryData.outWRte !== undefined ? batteryData.wChaMax * batteryData.outWRte : undefined,
|
|
1621
1653
|
activeChargeLimitW: activeChargeLimitW,
|
|
1622
1654
|
activeDischargeLimitW: activeDischargeLimitW,
|
|
@@ -1775,7 +1807,7 @@ class SunspecBattery extends BaseSunspecDevice {
|
|
|
1775
1807
|
battery: {
|
|
1776
1808
|
...appliance?.battery,
|
|
1777
1809
|
storageMode: this.mapToEnyoStorageMode(storageMode),
|
|
1778
|
-
maxChargingPowerW: batteryData
|
|
1810
|
+
maxChargingPowerW: batteryData?.wChaMax,
|
|
1779
1811
|
maxDischargePowerW: batteryData && batteryData.wChaMax !== undefined && batteryData.outWRte !== undefined ? batteryData.wChaMax * batteryData.outWRte : undefined,
|
|
1780
1812
|
activeChargeLimitW: this.computeActiveChargeLimitW(batteryData),
|
|
1781
1813
|
activeDischargeLimitW: this.computeActiveDischargeLimitW(batteryData),
|
|
@@ -1979,23 +2011,7 @@ class SunspecBattery extends BaseSunspecDevice {
|
|
|
1979
2011
|
return (batteryData.wChaMax * batteryData.outWRte) / 100;
|
|
1980
2012
|
}
|
|
1981
2013
|
determineStorageMode(controls) {
|
|
1982
|
-
|
|
1983
|
-
return null;
|
|
1984
|
-
}
|
|
1985
|
-
const chargeEnabled = (controls.storCtlMod & 0x0001) !== 0;
|
|
1986
|
-
const dischargeEnabled = (controls.storCtlMod & 0x0002) !== 0;
|
|
1987
|
-
if (chargeEnabled && dischargeEnabled) {
|
|
1988
|
-
return sunspec_interfaces_js_1.SunspecStorageMode.AUTO;
|
|
1989
|
-
}
|
|
1990
|
-
else if (chargeEnabled && !dischargeEnabled) {
|
|
1991
|
-
return sunspec_interfaces_js_1.SunspecStorageMode.CHARGE;
|
|
1992
|
-
}
|
|
1993
|
-
else if (!chargeEnabled && dischargeEnabled) {
|
|
1994
|
-
return sunspec_interfaces_js_1.SunspecStorageMode.DISCHARGE;
|
|
1995
|
-
}
|
|
1996
|
-
else {
|
|
1997
|
-
return sunspec_interfaces_js_1.SunspecStorageMode.HOLDING;
|
|
1998
|
-
}
|
|
2014
|
+
return deriveStorageMode(controls);
|
|
1999
2015
|
}
|
|
2000
2016
|
/**
|
|
2001
2017
|
* Check if grid charging is enabled
|
|
@@ -510,6 +510,21 @@ export declare function filterFeaturesByCalibrationResult(detected: EnyoBatteryF
|
|
|
510
510
|
/**
|
|
511
511
|
* Sunspec Battery implementation
|
|
512
512
|
*/
|
|
513
|
+
/**
|
|
514
|
+
* Derive the storage mode from Model 124's StorCtl_Mod bitfield.
|
|
515
|
+
*
|
|
516
|
+
* The bits mark *which rate setpoint the device is enforcing* — bit 0 for
|
|
517
|
+
* InWRte, bit 1 for OutWRte — not which direction is permitted. So no bit set
|
|
518
|
+
* is the autonomous mode, and both bits set is the most externally controlled
|
|
519
|
+
* state there is, which is the opposite of what the naming suggests. Reading
|
|
520
|
+
* them the other way round meant a battery running its own logic reported
|
|
521
|
+
* `holding`, a fully clamped one reported `auto`, and no appliance could ever
|
|
522
|
+
* report that it had been handed back to itself.
|
|
523
|
+
*
|
|
524
|
+
* @param controls - Model 124 control registers, or null when unreadable.
|
|
525
|
+
* @returns The mode, or null when StorCtl_Mod could not be read.
|
|
526
|
+
*/
|
|
527
|
+
export declare function deriveStorageMode(controls: SunspecBatteryControls | null): SunspecStorageMode | null;
|
|
513
528
|
export declare class SunspecBattery extends BaseSunspecDevice {
|
|
514
529
|
private readonly featureMode;
|
|
515
530
|
private readonly capabilities;
|
|
@@ -216,8 +216,15 @@ var SunspecBatteryEvent1;
|
|
|
216
216
|
*/
|
|
217
217
|
var SunspecStorageControlMode;
|
|
218
218
|
(function (SunspecStorageControlMode) {
|
|
219
|
+
/**
|
|
220
|
+
* No bit set — no external setpoint is active and the battery runs its own
|
|
221
|
+
* autonomous logic. This, not `CHARGE | DISCHARGE`, is what "auto" means on
|
|
222
|
+
* Model 124: the bits say *which rate setpoint is being enforced*, so
|
|
223
|
+
* clearing them is what hands control back to the device.
|
|
224
|
+
*/
|
|
225
|
+
SunspecStorageControlMode[SunspecStorageControlMode["NONE"] = 0] = "NONE";
|
|
219
226
|
SunspecStorageControlMode[SunspecStorageControlMode["CHARGE"] = 1] = "CHARGE";
|
|
220
|
-
SunspecStorageControlMode[SunspecStorageControlMode["DISCHARGE"] = 2] = "DISCHARGE"; // Bit 1 -
|
|
227
|
+
SunspecStorageControlMode[SunspecStorageControlMode["DISCHARGE"] = 2] = "DISCHARGE"; // Bit 1 - the OutWRte discharge-rate setpoint is active
|
|
221
228
|
// Note: Higher bits may control other modes like autonomous/self-consumption
|
|
222
229
|
// but these are device-specific and not standardized in SunSpec
|
|
223
230
|
})(SunspecStorageControlMode || (exports.SunspecStorageControlMode = SunspecStorageControlMode = {}));
|
|
@@ -262,7 +269,7 @@ var SunspecStorageMode;
|
|
|
262
269
|
SunspecStorageMode["CHARGE"] = "charge";
|
|
263
270
|
SunspecStorageMode["DISCHARGE"] = "discharge";
|
|
264
271
|
SunspecStorageMode["HOLDING"] = "holding";
|
|
265
|
-
SunspecStorageMode["AUTO"] = "auto"; //
|
|
272
|
+
SunspecStorageMode["AUTO"] = "auto"; // No external setpoint active (StorCtl_Mod = 0)
|
|
266
273
|
})(SunspecStorageMode || (exports.SunspecStorageMode = SunspecStorageMode = {}));
|
|
267
274
|
/**
|
|
268
275
|
* Battery control structure for writing to Model 124
|
|
@@ -503,7 +503,14 @@ export declare enum SunspecBatteryEvent1 {
|
|
|
503
503
|
* - To ensure external control works properly, the appropriate bits must be set
|
|
504
504
|
*/
|
|
505
505
|
export declare enum SunspecStorageControlMode {
|
|
506
|
-
|
|
506
|
+
/**
|
|
507
|
+
* No bit set — no external setpoint is active and the battery runs its own
|
|
508
|
+
* autonomous logic. This, not `CHARGE | DISCHARGE`, is what "auto" means on
|
|
509
|
+
* Model 124: the bits say *which rate setpoint is being enforced*, so
|
|
510
|
+
* clearing them is what hands control back to the device.
|
|
511
|
+
*/
|
|
512
|
+
NONE = 0,
|
|
513
|
+
CHARGE = 1,// Bit 0 - the InWRte charge-rate setpoint is active
|
|
507
514
|
DISCHARGE = 2
|
|
508
515
|
}
|
|
509
516
|
/**
|
|
@@ -778,9 +785,9 @@ export declare enum SunspecChargeSource {
|
|
|
778
785
|
* Storage modes for simplified battery control
|
|
779
786
|
*/
|
|
780
787
|
export declare enum SunspecStorageMode {
|
|
781
|
-
CHARGE = "charge",//
|
|
782
|
-
DISCHARGE = "discharge",//
|
|
783
|
-
HOLDING = "holding",//
|
|
788
|
+
CHARGE = "charge",// Charging is externally rate-limited via InWRte
|
|
789
|
+
DISCHARGE = "discharge",// Discharging is externally rate-limited via OutWRte
|
|
790
|
+
HOLDING = "holding",// Both directions clamped to 0 % — battery is parked
|
|
784
791
|
AUTO = "auto"
|
|
785
792
|
}
|
|
786
793
|
/**
|
|
@@ -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,
|
|
@@ -2245,30 +2277,43 @@ class SunspecModbusClient {
|
|
|
2245
2277
|
* Set battery storage mode (simplified interface)
|
|
2246
2278
|
*/
|
|
2247
2279
|
async setStorageMode(unitId, mode) {
|
|
2248
|
-
|
|
2280
|
+
// StorCtl_Mod says *which rate setpoint is enforced*, not what the
|
|
2281
|
+
// battery is allowed to do — so releasing control means clearing the
|
|
2282
|
+
// bits, and the rate registers have to be written alongside them.
|
|
2283
|
+
// Setting the bits without touching InWRte/OutWRte would enforce
|
|
2284
|
+
// whatever percentage was left behind by an earlier command.
|
|
2285
|
+
let controls;
|
|
2249
2286
|
switch (mode) {
|
|
2250
2287
|
case sunspec_interfaces_js_1.SunspecStorageMode.CHARGE:
|
|
2251
|
-
//
|
|
2252
|
-
|
|
2288
|
+
// Enforce the charge-rate setpoint only (bit 0 = 1, bit 1 = 0).
|
|
2289
|
+
controls = { storCtlMod: sunspec_interfaces_js_1.SunspecStorageControlMode.CHARGE };
|
|
2253
2290
|
break;
|
|
2254
2291
|
case sunspec_interfaces_js_1.SunspecStorageMode.DISCHARGE:
|
|
2255
|
-
//
|
|
2256
|
-
|
|
2292
|
+
// Enforce the discharge-rate setpoint only (bit 0 = 0, bit 1 = 1).
|
|
2293
|
+
controls = { storCtlMod: sunspec_interfaces_js_1.SunspecStorageControlMode.DISCHARGE };
|
|
2257
2294
|
break;
|
|
2258
2295
|
case sunspec_interfaces_js_1.SunspecStorageMode.HOLDING:
|
|
2259
|
-
//
|
|
2260
|
-
|
|
2296
|
+
// Park the battery: both setpoints enforced, both clamped to 0 %.
|
|
2297
|
+
controls = {
|
|
2298
|
+
storCtlMod: sunspec_interfaces_js_1.SunspecStorageControlMode.CHARGE | sunspec_interfaces_js_1.SunspecStorageControlMode.DISCHARGE,
|
|
2299
|
+
inWRte: 0,
|
|
2300
|
+
outWRte: 0,
|
|
2301
|
+
};
|
|
2261
2302
|
break;
|
|
2262
2303
|
case sunspec_interfaces_js_1.SunspecStorageMode.AUTO:
|
|
2263
|
-
//
|
|
2264
|
-
|
|
2304
|
+
// Hand control back: no setpoint enforced, both rates wide open.
|
|
2305
|
+
controls = {
|
|
2306
|
+
storCtlMod: sunspec_interfaces_js_1.SunspecStorageControlMode.NONE,
|
|
2307
|
+
inWRte: 100,
|
|
2308
|
+
outWRte: 100,
|
|
2309
|
+
};
|
|
2265
2310
|
break;
|
|
2266
2311
|
default:
|
|
2267
2312
|
console.error(`Invalid storage mode: ${mode}`);
|
|
2268
2313
|
return false;
|
|
2269
2314
|
}
|
|
2270
|
-
console.log(`Setting storage mode to ${mode} (
|
|
2271
|
-
return this.writeBatteryControls(unitId,
|
|
2315
|
+
console.log(`Setting storage mode to ${mode} (${JSON.stringify(controls)})`);
|
|
2316
|
+
return this.writeBatteryControls(unitId, controls);
|
|
2272
2317
|
}
|
|
2273
2318
|
/**
|
|
2274
2319
|
* Enable or disable grid charging
|
|
@@ -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.105';
|
|
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