@enyo-energy/sunspec-sdk 0.0.96 → 0.0.98
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 +253 -47
- package/dist/cjs/sunspec-devices.d.cts +106 -10
- package/dist/cjs/sunspec-interfaces.cjs +23 -1
- package/dist/cjs/sunspec-interfaces.d.cts +93 -0
- package/dist/cjs/version.cjs +1 -1
- package/dist/cjs/version.d.cts +1 -1
- package/dist/sunspec-devices.d.ts +106 -10
- package/dist/sunspec-devices.js +253 -48
- package/dist/sunspec-interfaces.d.ts +93 -0
- package/dist/sunspec-interfaces.js +22 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SunspecMeter = exports.SunspecBattery = exports.SunspecInverter = exports.BaseSunspecDevice = void 0;
|
|
4
4
|
exports.detectFeaturesFromRegisters = detectFeaturesFromRegisters;
|
|
5
|
+
exports.sumMpptBatteryPowerW = sumMpptBatteryPowerW;
|
|
5
6
|
exports.selectLiveBatteryPowerW = selectLiveBatteryPowerW;
|
|
6
7
|
exports.filterFeaturesByCalibrationResult = filterFeaturesByCalibrationResult;
|
|
7
8
|
const sunspec_interfaces_js_1 = require("./sunspec-interfaces.cjs");
|
|
@@ -336,6 +337,25 @@ class BaseSunspecDevice {
|
|
|
336
337
|
}
|
|
337
338
|
}
|
|
338
339
|
exports.BaseSunspecDevice = BaseSunspecDevice;
|
|
340
|
+
/**
|
|
341
|
+
* Coerce a possibly-`undefined`, `NaN`, or `±Infinity` register value to a finite
|
|
342
|
+
* number, substituting `fallback` (default `0`) otherwise. SunSpec scale-factor
|
|
343
|
+
* decodes and IEEE-754 float models can surface `NaN`/`Infinity` for a
|
|
344
|
+
* NOT_IMPLEMENTED or corrupt register, and `typeof NaN === 'number'` /
|
|
345
|
+
* `Infinity > 0 === true` slip past naive guards — this is the single choke point
|
|
346
|
+
* that keeps every published power/voltage/energy value finite.
|
|
347
|
+
*/
|
|
348
|
+
function finiteOr(value, fallback = 0) {
|
|
349
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : fallback;
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Like {@link finiteOr} but yields `undefined` for a non-finite/absent value, for
|
|
353
|
+
* optional reading fields (voltageL2/L3, acEnergyWh, batteryPowerW) where "no
|
|
354
|
+
* reliable value" is more honest than a fabricated `0`.
|
|
355
|
+
*/
|
|
356
|
+
function finiteOrUndefined(value) {
|
|
357
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
|
|
358
|
+
}
|
|
339
359
|
/**
|
|
340
360
|
* Sunspec Inverter implementation using dynamic model discovery
|
|
341
361
|
*/
|
|
@@ -496,8 +516,13 @@ class SunspecInverter extends BaseSunspecDevice {
|
|
|
496
516
|
// to Connected. No-op unless this device is still marked offline.
|
|
497
517
|
await this.reassertConnectedIfRecovered();
|
|
498
518
|
if (inverterData) {
|
|
499
|
-
|
|
500
|
-
|
|
519
|
+
// Build the same decoupled reading a direct getInverterReading() caller
|
|
520
|
+
// would get, then wrap it in the data-bus message. The battery-adjusted
|
|
521
|
+
// power is what we publish; meterValueWh is sanitized only here on the
|
|
522
|
+
// publish path (the raw reading intentionally leaves it unguarded).
|
|
523
|
+
const batteryPowerW = await this.fetchAcCorrectionBatteryPowerW(dcStrings.length, mpptDataList);
|
|
524
|
+
const reading = this.buildInverterReading(inverterData, dcStrings, batteryPowerW);
|
|
525
|
+
console.debug(`Got PV Power from ${reading.powerOrigin}: ${reading.inverterPowerW}W (inverter total raw ${reading.inverterPowerWRaw}W, DC strings ${reading.inverterPowerWDcStrings}W)`);
|
|
501
526
|
const inverterMessage = {
|
|
502
527
|
id: (0, node_crypto_1.randomUUID)(),
|
|
503
528
|
message: enyo_data_bus_value_js_1.EnyoDataBusMessageEnum.InverterValuesUpdateV1,
|
|
@@ -508,14 +533,14 @@ class SunspecInverter extends BaseSunspecDevice {
|
|
|
508
533
|
timestampIso: timestamp.toISOString(),
|
|
509
534
|
resolution,
|
|
510
535
|
data: {
|
|
511
|
-
pvPowerW,
|
|
536
|
+
pvPowerW: reading.inverterPowerW,
|
|
512
537
|
activePowerLimitationW: inverterSettings?.WMax,
|
|
513
|
-
state:
|
|
514
|
-
voltageL1:
|
|
515
|
-
voltageL2:
|
|
516
|
-
voltageL3:
|
|
538
|
+
state: reading.state,
|
|
539
|
+
voltageL1: reading.voltageL1,
|
|
540
|
+
voltageL2: reading.voltageL2,
|
|
541
|
+
voltageL3: reading.voltageL3,
|
|
517
542
|
meterValueWh: this.sanitizeAcEnergyWh(inverterData.acEnergy, inverterData.acEnergyScaleFactor),
|
|
518
|
-
strings:
|
|
543
|
+
strings: reading.strings
|
|
519
544
|
}
|
|
520
545
|
};
|
|
521
546
|
messages.push(inverterMessage);
|
|
@@ -818,6 +843,93 @@ class SunspecInverter extends BaseSunspecDevice {
|
|
|
818
843
|
};
|
|
819
844
|
return stateMap[state] || enyo_data_bus_value_js_1.EnyoInverterStateEnum.Off;
|
|
820
845
|
}
|
|
846
|
+
/**
|
|
847
|
+
* Read a decoupled snapshot of the inverter's live telemetry — PV power (raw
|
|
848
|
+
* and battery-adjusted), its resolved origin, operating state, voltages, AC
|
|
849
|
+
* energy and per-string DC data — WITHOUT constructing or sending any data-bus
|
|
850
|
+
* message. The consumer decides whether and how to publish it.
|
|
851
|
+
*
|
|
852
|
+
* This is the clean read primitive the SDK's own polling (`readData`) is built
|
|
853
|
+
* on, so a direct caller gets exactly the same numbers the bus would carry
|
|
854
|
+
* (except `acEnergyWh`, which is returned raw here and sanitized only on the
|
|
855
|
+
* publish path — see {@link SunspecInverterReading.acEnergyWh}).
|
|
856
|
+
*
|
|
857
|
+
* Returns `null` when not connected (after a reconnect attempt) or when the
|
|
858
|
+
* device exposes no readable inverter model.
|
|
859
|
+
*/
|
|
860
|
+
async getInverterReading() {
|
|
861
|
+
try {
|
|
862
|
+
if (!this.isConnected()) {
|
|
863
|
+
await this.tryReconnect();
|
|
864
|
+
if (!this.isConnected())
|
|
865
|
+
return null;
|
|
866
|
+
}
|
|
867
|
+
const inverterData = await this.sunspecClient.readInverterData(this.unitId);
|
|
868
|
+
if (!inverterData)
|
|
869
|
+
return null;
|
|
870
|
+
const mpptDataList = await this.sunspecClient.readAllMPPTData(this.unitId);
|
|
871
|
+
const dcStrings = this.mapMPPTToStrings(mpptDataList ?? []);
|
|
872
|
+
const batteryPowerW = await this.fetchAcCorrectionBatteryPowerW(dcStrings.length, mpptDataList ?? []);
|
|
873
|
+
return this.buildInverterReading(inverterData, dcStrings, batteryPowerW);
|
|
874
|
+
}
|
|
875
|
+
catch (error) {
|
|
876
|
+
// Bullet-proof contract: a decode/comms fault never propagates to the
|
|
877
|
+
// caller — it surfaces as a null reading, same as "no readable model".
|
|
878
|
+
console.warn(`Notify_Devs: ${this.constructor.name} ${this.applianceId}: getInverterReading failed: ${error}`);
|
|
879
|
+
return null;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
/**
|
|
883
|
+
* Fetch the live battery power (signed: positive = charging, negative =
|
|
884
|
+
* discharging) that {@link inverterTotalPvPowerW} backs out of the AC fallback.
|
|
885
|
+
* Only fetched when the inverter-total fallback will actually be used — the
|
|
886
|
+
* correction is irrelevant for the MPPT-string path — so a device with working
|
|
887
|
+
* strings never pays the extra battery read. Prefers the Model 160
|
|
888
|
+
* `StCha`/`StDisCha` strings, falling back to Model 124/802. `undefined` when
|
|
889
|
+
* no correction is needed or no live battery source exists.
|
|
890
|
+
*/
|
|
891
|
+
async fetchAcCorrectionBatteryPowerW(dcStringCount, mpptDataList) {
|
|
892
|
+
if (!this.wouldUseInverterTotal(dcStringCount))
|
|
893
|
+
return undefined;
|
|
894
|
+
const mpptBatteryPowerW = sumMpptBatteryPowerW(mpptDataList);
|
|
895
|
+
if (mpptBatteryPowerW !== undefined)
|
|
896
|
+
return mpptBatteryPowerW;
|
|
897
|
+
const batteryData = await this.sunspecClient.readBatteryData(this.unitId);
|
|
898
|
+
return selectLiveBatteryPowerW(undefined, batteryData);
|
|
899
|
+
}
|
|
900
|
+
/**
|
|
901
|
+
* Assemble a {@link SunspecInverterReading} from an already-read register set.
|
|
902
|
+
* Pure (no I/O, no side effects) so both `getInverterReading` and the polling
|
|
903
|
+
* `readData` produce identical readings from a single shared code path.
|
|
904
|
+
*/
|
|
905
|
+
buildInverterReading(inverterData, dcStrings, batteryPowerW) {
|
|
906
|
+
// Independent breakdown: compute each candidate power regardless of which the
|
|
907
|
+
// configured source selects, so nothing is hidden by the selection.
|
|
908
|
+
// - inverterPowerWDcStrings: the summed MPPT DC strings (always).
|
|
909
|
+
// - inverterPowerWRaw: the inverter's own total (DCW→AC), uncorrected (always).
|
|
910
|
+
// - inverterPowerW: the auto-calc value to use — selected source,
|
|
911
|
+
// battery-adjusted (via selectPvPowerW, overridable).
|
|
912
|
+
const dcStringsW = this.sumDcStringsW(dcStrings);
|
|
913
|
+
const inverterTotal = this.inverterTotalPvPowerW(inverterData, batteryPowerW);
|
|
914
|
+
const selected = this.selectPvPowerW(dcStrings, inverterData, batteryPowerW);
|
|
915
|
+
// Final clamp: the helpers already sanitize, but re-assert the finite/≥0
|
|
916
|
+
// contract here so an override of selectPvPowerW can never leak a non-finite
|
|
917
|
+
// or negative value onto the reading.
|
|
918
|
+
return {
|
|
919
|
+
inverterPowerW: Math.max(0, finiteOr(selected.batteryAdjustedPowerW)),
|
|
920
|
+
inverterPowerWRaw: Math.max(0, finiteOr(inverterTotal.rawPowerW)),
|
|
921
|
+
inverterPowerWDcStrings: Math.max(0, finiteOr(dcStringsW)),
|
|
922
|
+
powerOrigin: selected.origin,
|
|
923
|
+
batteryPowerW: finiteOrUndefined(batteryPowerW),
|
|
924
|
+
state: this.mapOperatingState(inverterData.operatingState),
|
|
925
|
+
operatingState: finiteOrUndefined(inverterData.operatingState),
|
|
926
|
+
voltageL1: finiteOr(inverterData.voltageAN),
|
|
927
|
+
voltageL2: finiteOrUndefined(inverterData.voltageBN),
|
|
928
|
+
voltageL3: finiteOrUndefined(inverterData.voltageCN),
|
|
929
|
+
acEnergyWh: finiteOrUndefined(inverterData.acEnergy),
|
|
930
|
+
strings: Array.isArray(dcStrings) ? dcStrings : [],
|
|
931
|
+
};
|
|
932
|
+
}
|
|
821
933
|
/**
|
|
822
934
|
* PV production in Watt, sourced according to the configured `pvPowerSource`:
|
|
823
935
|
*
|
|
@@ -834,31 +946,73 @@ class SunspecInverter extends BaseSunspecDevice {
|
|
|
834
946
|
* Both inverter-total steps require a strictly positive value, so NOT_IMPLEMENTED
|
|
835
947
|
* DCW (decodes to 0) falls through to AC, and a negative AC reading (e.g. a hybrid
|
|
836
948
|
* importing from grid) falls through to 0 rather than reporting negative production.
|
|
837
|
-
*
|
|
838
|
-
*
|
|
839
|
-
*
|
|
949
|
+
*
|
|
950
|
+
* The AC fallback backs out battery power via `batteryPowerW` (signed: positive =
|
|
951
|
+
* charging, negative = discharging). A hybrid's AC total is PV − charge + discharge,
|
|
952
|
+
* so real PV = AC + batteryPowerW. Without this, a battery discharging at night while
|
|
953
|
+
* the MPPT is unreadable would be reported as phantom PV production. `batteryPowerW`
|
|
954
|
+
* is undefined when no live battery source is available, in which case AC is used
|
|
955
|
+
* uncorrected (still better than reporting 0).
|
|
956
|
+
*
|
|
957
|
+
* The returned `pvPowerW` is always clamped to ≥ 0 — PV production is non-negative by
|
|
958
|
+
* definition and must never surface as a negative value on the data bus.
|
|
840
959
|
*/
|
|
841
|
-
selectPvPowerW(dcStrings, inverterData) {
|
|
842
|
-
const
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
return {
|
|
960
|
+
selectPvPowerW(dcStrings, inverterData, batteryPowerW) {
|
|
961
|
+
const strings = Array.isArray(dcStrings) ? dcStrings : [];
|
|
962
|
+
if (!this.wouldUseInverterTotal(strings.length)) {
|
|
963
|
+
const stringSumW = this.sumDcStringsW(strings);
|
|
964
|
+
// MPPT strings are already pure PV — raw and battery-adjusted coincide.
|
|
965
|
+
return { rawPowerW: stringSumW, batteryAdjustedPowerW: stringSumW, origin: sunspec_interfaces_js_1.SunspecPvPowerOrigin.MpptStrings };
|
|
847
966
|
}
|
|
848
|
-
return this.inverterTotalPvPowerW(inverterData);
|
|
967
|
+
return this.inverterTotalPvPowerW(inverterData, batteryPowerW);
|
|
849
968
|
}
|
|
850
969
|
/**
|
|
851
|
-
*
|
|
852
|
-
*
|
|
970
|
+
* Whether {@link selectPvPowerW} will source PV from the inverter total (rather than
|
|
971
|
+
* the summed MPPT strings) for the given string count. Mppt never does; Inverter
|
|
972
|
+
* always does; Auto does only when there are no strings. Exposed so `readData` can
|
|
973
|
+
* decide up front whether it needs to fetch battery power for the AC correction.
|
|
853
974
|
*/
|
|
854
|
-
|
|
855
|
-
if (
|
|
856
|
-
return
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
975
|
+
wouldUseInverterTotal(dcStringCount) {
|
|
976
|
+
if (this.pvPowerSource === sunspec_interfaces_js_1.SunspecPvPowerSource.Mppt)
|
|
977
|
+
return false;
|
|
978
|
+
if (this.pvPowerSource === sunspec_interfaces_js_1.SunspecPvPowerSource.Inverter)
|
|
979
|
+
return true;
|
|
980
|
+
return dcStringCount === 0; // Auto
|
|
981
|
+
}
|
|
982
|
+
/**
|
|
983
|
+
* Inverter total PV power from Model 101: DCW when strictly positive, else AC (with
|
|
984
|
+
* battery power backed out) when the result is strictly positive, else 0. See
|
|
985
|
+
* {@link selectPvPowerW} for the battery correction and why positivity matters.
|
|
986
|
+
*/
|
|
987
|
+
inverterTotalPvPowerW(inverterData, batteryPowerW) {
|
|
988
|
+
// finiteOr collapses NaN/Infinity to 0 so a corrupt decode fails the `> 0`
|
|
989
|
+
// guard (Infinity would otherwise pass `typeof === 'number' && > 0`).
|
|
990
|
+
const dcW = finiteOr(inverterData.dcPower);
|
|
991
|
+
if (dcW > 0) {
|
|
992
|
+
// Inverter DC total is already pure PV — no battery correction applies.
|
|
993
|
+
return { rawPowerW: dcW, batteryAdjustedPowerW: dcW, origin: sunspec_interfaces_js_1.SunspecPvPowerOrigin.InverterDc };
|
|
994
|
+
}
|
|
995
|
+
const acW = finiteOr(inverterData.acPower);
|
|
996
|
+
if (acW > 0) {
|
|
997
|
+
// AC = PV − charge + discharge on a hybrid; batteryPowerW is signed
|
|
998
|
+
// (positive = charging, negative = discharging), so PV = AC + batteryPowerW.
|
|
999
|
+
// rawPowerW keeps the uncorrected AC total; batteryAdjustedPowerW backs the
|
|
1000
|
+
// battery out and is clamped to ≥ 0. finiteOr(batteryPowerW) guarantees the
|
|
1001
|
+
// sum stays finite even if the battery source returned garbage.
|
|
1002
|
+
const correctedW = Math.max(0, acW + finiteOr(batteryPowerW));
|
|
1003
|
+
return { rawPowerW: acW, batteryAdjustedPowerW: correctedW, origin: sunspec_interfaces_js_1.SunspecPvPowerOrigin.InverterAc };
|
|
1004
|
+
}
|
|
1005
|
+
return { rawPowerW: 0, batteryAdjustedPowerW: 0, origin: sunspec_interfaces_js_1.SunspecPvPowerOrigin.None };
|
|
1006
|
+
}
|
|
1007
|
+
/**
|
|
1008
|
+
* Summed per-string MPPT DC power (Model 160), battery strings already filtered
|
|
1009
|
+
* out by {@link mapMPPTToStrings}. finiteOr per-string so one NaN/Infinity powerW
|
|
1010
|
+
* cannot poison the sum; clamped to ≥ 0. Feeds both the MPPT-source selection and
|
|
1011
|
+
* the always-reported {@link SunspecInverterReading.inverterPowerWDcStrings}.
|
|
1012
|
+
*/
|
|
1013
|
+
sumDcStringsW(dcStrings) {
|
|
1014
|
+
const strings = Array.isArray(dcStrings) ? dcStrings : [];
|
|
1015
|
+
return Math.max(0, strings.reduce((sum, s) => sum + finiteOr(s?.powerW), 0));
|
|
862
1016
|
}
|
|
863
1017
|
/**
|
|
864
1018
|
* Map MPPT data to DC string structure for data bus
|
|
@@ -1089,6 +1243,31 @@ function detectFeaturesFromRegisters(batteryData) {
|
|
|
1089
1243
|
* Exported as a free function so the preference logic can be unit-tested
|
|
1090
1244
|
* without the full `SunspecBattery` scaffold.
|
|
1091
1245
|
*/
|
|
1246
|
+
/**
|
|
1247
|
+
* Sum battery charge/discharge power from Model 160 MPPT strings.
|
|
1248
|
+
*
|
|
1249
|
+
* - `StCha 3` → charging power (added, positive)
|
|
1250
|
+
* - `StDisCha 4` → discharging power (subtracted)
|
|
1251
|
+
*
|
|
1252
|
+
* The result follows the battery-power sign convention used across this file:
|
|
1253
|
+
* positive = charging, negative = discharging. Returns `undefined` when neither
|
|
1254
|
+
* battery string is present, so callers can fall through to another source.
|
|
1255
|
+
*
|
|
1256
|
+
* Exported as a free function so it can be unit-tested and reused by both the
|
|
1257
|
+
* SunspecBattery live-power path and the SunspecInverter AC-fallback correction.
|
|
1258
|
+
*/
|
|
1259
|
+
function sumMpptBatteryPowerW(mpptDataList) {
|
|
1260
|
+
let batteryPowerW;
|
|
1261
|
+
for (const mppt of mpptDataList) {
|
|
1262
|
+
if (mppt.stringId === 'StCha 3' && mppt.dcPower !== undefined) {
|
|
1263
|
+
batteryPowerW = (batteryPowerW ?? 0) + mppt.dcPower;
|
|
1264
|
+
}
|
|
1265
|
+
else if (mppt.stringId === 'StDisCha 4' && mppt.dcPower !== undefined) {
|
|
1266
|
+
batteryPowerW = (batteryPowerW ?? 0) - mppt.dcPower;
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
return batteryPowerW;
|
|
1270
|
+
}
|
|
1092
1271
|
function selectLiveBatteryPowerW(mpptBatteryPowerW, batteryData) {
|
|
1093
1272
|
if (mpptBatteryPowerW !== undefined) {
|
|
1094
1273
|
return mpptBatteryPowerW;
|
|
@@ -1342,6 +1521,43 @@ class SunspecBattery extends BaseSunspecDevice {
|
|
|
1342
1521
|
return undefined;
|
|
1343
1522
|
}
|
|
1344
1523
|
}
|
|
1524
|
+
/**
|
|
1525
|
+
* Read a decoupled snapshot of the battery's live telemetry — state of charge,
|
|
1526
|
+
* signed power (positive = charging, negative = discharging) and charge state —
|
|
1527
|
+
* WITHOUT constructing or sending any data-bus message. The consumer decides
|
|
1528
|
+
* whether and how to publish it.
|
|
1529
|
+
*
|
|
1530
|
+
* This is the clean read primitive the SDK's own polling (`readData`) is built
|
|
1531
|
+
* on, so a direct caller gets exactly the same numbers the bus would carry.
|
|
1532
|
+
* Returns `null` when not connected (after a reconnect attempt) or when the
|
|
1533
|
+
* device exposes no readable battery model.
|
|
1534
|
+
*/
|
|
1535
|
+
async getBatteryReading() {
|
|
1536
|
+
if (!this.isConnected()) {
|
|
1537
|
+
await this.tryReconnect();
|
|
1538
|
+
if (!this.isConnected())
|
|
1539
|
+
return null;
|
|
1540
|
+
}
|
|
1541
|
+
const batteryData = await this.sunspecClient.readBatteryData(this.unitId);
|
|
1542
|
+
if (!batteryData)
|
|
1543
|
+
return null;
|
|
1544
|
+
const mpptDataList = await this.sunspecClient.readAllMPPTData(this.unitId);
|
|
1545
|
+
const mpptBatteryPowerW = this.extractBatteryPowerFromMPPT(mpptDataList);
|
|
1546
|
+
return this.buildBatteryReading(batteryData, mpptBatteryPowerW);
|
|
1547
|
+
}
|
|
1548
|
+
/**
|
|
1549
|
+
* Assemble a {@link SunspecBatteryReading} from an already-read register set.
|
|
1550
|
+
* Pure (no I/O, no side effects) so both `getBatteryReading` and the polling
|
|
1551
|
+
* `readData` produce identical readings from a single shared code path. See
|
|
1552
|
+
* {@link selectLiveBatteryPowerW} for the power source-preference rationale.
|
|
1553
|
+
*/
|
|
1554
|
+
buildBatteryReading(batteryData, mpptBatteryPowerW) {
|
|
1555
|
+
return {
|
|
1556
|
+
soc: batteryData.soc || batteryData.chaState || 0,
|
|
1557
|
+
powerW: selectLiveBatteryPowerW(mpptBatteryPowerW, batteryData),
|
|
1558
|
+
state: this.mapToEnyoBatteryState(batteryData.chaSt),
|
|
1559
|
+
};
|
|
1560
|
+
}
|
|
1345
1561
|
/**
|
|
1346
1562
|
* Update battery data and return data bus messages
|
|
1347
1563
|
*/
|
|
@@ -1368,12 +1584,13 @@ class SunspecBattery extends BaseSunspecDevice {
|
|
|
1368
1584
|
// to Connected. No-op unless this device is still marked offline.
|
|
1369
1585
|
await this.reassertConnectedIfRecovered();
|
|
1370
1586
|
if (batteryData) {
|
|
1371
|
-
//
|
|
1372
|
-
|
|
1587
|
+
// Build the same decoupled reading a direct getBatteryReading() caller
|
|
1588
|
+
// would get, then wrap it in the data-bus message.
|
|
1589
|
+
const reading = this.buildBatteryReading(batteryData, mpptBatteryPowerW);
|
|
1373
1590
|
// Feed the calibration driver's synchronous power cache. No-op when calibration
|
|
1374
1591
|
// was never configured.
|
|
1375
|
-
if (
|
|
1376
|
-
this.calibrationDriver.updateBatteryPowerCache(
|
|
1592
|
+
if (reading.powerW !== undefined && this.calibrationDriver) {
|
|
1593
|
+
this.calibrationDriver.updateBatteryPowerCache(reading.powerW);
|
|
1377
1594
|
}
|
|
1378
1595
|
const batteryMessage = {
|
|
1379
1596
|
id: (0, node_crypto_1.randomUUID)(),
|
|
@@ -1385,9 +1602,9 @@ class SunspecBattery extends BaseSunspecDevice {
|
|
|
1385
1602
|
timestampIso: timestamp.toISOString(),
|
|
1386
1603
|
resolution,
|
|
1387
1604
|
data: {
|
|
1388
|
-
batterySoC:
|
|
1389
|
-
batteryPowerW,
|
|
1390
|
-
state:
|
|
1605
|
+
batterySoC: reading.soc,
|
|
1606
|
+
batteryPowerW: reading.powerW,
|
|
1607
|
+
state: reading.state,
|
|
1391
1608
|
}
|
|
1392
1609
|
};
|
|
1393
1610
|
messages.push(batteryMessage);
|
|
@@ -1428,18 +1645,7 @@ class SunspecBattery extends BaseSunspecDevice {
|
|
|
1428
1645
|
* StDisCha 4: Battery discharging power (negative)
|
|
1429
1646
|
*/
|
|
1430
1647
|
extractBatteryPowerFromMPPT(mpptDataList) {
|
|
1431
|
-
|
|
1432
|
-
mpptDataList.forEach((mppt) => {
|
|
1433
|
-
if (mppt.stringId === 'StCha 3' && mppt.dcPower !== undefined) {
|
|
1434
|
-
// Charging power is positive
|
|
1435
|
-
batteryPowerW = (batteryPowerW || 0) + mppt.dcPower;
|
|
1436
|
-
}
|
|
1437
|
-
else if (mppt.stringId === 'StDisCha 4' && mppt.dcPower !== undefined) {
|
|
1438
|
-
// Discharging power is negative
|
|
1439
|
-
batteryPowerW = (batteryPowerW || 0) - mppt.dcPower;
|
|
1440
|
-
}
|
|
1441
|
-
});
|
|
1442
|
-
return batteryPowerW;
|
|
1648
|
+
return sumMpptBatteryPowerW(mpptDataList);
|
|
1443
1649
|
}
|
|
1444
1650
|
/**
|
|
1445
1651
|
* Determine battery state from MPPT strings
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type IRetryConfig, type SunspecBatteryBaseData, SunspecBatteryCapability, type SunspecBatteryControls, type SunspecBatteryData, type SunspecBatteryFeatureMode, type SunspecInverterData, SunspecInverterCapability, SunspecPvPowerSource, SunspecMeterCapability, type SunspecMPPTData, SunspecStorageMode } from "./sunspec-interfaces.cjs";
|
|
1
|
+
import { type IRetryConfig, type SunspecBatteryBaseData, SunspecBatteryCapability, type SunspecBatteryControls, type SunspecBatteryData, type SunspecBatteryFeatureMode, type SunspecInverterData, SunspecInverterCapability, SunspecPvPowerSource, SunspecPvPowerOrigin, type SunspecInverterReading, type SunspecBatteryReading, SunspecMeterCapability, type SunspecMPPTData, SunspecStorageMode } from "./sunspec-interfaces.cjs";
|
|
2
2
|
import { ApplianceManager, EnergyApp } from "@enyo-energy/energy-app-sdk";
|
|
3
3
|
import { type EnyoAppliance, type EnyoApplianceErrorCode, EnyoApplianceName } from "@enyo-energy/energy-app-sdk/dist/types/enyo-appliance.js";
|
|
4
4
|
import { EnyoNetworkDevice } from "@enyo-energy/energy-app-sdk/dist/types/enyo-network-device.js";
|
|
@@ -122,6 +122,18 @@ export declare abstract class BaseSunspecDevice {
|
|
|
122
122
|
*/
|
|
123
123
|
protected buildSnapshotService<T>(onRestore: (snapshot: CalibrationSnapshot<T>, reason: RestoreReason) => Promise<void>): SnapshotService<T> | undefined;
|
|
124
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* The result of the configured PV-source selection: the picked source's raw and
|
|
127
|
+
* battery-adjusted power plus its resolved origin. Internal to the
|
|
128
|
+
* source-selection helpers (`selectPvPowerW` / `inverterTotalPvPowerW`); the
|
|
129
|
+
* public {@link SunspecInverterReading} exposes an independent breakdown built
|
|
130
|
+
* from these plus the always-computed DC-strings and inverter-total figures.
|
|
131
|
+
*/
|
|
132
|
+
type SunspecPvPower = {
|
|
133
|
+
rawPowerW: number;
|
|
134
|
+
batteryAdjustedPowerW: number;
|
|
135
|
+
origin: SunspecPvPowerOrigin;
|
|
136
|
+
};
|
|
125
137
|
/**
|
|
126
138
|
* Sunspec Inverter implementation using dynamic model discovery
|
|
127
139
|
*/
|
|
@@ -239,6 +251,37 @@ export declare class SunspecInverter extends BaseSunspecDevice {
|
|
|
239
251
|
*/
|
|
240
252
|
getPowerProductionLimit(): Promise<number | null>;
|
|
241
253
|
private mapOperatingState;
|
|
254
|
+
/**
|
|
255
|
+
* Read a decoupled snapshot of the inverter's live telemetry — PV power (raw
|
|
256
|
+
* and battery-adjusted), its resolved origin, operating state, voltages, AC
|
|
257
|
+
* energy and per-string DC data — WITHOUT constructing or sending any data-bus
|
|
258
|
+
* message. The consumer decides whether and how to publish it.
|
|
259
|
+
*
|
|
260
|
+
* This is the clean read primitive the SDK's own polling (`readData`) is built
|
|
261
|
+
* on, so a direct caller gets exactly the same numbers the bus would carry
|
|
262
|
+
* (except `acEnergyWh`, which is returned raw here and sanitized only on the
|
|
263
|
+
* publish path — see {@link SunspecInverterReading.acEnergyWh}).
|
|
264
|
+
*
|
|
265
|
+
* Returns `null` when not connected (after a reconnect attempt) or when the
|
|
266
|
+
* device exposes no readable inverter model.
|
|
267
|
+
*/
|
|
268
|
+
getInverterReading(): Promise<SunspecInverterReading | null>;
|
|
269
|
+
/**
|
|
270
|
+
* Fetch the live battery power (signed: positive = charging, negative =
|
|
271
|
+
* discharging) that {@link inverterTotalPvPowerW} backs out of the AC fallback.
|
|
272
|
+
* Only fetched when the inverter-total fallback will actually be used — the
|
|
273
|
+
* correction is irrelevant for the MPPT-string path — so a device with working
|
|
274
|
+
* strings never pays the extra battery read. Prefers the Model 160
|
|
275
|
+
* `StCha`/`StDisCha` strings, falling back to Model 124/802. `undefined` when
|
|
276
|
+
* no correction is needed or no live battery source exists.
|
|
277
|
+
*/
|
|
278
|
+
private fetchAcCorrectionBatteryPowerW;
|
|
279
|
+
/**
|
|
280
|
+
* Assemble a {@link SunspecInverterReading} from an already-read register set.
|
|
281
|
+
* Pure (no I/O, no side effects) so both `getInverterReading` and the polling
|
|
282
|
+
* `readData` produce identical readings from a single shared code path.
|
|
283
|
+
*/
|
|
284
|
+
private buildInverterReading;
|
|
242
285
|
/**
|
|
243
286
|
* PV production in Watt, sourced according to the configured `pvPowerSource`:
|
|
244
287
|
*
|
|
@@ -255,19 +298,38 @@ export declare class SunspecInverter extends BaseSunspecDevice {
|
|
|
255
298
|
* Both inverter-total steps require a strictly positive value, so NOT_IMPLEMENTED
|
|
256
299
|
* DCW (decodes to 0) falls through to AC, and a negative AC reading (e.g. a hybrid
|
|
257
300
|
* importing from grid) falls through to 0 rather than reporting negative production.
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
*
|
|
301
|
+
*
|
|
302
|
+
* The AC fallback backs out battery power via `batteryPowerW` (signed: positive =
|
|
303
|
+
* charging, negative = discharging). A hybrid's AC total is PV − charge + discharge,
|
|
304
|
+
* so real PV = AC + batteryPowerW. Without this, a battery discharging at night while
|
|
305
|
+
* the MPPT is unreadable would be reported as phantom PV production. `batteryPowerW`
|
|
306
|
+
* is undefined when no live battery source is available, in which case AC is used
|
|
307
|
+
* uncorrected (still better than reporting 0).
|
|
308
|
+
*
|
|
309
|
+
* The returned `pvPowerW` is always clamped to ≥ 0 — PV production is non-negative by
|
|
310
|
+
* definition and must never surface as a negative value on the data bus.
|
|
261
311
|
*/
|
|
262
|
-
protected selectPvPowerW(dcStrings: EnyoDataBusInverterValuesV1String[], inverterData: SunspecInverterData):
|
|
263
|
-
pvPowerW: number;
|
|
264
|
-
source: string;
|
|
265
|
-
};
|
|
312
|
+
protected selectPvPowerW(dcStrings: EnyoDataBusInverterValuesV1String[], inverterData: SunspecInverterData, batteryPowerW?: number): SunspecPvPower;
|
|
266
313
|
/**
|
|
267
|
-
*
|
|
268
|
-
*
|
|
314
|
+
* Whether {@link selectPvPowerW} will source PV from the inverter total (rather than
|
|
315
|
+
* the summed MPPT strings) for the given string count. Mppt never does; Inverter
|
|
316
|
+
* always does; Auto does only when there are no strings. Exposed so `readData` can
|
|
317
|
+
* decide up front whether it needs to fetch battery power for the AC correction.
|
|
318
|
+
*/
|
|
319
|
+
private wouldUseInverterTotal;
|
|
320
|
+
/**
|
|
321
|
+
* Inverter total PV power from Model 101: DCW when strictly positive, else AC (with
|
|
322
|
+
* battery power backed out) when the result is strictly positive, else 0. See
|
|
323
|
+
* {@link selectPvPowerW} for the battery correction and why positivity matters.
|
|
269
324
|
*/
|
|
270
325
|
private inverterTotalPvPowerW;
|
|
326
|
+
/**
|
|
327
|
+
* Summed per-string MPPT DC power (Model 160), battery strings already filtered
|
|
328
|
+
* out by {@link mapMPPTToStrings}. finiteOr per-string so one NaN/Infinity powerW
|
|
329
|
+
* cannot poison the sum; clamped to ≥ 0. Feeds both the MPPT-source selection and
|
|
330
|
+
* the always-reported {@link SunspecInverterReading.inverterPowerWDcStrings}.
|
|
331
|
+
*/
|
|
332
|
+
private sumDcStringsW;
|
|
271
333
|
/**
|
|
272
334
|
* Map MPPT data to DC string structure for data bus
|
|
273
335
|
* Filters out battery-related strings (StCha 3, StDisCha 4)
|
|
@@ -346,6 +408,20 @@ export declare function detectFeaturesFromRegisters(batteryData: SunspecBatteryD
|
|
|
346
408
|
* Exported as a free function so the preference logic can be unit-tested
|
|
347
409
|
* without the full `SunspecBattery` scaffold.
|
|
348
410
|
*/
|
|
411
|
+
/**
|
|
412
|
+
* Sum battery charge/discharge power from Model 160 MPPT strings.
|
|
413
|
+
*
|
|
414
|
+
* - `StCha 3` → charging power (added, positive)
|
|
415
|
+
* - `StDisCha 4` → discharging power (subtracted)
|
|
416
|
+
*
|
|
417
|
+
* The result follows the battery-power sign convention used across this file:
|
|
418
|
+
* positive = charging, negative = discharging. Returns `undefined` when neither
|
|
419
|
+
* battery string is present, so callers can fall through to another source.
|
|
420
|
+
*
|
|
421
|
+
* Exported as a free function so it can be unit-tested and reused by both the
|
|
422
|
+
* SunspecBattery live-power path and the SunspecInverter AC-fallback correction.
|
|
423
|
+
*/
|
|
424
|
+
export declare function sumMpptBatteryPowerW(mpptDataList: SunspecMPPTData[]): number | undefined;
|
|
349
425
|
export declare function selectLiveBatteryPowerW(mpptBatteryPowerW: number | undefined, batteryData: SunspecBatteryData | null): number | undefined;
|
|
350
426
|
export declare function filterFeaturesByCalibrationResult(detected: EnyoBatteryFeature[], result: CalibrationResult | undefined, controllable: readonly EnyoBatteryFeature[]): EnyoBatteryFeature[];
|
|
351
427
|
/**
|
|
@@ -411,6 +487,25 @@ export declare class SunspecBattery extends BaseSunspecDevice {
|
|
|
411
487
|
* Map SunSpec battery charge state to Enyo battery state
|
|
412
488
|
*/
|
|
413
489
|
private mapToEnyoBatteryState;
|
|
490
|
+
/**
|
|
491
|
+
* Read a decoupled snapshot of the battery's live telemetry — state of charge,
|
|
492
|
+
* signed power (positive = charging, negative = discharging) and charge state —
|
|
493
|
+
* WITHOUT constructing or sending any data-bus message. The consumer decides
|
|
494
|
+
* whether and how to publish it.
|
|
495
|
+
*
|
|
496
|
+
* This is the clean read primitive the SDK's own polling (`readData`) is built
|
|
497
|
+
* on, so a direct caller gets exactly the same numbers the bus would carry.
|
|
498
|
+
* Returns `null` when not connected (after a reconnect attempt) or when the
|
|
499
|
+
* device exposes no readable battery model.
|
|
500
|
+
*/
|
|
501
|
+
getBatteryReading(): Promise<SunspecBatteryReading | null>;
|
|
502
|
+
/**
|
|
503
|
+
* Assemble a {@link SunspecBatteryReading} from an already-read register set.
|
|
504
|
+
* Pure (no I/O, no side effects) so both `getBatteryReading` and the polling
|
|
505
|
+
* `readData` produce identical readings from a single shared code path. See
|
|
506
|
+
* {@link selectLiveBatteryPowerW} for the power source-preference rationale.
|
|
507
|
+
*/
|
|
508
|
+
private buildBatteryReading;
|
|
414
509
|
/**
|
|
415
510
|
* Update battery data and return data bus messages
|
|
416
511
|
*/
|
|
@@ -577,3 +672,4 @@ export declare class SunspecMeter extends BaseSunspecDevice {
|
|
|
577
672
|
*/
|
|
578
673
|
readData(clockId: string, resolution: EnyoDataBusMessageResolution): Promise<EnyoDataBusMessage[]>;
|
|
579
674
|
}
|
|
675
|
+
export {};
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* SunSpec block interfaces with block numbers
|
|
4
4
|
*/
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.SUNSPEC_CONTROLLABLE_FEATURES = exports.SunspecMeterCapability = exports.SunspecBatteryFeatureModeKind = exports.SunspecBatteryCapability = exports.SunspecPvPowerSource = exports.SunspecInverterCapability = exports.SunspecStorageMode = exports.SunspecChargeSource = exports.SunspecVArPctMode = exports.SunspecEnableControl = exports.SunspecConnectionControl = exports.SunspecStorageControlMode = exports.SunspecBatteryEvent1 = exports.SunspecBatteryBankState = exports.SunspecBatteryType = exports.SunspecBatteryControlMode = exports.SunspecBatteryChargeState = exports.SunspecInverterOperatingState = exports.SunspecMPPTOperatingState = exports.SUNSPEC_CONNECTION_LOST_CODE = exports.SunspecInverterEvent1 = exports.SunspecModelId = exports.DEFAULT_RETRY_CONFIG = void 0;
|
|
6
|
+
exports.SUNSPEC_CONTROLLABLE_FEATURES = exports.SunspecMeterCapability = exports.SunspecBatteryFeatureModeKind = exports.SunspecBatteryCapability = exports.SunspecPvPowerOrigin = exports.SunspecPvPowerSource = exports.SunspecInverterCapability = exports.SunspecStorageMode = exports.SunspecChargeSource = exports.SunspecVArPctMode = exports.SunspecEnableControl = exports.SunspecConnectionControl = exports.SunspecStorageControlMode = exports.SunspecBatteryEvent1 = exports.SunspecBatteryBankState = exports.SunspecBatteryType = exports.SunspecBatteryControlMode = exports.SunspecBatteryChargeState = exports.SunspecInverterOperatingState = exports.SunspecMPPTOperatingState = exports.SUNSPEC_CONNECTION_LOST_CODE = exports.SunspecInverterEvent1 = exports.SunspecModelId = exports.DEFAULT_RETRY_CONFIG = void 0;
|
|
7
7
|
const enyo_battery_appliance_js_1 = require("@enyo-energy/energy-app-sdk/dist/types/enyo-battery-appliance.js");
|
|
8
8
|
exports.DEFAULT_RETRY_CONFIG = {
|
|
9
9
|
phases: [
|
|
@@ -282,6 +282,28 @@ var SunspecPvPowerSource;
|
|
|
282
282
|
SunspecPvPowerSource["Mppt"] = "mppt";
|
|
283
283
|
SunspecPvPowerSource["Inverter"] = "inverter";
|
|
284
284
|
})(SunspecPvPowerSource || (exports.SunspecPvPowerSource = SunspecPvPowerSource = {}));
|
|
285
|
+
/**
|
|
286
|
+
* The concrete register set an inverter's PV power reading was actually derived
|
|
287
|
+
* from — the *resolved* origin, as opposed to {@link SunspecPvPowerSource} which
|
|
288
|
+
* is the *configured* preference. Returned on {@link SunspecInverterReading} so a
|
|
289
|
+
* consumer can see which telemetry produced the number.
|
|
290
|
+
*
|
|
291
|
+
* - `MpptStrings`: summed per-string DC power from Model 160 (battery strings
|
|
292
|
+
* `StCha 3` / `StDisCha 4` excluded). Already pure PV — never battery-adjusted.
|
|
293
|
+
* - `InverterDc`: the inverter's total DC power (Model 101 `DCW`). Pure PV — never
|
|
294
|
+
* battery-adjusted.
|
|
295
|
+
* - `InverterAc`: the inverter's total AC power (Model 101 `W`). On a hybrid this
|
|
296
|
+
* includes battery flow, so `inverterPowerW` backs it out
|
|
297
|
+
* (PV = AC + batteryPowerW) while `inverterPowerWRaw` keeps the uncorrected AC value.
|
|
298
|
+
* - `None`: no strictly-positive inverter total was available; `inverterPowerW` is 0.
|
|
299
|
+
*/
|
|
300
|
+
var SunspecPvPowerOrigin;
|
|
301
|
+
(function (SunspecPvPowerOrigin) {
|
|
302
|
+
SunspecPvPowerOrigin["MpptStrings"] = "mppt-strings";
|
|
303
|
+
SunspecPvPowerOrigin["InverterDc"] = "inverter-dc";
|
|
304
|
+
SunspecPvPowerOrigin["InverterAc"] = "inverter-ac";
|
|
305
|
+
SunspecPvPowerOrigin["None"] = "none";
|
|
306
|
+
})(SunspecPvPowerOrigin || (exports.SunspecPvPowerOrigin = SunspecPvPowerOrigin = {}));
|
|
285
307
|
var SunspecBatteryCapability;
|
|
286
308
|
(function (SunspecBatteryCapability) {
|
|
287
309
|
SunspecBatteryCapability["GridCharging"] = "grid-charging";
|