@enyo-energy/sunspec-sdk 0.0.104 → 1.0.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/README.md +90 -0
- package/dist/cjs/sunspec-battery-schedule-handler.cjs +76 -28
- package/dist/cjs/sunspec-battery-schedule-handler.d.cts +33 -6
- package/dist/cjs/sunspec-devices.cjs +140 -29
- package/dist/cjs/sunspec-devices.d.cts +47 -3
- package/dist/cjs/sunspec-interfaces.cjs +9 -2
- package/dist/cjs/sunspec-interfaces.d.cts +73 -4
- package/dist/cjs/sunspec-modbus-client.cjs +280 -116
- package/dist/cjs/sunspec-modbus-client.d.cts +82 -5
- package/dist/cjs/version.cjs +1 -1
- package/dist/cjs/version.d.cts +1 -1
- package/dist/sunspec-battery-schedule-handler.d.ts +33 -6
- package/dist/sunspec-battery-schedule-handler.js +76 -28
- package/dist/sunspec-devices.d.ts +47 -3
- package/dist/sunspec-devices.js +139 -29
- package/dist/sunspec-interfaces.d.ts +73 -4
- package/dist/sunspec-interfaces.js +9 -2
- package/dist/sunspec-modbus-client.d.ts +82 -5
- package/dist/sunspec-modbus-client.js +277 -115
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -2
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* - pad: 0x8000 (always returns this value)
|
|
17
17
|
* - string: all registers 0x0000 (NULL)
|
|
18
18
|
*/
|
|
19
|
-
import { type SunspecInverterControls, type SunspecInverterData, type SunspecInverterSettings, type SunspecMeterData, type SunspecModel, type SunspecMPPTData, type SunspecBatteryData, type SunspecBatteryBaseData, type SunspecBatteryControls, SunspecStorageMode, type
|
|
19
|
+
import { type SunspecInverterControls, type SunspecInverterData, type SunspecInverterSettings, type SunspecMeterData, type SunspecModel, type SunspecMPPTData, type SunspecBatteryData, type SunspecBatteryBaseData, type SunspecBatteryControls, SunspecStorageMode, type IModbusTransportConfig, type IModbusResilienceConfig } from "./sunspec-interfaces.cjs";
|
|
20
20
|
import { EnergyAppModbusDataType, IConnectionHealth } from "@enyo-energy/energy-app-sdk/dist/implementations/modbus/interfaces.js";
|
|
21
21
|
import { EnergyAppModbus, EnergyAppModbusInstance } from "@enyo-energy/energy-app-sdk/dist/packages/energy-app-modbus.js";
|
|
22
22
|
import { EnergyApp } from "@enyo-energy/energy-app-sdk";
|
|
@@ -29,12 +29,61 @@ export declare class ModbusOperationTimeoutError extends Error {
|
|
|
29
29
|
readonly unitId: number;
|
|
30
30
|
readonly timeoutMs: number;
|
|
31
31
|
constructor(operation: string, unitId: number, timeoutMs: number);
|
|
32
|
+
/**
|
|
33
|
+
* `Error` has no enumerable own properties, so a consumer that logs `{ err }` through a
|
|
34
|
+
* JSON serializer emits `{}` and loses the message entirely. Give every error this client
|
|
35
|
+
* rejects with an explicit JSON shape so it survives `JSON.stringify` with a usable message.
|
|
36
|
+
*/
|
|
37
|
+
toJSON(): {
|
|
38
|
+
name: string;
|
|
39
|
+
message: string;
|
|
40
|
+
operation: string;
|
|
41
|
+
unitId: number;
|
|
42
|
+
timeoutMs: number;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Why a unit is refusing I/O without having attempted it. A discriminant field rather than a
|
|
47
|
+
* class hierarchy, so consumers can branch on the cause with a plain `===`.
|
|
48
|
+
*/
|
|
49
|
+
export type SunspecUnavailableReason = 'socket-poisoned';
|
|
50
|
+
/**
|
|
51
|
+
* Thrown when the client refuses a transaction because the unit is **already known to be down**,
|
|
52
|
+
* as opposed to a transaction that was actually attempted and failed.
|
|
53
|
+
*
|
|
54
|
+
* The distinction matters for logging. A poisoned socket produces exactly one genuine event (the
|
|
55
|
+
* timeout that poisoned it), but every read that lands during the outage would otherwise throw an
|
|
56
|
+
* error indistinguishable from real I/O failure — so a consumer catching per read has no choice but
|
|
57
|
+
* to log each one at ERROR. Callers should log the first {@link SunspecUnavailableError} of an
|
|
58
|
+
* outage (or nothing at all: the SDK already logs the transition) and stay quiet on the rest.
|
|
59
|
+
*
|
|
60
|
+
* {@link sinceMs} is how long the unit has been in this state, so a consumer that does want one
|
|
61
|
+
* line can say how stale the link is without tracking the outage itself.
|
|
62
|
+
*/
|
|
63
|
+
export declare class SunspecUnavailableError extends Error {
|
|
64
|
+
readonly reason: SunspecUnavailableReason;
|
|
65
|
+
readonly unitId: number;
|
|
66
|
+
readonly sinceMs: number;
|
|
67
|
+
constructor(reason: SunspecUnavailableReason, unitId: number, sinceMs: number);
|
|
68
|
+
/** See {@link ModbusOperationTimeoutError.toJSON}. */
|
|
69
|
+
toJSON(): {
|
|
70
|
+
name: string;
|
|
71
|
+
message: string;
|
|
72
|
+
reason: SunspecUnavailableReason;
|
|
73
|
+
unitId: number;
|
|
74
|
+
sinceMs: number;
|
|
75
|
+
};
|
|
32
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Type guard for {@link SunspecUnavailableError}. Prefer this over `instanceof` at package
|
|
79
|
+
* boundaries, where a duplicated copy of the SDK in the module graph would break identity.
|
|
80
|
+
*/
|
|
81
|
+
export declare function isSunspecUnavailableError(error: unknown): error is SunspecUnavailableError;
|
|
33
82
|
/**
|
|
34
83
|
* Get (or create) the singleton SunspecModbusClient for this network device.
|
|
35
84
|
* Increments the refcount; pair with `releaseSunspecClient` on teardown.
|
|
36
85
|
*
|
|
37
|
-
* NOTE: `
|
|
86
|
+
* NOTE: `transport`, `resilience` and `modbusFactory` are only applied when the client is FIRST
|
|
38
87
|
* created for a given host:port. Because the client is cached and refcounted, a later caller
|
|
39
88
|
* passing different config receives the already-created instance and its config is ignored.
|
|
40
89
|
*
|
|
@@ -42,7 +91,7 @@ export declare class ModbusOperationTimeoutError extends Error {
|
|
|
42
91
|
* opens a per-unit connection) instead of the one from `energyApp.useModbus()` — e.g. a mock for
|
|
43
92
|
* tests, or an alternative transport. When provided, `energyApp` may be passed as `undefined`.
|
|
44
93
|
*/
|
|
45
|
-
export declare function getOrCreateSunspecClient(energyApp: EnergyApp | undefined, host: string, port?: number,
|
|
94
|
+
export declare function getOrCreateSunspecClient(energyApp: EnergyApp | undefined, host: string, port?: number, transport?: IModbusTransportConfig, resilience?: IModbusResilienceConfig, modbusFactory?: EnergyAppModbus): SunspecModbusClient;
|
|
46
95
|
/**
|
|
47
96
|
* Release a refcount on the network-device client. When the last consumer releases,
|
|
48
97
|
* the client is fully disconnected (all units closed) and removed from the registry.
|
|
@@ -66,7 +115,7 @@ export declare function retryAsync<T>(fn: (attempt: number) => Promise<T>, opts:
|
|
|
66
115
|
}): Promise<T>;
|
|
67
116
|
export declare class SunspecModbusClient {
|
|
68
117
|
private energyApp;
|
|
69
|
-
private
|
|
118
|
+
private transport;
|
|
70
119
|
private modbusFactory?;
|
|
71
120
|
private modbusInstances;
|
|
72
121
|
private faultTolerantReaders;
|
|
@@ -83,8 +132,10 @@ export declare class SunspecModbusClient {
|
|
|
83
132
|
private txChains;
|
|
84
133
|
private resilience;
|
|
85
134
|
private poisonedUnits;
|
|
135
|
+
private poisonedAtByUnit;
|
|
86
136
|
private lastOkAtByUnit;
|
|
87
|
-
|
|
137
|
+
private consecutiveConnectFailures;
|
|
138
|
+
constructor(energyApp: EnergyApp | undefined, transport?: IModbusTransportConfig, resilience?: IModbusResilienceConfig, modbusFactory?: EnergyAppModbus | undefined);
|
|
88
139
|
/**
|
|
89
140
|
* The Modbus backend used to open per-unit connections: the caller-supplied {@link modbusFactory}
|
|
90
141
|
* if one was given, otherwise the host application's default (`energyApp.useModbus()`). The
|
|
@@ -238,12 +289,38 @@ export declare class SunspecModbusClient {
|
|
|
238
289
|
* timer is always cleared so a fast `fn` doesn't keep the event loop alive.
|
|
239
290
|
*/
|
|
240
291
|
private withTimeout;
|
|
292
|
+
/**
|
|
293
|
+
* Log a failed Modbus transaction from a reader/writer catch block.
|
|
294
|
+
*
|
|
295
|
+
* A {@link SunspecUnavailableError} is not a failure — it is this client refusing I/O on a link
|
|
296
|
+
* it has already reported dead once (one WARN on the transition, one INFO on recovery). Logging
|
|
297
|
+
* it at ERROR once per read, per read path, per poll cycle, for the whole outage, is what buries
|
|
298
|
+
* the errors that are actually actionable. So it drops to debug; everything else stays ERROR.
|
|
299
|
+
*/
|
|
300
|
+
private logConnectFailure;
|
|
301
|
+
/** True while `key`'s last attempt failed — used to keep repeat attempt chatter at debug. */
|
|
302
|
+
private isRetryingAfterFailure;
|
|
303
|
+
/** Forget `key`'s failure run on a success, so the next outage logs its first failure again. */
|
|
304
|
+
private clearConnectFailures;
|
|
305
|
+
private logTransactionError;
|
|
241
306
|
/**
|
|
242
307
|
* Mark a unit's socket as poisoned after a timeout and reflect the hang in the shared health
|
|
243
308
|
* gate immediately, so isHealthy() flips false this cycle instead of after the coarse
|
|
244
309
|
* 5-failure / 5-minute-staleness thresholds. Idempotent; only logs on the first poison.
|
|
245
310
|
*/
|
|
246
311
|
private poisonUnit;
|
|
312
|
+
/**
|
|
313
|
+
* Enter the poisoned state for a unit, logging the TRANSITION only. Everything downstream of
|
|
314
|
+
* this (every refused read, every poll cycle) is the same known state and must stay silent —
|
|
315
|
+
* one WARN in, one INFO out (see {@link logPoisonRecovery}), nothing in between.
|
|
316
|
+
*/
|
|
317
|
+
private enterPoisoned;
|
|
318
|
+
/**
|
|
319
|
+
* Close out a poisoned episode on a successful (re)open: one INFO carrying how long the unit
|
|
320
|
+
* was refusing I/O. No-op for a unit that was never poisoned, so a normal first connect stays
|
|
321
|
+
* quiet.
|
|
322
|
+
*/
|
|
323
|
+
private logPoisonRecovery;
|
|
247
324
|
/**
|
|
248
325
|
* Publicly invalidate a unit's socket so {@link isHealthy} returns false and the next
|
|
249
326
|
* readData()/tryReconnect() cycle recycles it — the same effect as a timeout poison but
|
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 = '1.0.0';
|
|
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
|
@@ -19,9 +19,10 @@ export interface BatteryControlsClient {
|
|
|
19
19
|
*/
|
|
20
20
|
export type BatteryModificationRecorder = Pick<SnapshotService<SunspecBatteryControls>, 'recordModification'>;
|
|
21
21
|
/**
|
|
22
|
-
* Snapshot of the writable Model 124 registers taken at schedule install time
|
|
23
|
-
*
|
|
24
|
-
*
|
|
22
|
+
* Snapshot of the writable Model 124 registers taken at schedule install time.
|
|
23
|
+
* The base library hands it back to `onRollback`, but the release path does not
|
|
24
|
+
* replay it — see `releaseToAuto`. It survives as the record of the installed
|
|
25
|
+
* `wChaMax` and the pre-schedule charge source. JSON-serialisable because
|
|
25
26
|
* `EnergyAppStorage` round-trips it.
|
|
26
27
|
*
|
|
27
28
|
* The snapshotted `wChaMax` doubles as the denominator for the `inWRte` /
|
|
@@ -63,8 +64,9 @@ export declare class SunspecBatteryScheduleHandler extends StorageScheduleHandle
|
|
|
63
64
|
* Sticky pre-schedule snapshot, captured once on the first `onInit` and
|
|
64
65
|
* held across every subsequent schedule-to-schedule replacement. Cleared
|
|
65
66
|
* only when a real rollback fires (`mode: auto` from the data bus or
|
|
66
|
-
* `dispose`).
|
|
67
|
-
*
|
|
67
|
+
* `dispose`). Supplies the `wChaMax` denominator for the percentage math
|
|
68
|
+
* and the charge source to hand back on release — taken from the *original*
|
|
69
|
+
* pre-schedule read rather than the register set the device happens to be
|
|
68
70
|
* in at the moment of replacement.
|
|
69
71
|
*/
|
|
70
72
|
private originalBaseline?;
|
|
@@ -76,6 +78,11 @@ export declare class SunspecBatteryScheduleHandler extends StorageScheduleHandle
|
|
|
76
78
|
* write the snapshot back during a replacement.
|
|
77
79
|
*/
|
|
78
80
|
private suppressNextRollbackWrite;
|
|
81
|
+
/**
|
|
82
|
+
* Mirrors the base class's private `disposed` flag so the auto-release
|
|
83
|
+
* path can tell "released and gone" from "released and still live".
|
|
84
|
+
*/
|
|
85
|
+
private isDisposed;
|
|
79
86
|
constructor(opts: SunspecBatteryScheduleHandlerOptions);
|
|
80
87
|
/**
|
|
81
88
|
* Override the base-class data-bus router so a schedule-to-schedule
|
|
@@ -86,9 +93,29 @@ export declare class SunspecBatteryScheduleHandler extends StorageScheduleHandle
|
|
|
86
93
|
* normally there.
|
|
87
94
|
*/
|
|
88
95
|
applyMessage(msg: EnyoDataBusSetStorageScheduleV1): Promise<void>;
|
|
96
|
+
dispose(): void;
|
|
89
97
|
protected onInit(_active: ActiveStorageScheduleEntry): Promise<SunspecScheduleRegisters>;
|
|
90
98
|
protected onChange(active: ActiveStorageScheduleEntry, _previous: ActiveStorageScheduleEntry | undefined): void;
|
|
91
|
-
protected onRollback(
|
|
99
|
+
protected onRollback(_registers: SunspecScheduleRegisters): void;
|
|
100
|
+
/**
|
|
101
|
+
* Hand the battery back to its own logic: clear StorCtl_Mod and open both
|
|
102
|
+
* rate setpoints back up to 100 %.
|
|
103
|
+
*
|
|
104
|
+
* This deliberately does *not* replay the snapshot captured at `onInit`.
|
|
105
|
+
* That snapshot is whatever the registers happened to hold when the
|
|
106
|
+
* schedule started — and if a previous schedule had already stranded a
|
|
107
|
+
* limit there, restoring it re-latches the limit and the battery can never
|
|
108
|
+
* climb out. The only correct end state for "we are no longer controlling
|
|
109
|
+
* this battery" is the one written here, and it is the same set of
|
|
110
|
+
* registers a schedule entry touches, so nothing else is left dangling.
|
|
111
|
+
*
|
|
112
|
+
* `wChaMax` is not restored because a schedule never writes it — the
|
|
113
|
+
* percentage math divides against it instead.
|
|
114
|
+
*
|
|
115
|
+
* @param chargeSource - `chaGriSet` to restore; falls back to PV-only,
|
|
116
|
+
* since grid charging is only ever switched on by a Charge entry.
|
|
117
|
+
*/
|
|
118
|
+
private releaseToAuto;
|
|
92
119
|
protected onError(err: Error): void;
|
|
93
120
|
private applyEntry;
|
|
94
121
|
}
|
|
@@ -19,8 +19,9 @@ export class SunspecBatteryScheduleHandler extends StorageScheduleHandler {
|
|
|
19
19
|
* Sticky pre-schedule snapshot, captured once on the first `onInit` and
|
|
20
20
|
* held across every subsequent schedule-to-schedule replacement. Cleared
|
|
21
21
|
* only when a real rollback fires (`mode: auto` from the data bus or
|
|
22
|
-
* `dispose`).
|
|
23
|
-
*
|
|
22
|
+
* `dispose`). Supplies the `wChaMax` denominator for the percentage math
|
|
23
|
+
* and the charge source to hand back on release — taken from the *original*
|
|
24
|
+
* pre-schedule read rather than the register set the device happens to be
|
|
24
25
|
* in at the moment of replacement.
|
|
25
26
|
*/
|
|
26
27
|
originalBaseline;
|
|
@@ -32,6 +33,11 @@ export class SunspecBatteryScheduleHandler extends StorageScheduleHandler {
|
|
|
32
33
|
* write the snapshot back during a replacement.
|
|
33
34
|
*/
|
|
34
35
|
suppressNextRollbackWrite = false;
|
|
36
|
+
/**
|
|
37
|
+
* Mirrors the base class's private `disposed` flag so the auto-release
|
|
38
|
+
* path can tell "released and gone" from "released and still live".
|
|
39
|
+
*/
|
|
40
|
+
isDisposed = false;
|
|
35
41
|
constructor(opts) {
|
|
36
42
|
super(opts);
|
|
37
43
|
this.sunspecClient = opts.sunspecClient;
|
|
@@ -49,11 +55,15 @@ export class SunspecBatteryScheduleHandler extends StorageScheduleHandler {
|
|
|
49
55
|
* normally there.
|
|
50
56
|
*/
|
|
51
57
|
async applyMessage(msg) {
|
|
52
|
-
|
|
58
|
+
const isForThisAppliance = msg.applianceId === this.applianceIdForLog;
|
|
59
|
+
if (isForThisAppliance
|
|
53
60
|
&& msg.data.mode === EnyoStorageScheduleModeEnum.Schedule
|
|
54
61
|
&& this.getActiveEntry() !== undefined) {
|
|
55
62
|
this.suppressNextRollbackWrite = true;
|
|
56
63
|
}
|
|
64
|
+
// Captured before the base class runs: with a schedule active, its
|
|
65
|
+
// `doReleaseSchedule → onRollback` already performs the auto release.
|
|
66
|
+
const hadActiveSchedule = this.getActiveEntry() !== undefined;
|
|
57
67
|
try {
|
|
58
68
|
await super.applyMessage(msg);
|
|
59
69
|
}
|
|
@@ -63,6 +73,30 @@ export class SunspecBatteryScheduleHandler extends StorageScheduleHandler {
|
|
|
63
73
|
// otherwise stay set and silently swallow the next real rollback.
|
|
64
74
|
this.suppressNextRollbackWrite = false;
|
|
65
75
|
}
|
|
76
|
+
if (isForThisAppliance
|
|
77
|
+
&& msg.data.mode === EnyoStorageScheduleModeEnum.Auto
|
|
78
|
+
&& !hadActiveSchedule
|
|
79
|
+
&& !this.isDisposed) {
|
|
80
|
+
// `mode: auto` with no schedule running is a no-op in the base
|
|
81
|
+
// library (`doReleaseSchedule` returns early when idle), so nothing
|
|
82
|
+
// would reach the device. That is not harmless: Model 124 setpoints
|
|
83
|
+
// latch — an interrupted schedule (modbus drop, package restart)
|
|
84
|
+
// leaves StorCtl_Mod and InWRte set, and with InOutWRte_RvrtTms = 0
|
|
85
|
+
// the inverter never reverts on its own. Seen in production as a
|
|
86
|
+
// battery pinned at ~2 % charge rate for hours while the energy
|
|
87
|
+
// manager sent `auto` every 40 s and got an `Accepted` back.
|
|
88
|
+
//
|
|
89
|
+
// Writing the release unconditionally is safe because it is
|
|
90
|
+
// idempotent: it is exactly the register set that means
|
|
91
|
+
// "no external control".
|
|
92
|
+
await this.releaseToAuto().catch(err => {
|
|
93
|
+
console.error(`SunspecBatteryScheduleHandler ${this.applianceIdForLog}: auto release failed: ${err}`);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
dispose() {
|
|
98
|
+
this.isDisposed = true;
|
|
99
|
+
super.dispose();
|
|
66
100
|
}
|
|
67
101
|
async onInit(_active) {
|
|
68
102
|
if (this.originalBaseline !== undefined) {
|
|
@@ -101,7 +135,7 @@ export class SunspecBatteryScheduleHandler extends StorageScheduleHandler {
|
|
|
101
135
|
console.error(`SunspecBatteryScheduleHandler ${this.applianceIdForLog}: onChange write failed for entry ${active.indexInSchedule}: ${err}`);
|
|
102
136
|
});
|
|
103
137
|
}
|
|
104
|
-
onRollback(
|
|
138
|
+
onRollback(_registers) {
|
|
105
139
|
if (this.suppressNextRollbackWrite) {
|
|
106
140
|
// Schedule-to-schedule replacement. Do NOT write the snapshot
|
|
107
141
|
// back; the new schedule's first `onChange` will own the
|
|
@@ -119,19 +153,41 @@ export class SunspecBatteryScheduleHandler extends StorageScheduleHandler {
|
|
|
119
153
|
// that follows — so a concurrent `onInit` queued behind this one
|
|
120
154
|
// (impossible today, but defensive) cannot observe a half-cleared
|
|
121
155
|
// state.
|
|
156
|
+
const chargeSource = this.originalBaseline?.chaGriSet;
|
|
122
157
|
this.originalBaseline = undefined;
|
|
123
158
|
this.installedWChaMax = undefined;
|
|
124
|
-
void (
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
159
|
+
void this.releaseToAuto(chargeSource).catch(err => {
|
|
160
|
+
console.error(`SunspecBatteryScheduleHandler ${this.applianceIdForLog}: onRollback failed: ${err}`);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Hand the battery back to its own logic: clear StorCtl_Mod and open both
|
|
165
|
+
* rate setpoints back up to 100 %.
|
|
166
|
+
*
|
|
167
|
+
* This deliberately does *not* replay the snapshot captured at `onInit`.
|
|
168
|
+
* That snapshot is whatever the registers happened to hold when the
|
|
169
|
+
* schedule started — and if a previous schedule had already stranded a
|
|
170
|
+
* limit there, restoring it re-latches the limit and the battery can never
|
|
171
|
+
* climb out. The only correct end state for "we are no longer controlling
|
|
172
|
+
* this battery" is the one written here, and it is the same set of
|
|
173
|
+
* registers a schedule entry touches, so nothing else is left dangling.
|
|
174
|
+
*
|
|
175
|
+
* `wChaMax` is not restored because a schedule never writes it — the
|
|
176
|
+
* percentage math divides against it instead.
|
|
177
|
+
*
|
|
178
|
+
* @param chargeSource - `chaGriSet` to restore; falls back to PV-only,
|
|
179
|
+
* since grid charging is only ever switched on by a Charge entry.
|
|
180
|
+
*/
|
|
181
|
+
async releaseToAuto(chargeSource) {
|
|
182
|
+
await this.sunspecClient.writeBatteryControls(this.unitId, {
|
|
183
|
+
storCtlMod: SunspecStorageControlMode.NONE,
|
|
184
|
+
chaGriSet: chargeSource ?? SunspecChargeSource.PV,
|
|
185
|
+
inWRte: 100,
|
|
186
|
+
outWRte: 100,
|
|
187
|
+
});
|
|
188
|
+
await this.getSnapshotService()?.recordModification([
|
|
189
|
+
'storCtlMod', 'chaGriSet', 'inWRte', 'outWRte',
|
|
190
|
+
]);
|
|
135
191
|
}
|
|
136
192
|
onError(err) {
|
|
137
193
|
this.onErrorCallback?.(err);
|
|
@@ -139,19 +195,11 @@ export class SunspecBatteryScheduleHandler extends StorageScheduleHandler {
|
|
|
139
195
|
async applyEntry(active) {
|
|
140
196
|
const { direction, powerW } = active.entry;
|
|
141
197
|
if (direction === EnyoStorageScheduleDirectionEnum.Idle) {
|
|
142
|
-
// Idle entry: hand the battery back to its own autonomous logic
|
|
143
|
-
//
|
|
144
|
-
//
|
|
145
|
-
//
|
|
146
|
-
|
|
147
|
-
const baselineRegisters = this.originalBaseline;
|
|
148
|
-
if (!baselineRegisters) {
|
|
149
|
-
throw new Error(`no usable baseline for idle entry (originalBaseline=undefined)`);
|
|
150
|
-
}
|
|
151
|
-
await this.sunspecClient.writeBatteryControls(this.unitId, baselineRegisters);
|
|
152
|
-
await this.getSnapshotService()?.recordModification([
|
|
153
|
-
'storCtlMod', 'chaGriSet', 'wChaMax', 'inWRte', 'outWRte',
|
|
154
|
-
]);
|
|
198
|
+
// Idle entry: hand the battery back to its own autonomous logic —
|
|
199
|
+
// the same release the `mode: auto` path performs. The schedule
|
|
200
|
+
// itself keeps running, so a subsequent Charge / Discharge entry
|
|
201
|
+
// can resume external control.
|
|
202
|
+
await this.releaseToAuto(this.originalBaseline?.chaGriSet);
|
|
155
203
|
return;
|
|
156
204
|
}
|
|
157
205
|
const baseline = this.installedWChaMax;
|
|
@@ -1,4 +1,4 @@
|
|
|
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, type SunspecCustomDeviceConfig } from "./sunspec-interfaces.js";
|
|
1
|
+
import { type IRetryConfig, type SunspecBatteryBaseData, SunspecBatteryCapability, type SunspecBatteryControls, type SunspecBatteryData, type SunspecBatteryFeatureMode, type SunspecInverterData, SunspecInverterCapability, SunspecPvPowerSource, type SunspecInverterMetadataConfig, type SunspecBatteryMetadataConfig, SunspecPvPowerOrigin, type SunspecInverterReading, type SunspecBatteryReading, SunspecMeterCapability, type SunspecMPPTData, SunspecStorageMode, type SunspecCustomDeviceConfig } from "./sunspec-interfaces.js";
|
|
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";
|
|
@@ -208,6 +208,7 @@ type SunspecPvPower = {
|
|
|
208
208
|
export declare class SunspecInverter extends BaseSunspecDevice {
|
|
209
209
|
private readonly capabilities;
|
|
210
210
|
private readonly pvPowerSource;
|
|
211
|
+
private readonly metadataConfig;
|
|
211
212
|
/** Emit a connection-lost faulted status after this many consecutive failed reconnect attempts. */
|
|
212
213
|
private static readonly CONNECTION_FAULT_THRESHOLD;
|
|
213
214
|
/**
|
|
@@ -233,7 +234,13 @@ export declare class SunspecInverter extends BaseSunspecDevice {
|
|
|
233
234
|
private acEnergyDropStreak;
|
|
234
235
|
/** Consecutive corrupt reads that confirm a genuine reset/resync (vs a transient glitch). */
|
|
235
236
|
private static readonly AC_ENERGY_RESET_CONFIRM;
|
|
236
|
-
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);
|
|
237
|
+
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, metadataConfig?: SunspecInverterMetadataConfig);
|
|
238
|
+
/**
|
|
239
|
+
* The consumer-supplied inverter facts, as a spreadable object that only contains the keys that
|
|
240
|
+
* were actually configured. Spreading `{ controlAllowed: undefined }` over an existing appliance
|
|
241
|
+
* would erase a value the host set elsewhere; omitting the key leaves it alone.
|
|
242
|
+
*/
|
|
243
|
+
private configuredInverterMetadata;
|
|
237
244
|
connect(): Promise<void>;
|
|
238
245
|
disconnect(): Promise<void>;
|
|
239
246
|
readData(clockId: string, resolution: EnyoDataBusMessageResolution): Promise<EnyoDataBusMessage[]>;
|
|
@@ -510,6 +517,21 @@ export declare function filterFeaturesByCalibrationResult(detected: EnyoBatteryF
|
|
|
510
517
|
/**
|
|
511
518
|
* Sunspec Battery implementation
|
|
512
519
|
*/
|
|
520
|
+
/**
|
|
521
|
+
* Derive the storage mode from Model 124's StorCtl_Mod bitfield.
|
|
522
|
+
*
|
|
523
|
+
* The bits mark *which rate setpoint the device is enforcing* — bit 0 for
|
|
524
|
+
* InWRte, bit 1 for OutWRte — not which direction is permitted. So no bit set
|
|
525
|
+
* is the autonomous mode, and both bits set is the most externally controlled
|
|
526
|
+
* state there is, which is the opposite of what the naming suggests. Reading
|
|
527
|
+
* them the other way round meant a battery running its own logic reported
|
|
528
|
+
* `holding`, a fully clamped one reported `auto`, and no appliance could ever
|
|
529
|
+
* report that it had been handed back to itself.
|
|
530
|
+
*
|
|
531
|
+
* @param controls - Model 124 control registers, or null when unreadable.
|
|
532
|
+
* @returns The mode, or null when StorCtl_Mod could not be read.
|
|
533
|
+
*/
|
|
534
|
+
export declare function deriveStorageMode(controls: SunspecBatteryControls | null): SunspecStorageMode | null;
|
|
513
535
|
export declare class SunspecBattery extends BaseSunspecDevice {
|
|
514
536
|
private readonly featureMode;
|
|
515
537
|
private readonly capabilities;
|
|
@@ -532,7 +554,29 @@ export declare class SunspecBattery extends BaseSunspecDevice {
|
|
|
532
554
|
* {@link computeAdvertisedFeatures}.
|
|
533
555
|
*/
|
|
534
556
|
private static readonly CONTROLLABLE_FEATURES;
|
|
535
|
-
constructor(energyApp: EnergyApp, name: EnyoApplianceName[], networkDevice: EnyoNetworkDevice, sunspecClient: SunspecModbusClient, applianceManager: ApplianceManager, featureMode: SunspecBatteryFeatureMode, unitId?: number, port?: number, baseAddress?: number, capabilities?: SunspecBatteryCapability[], retryConfig?: IRetryConfig, appliance?: EnyoAppliance, useTls?: boolean);
|
|
557
|
+
constructor(energyApp: EnergyApp, name: EnyoApplianceName[], networkDevice: EnyoNetworkDevice, sunspecClient: SunspecModbusClient, applianceManager: ApplianceManager, featureMode: SunspecBatteryFeatureMode, unitId?: number, port?: number, baseAddress?: number, capabilities?: SunspecBatteryCapability[], retryConfig?: IRetryConfig, appliance?: EnyoAppliance, useTls?: boolean, metadataConfig?: SunspecBatteryMetadataConfig);
|
|
558
|
+
/**
|
|
559
|
+
* Last known round-trip efficiency, seeded from the constructor config and replaceable at
|
|
560
|
+
* runtime via {@link setRoundTripEfficiencyPercent}. Undefined = not yet known; the SDK then
|
|
561
|
+
* writes nothing, leaving whatever the appliance store already holds.
|
|
562
|
+
*/
|
|
563
|
+
private roundTripEfficiencyPercent?;
|
|
564
|
+
/**
|
|
565
|
+
* Update the battery's measured round-trip efficiency (percent, 0-100) and push it to the
|
|
566
|
+
* appliance immediately. Meant to be called whenever the host recomputes the figure from
|
|
567
|
+
* completed cycles - it drifts with cell age and temperature, so a value fixed at construction
|
|
568
|
+
* goes stale. Pass `undefined` to stop asserting a value (the last published one stays in the
|
|
569
|
+
* appliance store; the SDK does not erase it).
|
|
570
|
+
*
|
|
571
|
+
* Safe to call before {@link connect}: the value is remembered and published with the rest of
|
|
572
|
+
* the metadata once the appliance exists.
|
|
573
|
+
*/
|
|
574
|
+
setRoundTripEfficiencyPercent(percent: number | undefined): Promise<void>;
|
|
575
|
+
/**
|
|
576
|
+
* The configured battery facts as a spreadable object holding only the keys actually known.
|
|
577
|
+
* Spreading an explicit `undefined` would erase a value the host set elsewhere.
|
|
578
|
+
*/
|
|
579
|
+
private configuredBatteryMetadata;
|
|
536
580
|
/**
|
|
537
581
|
* Wire the battery into `@enyo-energy/appliance-calibration`'s
|
|
538
582
|
* `BatteryCalibrator` test-charge flow. Call once after {@link connect}.
|