@enyo-energy/sunspec-sdk 1.6.0 → 1.8.0
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-battery-feature-calibrator.cjs +5 -3
- package/dist/cjs/sunspec-battery-feature-calibrator.d.cts +4 -1
- package/dist/cjs/sunspec-battery-grid-charge.cjs +32 -0
- package/dist/cjs/sunspec-battery-grid-charge.d.cts +9 -0
- package/dist/cjs/sunspec-battery-schedule-handler.cjs +6 -6
- package/dist/cjs/sunspec-battery-schedule-handler.d.cts +4 -1
- package/dist/cjs/sunspec-devices.cjs +38 -11
- package/dist/cjs/sunspec-devices.d.cts +15 -2
- package/dist/cjs/sunspec-interfaces.cjs +25 -1
- package/dist/cjs/sunspec-interfaces.d.cts +28 -0
- package/dist/cjs/sunspec-modbus-client.cjs +104 -22
- package/dist/cjs/sunspec-modbus-client.d.cts +18 -4
- package/dist/cjs/version.cjs +1 -1
- package/dist/cjs/version.d.cts +1 -1
- package/dist/sunspec-battery-feature-calibrator.d.ts +4 -1
- package/dist/sunspec-battery-feature-calibrator.js +6 -4
- package/dist/sunspec-battery-grid-charge.d.ts +9 -0
- package/dist/sunspec-battery-grid-charge.js +28 -0
- package/dist/sunspec-battery-schedule-handler.d.ts +4 -1
- package/dist/sunspec-battery-schedule-handler.js +7 -7
- package/dist/sunspec-devices.d.ts +15 -2
- package/dist/sunspec-devices.js +39 -12
- package/dist/sunspec-interfaces.d.ts +28 -0
- package/dist/sunspec-interfaces.js +24 -0
- package/dist/sunspec-modbus-client.d.ts +18 -4
- package/dist/sunspec-modbus-client.js +105 -23
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -2
|
@@ -555,6 +555,11 @@ export declare const MIN_BATTERY_RATE_REVERT_TIMEOUT_SECONDS = 15;
|
|
|
555
555
|
* "not implemented" marker.
|
|
556
556
|
*/
|
|
557
557
|
export declare const MAX_BATTERY_RATE_REVERT_TIMEOUT_SECONDS = 65534;
|
|
558
|
+
/**
|
|
559
|
+
* How often a battery limit write re-asserts InOutWRte_RvrtTms. Once per connect was not enough:
|
|
560
|
+
* a device can come up with, or be reset to, a value that drops every limit within a second.
|
|
561
|
+
*/
|
|
562
|
+
export declare const BATTERY_RATE_REVERT_REASSERT_MS = 60000;
|
|
558
563
|
/**
|
|
559
564
|
* Battery data structure based on Model 124 (Basic Storage Controls)
|
|
560
565
|
*
|
|
@@ -977,6 +982,24 @@ export declare enum SunspecBatteryCapability {
|
|
|
977
982
|
DischargeLimit = "discharge-limit",
|
|
978
983
|
ChargeLimit = "charge-limit"
|
|
979
984
|
}
|
|
985
|
+
/**
|
|
986
|
+
* How a Model 124 battery is made to charge from the grid. Vendors read the rate setpoints
|
|
987
|
+
* differently, so the host picks the form its hardware actually acts on.
|
|
988
|
+
*/
|
|
989
|
+
export declare enum SunspecGridChargeMethod {
|
|
990
|
+
/**
|
|
991
|
+
* StorCtl_Mod bit 0 + InWRte = the target rate. A charge-rate *cap* on devices that treat
|
|
992
|
+
* InWRte as a maximum: with no PV surplus, nothing is charged. The historical default.
|
|
993
|
+
*/
|
|
994
|
+
ChargeRateLimit = "charge-rate-limit",
|
|
995
|
+
/**
|
|
996
|
+
* StorCtl_Mod bit 1 + a negative OutWRte (-rate %), InWRte opened to 100 %. A discharge limit
|
|
997
|
+
* below zero is a minimum charge rate, which forces the charge whatever the PV does. Required
|
|
998
|
+
* by Fronius GEN24, where the bit-0 form only caps PV charging (same writes as evcc's and the
|
|
999
|
+
* Home Assistant fronius_modbus integration's "charge from grid").
|
|
1000
|
+
*/
|
|
1001
|
+
NegativeDischargeRate = "negative-discharge-rate"
|
|
1002
|
+
}
|
|
980
1003
|
/**
|
|
981
1004
|
* Battery facts that live outside the SunSpec register map, published onto `appliance.battery` when
|
|
982
1005
|
* the device connects. Fields left `undefined` are not written, so an existing value in the
|
|
@@ -996,6 +1019,11 @@ export interface SunspecBatteryMetadataConfig {
|
|
|
996
1019
|
*/
|
|
997
1020
|
roundTripEfficiencyPercent?: number;
|
|
998
1021
|
}
|
|
1022
|
+
/** Per-battery configuration: the published metadata plus how the device is driven. */
|
|
1023
|
+
export interface SunspecBatteryConfig extends SunspecBatteryMetadataConfig {
|
|
1024
|
+
/** Register form for a grid charge. Defaults to {@link SunspecGridChargeMethod.ChargeRateLimit}. */
|
|
1025
|
+
gridChargeMethod?: SunspecGridChargeMethod;
|
|
1026
|
+
}
|
|
999
1027
|
/**
|
|
1000
1028
|
* Selects how `SunspecBattery` populates `appliance.battery.features`.
|
|
1001
1029
|
*
|
|
@@ -247,6 +247,11 @@ export const MIN_BATTERY_RATE_REVERT_TIMEOUT_SECONDS = 15;
|
|
|
247
247
|
* "not implemented" marker.
|
|
248
248
|
*/
|
|
249
249
|
export const MAX_BATTERY_RATE_REVERT_TIMEOUT_SECONDS = 0xFFFE;
|
|
250
|
+
/**
|
|
251
|
+
* How often a battery limit write re-asserts InOutWRte_RvrtTms. Once per connect was not enough:
|
|
252
|
+
* a device can come up with, or be reset to, a value that drops every limit within a second.
|
|
253
|
+
*/
|
|
254
|
+
export const BATTERY_RATE_REVERT_REASSERT_MS = 60_000;
|
|
250
255
|
/**
|
|
251
256
|
* Enum values for connection control
|
|
252
257
|
*/
|
|
@@ -345,6 +350,25 @@ export var SunspecBatteryCapability;
|
|
|
345
350
|
SunspecBatteryCapability["DischargeLimit"] = "discharge-limit";
|
|
346
351
|
SunspecBatteryCapability["ChargeLimit"] = "charge-limit";
|
|
347
352
|
})(SunspecBatteryCapability || (SunspecBatteryCapability = {}));
|
|
353
|
+
/**
|
|
354
|
+
* How a Model 124 battery is made to charge from the grid. Vendors read the rate setpoints
|
|
355
|
+
* differently, so the host picks the form its hardware actually acts on.
|
|
356
|
+
*/
|
|
357
|
+
export var SunspecGridChargeMethod;
|
|
358
|
+
(function (SunspecGridChargeMethod) {
|
|
359
|
+
/**
|
|
360
|
+
* StorCtl_Mod bit 0 + InWRte = the target rate. A charge-rate *cap* on devices that treat
|
|
361
|
+
* InWRte as a maximum: with no PV surplus, nothing is charged. The historical default.
|
|
362
|
+
*/
|
|
363
|
+
SunspecGridChargeMethod["ChargeRateLimit"] = "charge-rate-limit";
|
|
364
|
+
/**
|
|
365
|
+
* StorCtl_Mod bit 1 + a negative OutWRte (-rate %), InWRte opened to 100 %. A discharge limit
|
|
366
|
+
* below zero is a minimum charge rate, which forces the charge whatever the PV does. Required
|
|
367
|
+
* by Fronius GEN24, where the bit-0 form only caps PV charging (same writes as evcc's and the
|
|
368
|
+
* Home Assistant fronius_modbus integration's "charge from grid").
|
|
369
|
+
*/
|
|
370
|
+
SunspecGridChargeMethod["NegativeDischargeRate"] = "negative-discharge-rate";
|
|
371
|
+
})(SunspecGridChargeMethod || (SunspecGridChargeMethod = {}));
|
|
348
372
|
/**
|
|
349
373
|
* Selects how `SunspecBattery` populates `appliance.battery.features`.
|
|
350
374
|
*
|
|
@@ -141,6 +141,8 @@ export declare class SunspecModbusClient {
|
|
|
141
141
|
private unitSessionReportedLost;
|
|
142
142
|
private batteryRateRevertTimeoutSeconds;
|
|
143
143
|
private batteryRateRevertWarnedUnits;
|
|
144
|
+
private batteryRateRevertAssertedAt;
|
|
145
|
+
private batteryRateRevertMismatchWarned;
|
|
144
146
|
constructor(energyApp: EnergyApp | undefined, transport?: IModbusTransportConfig, resilience?: IModbusResilienceConfig, modbusFactory?: EnergyAppModbus | undefined);
|
|
145
147
|
/**
|
|
146
148
|
* The Modbus backend used to open per-unit connections: the caller-supplied {@link modbusFactory}
|
|
@@ -677,8 +679,9 @@ export declare class SunspecModbusClient {
|
|
|
677
679
|
*/
|
|
678
680
|
readBatteryData(unitId: number): Promise<SunspecBatteryData | null>;
|
|
679
681
|
/**
|
|
680
|
-
* Set the Model 124 `InOutWRte_RvrtTms` (seconds) written
|
|
681
|
-
*
|
|
682
|
+
* Set the Model 124 `InOutWRte_RvrtTms` (seconds) written when the battery connects,
|
|
683
|
+
* after every reconnect, and again by a limit write at most every
|
|
684
|
+
* {@link BATTERY_RATE_REVERT_REASSERT_MS} — e.g. 30 or 60. When no fresh rate write arrives within this window
|
|
682
685
|
* the device drops the limit and manages itself again, so a hub that goes
|
|
683
686
|
* offline cannot leave the battery blocked. The schedule handler refreshes an
|
|
684
687
|
* active limit every third of this value.
|
|
@@ -698,8 +701,10 @@ export declare class SunspecModbusClient {
|
|
|
698
701
|
writeBatteryControls(unitId: number, controls: Partial<SunspecBatteryControls>): Promise<boolean>;
|
|
699
702
|
/**
|
|
700
703
|
* Write the configured InOutWRte_RvrtTms (Model 124 offset 15) to the device.
|
|
701
|
-
* Called
|
|
702
|
-
*
|
|
704
|
+
* Called by `SunspecBattery.connect()`, once per recovered connection, and by
|
|
705
|
+
* {@link writeBatteryControls} before a limit when the last write is older than
|
|
706
|
+
* {@link BATTERY_RATE_REVERT_REASSERT_MS} or a readback disagreed. The device should keep
|
|
707
|
+
* the value, but one GEN24 in the field sat on 1 s anyway.
|
|
703
708
|
*
|
|
704
709
|
* Never throws: a device that rejects the register still takes limits, it just
|
|
705
710
|
* won't revert them on its own (the pre-existing behaviour). The first failure
|
|
@@ -709,6 +714,15 @@ export declare class SunspecModbusClient {
|
|
|
709
714
|
* or the write failed.
|
|
710
715
|
*/
|
|
711
716
|
writeBatteryRateRevertTimeout(unitId: number): Promise<boolean>;
|
|
717
|
+
/** Whether a limit write should re-assert InOutWRte_RvrtTms first. */
|
|
718
|
+
private isBatteryRateRevertDue;
|
|
719
|
+
/**
|
|
720
|
+
* Compare a Model 124 readback of InOutWRte_RvrtTms with the configured value. A mismatch
|
|
721
|
+
* means our write never landed or something reset it. A new foreign value is logged once and
|
|
722
|
+
* makes the next limit write re-assert immediately; if the same value is still there after
|
|
723
|
+
* that, the device clamps or ignores the write and re-asserts fall back to the interval.
|
|
724
|
+
*/
|
|
725
|
+
private checkBatteryRateRevertReadback;
|
|
712
726
|
/**
|
|
713
727
|
* Set battery storage mode (simplified interface)
|
|
714
728
|
*/
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* - pad: 0x8000 (always returns this value)
|
|
17
17
|
* - string: all registers 0x0000 (NULL)
|
|
18
18
|
*/
|
|
19
|
-
import { SunspecModelId, SunspecBatteryChargeState, SunspecStorageControlMode, SunspecChargeSource, SunspecEnableControl, SunspecStorageMode, SunspecBatteryType, SunspecBatteryBankState, DEFAULT_MODBUS_RESILIENCE_CONFIG, DEFAULT_BATTERY_RATE_REVERT_TIMEOUT_SECONDS, MIN_BATTERY_RATE_REVERT_TIMEOUT_SECONDS, MAX_BATTERY_RATE_REVERT_TIMEOUT_SECONDS, } from "./sunspec-interfaces.js";
|
|
19
|
+
import { SunspecModelId, SunspecBatteryChargeState, SunspecStorageControlMode, SunspecChargeSource, SunspecEnableControl, SunspecStorageMode, SunspecBatteryType, SunspecBatteryBankState, DEFAULT_MODBUS_RESILIENCE_CONFIG, DEFAULT_BATTERY_RATE_REVERT_TIMEOUT_SECONDS, BATTERY_RATE_REVERT_REASSERT_MS, MIN_BATTERY_RATE_REVERT_TIMEOUT_SECONDS, MAX_BATTERY_RATE_REVERT_TIMEOUT_SECONDS, } from "./sunspec-interfaces.js";
|
|
20
20
|
import { EnergyAppModbusConnectionHealth } from "@enyo-energy/energy-app-sdk/dist/implementations/modbus/EnergyAppModbusConnectionHealth.js";
|
|
21
21
|
import { EnergyAppModbusFaultTolerantReader } from "@enyo-energy/energy-app-sdk/dist/implementations/modbus/EnergyAppModbusFaultTolerantReader.js";
|
|
22
22
|
import { EnergyAppModbusDataTypeConverter } from "@enyo-energy/energy-app-sdk/dist/implementations/modbus/EnergyAppModbusDataTypeConverter.js";
|
|
@@ -245,12 +245,21 @@ export class SunspecModbusClient {
|
|
|
245
245
|
// announces itself twice — once by the transport, once by this client's own close/reopen —
|
|
246
246
|
// and an app must not be told to re-authenticate twice for one loss.
|
|
247
247
|
unitSessionReportedLost = new Set();
|
|
248
|
-
// Model 124 InOutWRte_RvrtTms written
|
|
249
|
-
// setBatteryRateRevertTimeoutSeconds().
|
|
248
|
+
// Model 124 InOutWRte_RvrtTms written when a battery connects and re-asserted by limit writes.
|
|
249
|
+
// See setBatteryRateRevertTimeoutSeconds().
|
|
250
250
|
batteryRateRevertTimeoutSeconds = DEFAULT_BATTERY_RATE_REVERT_TIMEOUT_SECONDS;
|
|
251
251
|
// Units whose InOutWRte_RvrtTms write has already failed and been warned about. Later
|
|
252
252
|
// failures on the same unit stay silent so a device without the register warns once.
|
|
253
253
|
batteryRateRevertWarnedUnits = new Set();
|
|
254
|
+
// When InOutWRte_RvrtTms was last written, per unit — failed attempts included, so a device
|
|
255
|
+
// that rejects the register is retried once per interval rather than on every limit write.
|
|
256
|
+
// A limit write re-asserts it once this is older than BATTERY_RATE_REVERT_REASSERT_MS, or
|
|
257
|
+
// straight away after a readback showed a new foreign value (the entry is then dropped).
|
|
258
|
+
batteryRateRevertAssertedAt = new Map();
|
|
259
|
+
// Last RvrtTms readback that disagreed with the configured value, per unit. A device stuck on
|
|
260
|
+
// that value warns and triggers an immediate re-assert once; after that it waits for the
|
|
261
|
+
// interval, so a device that clamps or ignores the write is not rewritten on every limit.
|
|
262
|
+
batteryRateRevertMismatchWarned = new Map();
|
|
254
263
|
constructor(
|
|
255
264
|
// The host application. Only used to obtain the default Modbus backend via
|
|
256
265
|
// `energyApp.useModbus()`. May be omitted (`undefined`) when a `modbusFactory` is supplied.
|
|
@@ -595,6 +604,9 @@ export class SunspecModbusClient {
|
|
|
595
604
|
*/
|
|
596
605
|
async _openUnitOnce(host, port, unitId, connectTimeoutMs = this.resilience.connectTimeoutMs, useFallbackHosts = true) {
|
|
597
606
|
let candidate = null;
|
|
607
|
+
// Set once this attempt has failed; a connect that settles after that is nobody's socket.
|
|
608
|
+
let abandoned = false;
|
|
609
|
+
let connected = null;
|
|
598
610
|
const hosts = useFallbackHosts ? this.buildConnectHosts(host) : host;
|
|
599
611
|
const hostCount = Array.isArray(hosts) ? hosts.length : 1;
|
|
600
612
|
// Each host in the list gets its own `timeout`; bound the whole walk at count × budget so we
|
|
@@ -604,20 +616,40 @@ export class SunspecModbusClient {
|
|
|
604
616
|
// Enforce the connect budget ourselves in addition to forwarding it as the transport's
|
|
605
617
|
// `timeout`: the transport applies it to the TCP handshake, but a sleeping/half-open peer
|
|
606
618
|
// can still leave the returned promise pending — bound it independently.
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
619
|
+
//
|
|
620
|
+
// Losing that race must not leak the socket: the transport does not bound a TLS
|
|
621
|
+
// handshake, so it can still complete after we gave up. Nobody would own that
|
|
622
|
+
// instance, and with a dedicated (non-pooled) connection it stays a live session on
|
|
623
|
+
// the device — a KOSTAL KSEM caps its Modbus/TLS clients and locks us out once enough
|
|
624
|
+
// timed-out attempts have piled up. Close whatever settles after the attempt failed.
|
|
625
|
+
candidate = await this.withTimeout('connect', unitId, overallBudgetMs, () => {
|
|
626
|
+
const connecting = this.useModbus().connect({
|
|
627
|
+
host: hosts,
|
|
628
|
+
port,
|
|
629
|
+
unitId,
|
|
630
|
+
timeout: connectTimeoutMs,
|
|
631
|
+
useTls: this.connectionParams?.useTls,
|
|
632
|
+
cert: this.connectionParams?.cert,
|
|
633
|
+
key: this.connectionParams?.key,
|
|
634
|
+
ca: this.connectionParams?.ca,
|
|
635
|
+
rejectUnauthorized: this.connectionParams?.rejectUnauthorized,
|
|
636
|
+
noParallelRequests: this.transport.noParallelRequests,
|
|
637
|
+
waitBetweenMessagesMs: this.transport.waitBetweenMessagesMs,
|
|
638
|
+
pooledConnection: this.transport.pooledConnection
|
|
639
|
+
});
|
|
640
|
+
connecting.then(instance => {
|
|
641
|
+
if (!instance)
|
|
642
|
+
return;
|
|
643
|
+
if (!abandoned) {
|
|
644
|
+
connected = instance;
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
console.warn(`SunspecModbusClient: connect to ${host}:${port} unit ${unitId} completed after its ` +
|
|
648
|
+
`${overallBudgetMs}ms budget — closing the orphaned socket`);
|
|
649
|
+
void this.disconnectInstanceBounded(instance, unitId);
|
|
650
|
+
}, () => { });
|
|
651
|
+
return connecting;
|
|
652
|
+
});
|
|
621
653
|
if (!candidate) {
|
|
622
654
|
throw new Error(`useModbus().connect returned null for ${host}:${port} unit ${unitId}`);
|
|
623
655
|
}
|
|
@@ -644,8 +676,12 @@ export class SunspecModbusClient {
|
|
|
644
676
|
this.recordOpen();
|
|
645
677
|
}
|
|
646
678
|
catch (err) {
|
|
647
|
-
|
|
648
|
-
|
|
679
|
+
abandoned = true;
|
|
680
|
+
// `connected` covers a connect that settled after the budget fired but before we got
|
|
681
|
+
// here; one that settles even later is closed by the handler on the connect promise.
|
|
682
|
+
const orphan = candidate ?? connected;
|
|
683
|
+
if (orphan) {
|
|
684
|
+
await this.disconnectInstanceBounded(orphan, unitId);
|
|
649
685
|
}
|
|
650
686
|
this.modbusInstances.delete(unitId);
|
|
651
687
|
this.faultTolerantReaders.delete(unitId);
|
|
@@ -2458,6 +2494,7 @@ export class SunspecModbusClient {
|
|
|
2458
2494
|
// the object across many lines; stringify keeps the whole snapshot
|
|
2459
2495
|
// on one log entry so it stays grep-able and round-trippable.
|
|
2460
2496
|
console.debug(`[Battery] unit=${unitId} model=124 data=${JSON.stringify(data)}`);
|
|
2497
|
+
this.checkBatteryRateRevertReadback(unitId, data.inOutWRteRvrtTms);
|
|
2461
2498
|
return data;
|
|
2462
2499
|
}
|
|
2463
2500
|
else if (model.id === 802) {
|
|
@@ -2514,8 +2551,9 @@ export class SunspecModbusClient {
|
|
|
2514
2551
|
}
|
|
2515
2552
|
}
|
|
2516
2553
|
/**
|
|
2517
|
-
* Set the Model 124 `InOutWRte_RvrtTms` (seconds) written
|
|
2518
|
-
*
|
|
2554
|
+
* Set the Model 124 `InOutWRte_RvrtTms` (seconds) written when the battery connects,
|
|
2555
|
+
* after every reconnect, and again by a limit write at most every
|
|
2556
|
+
* {@link BATTERY_RATE_REVERT_REASSERT_MS} — e.g. 30 or 60. When no fresh rate write arrives within this window
|
|
2519
2557
|
* the device drops the limit and manages itself again, so a hub that goes
|
|
2520
2558
|
* offline cannot leave the battery blocked. The schedule handler refreshes an
|
|
2521
2559
|
* active limit every third of this value.
|
|
@@ -2534,6 +2572,8 @@ export class SunspecModbusClient {
|
|
|
2534
2572
|
`${MIN_BATTERY_RATE_REVERT_TIMEOUT_SECONDS} and ${MAX_BATTERY_RATE_REVERT_TIMEOUT_SECONDS}`);
|
|
2535
2573
|
}
|
|
2536
2574
|
this.batteryRateRevertTimeoutSeconds = seconds;
|
|
2575
|
+
// A new value is due on the next limit write, not a minute later.
|
|
2576
|
+
this.batteryRateRevertAssertedAt.clear();
|
|
2537
2577
|
}
|
|
2538
2578
|
/** Current Model 124 `InOutWRte_RvrtTms` in seconds. */
|
|
2539
2579
|
getBatteryRateRevertTimeoutSeconds() {
|
|
@@ -2600,6 +2640,13 @@ export class SunspecModbusClient {
|
|
|
2600
2640
|
// Storage control mode (Register 5) — written LAST so all
|
|
2601
2641
|
// governing parameters are already in place when the device
|
|
2602
2642
|
// transitions into the new mode.
|
|
2643
|
+
// A limit only holds as long as InOutWRte_RvrtTms allows. Re-assert our value before
|
|
2644
|
+
// the mode write so the new limit gets the full window — a device left on a foreign
|
|
2645
|
+
// value (1 s on a GEN24 in the field) drops every limit a second after each write.
|
|
2646
|
+
if (controls.storCtlMod !== undefined && controls.storCtlMod !== 0
|
|
2647
|
+
&& this.isBatteryRateRevertDue(unitId)) {
|
|
2648
|
+
await this.writeBatteryRateRevertTimeout(unitId);
|
|
2649
|
+
}
|
|
2603
2650
|
if (controls.storCtlMod !== undefined) {
|
|
2604
2651
|
// storCtlMod = 0 is rejected with Modbus Exception code 3 (Illegal Data Value)
|
|
2605
2652
|
// by some devices (e.g. Fronius GEN24). 0 means "no external control" per the
|
|
@@ -2623,8 +2670,10 @@ export class SunspecModbusClient {
|
|
|
2623
2670
|
}
|
|
2624
2671
|
/**
|
|
2625
2672
|
* Write the configured InOutWRte_RvrtTms (Model 124 offset 15) to the device.
|
|
2626
|
-
* Called
|
|
2627
|
-
*
|
|
2673
|
+
* Called by `SunspecBattery.connect()`, once per recovered connection, and by
|
|
2674
|
+
* {@link writeBatteryControls} before a limit when the last write is older than
|
|
2675
|
+
* {@link BATTERY_RATE_REVERT_REASSERT_MS} or a readback disagreed. The device should keep
|
|
2676
|
+
* the value, but one GEN24 in the field sat on 1 s anyway.
|
|
2628
2677
|
*
|
|
2629
2678
|
* Never throws: a device that rejects the register still takes limits, it just
|
|
2630
2679
|
* won't revert them on its own (the pre-existing behaviour). The first failure
|
|
@@ -2641,6 +2690,9 @@ export class SunspecModbusClient {
|
|
|
2641
2690
|
return false;
|
|
2642
2691
|
}
|
|
2643
2692
|
const seconds = this.batteryRateRevertTimeoutSeconds;
|
|
2693
|
+
// Stamp the attempt, not the success: a device that rejects the register must not be
|
|
2694
|
+
// retried (and logged as a failed write) on every limit write.
|
|
2695
|
+
this.batteryRateRevertAssertedAt.set(unitId, Date.now());
|
|
2644
2696
|
try {
|
|
2645
2697
|
await this.writeRegisterValue(unitId, model.address + 15, seconds, 'uint16');
|
|
2646
2698
|
console.log(`Unit ${unitId}: set battery rate revert timeout (InOutWRte_RvrtTms) to ${seconds}s`);
|
|
@@ -2655,6 +2707,36 @@ export class SunspecModbusClient {
|
|
|
2655
2707
|
return false;
|
|
2656
2708
|
}
|
|
2657
2709
|
}
|
|
2710
|
+
/** Whether a limit write should re-assert InOutWRte_RvrtTms first. */
|
|
2711
|
+
isBatteryRateRevertDue(unitId) {
|
|
2712
|
+
const assertedAt = this.batteryRateRevertAssertedAt.get(unitId);
|
|
2713
|
+
return assertedAt === undefined || Date.now() - assertedAt >= BATTERY_RATE_REVERT_REASSERT_MS;
|
|
2714
|
+
}
|
|
2715
|
+
/**
|
|
2716
|
+
* Compare a Model 124 readback of InOutWRte_RvrtTms with the configured value. A mismatch
|
|
2717
|
+
* means our write never landed or something reset it. A new foreign value is logged once and
|
|
2718
|
+
* makes the next limit write re-assert immediately; if the same value is still there after
|
|
2719
|
+
* that, the device clamps or ignores the write and re-asserts fall back to the interval.
|
|
2720
|
+
*/
|
|
2721
|
+
checkBatteryRateRevertReadback(unitId, readback) {
|
|
2722
|
+
if (readback === undefined)
|
|
2723
|
+
return;
|
|
2724
|
+
// Nothing written since connect or since setBatteryRateRevertTimeoutSeconds(): the device
|
|
2725
|
+
// still holding the old value is expected, and the next limit write asserts anyway.
|
|
2726
|
+
if (!this.batteryRateRevertAssertedAt.has(unitId))
|
|
2727
|
+
return;
|
|
2728
|
+
const expected = this.batteryRateRevertTimeoutSeconds;
|
|
2729
|
+
if (readback === expected) {
|
|
2730
|
+
this.batteryRateRevertMismatchWarned.delete(unitId);
|
|
2731
|
+
return;
|
|
2732
|
+
}
|
|
2733
|
+
if (this.batteryRateRevertMismatchWarned.get(unitId) !== readback) {
|
|
2734
|
+
this.batteryRateRevertMismatchWarned.set(unitId, readback);
|
|
2735
|
+
this.batteryRateRevertAssertedAt.delete(unitId);
|
|
2736
|
+
console.warn(`Unit ${unitId}: InOutWRte_RvrtTms reads ${readback}s, expected ${expected}s — ` +
|
|
2737
|
+
`re-asserting with the next battery limit write`);
|
|
2738
|
+
}
|
|
2739
|
+
}
|
|
2658
2740
|
/**
|
|
2659
2741
|
* Set battery storage mode (simplified interface)
|
|
2660
2742
|
*/
|
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": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "enyo Energy Sunspec SDK",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@enyo-energy/appliance-calibration": "0.0.3",
|
|
44
|
-
"@enyo-energy/energy-app-sdk": "1.
|
|
44
|
+
"@enyo-energy/energy-app-sdk": "1.42.0"
|
|
45
45
|
},
|
|
46
46
|
"volta": {
|
|
47
47
|
"node": "22.17.0"
|