@enyo-energy/sunspec-sdk 1.12.1 → 1.13.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-devices.cjs +49 -7
- package/dist/cjs/sunspec-devices.d.cts +15 -2
- package/dist/cjs/version.cjs +1 -1
- package/dist/cjs/version.d.cts +1 -1
- package/dist/sunspec-devices.d.ts +15 -2
- package/dist/sunspec-devices.js +49 -7
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
- package/dist/calibration-snapshot-service.d.ts +0 -67
- package/dist/calibration-snapshot-service.js +0 -160
- package/dist/cjs/calibration-snapshot-service.cjs +0 -164
- package/dist/cjs/calibration-snapshot-service.d.cts +0 -67
|
@@ -526,10 +526,15 @@ class SunspecInverter extends BaseSunspecDevice {
|
|
|
526
526
|
* reporting Faulted. Debounces transient single-poll FAULT reads (e.g. a
|
|
527
527
|
* DC-coupled battery disturbing a string) that would otherwise oscillate
|
|
528
528
|
* the appliance status. Recovery is immediate (counter resets on the first
|
|
529
|
-
* non-fault read).
|
|
529
|
+
* non-fault read). Used when getOperatingFaultGraceMs() returns undefined.
|
|
530
530
|
*/
|
|
531
531
|
static OPERATING_FAULT_THRESHOLD = 3;
|
|
532
532
|
consecutiveOperatingFaults = 0;
|
|
533
|
+
// Poll timestamp (ms) of the first read of the current uninterrupted
|
|
534
|
+
// operatingState=FAULT run; undefined while not in FAULT. Cleared on any
|
|
535
|
+
// gap in observation (failed read, offline, reconnect) so unobserved time
|
|
536
|
+
// never counts towards getOperatingFaultGraceMs().
|
|
537
|
+
operatingFaultSinceMs;
|
|
533
538
|
// In-memory only — never persisted. See SunspecInverterErrorState.
|
|
534
539
|
errorState = { activeCodes: [], lastStatus: 'healthy' };
|
|
535
540
|
snapshotService;
|
|
@@ -724,6 +729,9 @@ class SunspecInverter extends BaseSunspecDevice {
|
|
|
724
729
|
messages.push(inverterMessage);
|
|
725
730
|
await this.detectAndEmitStatusTransition(inverterData, timestamp);
|
|
726
731
|
}
|
|
732
|
+
else {
|
|
733
|
+
this.interruptOperatingFaultRun();
|
|
734
|
+
}
|
|
727
735
|
this.consecutiveReconnectFailures = 0;
|
|
728
736
|
if (this.applianceId) {
|
|
729
737
|
const appliance = await this.applianceManager.findApplianceById(this.applianceId);
|
|
@@ -877,6 +885,34 @@ class SunspecInverter extends BaseSunspecDevice {
|
|
|
877
885
|
}
|
|
878
886
|
};
|
|
879
887
|
}
|
|
888
|
+
/**
|
|
889
|
+
* How long operatingState=7 (FAULT) must be reported without interruption
|
|
890
|
+
* before the inverter is reported Faulted, in ms. undefined (the default)
|
|
891
|
+
* keeps the poll-count debounce of OPERATING_FAULT_THRESHOLD consecutive
|
|
892
|
+
* FAULT reads. Override for vendors that report short FAULT phases without
|
|
893
|
+
* an error code while operating normally. Recovery stays immediate, and
|
|
894
|
+
* decoded event-register error bits are not affected.
|
|
895
|
+
*/
|
|
896
|
+
getOperatingFaultGraceMs() {
|
|
897
|
+
return undefined;
|
|
898
|
+
}
|
|
899
|
+
interruptOperatingFaultRun() {
|
|
900
|
+
this.operatingFaultSinceMs = undefined;
|
|
901
|
+
}
|
|
902
|
+
isOperatingFaultSustained(operatingFaultRaw, timestamp) {
|
|
903
|
+
if (!operatingFaultRaw) {
|
|
904
|
+
this.consecutiveOperatingFaults = 0;
|
|
905
|
+
this.operatingFaultSinceMs = undefined;
|
|
906
|
+
return false;
|
|
907
|
+
}
|
|
908
|
+
this.consecutiveOperatingFaults++;
|
|
909
|
+
this.operatingFaultSinceMs ??= timestamp.getTime();
|
|
910
|
+
const graceMs = this.getOperatingFaultGraceMs();
|
|
911
|
+
if (graceMs === undefined) {
|
|
912
|
+
return this.consecutiveOperatingFaults >= SunspecInverter.OPERATING_FAULT_THRESHOLD;
|
|
913
|
+
}
|
|
914
|
+
return timestamp.getTime() - this.operatingFaultSinceMs >= graceMs;
|
|
915
|
+
}
|
|
880
916
|
async detectAndEmitStatusTransition(data, timestamp) {
|
|
881
917
|
if (!this.applianceId || !this.dataBus)
|
|
882
918
|
return;
|
|
@@ -888,13 +924,13 @@ class SunspecInverter extends BaseSunspecDevice {
|
|
|
888
924
|
// no error bits) reports Healthy even if it was previously faulted.
|
|
889
925
|
//
|
|
890
926
|
// A single FAULT read is debounced: we only treat it as a fault after
|
|
891
|
-
// OPERATING_FAULT_THRESHOLD consecutive FAULT polls,
|
|
892
|
-
//
|
|
893
|
-
//
|
|
894
|
-
//
|
|
927
|
+
// OPERATING_FAULT_THRESHOLD consecutive FAULT polls (or, if the subclass
|
|
928
|
+
// sets getOperatingFaultGraceMs(), after FAULT has persisted that long),
|
|
929
|
+
// so a transient wobble (e.g. a faulted DC-coupled battery on a string)
|
|
930
|
+
// does not oscillate the status. Recovery is immediate — the debounce
|
|
931
|
+
// resets on the first non-fault read. Event-register error bits stay immediate.
|
|
895
932
|
const operatingFaultRaw = data.operatingState === sunspec_interfaces_js_1.SunspecInverterOperatingState.FAULT;
|
|
896
|
-
|
|
897
|
-
const operatingFault = this.consecutiveOperatingFaults >= SunspecInverter.OPERATING_FAULT_THRESHOLD;
|
|
933
|
+
const operatingFault = this.isOperatingFaultSustained(operatingFaultRaw, timestamp);
|
|
898
934
|
const newStatus = (codeIds.length > 0 || operatingFault)
|
|
899
935
|
? enyo_appliance_js_1.EnyoApplianceStatusEnum.Faulted
|
|
900
936
|
: enyo_appliance_js_1.EnyoApplianceStatusEnum.Healthy;
|
|
@@ -934,7 +970,12 @@ class SunspecInverter extends BaseSunspecDevice {
|
|
|
934
970
|
getRequiredModelIds() {
|
|
935
971
|
return SunspecInverter.INVERTER_MODEL_IDS;
|
|
936
972
|
}
|
|
973
|
+
async markOffline() {
|
|
974
|
+
this.interruptOperatingFaultRun();
|
|
975
|
+
await super.markOffline();
|
|
976
|
+
}
|
|
937
977
|
async onConnectionFailure(consecutiveFailures) {
|
|
978
|
+
this.interruptOperatingFaultRun();
|
|
938
979
|
if (!this.applianceId || !this.dataBus)
|
|
939
980
|
return;
|
|
940
981
|
if (consecutiveFailures < SunspecInverter.CONNECTION_FAULT_THRESHOLD)
|
|
@@ -957,6 +998,7 @@ class SunspecInverter extends BaseSunspecDevice {
|
|
|
957
998
|
};
|
|
958
999
|
}
|
|
959
1000
|
async onConnectionRestored() {
|
|
1001
|
+
this.interruptOperatingFaultRun();
|
|
960
1002
|
if (!this.applianceId || !this.dataBus)
|
|
961
1003
|
return;
|
|
962
1004
|
if (this.errorState.lastStatus !== 'connection_lost')
|
|
@@ -223,10 +223,11 @@ export declare class SunspecInverter extends BaseSunspecDevice {
|
|
|
223
223
|
* reporting Faulted. Debounces transient single-poll FAULT reads (e.g. a
|
|
224
224
|
* DC-coupled battery disturbing a string) that would otherwise oscillate
|
|
225
225
|
* the appliance status. Recovery is immediate (counter resets on the first
|
|
226
|
-
* non-fault read).
|
|
226
|
+
* non-fault read). Used when getOperatingFaultGraceMs() returns undefined.
|
|
227
227
|
*/
|
|
228
228
|
private static readonly OPERATING_FAULT_THRESHOLD;
|
|
229
229
|
private consecutiveOperatingFaults;
|
|
230
|
+
private operatingFaultSinceMs?;
|
|
230
231
|
private errorState;
|
|
231
232
|
private snapshotService?;
|
|
232
233
|
private statusReassertedThisSession;
|
|
@@ -299,7 +300,18 @@ export declare class SunspecInverter extends BaseSunspecDevice {
|
|
|
299
300
|
protected mapVendorEventBit(_registerIndex: 1 | 2 | 3 | 4, _bit: number): EnyoApplianceErrorCode | undefined;
|
|
300
301
|
private hasErrorSetChanged;
|
|
301
302
|
private buildStatusMessage;
|
|
302
|
-
|
|
303
|
+
/**
|
|
304
|
+
* How long operatingState=7 (FAULT) must be reported without interruption
|
|
305
|
+
* before the inverter is reported Faulted, in ms. undefined (the default)
|
|
306
|
+
* keeps the poll-count debounce of OPERATING_FAULT_THRESHOLD consecutive
|
|
307
|
+
* FAULT reads. Override for vendors that report short FAULT phases without
|
|
308
|
+
* an error code while operating normally. Recovery stays immediate, and
|
|
309
|
+
* decoded event-register error bits are not affected.
|
|
310
|
+
*/
|
|
311
|
+
protected getOperatingFaultGraceMs(): number | undefined;
|
|
312
|
+
private interruptOperatingFaultRun;
|
|
313
|
+
private isOperatingFaultSustained;
|
|
314
|
+
protected detectAndEmitStatusTransition(data: SunspecInverterData, timestamp: Date): Promise<void>;
|
|
303
315
|
/**
|
|
304
316
|
* A reconnect has only recovered the inverter if discovery finds one of its
|
|
305
317
|
* data models. An SE gateway can accept the new socket and answer a partial
|
|
@@ -308,6 +320,7 @@ export declare class SunspecInverter extends BaseSunspecDevice {
|
|
|
308
320
|
* no 101/103/111/112/113 model and report 0 W. Require an actual inverter model.
|
|
309
321
|
*/
|
|
310
322
|
protected getRequiredModelIds(): readonly number[];
|
|
323
|
+
protected markOffline(): Promise<void>;
|
|
311
324
|
protected onConnectionFailure(consecutiveFailures: number): Promise<void>;
|
|
312
325
|
protected onConnectionRestored(): Promise<void>;
|
|
313
326
|
/**
|
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 = '1.
|
|
12
|
+
exports.SDK_VERSION = '1.13.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
|
@@ -223,10 +223,11 @@ export declare class SunspecInverter extends BaseSunspecDevice {
|
|
|
223
223
|
* reporting Faulted. Debounces transient single-poll FAULT reads (e.g. a
|
|
224
224
|
* DC-coupled battery disturbing a string) that would otherwise oscillate
|
|
225
225
|
* the appliance status. Recovery is immediate (counter resets on the first
|
|
226
|
-
* non-fault read).
|
|
226
|
+
* non-fault read). Used when getOperatingFaultGraceMs() returns undefined.
|
|
227
227
|
*/
|
|
228
228
|
private static readonly OPERATING_FAULT_THRESHOLD;
|
|
229
229
|
private consecutiveOperatingFaults;
|
|
230
|
+
private operatingFaultSinceMs?;
|
|
230
231
|
private errorState;
|
|
231
232
|
private snapshotService?;
|
|
232
233
|
private statusReassertedThisSession;
|
|
@@ -299,7 +300,18 @@ export declare class SunspecInverter extends BaseSunspecDevice {
|
|
|
299
300
|
protected mapVendorEventBit(_registerIndex: 1 | 2 | 3 | 4, _bit: number): EnyoApplianceErrorCode | undefined;
|
|
300
301
|
private hasErrorSetChanged;
|
|
301
302
|
private buildStatusMessage;
|
|
302
|
-
|
|
303
|
+
/**
|
|
304
|
+
* How long operatingState=7 (FAULT) must be reported without interruption
|
|
305
|
+
* before the inverter is reported Faulted, in ms. undefined (the default)
|
|
306
|
+
* keeps the poll-count debounce of OPERATING_FAULT_THRESHOLD consecutive
|
|
307
|
+
* FAULT reads. Override for vendors that report short FAULT phases without
|
|
308
|
+
* an error code while operating normally. Recovery stays immediate, and
|
|
309
|
+
* decoded event-register error bits are not affected.
|
|
310
|
+
*/
|
|
311
|
+
protected getOperatingFaultGraceMs(): number | undefined;
|
|
312
|
+
private interruptOperatingFaultRun;
|
|
313
|
+
private isOperatingFaultSustained;
|
|
314
|
+
protected detectAndEmitStatusTransition(data: SunspecInverterData, timestamp: Date): Promise<void>;
|
|
303
315
|
/**
|
|
304
316
|
* A reconnect has only recovered the inverter if discovery finds one of its
|
|
305
317
|
* data models. An SE gateway can accept the new socket and answer a partial
|
|
@@ -308,6 +320,7 @@ export declare class SunspecInverter extends BaseSunspecDevice {
|
|
|
308
320
|
* no 101/103/111/112/113 model and report 0 W. Require an actual inverter model.
|
|
309
321
|
*/
|
|
310
322
|
protected getRequiredModelIds(): readonly number[];
|
|
323
|
+
protected markOffline(): Promise<void>;
|
|
311
324
|
protected onConnectionFailure(consecutiveFailures: number): Promise<void>;
|
|
312
325
|
protected onConnectionRestored(): Promise<void>;
|
|
313
326
|
/**
|
package/dist/sunspec-devices.js
CHANGED
|
@@ -515,10 +515,15 @@ export class SunspecInverter extends BaseSunspecDevice {
|
|
|
515
515
|
* reporting Faulted. Debounces transient single-poll FAULT reads (e.g. a
|
|
516
516
|
* DC-coupled battery disturbing a string) that would otherwise oscillate
|
|
517
517
|
* the appliance status. Recovery is immediate (counter resets on the first
|
|
518
|
-
* non-fault read).
|
|
518
|
+
* non-fault read). Used when getOperatingFaultGraceMs() returns undefined.
|
|
519
519
|
*/
|
|
520
520
|
static OPERATING_FAULT_THRESHOLD = 3;
|
|
521
521
|
consecutiveOperatingFaults = 0;
|
|
522
|
+
// Poll timestamp (ms) of the first read of the current uninterrupted
|
|
523
|
+
// operatingState=FAULT run; undefined while not in FAULT. Cleared on any
|
|
524
|
+
// gap in observation (failed read, offline, reconnect) so unobserved time
|
|
525
|
+
// never counts towards getOperatingFaultGraceMs().
|
|
526
|
+
operatingFaultSinceMs;
|
|
522
527
|
// In-memory only — never persisted. See SunspecInverterErrorState.
|
|
523
528
|
errorState = { activeCodes: [], lastStatus: 'healthy' };
|
|
524
529
|
snapshotService;
|
|
@@ -713,6 +718,9 @@ export class SunspecInverter extends BaseSunspecDevice {
|
|
|
713
718
|
messages.push(inverterMessage);
|
|
714
719
|
await this.detectAndEmitStatusTransition(inverterData, timestamp);
|
|
715
720
|
}
|
|
721
|
+
else {
|
|
722
|
+
this.interruptOperatingFaultRun();
|
|
723
|
+
}
|
|
716
724
|
this.consecutiveReconnectFailures = 0;
|
|
717
725
|
if (this.applianceId) {
|
|
718
726
|
const appliance = await this.applianceManager.findApplianceById(this.applianceId);
|
|
@@ -866,6 +874,34 @@ export class SunspecInverter extends BaseSunspecDevice {
|
|
|
866
874
|
}
|
|
867
875
|
};
|
|
868
876
|
}
|
|
877
|
+
/**
|
|
878
|
+
* How long operatingState=7 (FAULT) must be reported without interruption
|
|
879
|
+
* before the inverter is reported Faulted, in ms. undefined (the default)
|
|
880
|
+
* keeps the poll-count debounce of OPERATING_FAULT_THRESHOLD consecutive
|
|
881
|
+
* FAULT reads. Override for vendors that report short FAULT phases without
|
|
882
|
+
* an error code while operating normally. Recovery stays immediate, and
|
|
883
|
+
* decoded event-register error bits are not affected.
|
|
884
|
+
*/
|
|
885
|
+
getOperatingFaultGraceMs() {
|
|
886
|
+
return undefined;
|
|
887
|
+
}
|
|
888
|
+
interruptOperatingFaultRun() {
|
|
889
|
+
this.operatingFaultSinceMs = undefined;
|
|
890
|
+
}
|
|
891
|
+
isOperatingFaultSustained(operatingFaultRaw, timestamp) {
|
|
892
|
+
if (!operatingFaultRaw) {
|
|
893
|
+
this.consecutiveOperatingFaults = 0;
|
|
894
|
+
this.operatingFaultSinceMs = undefined;
|
|
895
|
+
return false;
|
|
896
|
+
}
|
|
897
|
+
this.consecutiveOperatingFaults++;
|
|
898
|
+
this.operatingFaultSinceMs ??= timestamp.getTime();
|
|
899
|
+
const graceMs = this.getOperatingFaultGraceMs();
|
|
900
|
+
if (graceMs === undefined) {
|
|
901
|
+
return this.consecutiveOperatingFaults >= SunspecInverter.OPERATING_FAULT_THRESHOLD;
|
|
902
|
+
}
|
|
903
|
+
return timestamp.getTime() - this.operatingFaultSinceMs >= graceMs;
|
|
904
|
+
}
|
|
869
905
|
async detectAndEmitStatusTransition(data, timestamp) {
|
|
870
906
|
if (!this.applianceId || !this.dataBus)
|
|
871
907
|
return;
|
|
@@ -877,13 +913,13 @@ export class SunspecInverter extends BaseSunspecDevice {
|
|
|
877
913
|
// no error bits) reports Healthy even if it was previously faulted.
|
|
878
914
|
//
|
|
879
915
|
// A single FAULT read is debounced: we only treat it as a fault after
|
|
880
|
-
// OPERATING_FAULT_THRESHOLD consecutive FAULT polls,
|
|
881
|
-
//
|
|
882
|
-
//
|
|
883
|
-
//
|
|
916
|
+
// OPERATING_FAULT_THRESHOLD consecutive FAULT polls (or, if the subclass
|
|
917
|
+
// sets getOperatingFaultGraceMs(), after FAULT has persisted that long),
|
|
918
|
+
// so a transient wobble (e.g. a faulted DC-coupled battery on a string)
|
|
919
|
+
// does not oscillate the status. Recovery is immediate — the debounce
|
|
920
|
+
// resets on the first non-fault read. Event-register error bits stay immediate.
|
|
884
921
|
const operatingFaultRaw = data.operatingState === SunspecInverterOperatingState.FAULT;
|
|
885
|
-
|
|
886
|
-
const operatingFault = this.consecutiveOperatingFaults >= SunspecInverter.OPERATING_FAULT_THRESHOLD;
|
|
922
|
+
const operatingFault = this.isOperatingFaultSustained(operatingFaultRaw, timestamp);
|
|
887
923
|
const newStatus = (codeIds.length > 0 || operatingFault)
|
|
888
924
|
? EnyoApplianceStatusEnum.Faulted
|
|
889
925
|
: EnyoApplianceStatusEnum.Healthy;
|
|
@@ -923,7 +959,12 @@ export class SunspecInverter extends BaseSunspecDevice {
|
|
|
923
959
|
getRequiredModelIds() {
|
|
924
960
|
return SunspecInverter.INVERTER_MODEL_IDS;
|
|
925
961
|
}
|
|
962
|
+
async markOffline() {
|
|
963
|
+
this.interruptOperatingFaultRun();
|
|
964
|
+
await super.markOffline();
|
|
965
|
+
}
|
|
926
966
|
async onConnectionFailure(consecutiveFailures) {
|
|
967
|
+
this.interruptOperatingFaultRun();
|
|
927
968
|
if (!this.applianceId || !this.dataBus)
|
|
928
969
|
return;
|
|
929
970
|
if (consecutiveFailures < SunspecInverter.CONNECTION_FAULT_THRESHOLD)
|
|
@@ -946,6 +987,7 @@ export class SunspecInverter extends BaseSunspecDevice {
|
|
|
946
987
|
};
|
|
947
988
|
}
|
|
948
989
|
async onConnectionRestored() {
|
|
990
|
+
this.interruptOperatingFaultRun();
|
|
949
991
|
if (!this.applianceId || !this.dataBus)
|
|
950
992
|
return;
|
|
951
993
|
if (this.errorState.lastStatus !== 'connection_lost')
|
package/dist/version.d.ts
CHANGED
package/dist/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { EnergyAppStorage } from "@enyo-energy/energy-app-sdk/dist/packages/energy-app-storage.js";
|
|
2
|
-
import type { SunspecBatteryControls, SunspecInverterControls } from "./sunspec-interfaces.js";
|
|
3
|
-
export type CalibrationDeviceType = "inverter" | "battery";
|
|
4
|
-
/**
|
|
5
|
-
* Persisted snapshot of a device's writable control registers, taken at the start of a
|
|
6
|
-
* calibration. Used to roll the device back when calibration ends.
|
|
7
|
-
*
|
|
8
|
-
* `modifiedFields` records which control-block field names were touched by *other* write
|
|
9
|
-
* commands (SetInverterFeedInLimitV1, StartStorageGridChargeV1, etc.) while the calibration
|
|
10
|
-
* was active — on stop, only those fields are written back, leaving fields the calibration
|
|
11
|
-
* itself didn't disturb alone.
|
|
12
|
-
*/
|
|
13
|
-
export interface CalibrationSnapshot {
|
|
14
|
-
applianceId: string;
|
|
15
|
-
deviceType: CalibrationDeviceType;
|
|
16
|
-
unitId: number;
|
|
17
|
-
startedAtIso: string;
|
|
18
|
-
inverterControls?: SunspecInverterControls;
|
|
19
|
-
batteryControls?: SunspecBatteryControls;
|
|
20
|
-
modifiedFields: string[];
|
|
21
|
-
}
|
|
22
|
-
/** Calibration is capped at 5 minutes of wall time, including across restarts. */
|
|
23
|
-
export declare const CALIBRATION_AUTO_STOP_MS: number;
|
|
24
|
-
export type CalibrationRestoreReason = "stop" | "auto";
|
|
25
|
-
export type CalibrationRestoreCallback = (snapshot: CalibrationSnapshot, reason: CalibrationRestoreReason) => Promise<void>;
|
|
26
|
-
/**
|
|
27
|
-
* Per-appliance service that snapshots a SunSpec device's writable control registers when a
|
|
28
|
-
* calibration starts, tracks which registers other commands mutate while it is active, and
|
|
29
|
-
* restores those registers when calibration stops (either explicitly or via the 5-minute
|
|
30
|
-
* auto-stop timer).
|
|
31
|
-
*
|
|
32
|
-
* Snapshots are persisted via `EnergyAppStorage` so a crash/restart mid-calibration does not
|
|
33
|
-
* leave the device in a calibration state without a rollback. On `initialize()`, any stored
|
|
34
|
-
* snapshot is reloaded; the auto-stop deadline is computed from `startedAtIso` so total
|
|
35
|
-
* calibration time remains capped at `autoStopMs` across restarts.
|
|
36
|
-
*/
|
|
37
|
-
export declare class CalibrationSnapshotService {
|
|
38
|
-
private readonly storage;
|
|
39
|
-
private readonly applianceId;
|
|
40
|
-
private readonly onRestore;
|
|
41
|
-
private readonly autoStopMs;
|
|
42
|
-
private snapshot?;
|
|
43
|
-
private autoStopTimer?;
|
|
44
|
-
constructor(storage: EnergyAppStorage, applianceId: string, onRestore: CalibrationRestoreCallback, autoStopMs?: number);
|
|
45
|
-
private storageKey;
|
|
46
|
-
initialize(): Promise<void>;
|
|
47
|
-
isCalibrating(): boolean;
|
|
48
|
-
getSnapshot(): CalibrationSnapshot | undefined;
|
|
49
|
-
startCalibration(input: {
|
|
50
|
-
deviceType: CalibrationDeviceType;
|
|
51
|
-
unitId: number;
|
|
52
|
-
inverterControls?: SunspecInverterControls;
|
|
53
|
-
batteryControls?: SunspecBatteryControls;
|
|
54
|
-
}): Promise<void>;
|
|
55
|
-
recordModification(fields: string[]): Promise<void>;
|
|
56
|
-
/**
|
|
57
|
-
* Clear the timer, remove the snapshot from memory + storage, and return the snapshot so
|
|
58
|
-
* the caller can restore the modified registers. Returns undefined if no calibration was
|
|
59
|
-
* active.
|
|
60
|
-
*/
|
|
61
|
-
stopCalibration(): Promise<CalibrationSnapshot | undefined>;
|
|
62
|
-
private remainingMs;
|
|
63
|
-
private scheduleAutoStop;
|
|
64
|
-
private clearAutoStopTimer;
|
|
65
|
-
private fireAutoStop;
|
|
66
|
-
private persist;
|
|
67
|
-
}
|
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
/** Calibration is capped at 5 minutes of wall time, including across restarts. */
|
|
2
|
-
export const CALIBRATION_AUTO_STOP_MS = 5 * 60 * 1000;
|
|
3
|
-
/**
|
|
4
|
-
* Per-appliance service that snapshots a SunSpec device's writable control registers when a
|
|
5
|
-
* calibration starts, tracks which registers other commands mutate while it is active, and
|
|
6
|
-
* restores those registers when calibration stops (either explicitly or via the 5-minute
|
|
7
|
-
* auto-stop timer).
|
|
8
|
-
*
|
|
9
|
-
* Snapshots are persisted via `EnergyAppStorage` so a crash/restart mid-calibration does not
|
|
10
|
-
* leave the device in a calibration state without a rollback. On `initialize()`, any stored
|
|
11
|
-
* snapshot is reloaded; the auto-stop deadline is computed from `startedAtIso` so total
|
|
12
|
-
* calibration time remains capped at `autoStopMs` across restarts.
|
|
13
|
-
*/
|
|
14
|
-
export class CalibrationSnapshotService {
|
|
15
|
-
storage;
|
|
16
|
-
applianceId;
|
|
17
|
-
onRestore;
|
|
18
|
-
autoStopMs;
|
|
19
|
-
snapshot;
|
|
20
|
-
autoStopTimer;
|
|
21
|
-
constructor(storage, applianceId, onRestore, autoStopMs = CALIBRATION_AUTO_STOP_MS) {
|
|
22
|
-
this.storage = storage;
|
|
23
|
-
this.applianceId = applianceId;
|
|
24
|
-
this.onRestore = onRestore;
|
|
25
|
-
this.autoStopMs = autoStopMs;
|
|
26
|
-
}
|
|
27
|
-
storageKey() {
|
|
28
|
-
return `sunspec-calibration-snapshot-${this.applianceId}`;
|
|
29
|
-
}
|
|
30
|
-
async initialize() {
|
|
31
|
-
try {
|
|
32
|
-
const loaded = await this.storage.load(this.storageKey());
|
|
33
|
-
if (!loaded) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
this.snapshot = loaded;
|
|
37
|
-
console.log(`CalibrationSnapshotService ${this.applianceId}: loaded persisted snapshot (started ${loaded.startedAtIso}, modifiedFields=[${loaded.modifiedFields.join(', ')}])`);
|
|
38
|
-
const remainingMs = this.remainingMs(loaded);
|
|
39
|
-
if (remainingMs <= 0) {
|
|
40
|
-
console.warn(`CalibrationSnapshotService ${this.applianceId}: snapshot exceeded ${this.autoStopMs}ms deadline — auto-restoring immediately`);
|
|
41
|
-
await this.fireAutoStop();
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
this.scheduleAutoStop(remainingMs);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
catch (error) {
|
|
48
|
-
console.error(`CalibrationSnapshotService ${this.applianceId}: failed to load persisted snapshot: ${error}`);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
isCalibrating() {
|
|
52
|
-
return this.snapshot !== undefined;
|
|
53
|
-
}
|
|
54
|
-
getSnapshot() {
|
|
55
|
-
return this.snapshot;
|
|
56
|
-
}
|
|
57
|
-
async startCalibration(input) {
|
|
58
|
-
this.clearAutoStopTimer();
|
|
59
|
-
this.snapshot = {
|
|
60
|
-
applianceId: this.applianceId,
|
|
61
|
-
deviceType: input.deviceType,
|
|
62
|
-
unitId: input.unitId,
|
|
63
|
-
startedAtIso: new Date().toISOString(),
|
|
64
|
-
inverterControls: input.inverterControls,
|
|
65
|
-
batteryControls: input.batteryControls,
|
|
66
|
-
modifiedFields: [],
|
|
67
|
-
};
|
|
68
|
-
await this.persist();
|
|
69
|
-
this.scheduleAutoStop(this.autoStopMs);
|
|
70
|
-
console.log(`CalibrationSnapshotService ${this.applianceId}: calibration started for ${input.deviceType} (auto-stop in ${this.autoStopMs}ms)`);
|
|
71
|
-
}
|
|
72
|
-
async recordModification(fields) {
|
|
73
|
-
if (!this.snapshot) {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
let added = false;
|
|
77
|
-
for (const field of fields) {
|
|
78
|
-
if (!this.snapshot.modifiedFields.includes(field)) {
|
|
79
|
-
this.snapshot.modifiedFields.push(field);
|
|
80
|
-
added = true;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
if (added) {
|
|
84
|
-
await this.persist();
|
|
85
|
-
console.log(`CalibrationSnapshotService ${this.applianceId}: tracked modified fields [${this.snapshot.modifiedFields.join(', ')}]`);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Clear the timer, remove the snapshot from memory + storage, and return the snapshot so
|
|
90
|
-
* the caller can restore the modified registers. Returns undefined if no calibration was
|
|
91
|
-
* active.
|
|
92
|
-
*/
|
|
93
|
-
async stopCalibration() {
|
|
94
|
-
this.clearAutoStopTimer();
|
|
95
|
-
const snap = this.snapshot;
|
|
96
|
-
this.snapshot = undefined;
|
|
97
|
-
if (snap) {
|
|
98
|
-
try {
|
|
99
|
-
await this.storage.remove(this.storageKey());
|
|
100
|
-
}
|
|
101
|
-
catch (error) {
|
|
102
|
-
console.error(`CalibrationSnapshotService ${this.applianceId}: failed to remove persisted snapshot: ${error}`);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return snap;
|
|
106
|
-
}
|
|
107
|
-
remainingMs(snap) {
|
|
108
|
-
const startedAtMs = Date.parse(snap.startedAtIso);
|
|
109
|
-
if (Number.isNaN(startedAtMs)) {
|
|
110
|
-
return this.autoStopMs;
|
|
111
|
-
}
|
|
112
|
-
return startedAtMs + this.autoStopMs - Date.now();
|
|
113
|
-
}
|
|
114
|
-
scheduleAutoStop(delayMs) {
|
|
115
|
-
this.clearAutoStopTimer();
|
|
116
|
-
this.autoStopTimer = setTimeout(() => {
|
|
117
|
-
void this.fireAutoStop();
|
|
118
|
-
}, delayMs);
|
|
119
|
-
if (typeof this.autoStopTimer.unref === "function") {
|
|
120
|
-
this.autoStopTimer.unref();
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
clearAutoStopTimer() {
|
|
124
|
-
if (this.autoStopTimer) {
|
|
125
|
-
clearTimeout(this.autoStopTimer);
|
|
126
|
-
this.autoStopTimer = undefined;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
async fireAutoStop() {
|
|
130
|
-
const snap = this.snapshot;
|
|
131
|
-
this.snapshot = undefined;
|
|
132
|
-
this.clearAutoStopTimer();
|
|
133
|
-
if (!snap) {
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
try {
|
|
137
|
-
await this.storage.remove(this.storageKey());
|
|
138
|
-
}
|
|
139
|
-
catch (error) {
|
|
140
|
-
console.error(`CalibrationSnapshotService ${this.applianceId}: failed to remove persisted snapshot during auto-stop: ${error}`);
|
|
141
|
-
}
|
|
142
|
-
try {
|
|
143
|
-
await this.onRestore(snap, "auto");
|
|
144
|
-
}
|
|
145
|
-
catch (error) {
|
|
146
|
-
console.error(`CalibrationSnapshotService ${this.applianceId}: auto-stop restore failed: ${error}`);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
async persist() {
|
|
150
|
-
if (!this.snapshot) {
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
try {
|
|
154
|
-
await this.storage.save(this.storageKey(), this.snapshot);
|
|
155
|
-
}
|
|
156
|
-
catch (error) {
|
|
157
|
-
console.error(`CalibrationSnapshotService ${this.applianceId}: failed to persist snapshot: ${error}`);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CalibrationSnapshotService = exports.CALIBRATION_AUTO_STOP_MS = void 0;
|
|
4
|
-
/** Calibration is capped at 5 minutes of wall time, including across restarts. */
|
|
5
|
-
exports.CALIBRATION_AUTO_STOP_MS = 5 * 60 * 1000;
|
|
6
|
-
/**
|
|
7
|
-
* Per-appliance service that snapshots a SunSpec device's writable control registers when a
|
|
8
|
-
* calibration starts, tracks which registers other commands mutate while it is active, and
|
|
9
|
-
* restores those registers when calibration stops (either explicitly or via the 5-minute
|
|
10
|
-
* auto-stop timer).
|
|
11
|
-
*
|
|
12
|
-
* Snapshots are persisted via `EnergyAppStorage` so a crash/restart mid-calibration does not
|
|
13
|
-
* leave the device in a calibration state without a rollback. On `initialize()`, any stored
|
|
14
|
-
* snapshot is reloaded; the auto-stop deadline is computed from `startedAtIso` so total
|
|
15
|
-
* calibration time remains capped at `autoStopMs` across restarts.
|
|
16
|
-
*/
|
|
17
|
-
class CalibrationSnapshotService {
|
|
18
|
-
storage;
|
|
19
|
-
applianceId;
|
|
20
|
-
onRestore;
|
|
21
|
-
autoStopMs;
|
|
22
|
-
snapshot;
|
|
23
|
-
autoStopTimer;
|
|
24
|
-
constructor(storage, applianceId, onRestore, autoStopMs = exports.CALIBRATION_AUTO_STOP_MS) {
|
|
25
|
-
this.storage = storage;
|
|
26
|
-
this.applianceId = applianceId;
|
|
27
|
-
this.onRestore = onRestore;
|
|
28
|
-
this.autoStopMs = autoStopMs;
|
|
29
|
-
}
|
|
30
|
-
storageKey() {
|
|
31
|
-
return `sunspec-calibration-snapshot-${this.applianceId}`;
|
|
32
|
-
}
|
|
33
|
-
async initialize() {
|
|
34
|
-
try {
|
|
35
|
-
const loaded = await this.storage.load(this.storageKey());
|
|
36
|
-
if (!loaded) {
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
this.snapshot = loaded;
|
|
40
|
-
console.log(`CalibrationSnapshotService ${this.applianceId}: loaded persisted snapshot (started ${loaded.startedAtIso}, modifiedFields=[${loaded.modifiedFields.join(', ')}])`);
|
|
41
|
-
const remainingMs = this.remainingMs(loaded);
|
|
42
|
-
if (remainingMs <= 0) {
|
|
43
|
-
console.warn(`CalibrationSnapshotService ${this.applianceId}: snapshot exceeded ${this.autoStopMs}ms deadline — auto-restoring immediately`);
|
|
44
|
-
await this.fireAutoStop();
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
this.scheduleAutoStop(remainingMs);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
catch (error) {
|
|
51
|
-
console.error(`CalibrationSnapshotService ${this.applianceId}: failed to load persisted snapshot: ${error}`);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
isCalibrating() {
|
|
55
|
-
return this.snapshot !== undefined;
|
|
56
|
-
}
|
|
57
|
-
getSnapshot() {
|
|
58
|
-
return this.snapshot;
|
|
59
|
-
}
|
|
60
|
-
async startCalibration(input) {
|
|
61
|
-
this.clearAutoStopTimer();
|
|
62
|
-
this.snapshot = {
|
|
63
|
-
applianceId: this.applianceId,
|
|
64
|
-
deviceType: input.deviceType,
|
|
65
|
-
unitId: input.unitId,
|
|
66
|
-
startedAtIso: new Date().toISOString(),
|
|
67
|
-
inverterControls: input.inverterControls,
|
|
68
|
-
batteryControls: input.batteryControls,
|
|
69
|
-
modifiedFields: [],
|
|
70
|
-
};
|
|
71
|
-
await this.persist();
|
|
72
|
-
this.scheduleAutoStop(this.autoStopMs);
|
|
73
|
-
console.log(`CalibrationSnapshotService ${this.applianceId}: calibration started for ${input.deviceType} (auto-stop in ${this.autoStopMs}ms)`);
|
|
74
|
-
}
|
|
75
|
-
async recordModification(fields) {
|
|
76
|
-
if (!this.snapshot) {
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
let added = false;
|
|
80
|
-
for (const field of fields) {
|
|
81
|
-
if (!this.snapshot.modifiedFields.includes(field)) {
|
|
82
|
-
this.snapshot.modifiedFields.push(field);
|
|
83
|
-
added = true;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
if (added) {
|
|
87
|
-
await this.persist();
|
|
88
|
-
console.log(`CalibrationSnapshotService ${this.applianceId}: tracked modified fields [${this.snapshot.modifiedFields.join(', ')}]`);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Clear the timer, remove the snapshot from memory + storage, and return the snapshot so
|
|
93
|
-
* the caller can restore the modified registers. Returns undefined if no calibration was
|
|
94
|
-
* active.
|
|
95
|
-
*/
|
|
96
|
-
async stopCalibration() {
|
|
97
|
-
this.clearAutoStopTimer();
|
|
98
|
-
const snap = this.snapshot;
|
|
99
|
-
this.snapshot = undefined;
|
|
100
|
-
if (snap) {
|
|
101
|
-
try {
|
|
102
|
-
await this.storage.remove(this.storageKey());
|
|
103
|
-
}
|
|
104
|
-
catch (error) {
|
|
105
|
-
console.error(`CalibrationSnapshotService ${this.applianceId}: failed to remove persisted snapshot: ${error}`);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
return snap;
|
|
109
|
-
}
|
|
110
|
-
remainingMs(snap) {
|
|
111
|
-
const startedAtMs = Date.parse(snap.startedAtIso);
|
|
112
|
-
if (Number.isNaN(startedAtMs)) {
|
|
113
|
-
return this.autoStopMs;
|
|
114
|
-
}
|
|
115
|
-
return startedAtMs + this.autoStopMs - Date.now();
|
|
116
|
-
}
|
|
117
|
-
scheduleAutoStop(delayMs) {
|
|
118
|
-
this.clearAutoStopTimer();
|
|
119
|
-
this.autoStopTimer = setTimeout(() => {
|
|
120
|
-
void this.fireAutoStop();
|
|
121
|
-
}, delayMs);
|
|
122
|
-
if (typeof this.autoStopTimer.unref === "function") {
|
|
123
|
-
this.autoStopTimer.unref();
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
clearAutoStopTimer() {
|
|
127
|
-
if (this.autoStopTimer) {
|
|
128
|
-
clearTimeout(this.autoStopTimer);
|
|
129
|
-
this.autoStopTimer = undefined;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
async fireAutoStop() {
|
|
133
|
-
const snap = this.snapshot;
|
|
134
|
-
this.snapshot = undefined;
|
|
135
|
-
this.clearAutoStopTimer();
|
|
136
|
-
if (!snap) {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
try {
|
|
140
|
-
await this.storage.remove(this.storageKey());
|
|
141
|
-
}
|
|
142
|
-
catch (error) {
|
|
143
|
-
console.error(`CalibrationSnapshotService ${this.applianceId}: failed to remove persisted snapshot during auto-stop: ${error}`);
|
|
144
|
-
}
|
|
145
|
-
try {
|
|
146
|
-
await this.onRestore(snap, "auto");
|
|
147
|
-
}
|
|
148
|
-
catch (error) {
|
|
149
|
-
console.error(`CalibrationSnapshotService ${this.applianceId}: auto-stop restore failed: ${error}`);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
async persist() {
|
|
153
|
-
if (!this.snapshot) {
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
try {
|
|
157
|
-
await this.storage.save(this.storageKey(), this.snapshot);
|
|
158
|
-
}
|
|
159
|
-
catch (error) {
|
|
160
|
-
console.error(`CalibrationSnapshotService ${this.applianceId}: failed to persist snapshot: ${error}`);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
exports.CalibrationSnapshotService = CalibrationSnapshotService;
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { EnergyAppStorage } from "@enyo-energy/energy-app-sdk/dist/packages/energy-app-storage.js";
|
|
2
|
-
import type { SunspecBatteryControls, SunspecInverterControls } from "./sunspec-interfaces.cjs";
|
|
3
|
-
export type CalibrationDeviceType = "inverter" | "battery";
|
|
4
|
-
/**
|
|
5
|
-
* Persisted snapshot of a device's writable control registers, taken at the start of a
|
|
6
|
-
* calibration. Used to roll the device back when calibration ends.
|
|
7
|
-
*
|
|
8
|
-
* `modifiedFields` records which control-block field names were touched by *other* write
|
|
9
|
-
* commands (SetInverterFeedInLimitV1, StartStorageGridChargeV1, etc.) while the calibration
|
|
10
|
-
* was active — on stop, only those fields are written back, leaving fields the calibration
|
|
11
|
-
* itself didn't disturb alone.
|
|
12
|
-
*/
|
|
13
|
-
export interface CalibrationSnapshot {
|
|
14
|
-
applianceId: string;
|
|
15
|
-
deviceType: CalibrationDeviceType;
|
|
16
|
-
unitId: number;
|
|
17
|
-
startedAtIso: string;
|
|
18
|
-
inverterControls?: SunspecInverterControls;
|
|
19
|
-
batteryControls?: SunspecBatteryControls;
|
|
20
|
-
modifiedFields: string[];
|
|
21
|
-
}
|
|
22
|
-
/** Calibration is capped at 5 minutes of wall time, including across restarts. */
|
|
23
|
-
export declare const CALIBRATION_AUTO_STOP_MS: number;
|
|
24
|
-
export type CalibrationRestoreReason = "stop" | "auto";
|
|
25
|
-
export type CalibrationRestoreCallback = (snapshot: CalibrationSnapshot, reason: CalibrationRestoreReason) => Promise<void>;
|
|
26
|
-
/**
|
|
27
|
-
* Per-appliance service that snapshots a SunSpec device's writable control registers when a
|
|
28
|
-
* calibration starts, tracks which registers other commands mutate while it is active, and
|
|
29
|
-
* restores those registers when calibration stops (either explicitly or via the 5-minute
|
|
30
|
-
* auto-stop timer).
|
|
31
|
-
*
|
|
32
|
-
* Snapshots are persisted via `EnergyAppStorage` so a crash/restart mid-calibration does not
|
|
33
|
-
* leave the device in a calibration state without a rollback. On `initialize()`, any stored
|
|
34
|
-
* snapshot is reloaded; the auto-stop deadline is computed from `startedAtIso` so total
|
|
35
|
-
* calibration time remains capped at `autoStopMs` across restarts.
|
|
36
|
-
*/
|
|
37
|
-
export declare class CalibrationSnapshotService {
|
|
38
|
-
private readonly storage;
|
|
39
|
-
private readonly applianceId;
|
|
40
|
-
private readonly onRestore;
|
|
41
|
-
private readonly autoStopMs;
|
|
42
|
-
private snapshot?;
|
|
43
|
-
private autoStopTimer?;
|
|
44
|
-
constructor(storage: EnergyAppStorage, applianceId: string, onRestore: CalibrationRestoreCallback, autoStopMs?: number);
|
|
45
|
-
private storageKey;
|
|
46
|
-
initialize(): Promise<void>;
|
|
47
|
-
isCalibrating(): boolean;
|
|
48
|
-
getSnapshot(): CalibrationSnapshot | undefined;
|
|
49
|
-
startCalibration(input: {
|
|
50
|
-
deviceType: CalibrationDeviceType;
|
|
51
|
-
unitId: number;
|
|
52
|
-
inverterControls?: SunspecInverterControls;
|
|
53
|
-
batteryControls?: SunspecBatteryControls;
|
|
54
|
-
}): Promise<void>;
|
|
55
|
-
recordModification(fields: string[]): Promise<void>;
|
|
56
|
-
/**
|
|
57
|
-
* Clear the timer, remove the snapshot from memory + storage, and return the snapshot so
|
|
58
|
-
* the caller can restore the modified registers. Returns undefined if no calibration was
|
|
59
|
-
* active.
|
|
60
|
-
*/
|
|
61
|
-
stopCalibration(): Promise<CalibrationSnapshot | undefined>;
|
|
62
|
-
private remainingMs;
|
|
63
|
-
private scheduleAutoStop;
|
|
64
|
-
private clearAutoStopTimer;
|
|
65
|
-
private fireAutoStop;
|
|
66
|
-
private persist;
|
|
67
|
-
}
|