@enyo-energy/sunspec-sdk 0.0.95 → 0.0.96
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 +84 -1
- package/dist/cjs/sunspec-devices.d.cts +50 -2
- package/dist/cjs/sunspec-interfaces.d.cts +1 -0
- package/dist/cjs/sunspec-modbus-client.cjs +6 -2
- package/dist/cjs/version.cjs +1 -1
- package/dist/cjs/version.d.cts +1 -1
- package/dist/sunspec-devices.d.ts +50 -2
- package/dist/sunspec-devices.js +84 -1
- package/dist/sunspec-interfaces.d.ts +1 -0
- package/dist/sunspec-modbus-client.js +6 -2
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -2
|
@@ -362,6 +362,13 @@ class SunspecInverter extends BaseSunspecDevice {
|
|
|
362
362
|
// state reads healthy), so we force one re-assert of the current status on
|
|
363
363
|
// the first successful read of each session to converge the core.
|
|
364
364
|
statusReassertedThisSession = false;
|
|
365
|
+
// Last accepted cumulative AC-energy reading (Wh) and how many consecutive
|
|
366
|
+
// polls have reported a corrupt one (a decrease or a ≥½-register-quantum jump
|
|
367
|
+
// — the acc32 16-bit word-boundary artifact). See sanitizeAcEnergyWh.
|
|
368
|
+
lastGoodAcEnergyWh;
|
|
369
|
+
acEnergyDropStreak = 0;
|
|
370
|
+
/** Consecutive corrupt reads that confirm a genuine reset/resync (vs a transient glitch). */
|
|
371
|
+
static AC_ENERGY_RESET_CONFIRM = 3;
|
|
365
372
|
constructor(energyApp, name, networkDevice, sunspecClient, applianceManager, unitId = 1, port = 502, baseAddress = 40000, capabilities = [], retryConfig, appliance, useTls,
|
|
366
373
|
// Where pvPowerW is sourced from — see SunspecPvPowerSource. Defaults to
|
|
367
374
|
// Auto (MPPT strings, then Model 101 DCW/AC fallback). Use Mppt to report
|
|
@@ -507,7 +514,7 @@ class SunspecInverter extends BaseSunspecDevice {
|
|
|
507
514
|
voltageL1: inverterData.voltageAN || 0,
|
|
508
515
|
voltageL2: inverterData.voltageBN ?? undefined,
|
|
509
516
|
voltageL3: inverterData.voltageCN ?? undefined,
|
|
510
|
-
meterValueWh: inverterData.acEnergy,
|
|
517
|
+
meterValueWh: this.sanitizeAcEnergyWh(inverterData.acEnergy, inverterData.acEnergyScaleFactor),
|
|
511
518
|
strings: dcStrings
|
|
512
519
|
}
|
|
513
520
|
};
|
|
@@ -533,6 +540,54 @@ class SunspecInverter extends BaseSunspecDevice {
|
|
|
533
540
|
}
|
|
534
541
|
return messages;
|
|
535
542
|
}
|
|
543
|
+
/**
|
|
544
|
+
* Guard the cumulative AC-energy reading before it is published as
|
|
545
|
+
* `meterValueWh`. The SunSpec `WH` register is an `acc32` (high word + low
|
|
546
|
+
* word) × 10^WH_SF; at a low-word rollover / wake-up the words can be
|
|
547
|
+
* mis-combined, so a poll jumps by exactly one 16-bit word
|
|
548
|
+
* (`65536 × 10^WH_SF`) up or down and back — Dirk Wasow's flip-flop of
|
|
549
|
+
* 6,553,600 Wh. A lifetime accumulator is also monotonic non-decreasing.
|
|
550
|
+
*
|
|
551
|
+
* So a reading is corrupt when it either DECREASES or JUMPS UP by ≥ half a
|
|
552
|
+
* register quantum (real per-poll production is orders of magnitude smaller):
|
|
553
|
+
* we republish the last good value rather than emit it (a raw `max-min`
|
|
554
|
+
* downstream would otherwise inflate it into ~a whole register word). A
|
|
555
|
+
* suspect level that persists for `AC_ENERGY_RESET_CONFIRM` consecutive polls
|
|
556
|
+
* is treated as a genuine reset/resync and adopted.
|
|
557
|
+
*
|
|
558
|
+
* Float models (111/112/113) report `WH` as an IEEE-754 float with no acc32
|
|
559
|
+
* word-boundary artifact; they leave `scaleFactor` undefined. For them there
|
|
560
|
+
* is no register quantum, so we enforce monotonicity only (reject decreases)
|
|
561
|
+
* and accept any increase — the ½-quantum upper bound applies to int+SF
|
|
562
|
+
* models (101/103) alone.
|
|
563
|
+
*/
|
|
564
|
+
sanitizeAcEnergyWh(raw, scaleFactor) {
|
|
565
|
+
if (raw == null)
|
|
566
|
+
return undefined;
|
|
567
|
+
if (this.lastGoodAcEnergyWh == null) {
|
|
568
|
+
this.lastGoodAcEnergyWh = raw;
|
|
569
|
+
this.acEnergyDropStreak = 0;
|
|
570
|
+
return raw;
|
|
571
|
+
}
|
|
572
|
+
const halfQuantumWh = scaleFactor != null ? 0.5 * 65536 * Math.pow(10, scaleFactor) : Infinity;
|
|
573
|
+
const delta = raw - this.lastGoodAcEnergyWh;
|
|
574
|
+
const plausible = delta >= 0 && delta < halfQuantumWh;
|
|
575
|
+
if (plausible) {
|
|
576
|
+
this.lastGoodAcEnergyWh = raw;
|
|
577
|
+
this.acEnergyDropStreak = 0;
|
|
578
|
+
return raw;
|
|
579
|
+
}
|
|
580
|
+
// A decrease or a ≥½-quantum jump — the acc32 word-boundary artifact.
|
|
581
|
+
this.acEnergyDropStreak++;
|
|
582
|
+
if (this.acEnergyDropStreak >= SunspecInverter.AC_ENERGY_RESET_CONFIRM) {
|
|
583
|
+
console.warn(`Notify_Devs: ${this.constructor.name} ${this.applianceId}: AC energy resync confirmed (${this.lastGoodAcEnergyWh} -> ${raw} Wh), adopting new baseline`);
|
|
584
|
+
this.lastGoodAcEnergyWh = raw;
|
|
585
|
+
this.acEnergyDropStreak = 0;
|
|
586
|
+
return raw;
|
|
587
|
+
}
|
|
588
|
+
console.warn(`Notify_Devs: ${this.constructor.name} ${this.applianceId}: rejecting corrupt AC energy reading ${raw} Wh (last good ${this.lastGoodAcEnergyWh} Wh, Δ=${delta}), republishing last good`);
|
|
589
|
+
return this.lastGoodAcEnergyWh;
|
|
590
|
+
}
|
|
536
591
|
/**
|
|
537
592
|
* Decode all active error bits from the inverter event registers into
|
|
538
593
|
* `EnyoApplianceErrorCode`s. Override in a subclass to customize the
|
|
@@ -720,6 +775,34 @@ class SunspecInverter extends BaseSunspecDevice {
|
|
|
720
775
|
return null;
|
|
721
776
|
return limitW;
|
|
722
777
|
}
|
|
778
|
+
/**
|
|
779
|
+
* Set the grid-operator power production (feed-in) limit on the inverter.
|
|
780
|
+
*
|
|
781
|
+
* Writes Model 123 `WMaxLimPct` + `WMaxLim_Ena` (as a percentage of the
|
|
782
|
+
* Model 121 `WMax` nameplate). Pass `null` to remove the limit and return
|
|
783
|
+
* the inverter to unthrottled production.
|
|
784
|
+
*
|
|
785
|
+
* The SDK exposes this so a consumer that receives a grid-operator
|
|
786
|
+
* production-limit command (from the data bus or elsewhere) can apply it;
|
|
787
|
+
* subscribing to and reacting to that command is the consumer's job.
|
|
788
|
+
*
|
|
789
|
+
* @param limitW - Maximum production power in Watts, or `null` to remove the limit
|
|
790
|
+
* @returns `true` if the write succeeded, `false` otherwise
|
|
791
|
+
*/
|
|
792
|
+
async setPowerProductionLimit(limitW) {
|
|
793
|
+
return this.sunspecClient.setFeedInLimit(this.unitId, limitW);
|
|
794
|
+
}
|
|
795
|
+
/**
|
|
796
|
+
* Read the currently active grid-operator power production (feed-in) limit
|
|
797
|
+
* from the inverter, in Watts. Returns `null` when no limit is active, the
|
|
798
|
+
* registers cannot be read, or the configured limit is not actually
|
|
799
|
+
* throttling (limit ≥ `WMax`). See {@link computeActiveProductionLimitW}.
|
|
800
|
+
*/
|
|
801
|
+
async getPowerProductionLimit() {
|
|
802
|
+
const settings = await this.sunspecClient.readInverterSettings(this.unitId);
|
|
803
|
+
const controls = await this.sunspecClient.readInverterControls(this.unitId);
|
|
804
|
+
return this.computeActiveProductionLimitW(settings, controls);
|
|
805
|
+
}
|
|
723
806
|
mapOperatingState(state) {
|
|
724
807
|
if (!state)
|
|
725
808
|
return enyo_data_bus_value_js_1.EnyoInverterStateEnum.Off;
|
|
@@ -142,10 +142,36 @@ export declare class SunspecInverter extends BaseSunspecDevice {
|
|
|
142
142
|
private errorState;
|
|
143
143
|
private snapshotService?;
|
|
144
144
|
private statusReassertedThisSession;
|
|
145
|
+
private lastGoodAcEnergyWh?;
|
|
146
|
+
private acEnergyDropStreak;
|
|
147
|
+
/** Consecutive corrupt reads that confirm a genuine reset/resync (vs a transient glitch). */
|
|
148
|
+
private static readonly AC_ENERGY_RESET_CONFIRM;
|
|
145
149
|
constructor(energyApp: EnergyApp, name: EnyoApplianceName[], networkDevice: EnyoNetworkDevice, sunspecClient: SunspecModbusClient, applianceManager: ApplianceManager, unitId?: number, port?: number, baseAddress?: number, capabilities?: SunspecInverterCapability[], retryConfig?: IRetryConfig, appliance?: EnyoAppliance, useTls?: boolean, pvPowerSource?: SunspecPvPowerSource);
|
|
146
150
|
connect(): Promise<void>;
|
|
147
151
|
disconnect(): Promise<void>;
|
|
148
|
-
readData(clockId: string, resolution:
|
|
152
|
+
readData(clockId: string, resolution: EnyoDataBusMessageResolution): Promise<EnyoDataBusMessage[]>;
|
|
153
|
+
/**
|
|
154
|
+
* Guard the cumulative AC-energy reading before it is published as
|
|
155
|
+
* `meterValueWh`. The SunSpec `WH` register is an `acc32` (high word + low
|
|
156
|
+
* word) × 10^WH_SF; at a low-word rollover / wake-up the words can be
|
|
157
|
+
* mis-combined, so a poll jumps by exactly one 16-bit word
|
|
158
|
+
* (`65536 × 10^WH_SF`) up or down and back — Dirk Wasow's flip-flop of
|
|
159
|
+
* 6,553,600 Wh. A lifetime accumulator is also monotonic non-decreasing.
|
|
160
|
+
*
|
|
161
|
+
* So a reading is corrupt when it either DECREASES or JUMPS UP by ≥ half a
|
|
162
|
+
* register quantum (real per-poll production is orders of magnitude smaller):
|
|
163
|
+
* we republish the last good value rather than emit it (a raw `max-min`
|
|
164
|
+
* downstream would otherwise inflate it into ~a whole register word). A
|
|
165
|
+
* suspect level that persists for `AC_ENERGY_RESET_CONFIRM` consecutive polls
|
|
166
|
+
* is treated as a genuine reset/resync and adopted.
|
|
167
|
+
*
|
|
168
|
+
* Float models (111/112/113) report `WH` as an IEEE-754 float with no acc32
|
|
169
|
+
* word-boundary artifact; they leave `scaleFactor` undefined. For them there
|
|
170
|
+
* is no register quantum, so we enforce monotonicity only (reject decreases)
|
|
171
|
+
* and accept any increase — the ½-quantum upper bound applies to int+SF
|
|
172
|
+
* models (101/103) alone.
|
|
173
|
+
*/
|
|
174
|
+
private sanitizeAcEnergyWh;
|
|
149
175
|
/**
|
|
150
176
|
* Decode all active error bits from the inverter event registers into
|
|
151
177
|
* `EnyoApplianceErrorCode`s. Override in a subclass to customize the
|
|
@@ -190,6 +216,28 @@ export declare class SunspecInverter extends BaseSunspecDevice {
|
|
|
190
216
|
* returned (not undefined) so the field is cleared on the appliance.
|
|
191
217
|
*/
|
|
192
218
|
private computeActiveProductionLimitW;
|
|
219
|
+
/**
|
|
220
|
+
* Set the grid-operator power production (feed-in) limit on the inverter.
|
|
221
|
+
*
|
|
222
|
+
* Writes Model 123 `WMaxLimPct` + `WMaxLim_Ena` (as a percentage of the
|
|
223
|
+
* Model 121 `WMax` nameplate). Pass `null` to remove the limit and return
|
|
224
|
+
* the inverter to unthrottled production.
|
|
225
|
+
*
|
|
226
|
+
* The SDK exposes this so a consumer that receives a grid-operator
|
|
227
|
+
* production-limit command (from the data bus or elsewhere) can apply it;
|
|
228
|
+
* subscribing to and reacting to that command is the consumer's job.
|
|
229
|
+
*
|
|
230
|
+
* @param limitW - Maximum production power in Watts, or `null` to remove the limit
|
|
231
|
+
* @returns `true` if the write succeeded, `false` otherwise
|
|
232
|
+
*/
|
|
233
|
+
setPowerProductionLimit(limitW: number | null): Promise<boolean>;
|
|
234
|
+
/**
|
|
235
|
+
* Read the currently active grid-operator power production (feed-in) limit
|
|
236
|
+
* from the inverter, in Watts. Returns `null` when no limit is active, the
|
|
237
|
+
* registers cannot be read, or the configured limit is not actually
|
|
238
|
+
* throttling (limit ≥ `WMax`). See {@link computeActiveProductionLimitW}.
|
|
239
|
+
*/
|
|
240
|
+
getPowerProductionLimit(): Promise<number | null>;
|
|
193
241
|
private mapOperatingState;
|
|
194
242
|
/**
|
|
195
243
|
* PV production in Watt, sourced according to the configured `pvPowerSource`:
|
|
@@ -366,7 +414,7 @@ export declare class SunspecBattery extends BaseSunspecDevice {
|
|
|
366
414
|
/**
|
|
367
415
|
* Update battery data and return data bus messages
|
|
368
416
|
*/
|
|
369
|
-
readData(clockId: string, resolution:
|
|
417
|
+
readData(clockId: string, resolution: EnyoDataBusMessageResolution): Promise<EnyoDataBusMessage[]>;
|
|
370
418
|
/**
|
|
371
419
|
* Extract battery power from MPPT strings with specific IDs
|
|
372
420
|
* StCha 3: Battery charging power (positive)
|
|
@@ -976,9 +976,11 @@ class SunspecModbusClient {
|
|
|
976
976
|
this.logRegisterRead(103, 50, 'Vendor Events 4', data.vendorEvents4, 'bitfield32');
|
|
977
977
|
const acEnergy = this.extractValue(buffer, 24, 'uint32', 2);
|
|
978
978
|
this.logRegisterRead(103, 24, 'AC Energy', acEnergy, 'acc32');
|
|
979
|
+
const whSf = this.isUnimplementedValue(scaleFactors.WH_SF, 'int16') ? 0 : scaleFactors.WH_SF;
|
|
979
980
|
data.acEnergy = !this.isUnimplementedValue(acEnergy, 'acc32')
|
|
980
|
-
? acEnergy * Math.pow(10,
|
|
981
|
+
? acEnergy * Math.pow(10, whSf)
|
|
981
982
|
: undefined;
|
|
983
|
+
data.acEnergyScaleFactor = whSf;
|
|
982
984
|
console.debug('[Model 103] Inverter Data:', data);
|
|
983
985
|
return data;
|
|
984
986
|
}
|
|
@@ -1038,9 +1040,11 @@ class SunspecModbusClient {
|
|
|
1038
1040
|
// Read AC Energy (32-bit accumulator) - Offset 24-25
|
|
1039
1041
|
const acEnergy = this.extractValue(buffer, 24, 'uint32', 2);
|
|
1040
1042
|
this.logRegisterRead(101, 24, 'AC Energy', acEnergy, 'acc32');
|
|
1043
|
+
const whSf = this.isUnimplementedValue(scaleFactors.WH_SF, 'int16') ? 0 : scaleFactors.WH_SF;
|
|
1041
1044
|
data.acEnergy = !this.isUnimplementedValue(acEnergy, 'acc32')
|
|
1042
|
-
? acEnergy * Math.pow(10,
|
|
1045
|
+
? acEnergy * Math.pow(10, whSf)
|
|
1043
1046
|
: undefined;
|
|
1047
|
+
data.acEnergyScaleFactor = whSf;
|
|
1044
1048
|
console.debug('[Model 101] Single Phase Inverter Data:', data);
|
|
1045
1049
|
return data;
|
|
1046
1050
|
}
|
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.96';
|
|
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
|
@@ -142,10 +142,36 @@ export declare class SunspecInverter extends BaseSunspecDevice {
|
|
|
142
142
|
private errorState;
|
|
143
143
|
private snapshotService?;
|
|
144
144
|
private statusReassertedThisSession;
|
|
145
|
+
private lastGoodAcEnergyWh?;
|
|
146
|
+
private acEnergyDropStreak;
|
|
147
|
+
/** Consecutive corrupt reads that confirm a genuine reset/resync (vs a transient glitch). */
|
|
148
|
+
private static readonly AC_ENERGY_RESET_CONFIRM;
|
|
145
149
|
constructor(energyApp: EnergyApp, name: EnyoApplianceName[], networkDevice: EnyoNetworkDevice, sunspecClient: SunspecModbusClient, applianceManager: ApplianceManager, unitId?: number, port?: number, baseAddress?: number, capabilities?: SunspecInverterCapability[], retryConfig?: IRetryConfig, appliance?: EnyoAppliance, useTls?: boolean, pvPowerSource?: SunspecPvPowerSource);
|
|
146
150
|
connect(): Promise<void>;
|
|
147
151
|
disconnect(): Promise<void>;
|
|
148
|
-
readData(clockId: string, resolution:
|
|
152
|
+
readData(clockId: string, resolution: EnyoDataBusMessageResolution): Promise<EnyoDataBusMessage[]>;
|
|
153
|
+
/**
|
|
154
|
+
* Guard the cumulative AC-energy reading before it is published as
|
|
155
|
+
* `meterValueWh`. The SunSpec `WH` register is an `acc32` (high word + low
|
|
156
|
+
* word) × 10^WH_SF; at a low-word rollover / wake-up the words can be
|
|
157
|
+
* mis-combined, so a poll jumps by exactly one 16-bit word
|
|
158
|
+
* (`65536 × 10^WH_SF`) up or down and back — Dirk Wasow's flip-flop of
|
|
159
|
+
* 6,553,600 Wh. A lifetime accumulator is also monotonic non-decreasing.
|
|
160
|
+
*
|
|
161
|
+
* So a reading is corrupt when it either DECREASES or JUMPS UP by ≥ half a
|
|
162
|
+
* register quantum (real per-poll production is orders of magnitude smaller):
|
|
163
|
+
* we republish the last good value rather than emit it (a raw `max-min`
|
|
164
|
+
* downstream would otherwise inflate it into ~a whole register word). A
|
|
165
|
+
* suspect level that persists for `AC_ENERGY_RESET_CONFIRM` consecutive polls
|
|
166
|
+
* is treated as a genuine reset/resync and adopted.
|
|
167
|
+
*
|
|
168
|
+
* Float models (111/112/113) report `WH` as an IEEE-754 float with no acc32
|
|
169
|
+
* word-boundary artifact; they leave `scaleFactor` undefined. For them there
|
|
170
|
+
* is no register quantum, so we enforce monotonicity only (reject decreases)
|
|
171
|
+
* and accept any increase — the ½-quantum upper bound applies to int+SF
|
|
172
|
+
* models (101/103) alone.
|
|
173
|
+
*/
|
|
174
|
+
private sanitizeAcEnergyWh;
|
|
149
175
|
/**
|
|
150
176
|
* Decode all active error bits from the inverter event registers into
|
|
151
177
|
* `EnyoApplianceErrorCode`s. Override in a subclass to customize the
|
|
@@ -190,6 +216,28 @@ export declare class SunspecInverter extends BaseSunspecDevice {
|
|
|
190
216
|
* returned (not undefined) so the field is cleared on the appliance.
|
|
191
217
|
*/
|
|
192
218
|
private computeActiveProductionLimitW;
|
|
219
|
+
/**
|
|
220
|
+
* Set the grid-operator power production (feed-in) limit on the inverter.
|
|
221
|
+
*
|
|
222
|
+
* Writes Model 123 `WMaxLimPct` + `WMaxLim_Ena` (as a percentage of the
|
|
223
|
+
* Model 121 `WMax` nameplate). Pass `null` to remove the limit and return
|
|
224
|
+
* the inverter to unthrottled production.
|
|
225
|
+
*
|
|
226
|
+
* The SDK exposes this so a consumer that receives a grid-operator
|
|
227
|
+
* production-limit command (from the data bus or elsewhere) can apply it;
|
|
228
|
+
* subscribing to and reacting to that command is the consumer's job.
|
|
229
|
+
*
|
|
230
|
+
* @param limitW - Maximum production power in Watts, or `null` to remove the limit
|
|
231
|
+
* @returns `true` if the write succeeded, `false` otherwise
|
|
232
|
+
*/
|
|
233
|
+
setPowerProductionLimit(limitW: number | null): Promise<boolean>;
|
|
234
|
+
/**
|
|
235
|
+
* Read the currently active grid-operator power production (feed-in) limit
|
|
236
|
+
* from the inverter, in Watts. Returns `null` when no limit is active, the
|
|
237
|
+
* registers cannot be read, or the configured limit is not actually
|
|
238
|
+
* throttling (limit ≥ `WMax`). See {@link computeActiveProductionLimitW}.
|
|
239
|
+
*/
|
|
240
|
+
getPowerProductionLimit(): Promise<number | null>;
|
|
193
241
|
private mapOperatingState;
|
|
194
242
|
/**
|
|
195
243
|
* PV production in Watt, sourced according to the configured `pvPowerSource`:
|
|
@@ -366,7 +414,7 @@ export declare class SunspecBattery extends BaseSunspecDevice {
|
|
|
366
414
|
/**
|
|
367
415
|
* Update battery data and return data bus messages
|
|
368
416
|
*/
|
|
369
|
-
readData(clockId: string, resolution:
|
|
417
|
+
readData(clockId: string, resolution: EnyoDataBusMessageResolution): Promise<EnyoDataBusMessage[]>;
|
|
370
418
|
/**
|
|
371
419
|
* Extract battery power from MPPT strings with specific IDs
|
|
372
420
|
* StCha 3: Battery charging power (positive)
|
package/dist/sunspec-devices.js
CHANGED
|
@@ -355,6 +355,13 @@ export class SunspecInverter extends BaseSunspecDevice {
|
|
|
355
355
|
// state reads healthy), so we force one re-assert of the current status on
|
|
356
356
|
// the first successful read of each session to converge the core.
|
|
357
357
|
statusReassertedThisSession = false;
|
|
358
|
+
// Last accepted cumulative AC-energy reading (Wh) and how many consecutive
|
|
359
|
+
// polls have reported a corrupt one (a decrease or a ≥½-register-quantum jump
|
|
360
|
+
// — the acc32 16-bit word-boundary artifact). See sanitizeAcEnergyWh.
|
|
361
|
+
lastGoodAcEnergyWh;
|
|
362
|
+
acEnergyDropStreak = 0;
|
|
363
|
+
/** Consecutive corrupt reads that confirm a genuine reset/resync (vs a transient glitch). */
|
|
364
|
+
static AC_ENERGY_RESET_CONFIRM = 3;
|
|
358
365
|
constructor(energyApp, name, networkDevice, sunspecClient, applianceManager, unitId = 1, port = 502, baseAddress = 40000, capabilities = [], retryConfig, appliance, useTls,
|
|
359
366
|
// Where pvPowerW is sourced from — see SunspecPvPowerSource. Defaults to
|
|
360
367
|
// Auto (MPPT strings, then Model 101 DCW/AC fallback). Use Mppt to report
|
|
@@ -500,7 +507,7 @@ export class SunspecInverter extends BaseSunspecDevice {
|
|
|
500
507
|
voltageL1: inverterData.voltageAN || 0,
|
|
501
508
|
voltageL2: inverterData.voltageBN ?? undefined,
|
|
502
509
|
voltageL3: inverterData.voltageCN ?? undefined,
|
|
503
|
-
meterValueWh: inverterData.acEnergy,
|
|
510
|
+
meterValueWh: this.sanitizeAcEnergyWh(inverterData.acEnergy, inverterData.acEnergyScaleFactor),
|
|
504
511
|
strings: dcStrings
|
|
505
512
|
}
|
|
506
513
|
};
|
|
@@ -526,6 +533,54 @@ export class SunspecInverter extends BaseSunspecDevice {
|
|
|
526
533
|
}
|
|
527
534
|
return messages;
|
|
528
535
|
}
|
|
536
|
+
/**
|
|
537
|
+
* Guard the cumulative AC-energy reading before it is published as
|
|
538
|
+
* `meterValueWh`. The SunSpec `WH` register is an `acc32` (high word + low
|
|
539
|
+
* word) × 10^WH_SF; at a low-word rollover / wake-up the words can be
|
|
540
|
+
* mis-combined, so a poll jumps by exactly one 16-bit word
|
|
541
|
+
* (`65536 × 10^WH_SF`) up or down and back — Dirk Wasow's flip-flop of
|
|
542
|
+
* 6,553,600 Wh. A lifetime accumulator is also monotonic non-decreasing.
|
|
543
|
+
*
|
|
544
|
+
* So a reading is corrupt when it either DECREASES or JUMPS UP by ≥ half a
|
|
545
|
+
* register quantum (real per-poll production is orders of magnitude smaller):
|
|
546
|
+
* we republish the last good value rather than emit it (a raw `max-min`
|
|
547
|
+
* downstream would otherwise inflate it into ~a whole register word). A
|
|
548
|
+
* suspect level that persists for `AC_ENERGY_RESET_CONFIRM` consecutive polls
|
|
549
|
+
* is treated as a genuine reset/resync and adopted.
|
|
550
|
+
*
|
|
551
|
+
* Float models (111/112/113) report `WH` as an IEEE-754 float with no acc32
|
|
552
|
+
* word-boundary artifact; they leave `scaleFactor` undefined. For them there
|
|
553
|
+
* is no register quantum, so we enforce monotonicity only (reject decreases)
|
|
554
|
+
* and accept any increase — the ½-quantum upper bound applies to int+SF
|
|
555
|
+
* models (101/103) alone.
|
|
556
|
+
*/
|
|
557
|
+
sanitizeAcEnergyWh(raw, scaleFactor) {
|
|
558
|
+
if (raw == null)
|
|
559
|
+
return undefined;
|
|
560
|
+
if (this.lastGoodAcEnergyWh == null) {
|
|
561
|
+
this.lastGoodAcEnergyWh = raw;
|
|
562
|
+
this.acEnergyDropStreak = 0;
|
|
563
|
+
return raw;
|
|
564
|
+
}
|
|
565
|
+
const halfQuantumWh = scaleFactor != null ? 0.5 * 65536 * Math.pow(10, scaleFactor) : Infinity;
|
|
566
|
+
const delta = raw - this.lastGoodAcEnergyWh;
|
|
567
|
+
const plausible = delta >= 0 && delta < halfQuantumWh;
|
|
568
|
+
if (plausible) {
|
|
569
|
+
this.lastGoodAcEnergyWh = raw;
|
|
570
|
+
this.acEnergyDropStreak = 0;
|
|
571
|
+
return raw;
|
|
572
|
+
}
|
|
573
|
+
// A decrease or a ≥½-quantum jump — the acc32 word-boundary artifact.
|
|
574
|
+
this.acEnergyDropStreak++;
|
|
575
|
+
if (this.acEnergyDropStreak >= SunspecInverter.AC_ENERGY_RESET_CONFIRM) {
|
|
576
|
+
console.warn(`Notify_Devs: ${this.constructor.name} ${this.applianceId}: AC energy resync confirmed (${this.lastGoodAcEnergyWh} -> ${raw} Wh), adopting new baseline`);
|
|
577
|
+
this.lastGoodAcEnergyWh = raw;
|
|
578
|
+
this.acEnergyDropStreak = 0;
|
|
579
|
+
return raw;
|
|
580
|
+
}
|
|
581
|
+
console.warn(`Notify_Devs: ${this.constructor.name} ${this.applianceId}: rejecting corrupt AC energy reading ${raw} Wh (last good ${this.lastGoodAcEnergyWh} Wh, Δ=${delta}), republishing last good`);
|
|
582
|
+
return this.lastGoodAcEnergyWh;
|
|
583
|
+
}
|
|
529
584
|
/**
|
|
530
585
|
* Decode all active error bits from the inverter event registers into
|
|
531
586
|
* `EnyoApplianceErrorCode`s. Override in a subclass to customize the
|
|
@@ -713,6 +768,34 @@ export class SunspecInverter extends BaseSunspecDevice {
|
|
|
713
768
|
return null;
|
|
714
769
|
return limitW;
|
|
715
770
|
}
|
|
771
|
+
/**
|
|
772
|
+
* Set the grid-operator power production (feed-in) limit on the inverter.
|
|
773
|
+
*
|
|
774
|
+
* Writes Model 123 `WMaxLimPct` + `WMaxLim_Ena` (as a percentage of the
|
|
775
|
+
* Model 121 `WMax` nameplate). Pass `null` to remove the limit and return
|
|
776
|
+
* the inverter to unthrottled production.
|
|
777
|
+
*
|
|
778
|
+
* The SDK exposes this so a consumer that receives a grid-operator
|
|
779
|
+
* production-limit command (from the data bus or elsewhere) can apply it;
|
|
780
|
+
* subscribing to and reacting to that command is the consumer's job.
|
|
781
|
+
*
|
|
782
|
+
* @param limitW - Maximum production power in Watts, or `null` to remove the limit
|
|
783
|
+
* @returns `true` if the write succeeded, `false` otherwise
|
|
784
|
+
*/
|
|
785
|
+
async setPowerProductionLimit(limitW) {
|
|
786
|
+
return this.sunspecClient.setFeedInLimit(this.unitId, limitW);
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* Read the currently active grid-operator power production (feed-in) limit
|
|
790
|
+
* from the inverter, in Watts. Returns `null` when no limit is active, the
|
|
791
|
+
* registers cannot be read, or the configured limit is not actually
|
|
792
|
+
* throttling (limit ≥ `WMax`). See {@link computeActiveProductionLimitW}.
|
|
793
|
+
*/
|
|
794
|
+
async getPowerProductionLimit() {
|
|
795
|
+
const settings = await this.sunspecClient.readInverterSettings(this.unitId);
|
|
796
|
+
const controls = await this.sunspecClient.readInverterControls(this.unitId);
|
|
797
|
+
return this.computeActiveProductionLimitW(settings, controls);
|
|
798
|
+
}
|
|
716
799
|
mapOperatingState(state) {
|
|
717
800
|
if (!state)
|
|
718
801
|
return EnyoInverterStateEnum.Off;
|
|
@@ -971,9 +971,11 @@ export class SunspecModbusClient {
|
|
|
971
971
|
this.logRegisterRead(103, 50, 'Vendor Events 4', data.vendorEvents4, 'bitfield32');
|
|
972
972
|
const acEnergy = this.extractValue(buffer, 24, 'uint32', 2);
|
|
973
973
|
this.logRegisterRead(103, 24, 'AC Energy', acEnergy, 'acc32');
|
|
974
|
+
const whSf = this.isUnimplementedValue(scaleFactors.WH_SF, 'int16') ? 0 : scaleFactors.WH_SF;
|
|
974
975
|
data.acEnergy = !this.isUnimplementedValue(acEnergy, 'acc32')
|
|
975
|
-
? acEnergy * Math.pow(10,
|
|
976
|
+
? acEnergy * Math.pow(10, whSf)
|
|
976
977
|
: undefined;
|
|
978
|
+
data.acEnergyScaleFactor = whSf;
|
|
977
979
|
console.debug('[Model 103] Inverter Data:', data);
|
|
978
980
|
return data;
|
|
979
981
|
}
|
|
@@ -1033,9 +1035,11 @@ export class SunspecModbusClient {
|
|
|
1033
1035
|
// Read AC Energy (32-bit accumulator) - Offset 24-25
|
|
1034
1036
|
const acEnergy = this.extractValue(buffer, 24, 'uint32', 2);
|
|
1035
1037
|
this.logRegisterRead(101, 24, 'AC Energy', acEnergy, 'acc32');
|
|
1038
|
+
const whSf = this.isUnimplementedValue(scaleFactors.WH_SF, 'int16') ? 0 : scaleFactors.WH_SF;
|
|
1036
1039
|
data.acEnergy = !this.isUnimplementedValue(acEnergy, 'acc32')
|
|
1037
|
-
? acEnergy * Math.pow(10,
|
|
1040
|
+
? acEnergy * Math.pow(10, whSf)
|
|
1038
1041
|
: undefined;
|
|
1042
|
+
data.acEnergyScaleFactor = whSf;
|
|
1039
1043
|
console.debug('[Model 101] Single Phase Inverter Data:', data);
|
|
1040
1044
|
return data;
|
|
1041
1045
|
}
|
package/dist/version.d.ts
CHANGED
package/dist/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@enyo-energy/sunspec-sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.96",
|
|
4
4
|
"description": "enyo Energy Sunspec SDK",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@enyo-energy/appliance-calibration": "0.0.2",
|
|
44
|
-
"@enyo-energy/energy-app-sdk": "^0.0.
|
|
44
|
+
"@enyo-energy/energy-app-sdk": "^0.0.175"
|
|
45
45
|
},
|
|
46
46
|
"volta": {
|
|
47
47
|
"node": "22.17.0"
|