@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
package/dist/sunspec-devices.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SunspecBatteryChargeState, SunspecBatteryFeatureModeKind, SunspecEnableControl, SunspecInverterCapability, SunspecPvPowerSource, SunspecInverterEvent1, SunspecInverterOperatingState, SunspecModelId, SunspecMPPTOperatingState, SunspecStorageMode, SUNSPEC_CONNECTION_LOST_CODE } from "./sunspec-interfaces.js";
|
|
1
|
+
import { SunspecBatteryChargeState, SunspecBatteryFeatureModeKind, SunspecEnableControl, SunspecInverterCapability, SunspecPvPowerSource, SunspecPvPowerOrigin, SunspecInverterEvent1, SunspecInverterOperatingState, SunspecModelId, SunspecMPPTOperatingState, SunspecStorageMode, SUNSPEC_CONNECTION_LOST_CODE } from "./sunspec-interfaces.js";
|
|
2
2
|
import { randomUUID } from "node:crypto";
|
|
3
3
|
import { EnyoApplianceConnectionType, EnyoApplianceStateEnum, EnyoApplianceStatusEnum, EnyoApplianceTopologyFeatureEnum, EnyoApplianceTypeEnum } from "@enyo-energy/energy-app-sdk/dist/types/enyo-appliance.js";
|
|
4
4
|
import { ConnectionRetryManager } from "./connection-retry-manager.js";
|
|
@@ -329,6 +329,25 @@ export class BaseSunspecDevice {
|
|
|
329
329
|
});
|
|
330
330
|
}
|
|
331
331
|
}
|
|
332
|
+
/**
|
|
333
|
+
* Coerce a possibly-`undefined`, `NaN`, or `±Infinity` register value to a finite
|
|
334
|
+
* number, substituting `fallback` (default `0`) otherwise. SunSpec scale-factor
|
|
335
|
+
* decodes and IEEE-754 float models can surface `NaN`/`Infinity` for a
|
|
336
|
+
* NOT_IMPLEMENTED or corrupt register, and `typeof NaN === 'number'` /
|
|
337
|
+
* `Infinity > 0 === true` slip past naive guards — this is the single choke point
|
|
338
|
+
* that keeps every published power/voltage/energy value finite.
|
|
339
|
+
*/
|
|
340
|
+
function finiteOr(value, fallback = 0) {
|
|
341
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : fallback;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Like {@link finiteOr} but yields `undefined` for a non-finite/absent value, for
|
|
345
|
+
* optional reading fields (voltageL2/L3, acEnergyWh, batteryPowerW) where "no
|
|
346
|
+
* reliable value" is more honest than a fabricated `0`.
|
|
347
|
+
*/
|
|
348
|
+
function finiteOrUndefined(value) {
|
|
349
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
|
|
350
|
+
}
|
|
332
351
|
/**
|
|
333
352
|
* Sunspec Inverter implementation using dynamic model discovery
|
|
334
353
|
*/
|
|
@@ -489,8 +508,13 @@ export class SunspecInverter extends BaseSunspecDevice {
|
|
|
489
508
|
// to Connected. No-op unless this device is still marked offline.
|
|
490
509
|
await this.reassertConnectedIfRecovered();
|
|
491
510
|
if (inverterData) {
|
|
492
|
-
|
|
493
|
-
|
|
511
|
+
// Build the same decoupled reading a direct getInverterReading() caller
|
|
512
|
+
// would get, then wrap it in the data-bus message. The battery-adjusted
|
|
513
|
+
// power is what we publish; meterValueWh is sanitized only here on the
|
|
514
|
+
// publish path (the raw reading intentionally leaves it unguarded).
|
|
515
|
+
const batteryPowerW = await this.fetchAcCorrectionBatteryPowerW(dcStrings.length, mpptDataList);
|
|
516
|
+
const reading = this.buildInverterReading(inverterData, dcStrings, batteryPowerW);
|
|
517
|
+
console.debug(`Got PV Power from ${reading.powerOrigin}: ${reading.inverterPowerW}W (inverter total raw ${reading.inverterPowerWRaw}W, DC strings ${reading.inverterPowerWDcStrings}W)`);
|
|
494
518
|
const inverterMessage = {
|
|
495
519
|
id: randomUUID(),
|
|
496
520
|
message: EnyoDataBusMessageEnum.InverterValuesUpdateV1,
|
|
@@ -501,14 +525,14 @@ export class SunspecInverter extends BaseSunspecDevice {
|
|
|
501
525
|
timestampIso: timestamp.toISOString(),
|
|
502
526
|
resolution,
|
|
503
527
|
data: {
|
|
504
|
-
pvPowerW,
|
|
528
|
+
pvPowerW: reading.inverterPowerW,
|
|
505
529
|
activePowerLimitationW: inverterSettings?.WMax,
|
|
506
|
-
state:
|
|
507
|
-
voltageL1:
|
|
508
|
-
voltageL2:
|
|
509
|
-
voltageL3:
|
|
530
|
+
state: reading.state,
|
|
531
|
+
voltageL1: reading.voltageL1,
|
|
532
|
+
voltageL2: reading.voltageL2,
|
|
533
|
+
voltageL3: reading.voltageL3,
|
|
510
534
|
meterValueWh: this.sanitizeAcEnergyWh(inverterData.acEnergy, inverterData.acEnergyScaleFactor),
|
|
511
|
-
strings:
|
|
535
|
+
strings: reading.strings
|
|
512
536
|
}
|
|
513
537
|
};
|
|
514
538
|
messages.push(inverterMessage);
|
|
@@ -811,6 +835,93 @@ export class SunspecInverter extends BaseSunspecDevice {
|
|
|
811
835
|
};
|
|
812
836
|
return stateMap[state] || EnyoInverterStateEnum.Off;
|
|
813
837
|
}
|
|
838
|
+
/**
|
|
839
|
+
* Read a decoupled snapshot of the inverter's live telemetry — PV power (raw
|
|
840
|
+
* and battery-adjusted), its resolved origin, operating state, voltages, AC
|
|
841
|
+
* energy and per-string DC data — WITHOUT constructing or sending any data-bus
|
|
842
|
+
* message. The consumer decides whether and how to publish it.
|
|
843
|
+
*
|
|
844
|
+
* This is the clean read primitive the SDK's own polling (`readData`) is built
|
|
845
|
+
* on, so a direct caller gets exactly the same numbers the bus would carry
|
|
846
|
+
* (except `acEnergyWh`, which is returned raw here and sanitized only on the
|
|
847
|
+
* publish path — see {@link SunspecInverterReading.acEnergyWh}).
|
|
848
|
+
*
|
|
849
|
+
* Returns `null` when not connected (after a reconnect attempt) or when the
|
|
850
|
+
* device exposes no readable inverter model.
|
|
851
|
+
*/
|
|
852
|
+
async getInverterReading() {
|
|
853
|
+
try {
|
|
854
|
+
if (!this.isConnected()) {
|
|
855
|
+
await this.tryReconnect();
|
|
856
|
+
if (!this.isConnected())
|
|
857
|
+
return null;
|
|
858
|
+
}
|
|
859
|
+
const inverterData = await this.sunspecClient.readInverterData(this.unitId);
|
|
860
|
+
if (!inverterData)
|
|
861
|
+
return null;
|
|
862
|
+
const mpptDataList = await this.sunspecClient.readAllMPPTData(this.unitId);
|
|
863
|
+
const dcStrings = this.mapMPPTToStrings(mpptDataList ?? []);
|
|
864
|
+
const batteryPowerW = await this.fetchAcCorrectionBatteryPowerW(dcStrings.length, mpptDataList ?? []);
|
|
865
|
+
return this.buildInverterReading(inverterData, dcStrings, batteryPowerW);
|
|
866
|
+
}
|
|
867
|
+
catch (error) {
|
|
868
|
+
// Bullet-proof contract: a decode/comms fault never propagates to the
|
|
869
|
+
// caller — it surfaces as a null reading, same as "no readable model".
|
|
870
|
+
console.warn(`Notify_Devs: ${this.constructor.name} ${this.applianceId}: getInverterReading failed: ${error}`);
|
|
871
|
+
return null;
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
/**
|
|
875
|
+
* Fetch the live battery power (signed: positive = charging, negative =
|
|
876
|
+
* discharging) that {@link inverterTotalPvPowerW} backs out of the AC fallback.
|
|
877
|
+
* Only fetched when the inverter-total fallback will actually be used — the
|
|
878
|
+
* correction is irrelevant for the MPPT-string path — so a device with working
|
|
879
|
+
* strings never pays the extra battery read. Prefers the Model 160
|
|
880
|
+
* `StCha`/`StDisCha` strings, falling back to Model 124/802. `undefined` when
|
|
881
|
+
* no correction is needed or no live battery source exists.
|
|
882
|
+
*/
|
|
883
|
+
async fetchAcCorrectionBatteryPowerW(dcStringCount, mpptDataList) {
|
|
884
|
+
if (!this.wouldUseInverterTotal(dcStringCount))
|
|
885
|
+
return undefined;
|
|
886
|
+
const mpptBatteryPowerW = sumMpptBatteryPowerW(mpptDataList);
|
|
887
|
+
if (mpptBatteryPowerW !== undefined)
|
|
888
|
+
return mpptBatteryPowerW;
|
|
889
|
+
const batteryData = await this.sunspecClient.readBatteryData(this.unitId);
|
|
890
|
+
return selectLiveBatteryPowerW(undefined, batteryData);
|
|
891
|
+
}
|
|
892
|
+
/**
|
|
893
|
+
* Assemble a {@link SunspecInverterReading} from an already-read register set.
|
|
894
|
+
* Pure (no I/O, no side effects) so both `getInverterReading` and the polling
|
|
895
|
+
* `readData` produce identical readings from a single shared code path.
|
|
896
|
+
*/
|
|
897
|
+
buildInverterReading(inverterData, dcStrings, batteryPowerW) {
|
|
898
|
+
// Independent breakdown: compute each candidate power regardless of which the
|
|
899
|
+
// configured source selects, so nothing is hidden by the selection.
|
|
900
|
+
// - inverterPowerWDcStrings: the summed MPPT DC strings (always).
|
|
901
|
+
// - inverterPowerWRaw: the inverter's own total (DCW→AC), uncorrected (always).
|
|
902
|
+
// - inverterPowerW: the auto-calc value to use — selected source,
|
|
903
|
+
// battery-adjusted (via selectPvPowerW, overridable).
|
|
904
|
+
const dcStringsW = this.sumDcStringsW(dcStrings);
|
|
905
|
+
const inverterTotal = this.inverterTotalPvPowerW(inverterData, batteryPowerW);
|
|
906
|
+
const selected = this.selectPvPowerW(dcStrings, inverterData, batteryPowerW);
|
|
907
|
+
// Final clamp: the helpers already sanitize, but re-assert the finite/≥0
|
|
908
|
+
// contract here so an override of selectPvPowerW can never leak a non-finite
|
|
909
|
+
// or negative value onto the reading.
|
|
910
|
+
return {
|
|
911
|
+
inverterPowerW: Math.max(0, finiteOr(selected.batteryAdjustedPowerW)),
|
|
912
|
+
inverterPowerWRaw: Math.max(0, finiteOr(inverterTotal.rawPowerW)),
|
|
913
|
+
inverterPowerWDcStrings: Math.max(0, finiteOr(dcStringsW)),
|
|
914
|
+
powerOrigin: selected.origin,
|
|
915
|
+
batteryPowerW: finiteOrUndefined(batteryPowerW),
|
|
916
|
+
state: this.mapOperatingState(inverterData.operatingState),
|
|
917
|
+
operatingState: finiteOrUndefined(inverterData.operatingState),
|
|
918
|
+
voltageL1: finiteOr(inverterData.voltageAN),
|
|
919
|
+
voltageL2: finiteOrUndefined(inverterData.voltageBN),
|
|
920
|
+
voltageL3: finiteOrUndefined(inverterData.voltageCN),
|
|
921
|
+
acEnergyWh: finiteOrUndefined(inverterData.acEnergy),
|
|
922
|
+
strings: Array.isArray(dcStrings) ? dcStrings : [],
|
|
923
|
+
};
|
|
924
|
+
}
|
|
814
925
|
/**
|
|
815
926
|
* PV production in Watt, sourced according to the configured `pvPowerSource`:
|
|
816
927
|
*
|
|
@@ -827,31 +938,73 @@ export class SunspecInverter extends BaseSunspecDevice {
|
|
|
827
938
|
* Both inverter-total steps require a strictly positive value, so NOT_IMPLEMENTED
|
|
828
939
|
* DCW (decodes to 0) falls through to AC, and a negative AC reading (e.g. a hybrid
|
|
829
940
|
* importing from grid) falls through to 0 rather than reporting negative production.
|
|
830
|
-
*
|
|
831
|
-
*
|
|
832
|
-
*
|
|
941
|
+
*
|
|
942
|
+
* The AC fallback backs out battery power via `batteryPowerW` (signed: positive =
|
|
943
|
+
* charging, negative = discharging). A hybrid's AC total is PV − charge + discharge,
|
|
944
|
+
* so real PV = AC + batteryPowerW. Without this, a battery discharging at night while
|
|
945
|
+
* the MPPT is unreadable would be reported as phantom PV production. `batteryPowerW`
|
|
946
|
+
* is undefined when no live battery source is available, in which case AC is used
|
|
947
|
+
* uncorrected (still better than reporting 0).
|
|
948
|
+
*
|
|
949
|
+
* The returned `pvPowerW` is always clamped to ≥ 0 — PV production is non-negative by
|
|
950
|
+
* definition and must never surface as a negative value on the data bus.
|
|
833
951
|
*/
|
|
834
|
-
selectPvPowerW(dcStrings, inverterData) {
|
|
835
|
-
const
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
return {
|
|
952
|
+
selectPvPowerW(dcStrings, inverterData, batteryPowerW) {
|
|
953
|
+
const strings = Array.isArray(dcStrings) ? dcStrings : [];
|
|
954
|
+
if (!this.wouldUseInverterTotal(strings.length)) {
|
|
955
|
+
const stringSumW = this.sumDcStringsW(strings);
|
|
956
|
+
// MPPT strings are already pure PV — raw and battery-adjusted coincide.
|
|
957
|
+
return { rawPowerW: stringSumW, batteryAdjustedPowerW: stringSumW, origin: SunspecPvPowerOrigin.MpptStrings };
|
|
840
958
|
}
|
|
841
|
-
return this.inverterTotalPvPowerW(inverterData);
|
|
959
|
+
return this.inverterTotalPvPowerW(inverterData, batteryPowerW);
|
|
842
960
|
}
|
|
843
961
|
/**
|
|
844
|
-
*
|
|
845
|
-
*
|
|
962
|
+
* Whether {@link selectPvPowerW} will source PV from the inverter total (rather than
|
|
963
|
+
* the summed MPPT strings) for the given string count. Mppt never does; Inverter
|
|
964
|
+
* always does; Auto does only when there are no strings. Exposed so `readData` can
|
|
965
|
+
* decide up front whether it needs to fetch battery power for the AC correction.
|
|
846
966
|
*/
|
|
847
|
-
|
|
848
|
-
if (
|
|
849
|
-
return
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
967
|
+
wouldUseInverterTotal(dcStringCount) {
|
|
968
|
+
if (this.pvPowerSource === SunspecPvPowerSource.Mppt)
|
|
969
|
+
return false;
|
|
970
|
+
if (this.pvPowerSource === SunspecPvPowerSource.Inverter)
|
|
971
|
+
return true;
|
|
972
|
+
return dcStringCount === 0; // Auto
|
|
973
|
+
}
|
|
974
|
+
/**
|
|
975
|
+
* Inverter total PV power from Model 101: DCW when strictly positive, else AC (with
|
|
976
|
+
* battery power backed out) when the result is strictly positive, else 0. See
|
|
977
|
+
* {@link selectPvPowerW} for the battery correction and why positivity matters.
|
|
978
|
+
*/
|
|
979
|
+
inverterTotalPvPowerW(inverterData, batteryPowerW) {
|
|
980
|
+
// finiteOr collapses NaN/Infinity to 0 so a corrupt decode fails the `> 0`
|
|
981
|
+
// guard (Infinity would otherwise pass `typeof === 'number' && > 0`).
|
|
982
|
+
const dcW = finiteOr(inverterData.dcPower);
|
|
983
|
+
if (dcW > 0) {
|
|
984
|
+
// Inverter DC total is already pure PV — no battery correction applies.
|
|
985
|
+
return { rawPowerW: dcW, batteryAdjustedPowerW: dcW, origin: SunspecPvPowerOrigin.InverterDc };
|
|
986
|
+
}
|
|
987
|
+
const acW = finiteOr(inverterData.acPower);
|
|
988
|
+
if (acW > 0) {
|
|
989
|
+
// AC = PV − charge + discharge on a hybrid; batteryPowerW is signed
|
|
990
|
+
// (positive = charging, negative = discharging), so PV = AC + batteryPowerW.
|
|
991
|
+
// rawPowerW keeps the uncorrected AC total; batteryAdjustedPowerW backs the
|
|
992
|
+
// battery out and is clamped to ≥ 0. finiteOr(batteryPowerW) guarantees the
|
|
993
|
+
// sum stays finite even if the battery source returned garbage.
|
|
994
|
+
const correctedW = Math.max(0, acW + finiteOr(batteryPowerW));
|
|
995
|
+
return { rawPowerW: acW, batteryAdjustedPowerW: correctedW, origin: SunspecPvPowerOrigin.InverterAc };
|
|
996
|
+
}
|
|
997
|
+
return { rawPowerW: 0, batteryAdjustedPowerW: 0, origin: SunspecPvPowerOrigin.None };
|
|
998
|
+
}
|
|
999
|
+
/**
|
|
1000
|
+
* Summed per-string MPPT DC power (Model 160), battery strings already filtered
|
|
1001
|
+
* out by {@link mapMPPTToStrings}. finiteOr per-string so one NaN/Infinity powerW
|
|
1002
|
+
* cannot poison the sum; clamped to ≥ 0. Feeds both the MPPT-source selection and
|
|
1003
|
+
* the always-reported {@link SunspecInverterReading.inverterPowerWDcStrings}.
|
|
1004
|
+
*/
|
|
1005
|
+
sumDcStringsW(dcStrings) {
|
|
1006
|
+
const strings = Array.isArray(dcStrings) ? dcStrings : [];
|
|
1007
|
+
return Math.max(0, strings.reduce((sum, s) => sum + finiteOr(s?.powerW), 0));
|
|
855
1008
|
}
|
|
856
1009
|
/**
|
|
857
1010
|
* Map MPPT data to DC string structure for data bus
|
|
@@ -1081,6 +1234,31 @@ export function detectFeaturesFromRegisters(batteryData) {
|
|
|
1081
1234
|
* Exported as a free function so the preference logic can be unit-tested
|
|
1082
1235
|
* without the full `SunspecBattery` scaffold.
|
|
1083
1236
|
*/
|
|
1237
|
+
/**
|
|
1238
|
+
* Sum battery charge/discharge power from Model 160 MPPT strings.
|
|
1239
|
+
*
|
|
1240
|
+
* - `StCha 3` → charging power (added, positive)
|
|
1241
|
+
* - `StDisCha 4` → discharging power (subtracted)
|
|
1242
|
+
*
|
|
1243
|
+
* The result follows the battery-power sign convention used across this file:
|
|
1244
|
+
* positive = charging, negative = discharging. Returns `undefined` when neither
|
|
1245
|
+
* battery string is present, so callers can fall through to another source.
|
|
1246
|
+
*
|
|
1247
|
+
* Exported as a free function so it can be unit-tested and reused by both the
|
|
1248
|
+
* SunspecBattery live-power path and the SunspecInverter AC-fallback correction.
|
|
1249
|
+
*/
|
|
1250
|
+
export function sumMpptBatteryPowerW(mpptDataList) {
|
|
1251
|
+
let batteryPowerW;
|
|
1252
|
+
for (const mppt of mpptDataList) {
|
|
1253
|
+
if (mppt.stringId === 'StCha 3' && mppt.dcPower !== undefined) {
|
|
1254
|
+
batteryPowerW = (batteryPowerW ?? 0) + mppt.dcPower;
|
|
1255
|
+
}
|
|
1256
|
+
else if (mppt.stringId === 'StDisCha 4' && mppt.dcPower !== undefined) {
|
|
1257
|
+
batteryPowerW = (batteryPowerW ?? 0) - mppt.dcPower;
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
return batteryPowerW;
|
|
1261
|
+
}
|
|
1084
1262
|
export function selectLiveBatteryPowerW(mpptBatteryPowerW, batteryData) {
|
|
1085
1263
|
if (mpptBatteryPowerW !== undefined) {
|
|
1086
1264
|
return mpptBatteryPowerW;
|
|
@@ -1334,6 +1512,43 @@ export class SunspecBattery extends BaseSunspecDevice {
|
|
|
1334
1512
|
return undefined;
|
|
1335
1513
|
}
|
|
1336
1514
|
}
|
|
1515
|
+
/**
|
|
1516
|
+
* Read a decoupled snapshot of the battery's live telemetry — state of charge,
|
|
1517
|
+
* signed power (positive = charging, negative = discharging) and charge state —
|
|
1518
|
+
* WITHOUT constructing or sending any data-bus message. The consumer decides
|
|
1519
|
+
* whether and how to publish it.
|
|
1520
|
+
*
|
|
1521
|
+
* This is the clean read primitive the SDK's own polling (`readData`) is built
|
|
1522
|
+
* on, so a direct caller gets exactly the same numbers the bus would carry.
|
|
1523
|
+
* Returns `null` when not connected (after a reconnect attempt) or when the
|
|
1524
|
+
* device exposes no readable battery model.
|
|
1525
|
+
*/
|
|
1526
|
+
async getBatteryReading() {
|
|
1527
|
+
if (!this.isConnected()) {
|
|
1528
|
+
await this.tryReconnect();
|
|
1529
|
+
if (!this.isConnected())
|
|
1530
|
+
return null;
|
|
1531
|
+
}
|
|
1532
|
+
const batteryData = await this.sunspecClient.readBatteryData(this.unitId);
|
|
1533
|
+
if (!batteryData)
|
|
1534
|
+
return null;
|
|
1535
|
+
const mpptDataList = await this.sunspecClient.readAllMPPTData(this.unitId);
|
|
1536
|
+
const mpptBatteryPowerW = this.extractBatteryPowerFromMPPT(mpptDataList);
|
|
1537
|
+
return this.buildBatteryReading(batteryData, mpptBatteryPowerW);
|
|
1538
|
+
}
|
|
1539
|
+
/**
|
|
1540
|
+
* Assemble a {@link SunspecBatteryReading} from an already-read register set.
|
|
1541
|
+
* Pure (no I/O, no side effects) so both `getBatteryReading` and the polling
|
|
1542
|
+
* `readData` produce identical readings from a single shared code path. See
|
|
1543
|
+
* {@link selectLiveBatteryPowerW} for the power source-preference rationale.
|
|
1544
|
+
*/
|
|
1545
|
+
buildBatteryReading(batteryData, mpptBatteryPowerW) {
|
|
1546
|
+
return {
|
|
1547
|
+
soc: batteryData.soc || batteryData.chaState || 0,
|
|
1548
|
+
powerW: selectLiveBatteryPowerW(mpptBatteryPowerW, batteryData),
|
|
1549
|
+
state: this.mapToEnyoBatteryState(batteryData.chaSt),
|
|
1550
|
+
};
|
|
1551
|
+
}
|
|
1337
1552
|
/**
|
|
1338
1553
|
* Update battery data and return data bus messages
|
|
1339
1554
|
*/
|
|
@@ -1360,12 +1575,13 @@ export class SunspecBattery extends BaseSunspecDevice {
|
|
|
1360
1575
|
// to Connected. No-op unless this device is still marked offline.
|
|
1361
1576
|
await this.reassertConnectedIfRecovered();
|
|
1362
1577
|
if (batteryData) {
|
|
1363
|
-
//
|
|
1364
|
-
|
|
1578
|
+
// Build the same decoupled reading a direct getBatteryReading() caller
|
|
1579
|
+
// would get, then wrap it in the data-bus message.
|
|
1580
|
+
const reading = this.buildBatteryReading(batteryData, mpptBatteryPowerW);
|
|
1365
1581
|
// Feed the calibration driver's synchronous power cache. No-op when calibration
|
|
1366
1582
|
// was never configured.
|
|
1367
|
-
if (
|
|
1368
|
-
this.calibrationDriver.updateBatteryPowerCache(
|
|
1583
|
+
if (reading.powerW !== undefined && this.calibrationDriver) {
|
|
1584
|
+
this.calibrationDriver.updateBatteryPowerCache(reading.powerW);
|
|
1369
1585
|
}
|
|
1370
1586
|
const batteryMessage = {
|
|
1371
1587
|
id: randomUUID(),
|
|
@@ -1377,9 +1593,9 @@ export class SunspecBattery extends BaseSunspecDevice {
|
|
|
1377
1593
|
timestampIso: timestamp.toISOString(),
|
|
1378
1594
|
resolution,
|
|
1379
1595
|
data: {
|
|
1380
|
-
batterySoC:
|
|
1381
|
-
batteryPowerW,
|
|
1382
|
-
state:
|
|
1596
|
+
batterySoC: reading.soc,
|
|
1597
|
+
batteryPowerW: reading.powerW,
|
|
1598
|
+
state: reading.state,
|
|
1383
1599
|
}
|
|
1384
1600
|
};
|
|
1385
1601
|
messages.push(batteryMessage);
|
|
@@ -1420,18 +1636,7 @@ export class SunspecBattery extends BaseSunspecDevice {
|
|
|
1420
1636
|
* StDisCha 4: Battery discharging power (negative)
|
|
1421
1637
|
*/
|
|
1422
1638
|
extractBatteryPowerFromMPPT(mpptDataList) {
|
|
1423
|
-
|
|
1424
|
-
mpptDataList.forEach((mppt) => {
|
|
1425
|
-
if (mppt.stringId === 'StCha 3' && mppt.dcPower !== undefined) {
|
|
1426
|
-
// Charging power is positive
|
|
1427
|
-
batteryPowerW = (batteryPowerW || 0) + mppt.dcPower;
|
|
1428
|
-
}
|
|
1429
|
-
else if (mppt.stringId === 'StDisCha 4' && mppt.dcPower !== undefined) {
|
|
1430
|
-
// Discharging power is negative
|
|
1431
|
-
batteryPowerW = (batteryPowerW || 0) - mppt.dcPower;
|
|
1432
|
-
}
|
|
1433
|
-
});
|
|
1434
|
-
return batteryPowerW;
|
|
1639
|
+
return sumMpptBatteryPowerW(mpptDataList);
|
|
1435
1640
|
}
|
|
1436
1641
|
/**
|
|
1437
1642
|
* Determine battery state from MPPT strings
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* SunSpec block interfaces with block numbers
|
|
3
3
|
*/
|
|
4
4
|
import { EnyoBatteryFeature } from "@enyo-energy/energy-app-sdk/dist/types/enyo-battery-appliance.js";
|
|
5
|
+
import { EnyoBatteryStateEnum, EnyoDataBusInverterValuesV1String, EnyoInverterStateEnum } from "@enyo-energy/energy-app-sdk/dist/types/enyo-data-bus-value.js";
|
|
5
6
|
/**
|
|
6
7
|
* A single phase in the tiered retry schedule
|
|
7
8
|
*/
|
|
@@ -665,6 +666,98 @@ export declare enum SunspecPvPowerSource {
|
|
|
665
666
|
Mppt = "mppt",
|
|
666
667
|
Inverter = "inverter"
|
|
667
668
|
}
|
|
669
|
+
/**
|
|
670
|
+
* The concrete register set an inverter's PV power reading was actually derived
|
|
671
|
+
* from — the *resolved* origin, as opposed to {@link SunspecPvPowerSource} which
|
|
672
|
+
* is the *configured* preference. Returned on {@link SunspecInverterReading} so a
|
|
673
|
+
* consumer can see which telemetry produced the number.
|
|
674
|
+
*
|
|
675
|
+
* - `MpptStrings`: summed per-string DC power from Model 160 (battery strings
|
|
676
|
+
* `StCha 3` / `StDisCha 4` excluded). Already pure PV — never battery-adjusted.
|
|
677
|
+
* - `InverterDc`: the inverter's total DC power (Model 101 `DCW`). Pure PV — never
|
|
678
|
+
* battery-adjusted.
|
|
679
|
+
* - `InverterAc`: the inverter's total AC power (Model 101 `W`). On a hybrid this
|
|
680
|
+
* includes battery flow, so `inverterPowerW` backs it out
|
|
681
|
+
* (PV = AC + batteryPowerW) while `inverterPowerWRaw` keeps the uncorrected AC value.
|
|
682
|
+
* - `None`: no strictly-positive inverter total was available; `inverterPowerW` is 0.
|
|
683
|
+
*/
|
|
684
|
+
export declare enum SunspecPvPowerOrigin {
|
|
685
|
+
MpptStrings = "mppt-strings",
|
|
686
|
+
InverterDc = "inverter-dc",
|
|
687
|
+
InverterAc = "inverter-ac",
|
|
688
|
+
None = "none"
|
|
689
|
+
}
|
|
690
|
+
/**
|
|
691
|
+
* A decoupled snapshot of an inverter's live telemetry, produced by
|
|
692
|
+
* `SunspecInverter.getInverterReading()`. Contains no data-bus message and is
|
|
693
|
+
* never sent anywhere — the consumer decides whether to publish it. The same
|
|
694
|
+
* data drives the SDK's own `InverterValuesUpdateV1` message when polling is used.
|
|
695
|
+
*/
|
|
696
|
+
export interface SunspecInverterReading {
|
|
697
|
+
/**
|
|
698
|
+
* The auto-calculated PV production (W) to use — the configured source's value
|
|
699
|
+
* (see {@link SunspecPvPowerSource}) with battery charge/discharge backed out of
|
|
700
|
+
* the AC fallback, clamped to ≥ 0. `powerOrigin` says which source produced it.
|
|
701
|
+
* This is the value the SDK publishes as `pvPowerW` when it builds the data-bus
|
|
702
|
+
* message itself.
|
|
703
|
+
*/
|
|
704
|
+
inverterPowerW: number;
|
|
705
|
+
/**
|
|
706
|
+
* The inverter's own total power (W), UNCORRECTED: Model 101 `DCW` when strictly
|
|
707
|
+
* positive, else AC (`W`) when strictly positive, else 0. Clamped to ≥ 0. Always
|
|
708
|
+
* computed and reported independently of `powerOrigin`, so it is available even
|
|
709
|
+
* when {@link inverterPowerW} is sourced from the MPPT strings. Battery flow is
|
|
710
|
+
* NOT backed out here — for the AC branch that is exactly {@link inverterPowerW}
|
|
711
|
+
* minus the battery correction.
|
|
712
|
+
*/
|
|
713
|
+
inverterPowerWRaw: number;
|
|
714
|
+
/**
|
|
715
|
+
* Summed per-string MPPT DC power (W) from Model 160 (battery strings
|
|
716
|
+
* `StCha 3` / `StDisCha 4` excluded), clamped to ≥ 0. Always computed and
|
|
717
|
+
* reported independently of `powerOrigin` — `0` when no PV strings are readable.
|
|
718
|
+
*/
|
|
719
|
+
inverterPowerWDcStrings: number;
|
|
720
|
+
/** Which register set {@link inverterPowerW} was selected from. */
|
|
721
|
+
powerOrigin: SunspecPvPowerOrigin;
|
|
722
|
+
/**
|
|
723
|
+
* The live battery power (W) backed out of the AC fallback, signed
|
|
724
|
+
* (positive = charging, negative = discharging). Only fetched when the AC/DC
|
|
725
|
+
* inverter-total fallback is in use; `undefined` otherwise or when no live
|
|
726
|
+
* battery source is available.
|
|
727
|
+
*/
|
|
728
|
+
batteryPowerW?: number;
|
|
729
|
+
/** Operating state mapped to the Enyo enum. */
|
|
730
|
+
state: EnyoInverterStateEnum;
|
|
731
|
+
/** Raw SunSpec operating state (`St`), unmapped. */
|
|
732
|
+
operatingState?: number;
|
|
733
|
+
voltageL1: number;
|
|
734
|
+
voltageL2?: number;
|
|
735
|
+
voltageL3?: number;
|
|
736
|
+
/**
|
|
737
|
+
* Raw cumulative AC energy (Wh) straight from the register. NOT sanitized —
|
|
738
|
+
* the SDK's acc32 word-boundary guard is applied only on the publish path, so
|
|
739
|
+
* a consumer reading directly should expect the occasional register artifact.
|
|
740
|
+
*/
|
|
741
|
+
acEnergyWh?: number;
|
|
742
|
+
/** Per-string DC telemetry (Model 160), battery strings filtered out. */
|
|
743
|
+
strings: EnyoDataBusInverterValuesV1String[];
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* A decoupled snapshot of a battery's live telemetry, produced by
|
|
747
|
+
* `SunspecBattery.getBatteryReading()`. Contains no data-bus message.
|
|
748
|
+
*/
|
|
749
|
+
export interface SunspecBatteryReading {
|
|
750
|
+
/** State of charge (%). */
|
|
751
|
+
soc: number;
|
|
752
|
+
/**
|
|
753
|
+
* Live battery power (W), signed: positive = charging, negative = discharging.
|
|
754
|
+
* `undefined` when the device exposes no live power source (e.g. a Model
|
|
755
|
+
* 124-only battery with no MPPT battery strings).
|
|
756
|
+
*/
|
|
757
|
+
powerW?: number;
|
|
758
|
+
/** Charge/discharge state mapped to the Enyo enum. */
|
|
759
|
+
state?: EnyoBatteryStateEnum;
|
|
760
|
+
}
|
|
668
761
|
export declare enum SunspecBatteryCapability {
|
|
669
762
|
GridCharging = "grid-charging",
|
|
670
763
|
DischargeLimit = "discharge-limit",
|
|
@@ -279,6 +279,28 @@ export var SunspecPvPowerSource;
|
|
|
279
279
|
SunspecPvPowerSource["Mppt"] = "mppt";
|
|
280
280
|
SunspecPvPowerSource["Inverter"] = "inverter";
|
|
281
281
|
})(SunspecPvPowerSource || (SunspecPvPowerSource = {}));
|
|
282
|
+
/**
|
|
283
|
+
* The concrete register set an inverter's PV power reading was actually derived
|
|
284
|
+
* from — the *resolved* origin, as opposed to {@link SunspecPvPowerSource} which
|
|
285
|
+
* is the *configured* preference. Returned on {@link SunspecInverterReading} so a
|
|
286
|
+
* consumer can see which telemetry produced the number.
|
|
287
|
+
*
|
|
288
|
+
* - `MpptStrings`: summed per-string DC power from Model 160 (battery strings
|
|
289
|
+
* `StCha 3` / `StDisCha 4` excluded). Already pure PV — never battery-adjusted.
|
|
290
|
+
* - `InverterDc`: the inverter's total DC power (Model 101 `DCW`). Pure PV — never
|
|
291
|
+
* battery-adjusted.
|
|
292
|
+
* - `InverterAc`: the inverter's total AC power (Model 101 `W`). On a hybrid this
|
|
293
|
+
* includes battery flow, so `inverterPowerW` backs it out
|
|
294
|
+
* (PV = AC + batteryPowerW) while `inverterPowerWRaw` keeps the uncorrected AC value.
|
|
295
|
+
* - `None`: no strictly-positive inverter total was available; `inverterPowerW` is 0.
|
|
296
|
+
*/
|
|
297
|
+
export var SunspecPvPowerOrigin;
|
|
298
|
+
(function (SunspecPvPowerOrigin) {
|
|
299
|
+
SunspecPvPowerOrigin["MpptStrings"] = "mppt-strings";
|
|
300
|
+
SunspecPvPowerOrigin["InverterDc"] = "inverter-dc";
|
|
301
|
+
SunspecPvPowerOrigin["InverterAc"] = "inverter-ac";
|
|
302
|
+
SunspecPvPowerOrigin["None"] = "none";
|
|
303
|
+
})(SunspecPvPowerOrigin || (SunspecPvPowerOrigin = {}));
|
|
282
304
|
export var SunspecBatteryCapability;
|
|
283
305
|
(function (SunspecBatteryCapability) {
|
|
284
306
|
SunspecBatteryCapability["GridCharging"] = "grid-charging";
|
package/dist/version.d.ts
CHANGED
package/dist/version.js
CHANGED